|
@@ -1,127 +1,128 @@
|
|
-package com.js.kbt.socket;
|
|
|
|
-
|
|
|
|
-import io.netty.channel.ChannelFuture;
|
|
|
|
-import io.netty.channel.ChannelOption;
|
|
|
|
-import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
|
-
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.List;
|
|
|
|
-
|
|
|
|
-import javax.annotation.Resource;
|
|
|
|
-
|
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
|
-
|
|
|
|
-@Service("helloServer")
|
|
|
|
-public class HelloServer {
|
|
|
|
-
|
|
|
|
- @Resource(name="helloServerInitializer")
|
|
|
|
- public HelloServerInitializer helloServerInitializer;
|
|
|
|
-
|
|
|
|
- @Resource(name="myServerBootstrap")
|
|
|
|
- private MyServerBootstrap myServerBootstrap;
|
|
|
|
-
|
|
|
|
- @Resource(name="bossGroup")
|
|
|
|
- private BossGroup bossGroup;
|
|
|
|
-
|
|
|
|
- @Resource(name="workerGroup")
|
|
|
|
- private WorkerGroup workerGroup;
|
|
|
|
-
|
|
|
|
- public static boolean isStarted = false;
|
|
|
|
- /*
|
|
|
|
- * 创建服务端监听端口
|
|
|
|
- */
|
|
|
|
-
|
|
|
|
- private static final int portNumber = 2019;
|
|
|
|
-
|
|
|
|
- public static HashMap<String, UserHandler> deviceMap = new HashMap<String, UserHandler>();
|
|
|
|
- public static List<String> closePendingList = new ArrayList<String>();
|
|
|
|
- public static final Object locker = new Object();
|
|
|
|
-
|
|
|
|
- public static int sendTCPCmd(String identity, String cmd) {
|
|
|
|
- if (deviceMap.containsKey(identity)) {
|
|
|
|
- deviceMap.get(identity).sendMsg(cmd);
|
|
|
|
- return 1;
|
|
|
|
- } else {
|
|
|
|
- org.apache.log4j.Logger.getLogger(UserHandler.class).info("设备未登录=="+identity+", all size="+deviceMap.size());
|
|
|
|
- return -1;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public void startServer() throws InterruptedException {
|
|
|
|
- System.out.println("@@@@@@@@@@startServer=======");
|
|
|
|
- if (isStarted) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- new Thread() {
|
|
|
|
- public void run() {
|
|
|
|
- System.out.println("启动Game Server spring注入");
|
|
|
|
- try {
|
|
|
|
- myServerBootstrap.group(bossGroup, workerGroup);
|
|
|
|
- myServerBootstrap.channel(NioServerSocketChannel.class);
|
|
|
|
- myServerBootstrap.option(ChannelOption.TCP_NODELAY, true);
|
|
|
|
- myServerBootstrap.childHandler(helloServerInitializer)
|
|
|
|
- .childOption(ChannelOption.SO_KEEPALIVE, true)
|
|
|
|
- .childOption(ChannelOption.SO_REUSEADDR, true);
|
|
|
|
- isStarted = true;
|
|
|
|
- System.out.println("设置模式启动");
|
|
|
|
- org.apache.log4j.Logger.getLogger(HelloServer.class).info("设置模式启动===Logger");
|
|
|
|
- //服务器绑定端口监听
|
|
|
|
- ChannelFuture channelFuture = myServerBootstrap.bind(portNumber).sync();
|
|
|
|
-
|
|
|
|
- //监听服务器关闭监听
|
|
|
|
- channelFuture.channel().closeFuture().sync();
|
|
|
|
- } catch (InterruptedException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }.start();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 断开连接,如果尚未连接上,加入断开队列
|
|
|
|
- * @param identity
|
|
|
|
- */
|
|
|
|
- public static void closeConnection(String identity) {
|
|
|
|
- synchronized (locker) {
|
|
|
|
- if (deviceMap.containsKey(identity)) {
|
|
|
|
- deviceMap.get(identity).sendMsg("CANCEL");
|
|
|
|
- deviceMap.get(identity).close();
|
|
|
|
- } else {
|
|
|
|
- closePendingList.add(identity);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 从取消列表中移除,保证该连接不会被断开
|
|
|
|
- * @param identity
|
|
|
|
- */
|
|
|
|
- public static void activeDevice(String identity) {
|
|
|
|
- synchronized (locker) {
|
|
|
|
- if (closePendingList.contains(identity)) {
|
|
|
|
- closePendingList.remove(identity);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public void shutdown() {
|
|
|
|
- System.out.println("关闭Game Server");
|
|
|
|
- workerGroup.shutdownGracefully();
|
|
|
|
- bossGroup.shutdownGracefully();
|
|
|
|
- deviceMap.clear();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- public static void main(String[] args) throws InterruptedException {
|
|
|
|
-// HelloServer server = new HelloServer();
|
|
|
|
-// server.startServer();
|
|
|
|
-
|
|
|
|
- List<String> testList = new ArrayList<String>();
|
|
|
|
- testList.add("12345");
|
|
|
|
- System.out.println(testList.size());
|
|
|
|
- testList.remove("12345");
|
|
|
|
- System.out.println(testList.size());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+package com.js.kbt.socket;
|
|
|
|
+
|
|
|
|
+import io.netty.channel.ChannelFuture;
|
|
|
|
+import io.netty.channel.ChannelOption;
|
|
|
|
+import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
|
+
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+@Service("helloServer")
|
|
|
|
+public class HelloServer {
|
|
|
|
+
|
|
|
|
+ @Resource(name="helloServerInitializer")
|
|
|
|
+ public HelloServerInitializer helloServerInitializer;
|
|
|
|
+
|
|
|
|
+ @Resource(name="myServerBootstrap")
|
|
|
|
+ private MyServerBootstrap myServerBootstrap;
|
|
|
|
+
|
|
|
|
+ @Resource(name="bossGroup")
|
|
|
|
+ private BossGroup bossGroup;
|
|
|
|
+
|
|
|
|
+ @Resource(name="workerGroup")
|
|
|
|
+ private WorkerGroup workerGroup;
|
|
|
|
+
|
|
|
|
+ public static boolean isStarted = false;
|
|
|
|
+ /*
|
|
|
|
+ * 创建服务端监听端口
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ private static final int portNumber = 2019;
|
|
|
|
+
|
|
|
|
+ public static HashMap<String, UserHandler> deviceMap = new HashMap<String, UserHandler>();
|
|
|
|
+ public static List<String> closePendingList = new ArrayList<String>();
|
|
|
|
+ public static final Object locker = new Object();
|
|
|
|
+
|
|
|
|
+ public static int sendTCPCmd(String identity, String cmd) {
|
|
|
|
+ if (deviceMap.containsKey(identity)) {
|
|
|
|
+ deviceMap.get(identity).sendMsg(cmd);
|
|
|
|
+ return 1;
|
|
|
|
+ } else {
|
|
|
|
+ org.apache.log4j.Logger.getLogger(UserHandler.class).info("设备未登录=="+identity+", all size="+deviceMap.size());
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void startServer() throws InterruptedException {
|
|
|
|
+ System.out.println("@@@@@@@@@@startServer=======");
|
|
|
|
+ if (isStarted) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ new Thread() {
|
|
|
|
+ public void run() {
|
|
|
|
+ System.out.println("启动Game Server spring注入");
|
|
|
|
+ try {
|
|
|
|
+ myServerBootstrap.group(bossGroup, workerGroup);
|
|
|
|
+ myServerBootstrap.channel(NioServerSocketChannel.class);
|
|
|
|
+ myServerBootstrap.option(ChannelOption.TCP_NODELAY, true);
|
|
|
|
+ myServerBootstrap.childHandler(helloServerInitializer)
|
|
|
|
+ .childOption(ChannelOption.SO_RCVBUF, 1048576*20)
|
|
|
|
+ .childOption(ChannelOption.SO_KEEPALIVE, true)
|
|
|
|
+ .childOption(ChannelOption.SO_REUSEADDR, true);
|
|
|
|
+ isStarted = true;
|
|
|
|
+ System.out.println("设置模式启动");
|
|
|
|
+ org.apache.log4j.Logger.getLogger(HelloServer.class).info("设置模式启动===Logger");
|
|
|
|
+ //服务器绑定端口监听
|
|
|
|
+ ChannelFuture channelFuture = myServerBootstrap.bind(portNumber).sync();
|
|
|
|
+
|
|
|
|
+ //监听服务器关闭监听
|
|
|
|
+ channelFuture.channel().closeFuture().sync();
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }.start();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 断开连接,如果尚未连接上,加入断开队列
|
|
|
|
+ * @param identity
|
|
|
|
+ */
|
|
|
|
+ public static void closeConnection(String identity) {
|
|
|
|
+ synchronized (locker) {
|
|
|
|
+ if (deviceMap.containsKey(identity)) {
|
|
|
|
+ deviceMap.get(identity).sendMsg("CANCEL");
|
|
|
|
+ deviceMap.get(identity).close();
|
|
|
|
+ } else {
|
|
|
|
+ closePendingList.add(identity);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 从取消列表中移除,保证该连接不会被断开
|
|
|
|
+ * @param identity
|
|
|
|
+ */
|
|
|
|
+ public static void activeDevice(String identity) {
|
|
|
|
+ synchronized (locker) {
|
|
|
|
+ if (closePendingList.contains(identity)) {
|
|
|
|
+ closePendingList.remove(identity);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void shutdown() {
|
|
|
|
+ System.out.println("关闭Game Server");
|
|
|
|
+ workerGroup.shutdownGracefully();
|
|
|
|
+ bossGroup.shutdownGracefully();
|
|
|
|
+ deviceMap.clear();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) throws InterruptedException {
|
|
|
|
+// HelloServer server = new HelloServer();
|
|
|
|
+// server.startServer();
|
|
|
|
+
|
|
|
|
+ List<String> testList = new ArrayList<String>();
|
|
|
|
+ testList.add("12345");
|
|
|
|
+ System.out.println(testList.size());
|
|
|
|
+ testList.remove("12345");
|
|
|
|
+ System.out.println(testList.size());
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|