增加注释

This commit is contained in:
limqhz
2020-06-24 14:15:36 +08:00
parent 644c0063e7
commit e5d5c228cf
5 changed files with 28 additions and 10 deletions

View File

@@ -2,10 +2,7 @@ package com.sv.netty.netty;
import com.google.common.collect.Maps;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.slf4j.Logger;
@@ -91,14 +88,19 @@ public class BootService {
bootstrap.childOption(ChannelOption.SO_KEEPALIVE,keepalive);
// 绑定一个端口并且同步 启动服务器
serverChannelFuture = bootstrap.bind(new InetSocketAddress(port)).sync();
logger.info("成功bind端口:" + port);
// serverChannelFuture的用处
serverChannelFuture.addListener(f -> {
if (f.isSuccess()){
logger.info("成功bind端口:" + port);
}
});
// 对关闭通道进行监听 (异步模型)
serverChannelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
logger.error("启动NETTY TCP异常:", e);
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}

View File

@@ -33,6 +33,11 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
super.channelActive(ctx);
}
/**
* 当通道失效就会触发
* @param ctx
* @throws Exception
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);

View File

@@ -28,6 +28,11 @@ public class ServerHandler extends ChannelInboundHandlerAdapter {
messageService = SpringContextHolder.getBean("messageService");
}
/**
* 通道就绪事件
* @param ctx
* @throws Exception
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
@@ -42,6 +47,10 @@ public class ServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
super.channelRead(ctx, msg);
//taskQueue
// ctx.channel().eventLoop().execute()
//scheduleTaskQueue
// ctx.channel().eventLoop().schedule()
String clientIp = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getClientIp();
try {
messageService.receive(ctx.channel(), msg.toString());

View File

@@ -29,11 +29,13 @@ public class ServerProtocolInitializer extends ChannelInitializer<SocketChannel>
protected void initChannel(SocketChannel ch){
ChannelPipeline pipeline = ch.pipeline();
logger.info("ServerProtocolInitializer");
// 通过指定的长度来标识整包的信息,这样就可以自动的处理粘包和半包的问题
pipeline.addFirst(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 4, 4, 0, 0));
pipeline.addLast(new IdleStateHandler(IDLE_TIME, READ_TIME, WRITE_TIME));
pipeline.addLast(new ReadTimeoutHandler(READ_TIMEOUT));
pipeline.addLast(new MessageDecoder());
pipeline.addLast(new MessageEncoder());
// 心跳检测机制通过调用触发下一个handler userEventTriggered 方法
pipeline.addLast(new IdleStateHandler(READ_TIME, WRITE_TIME,IDLE_TIME));
pipeline.addLast(new ServerHandler());
}