Commit 5c3cce7e authored by liujiahua123123's avatar liujiahua123123

network

parent e342261e
...@@ -84,16 +84,17 @@ public class MiraiServer { ...@@ -84,16 +84,17 @@ public class MiraiServer {
this.setting = new MiraiConfig(setting); this.setting = new MiraiConfig(setting);
} }
int port = this.setting.getMapSection("network").getInt("port"); MiraiMapSection qqs = this.setting.getMapSection("qq");
MiraiNetwork.start(port); qqs.forEach((a,p) -> {
Thread.yield(); this.getLogger().log(LoggerTextFormat.SKY_BLUE + "Finding available ports between " + "1-65536");
if(MiraiNetwork.getLastError()!=null){ try {
this.getLogger().log(LoggerTextFormat.RED + "an error occurred when staring network layer"); int port = MiraiNetwork.getAvailablePort();
this.shutdown();
}
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "Listening on port " + port); this.getLogger().log(LoggerTextFormat.SKY_BLUE + "Listening on port " + port);
} catch (IOException e) {
e.printStackTrace();
}
});
} }
public void initSetting(File setting){ public void initSetting(File setting){
...@@ -108,7 +109,7 @@ public class MiraiServer { ...@@ -108,7 +109,7 @@ public class MiraiServer {
} }
this.setting = new MiraiConfig(setting); this.setting = new MiraiConfig(setting);
MiraiMapSection network = this.setting.getMapSection("network"); MiraiMapSection network = this.setting.getMapSection("network");
network.put("port",19139);
MiraiMapSection qqs = this.setting.getMapSection("qq"); MiraiMapSection qqs = this.setting.getMapSection("qq");
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
this.getLogger().log(LoggerTextFormat.SKY_BLUE + "input one " + LoggerTextFormat.RED + " QQ number " + LoggerTextFormat.SKY_BLUE +"for default robot"); this.getLogger().log(LoggerTextFormat.SKY_BLUE + "input one " + LoggerTextFormat.RED + " QQ number " + LoggerTextFormat.SKY_BLUE +"for default robot");
......
package net.mamoe.mirai.network; package net.mamoe.mirai.network;
import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import lombok.Getter; import lombok.Getter;
import net.mamoe.mirai.MiraiServer;
import net.mamoe.mirai.event.events.server.ServerDisableEvent;
import java.io.IOException; import java.io.IOException;
import java.net.ServerSocket; import java.net.ServerSocket;
public class MiraiNetwork { public class MiraiNetwork {
private static ServerBootstrap server;
private static Thread thread;
@Getter @Getter
private static volatile Throwable lastError = null; private static volatile Throwable lastError = null;
public static void start(int port){ public static void start(int port){
thread = new Thread(() -> {
if (server != null) {
throw new RuntimeException("there is already a ServerBootstrap instance");
}
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
server = new ServerBootstrap();
server.group(bossGroup, workerGroup);
server.channel(NioServerSocketChannel.class);
//b.option(ChannelOption.SO_BACKLOG, 100);
//b.handler(new LoggingHandler(LogLevel.INFO));
server.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("bytesDecoder", new ByteArrayDecoder());
pipeline.addLast("bytesEncoder", new ByteArrayEncoder());
pipeline.addLast("handler", new NetworkPacketHandler());
}
});
server.bind(port).sync().channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
lastError = e;
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
});
thread.start();
MiraiServer.getInstance().getEventManager().onEvent(ServerDisableEvent.class).setHandler(a -> {
thread.interrupt();
});
} }
......
package net.mamoe.mirai.network;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.nio.charset.StandardCharsets;
public class MiraiUDPClient {
private DatagramSocket localUDPSocket;
private Thread thread;
public MiraiUDPClient(InetAddress target, int targetPort, int localPort) {
try{
this.localUDPSocket = new DatagramSocket(localPort);
this.localUDPSocket.connect(target,targetPort);
this.localUDPSocket.setReuseAddress(true);
this.thread = new Thread(() -> {
try {
while (true)
{
byte[] data = new byte[1024];
// 接收数据报的包
DatagramPacket packet = new DatagramPacket(data, data.length);
DatagramSocket localUDPSocket = MiraiUDPClient.this.localUDPSocket;
if ((localUDPSocket == null) || (localUDPSocket.isClosed())) continue;
// 阻塞直到收到数据
localUDPSocket.receive(packet);
// 解析服务端发过来的数据
MiraiUDPClient.this.onReceive(packet);
}
} catch (Exception e) {
e.printStackTrace();
}
});
this.thread.start();
} catch (SocketException e) {
e.printStackTrace();
}
}
public void onReceive(DatagramPacket packet){
System.out.println(new String(packet.getData(), 0 , packet.getLength(), StandardCharsets.UTF_8));
}
public void send(DatagramPacket packet) throws IOException {
this.localUDPSocket.send(packet);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment