diff --git a/netty-04-chat/src/main/java/com/kwan/shuyu/server/ChatServer.java b/netty-04-chat/src/main/java/com/kwan/shuyu/server/ChatServer.java index 694d1da6db97cdcb6c2983af7847e186bb148fbd..5cf561b5a982e15aa5844d5461afd98689e449b8 100644 --- a/netty-04-chat/src/main/java/com/kwan/shuyu/server/ChatServer.java +++ b/netty-04-chat/src/main/java/com/kwan/shuyu/server/ChatServer.java @@ -27,17 +27,18 @@ import lombok.extern.slf4j.Slf4j; */ @Slf4j public class ChatServer { - public static void main(String[] args) throws InterruptedException { + public static void main(String[] args) throws Exception { final NioEventLoopGroup boss = new NioEventLoopGroup(); final NioEventLoopGroup worker = new NioEventLoopGroup(); - // 局部变量 + // 用来判断 是不是读 空闲时间过长,或写空闲时间过长 (读,写,读写空闲时间限制) 0表示不关心 + final IdleStateHandler IDLE_STATE_HANDLER = new IdleStateHandler(12, 0, 0); MessageCodecSharable MESSAGE_CODEC = new MessageCodecSharable(); - LoggingHandler LOGGING_HANDLER = new LoggingHandler(LogLevel.DEBUG); - LoginRequestMessageHandler LOGIN_HANDLER = new LoginRequestMessageHandler(); //--登录---处理器 - final ChatRequestMessageHandler CHAT_HANDLER = new ChatRequestMessageHandler();//--单聊---处理器 - final GroupCreateRequestMessageHandler GROUP_CREATE_HANDLER = new GroupCreateRequestMessageHandler();//--创建群聊---处理器 - final GroupChatRequestMessageHandler GROUP_CHAT_HANDLER = new GroupChatRequestMessageHandler(); //--群聊---处理器 - final QuitHandler QUIT_HANDLER = new QuitHandler(); //--断开连接---处理器 + LoggingHandler LOGGING_HANDLER = new LoggingHandler(LogLevel.DEBUG);//打印日志 + LoginRequestMessageHandler LOGIN_HANDLER = new LoginRequestMessageHandler(); + final ChatRequestMessageHandler CHAT_HANDLER = new ChatRequestMessageHandler(); + final GroupCreateRequestMessageHandler GROUP_CREATE_HANDLER = new GroupCreateRequestMessageHandler(); + final GroupChatRequestMessageHandler GROUP_CHAT_HANDLER = new GroupChatRequestMessageHandler(); + final QuitHandler QUIT_HANDLER = new QuitHandler(); try { final ServerBootstrap bs = new ServerBootstrap(); bs.channel(NioServerSocketChannel.class); @@ -45,8 +46,7 @@ public class ChatServer { bs.childHandler(new ChannelInitializer() { @Override protected void initChannel(SocketChannel ch) throws Exception { - // 用来判断 是不是读 空闲时间过长,或写空闲时间过长 (读,写,读写空闲时间限制) 0表示不关心 - ch.pipeline().addLast(new IdleStateHandler(12, 0, 0)); + ch.pipeline().addLast(IDLE_STATE_HANDLER); /* ################################################################ ##### ChannelDuplexHandler 可以同时作为 入站 和 出站处理器 ####### @@ -65,6 +65,12 @@ public class ChatServer { log.debug("==============================已经12秒没读到数据了!===================================="); ctx.channel().close(); } + if (event.state() == IdleState.WRITER_IDLE) { + log.debug("==============================写超时!===================================="); + } + if (event.state() == IdleState.ALL_IDLE) { + log.debug("==============================读写超时!===================================="); + } } }); ch.pipeline().addLast(QUIT_HANDLER); //--断开连接---处理器 diff --git a/netty-04-chat/src/main/java/com/kwan/shuyu/test/OnlyTest.java b/netty-04-chat/src/main/java/com/kwan/shuyu/test/ComputeIfPresentTest.java similarity index 58% rename from netty-04-chat/src/main/java/com/kwan/shuyu/test/OnlyTest.java rename to netty-04-chat/src/main/java/com/kwan/shuyu/test/ComputeIfPresentTest.java index ecf328ac6fab19652987fbbc727d34811af99151..6682c53386cd165976370b57e490e48dea3cb79b 100644 --- a/netty-04-chat/src/main/java/com/kwan/shuyu/test/OnlyTest.java +++ b/netty-04-chat/src/main/java/com/kwan/shuyu/test/ComputeIfPresentTest.java @@ -5,20 +5,15 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -public class OnlyTest { +/** + * 测试 集合的 computeIfPresent 如果存在key则计算 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/28 18:25 + */ +public class ComputeIfPresentTest { public static void main(String[] args) { -/* - // 【例题 1】 - System.out.println(Config.getServerPort()); - final int ordinal = Config.getMySerializerAlgorithm().ordinal(); - System.out.println("ordinal = " + ordinal); - String s = "aaaaaaaaaa"; - final byte[] bytes = Config.getMySerializerAlgorithm().serializ(s); - System.out.println(Arrays.toString(bytes)); -*/ - // 【例题 2】 -// System.out.println(MySerializer.Algorithm.Json.ordinal()); - // 【例题 3】 测试 集合的 computeIfPresent 如果存在key则计算 final Map> map = new HashMap<>(); final HashSet set1 = new HashSet<>(); set1.add("张三"); @@ -33,7 +28,6 @@ public class OnlyTest { map.put("ql2", set2); final boolean re = map.get("ql1").remove("abc"); System.out.println(re); // true or false - //map.get("ql6").add("赵六"); map.computeIfPresent("ql6", (k, v) -> { v.add("赵六"); return v; diff --git a/netty-04-chat/src/main/java/com/kwan/shuyu/test/ConfigTest.java b/netty-04-chat/src/main/java/com/kwan/shuyu/test/ConfigTest.java new file mode 100644 index 0000000000000000000000000000000000000000..6fc81f655f18fa9c7e1962ade3dad065ead70460 --- /dev/null +++ b/netty-04-chat/src/main/java/com/kwan/shuyu/test/ConfigTest.java @@ -0,0 +1,25 @@ +package com.kwan.shuyu.test; + +import com.kwan.shuyu.config.Config; + +import java.util.Arrays; + + +/** + * 测试config + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/28 18:18 + */ +public class ConfigTest { + public static void main(String[] args) { + System.out.println(Config.getServerPort()); + //ordinal返回枚举的位置下标 + final int ordinal = Config.getMySerializerAlgorithm().ordinal(); + System.out.println("ordinal = " + ordinal); + String s = "aaaaaaaaaa"; + final byte[] bytes = Config.getMySerializerAlgorithm().serializ(s); + System.out.println(Arrays.toString(bytes)); + } +} \ No newline at end of file diff --git a/netty-04-chat/src/main/java/com/kwan/shuyu/test/OrdinalTest.java b/netty-04-chat/src/main/java/com/kwan/shuyu/test/OrdinalTest.java new file mode 100644 index 0000000000000000000000000000000000000000..00b3c4df1bdb071897cff8846b6860dd6be2f932 --- /dev/null +++ b/netty-04-chat/src/main/java/com/kwan/shuyu/test/OrdinalTest.java @@ -0,0 +1,17 @@ +package com.kwan.shuyu.test; + +import com.kwan.shuyu.protocol.MySerializer; + + +/** + * ordinal测试 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/28 18:25 + */ +public class OrdinalTest { + public static void main(String[] args) { + System.out.println(MySerializer.Algorithm.Json.ordinal()); + } +} \ No newline at end of file diff --git a/netty-04-chat/src/main/java/com/kwan/shuyu/test/TestAlgorithm.java b/netty-04-chat/src/main/java/com/kwan/shuyu/test/TestAlgorithm.java index db87198a483f8c1f100245fdf5c35e410285746c..0259acef1ede14b086d6088e387779eac18e900f 100644 --- a/netty-04-chat/src/main/java/com/kwan/shuyu/test/TestAlgorithm.java +++ b/netty-04-chat/src/main/java/com/kwan/shuyu/test/TestAlgorithm.java @@ -10,6 +10,14 @@ import io.netty.channel.embedded.EmbeddedChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; + +/** + * 测试序列化算法 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/28 18:28 + */ public class TestAlgorithm { public static void main(String[] args) { // 局部变量 @@ -17,24 +25,25 @@ public class TestAlgorithm { LoggingHandler LOGGING_HANDLER = new LoggingHandler(LogLevel.DEBUG); final EmbeddedChannel embeddedChannel = new EmbeddedChannel( LOGGING_HANDLER, -// new ProcotolFrameDecoder(), // 帧解码器 【与自定义编解码器 MessageCodecSharable一起配置参数】 MESSAGE_CODEC, LOGGING_HANDLER ); LoginRequestMessage message = new LoginRequestMessage("张三", "123456"); - /* ######################################################## - ####### 出站测试 【出站 自动编码】 encode ########## - ########################################################*/ -// embeddedChannel.writeOutbound(message); - /* ######################################################### - ####### 入站测试 【入站 自动解码】 decode ############ - #########################################################*/ final ByteBuf buf = messageToByteBuf(message); embeddedChannel.writeInbound(buf); } + + /** + * 消息转 ByteBuf + * + * @version : 2.2.0 + * @author : qinyingjie + * @date : 2023/4/28 18:29 + */ private static ByteBuf messageToByteBuf(Message msg) { ByteBuf out = ByteBufAllocator.DEFAULT.buffer(); + //依次写入ByteBuf out.writeBytes(new byte[]{1, 2, 3, 4}); // 4字节的 魔数 out.writeByte(1); // 1字节的 版本 out.writeByte(Config.getMySerializerAlgorithm().ordinal()); // 1字节的 序列化方式 0-jdk,1-json diff --git a/netty-04-chat/src/main/java/com/kwan/shuyu/test/TestBacklogServer.java b/netty-04-chat/src/main/java/com/kwan/shuyu/test/TestBacklogServer.java index 36a62f66d379b324bdafe75f0e1d909ff7b87cbd..419db09fa36c3e82e1e81bb427157a2ab21757b1 100644 --- a/netty-04-chat/src/main/java/com/kwan/shuyu/test/TestBacklogServer.java +++ b/netty-04-chat/src/main/java/com/kwan/shuyu/test/TestBacklogServer.java @@ -32,7 +32,7 @@ public class TestBacklogServer { /** * 调整 全连接队列大小 【注意这里和系统的取最小值为准,所以系统也要配】 */ - bs.option(ChannelOption.SO_BACKLOG, 5);// 超出报错:java.net.ConnectException: Connection refused: no further information + bs.option(ChannelOption.SO_BACKLOG, 2);// 超出报错:java.net.ConnectException: Connection refused: no further information bs.group(new NioEventLoopGroup()); bs.channel(NioServerSocketChannel.class); bs.childHandler(new ChannelInitializer() { diff --git a/netty-04-chat/src/main/java/com/kwan/shuyu/test/TestConnectionTimeout.java b/netty-04-chat/src/main/java/com/kwan/shuyu/test/TestConnectionTimeout.java index 27dab7c9b4d9f81b75d7533d5a657d192318df2b..390141a0ca672f549ce89e68537991990535a283 100644 --- a/netty-04-chat/src/main/java/com/kwan/shuyu/test/TestConnectionTimeout.java +++ b/netty-04-chat/src/main/java/com/kwan/shuyu/test/TestConnectionTimeout.java @@ -10,12 +10,14 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class TestConnectionTimeout { - /* - 配置参数: - 1. 客户端通过 .option() 方法配置参数 给 SocketChannel 配置餐年数 (因为对于客户端来说就一个Socket) - 2. 服务端 - new ServerBootstrap().option() // 给 ServerSocketChannel 配置参数 - new ServerBootstrap().childHandler() // 给 SocketChannel 配置参数 + /** + * 配置参数: + * 1. 客户端通过 .option() 方法配置参数 给 SocketChannel 配置参数 (因为对于客户端来说就一个Socket) + * 2. 服务端 + * new ServerBootstrap().option() // 给 ServerSocketChannel 配置参数 + * new ServerBootstrap().childHandler() // 给 SocketChannel 配置参数 + * + * @param args */ public static void main(String[] args) { final NioEventLoopGroup group = new NioEventLoopGroup(); diff --git a/netty-04-chat/src/main/resources/application.properties b/netty-04-chat/src/main/resources/application.properties index ea7141d6c521ecaa278b4ca8a7c5c186b302a08b..fa829805750330a9717f82d1b72eb3c855112495 100644 --- a/netty-04-chat/src/main/resources/application.properties +++ b/netty-04-chat/src/main/resources/application.properties @@ -1,6 +1,6 @@ -#˿ں -server.port = 6000 -# Java ѡ лJson ѡJsonл -mySerializer.algorithm=Json +#\u7AEF\u53E3\u53F7 +server.port=6000 +# \u5E8F\u5217\u5316\u65B9\u5F0F Java Json +mySerializer.algorithm=Java com.rpc.server.service.HelloService=com.rpc.server.service.HelloServiceImpl2 \ No newline at end of file