增加注释
This commit is contained in:
6
gradle/wrapper/gradle-wrapper.properties
vendored
6
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,6 +1,6 @@
|
|||||||
#Wed Aug 01 15:00:16 CST 2018
|
#Thu Jun 04 17:02:40 CST 2020
|
||||||
|
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-all.zip
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-rc-2-bin.zip
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
|||||||
@@ -2,10 +2,7 @@ package com.sv.netty.netty;
|
|||||||
|
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import io.netty.bootstrap.ServerBootstrap;
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.*;
|
||||||
import io.netty.channel.ChannelHandler;
|
|
||||||
import io.netty.channel.ChannelOption;
|
|
||||||
import io.netty.channel.EventLoopGroup;
|
|
||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -91,14 +88,19 @@ public class BootService {
|
|||||||
bootstrap.childOption(ChannelOption.SO_KEEPALIVE,keepalive);
|
bootstrap.childOption(ChannelOption.SO_KEEPALIVE,keepalive);
|
||||||
// 绑定一个端口并且同步 启动服务器
|
// 绑定一个端口并且同步 启动服务器
|
||||||
serverChannelFuture = bootstrap.bind(new InetSocketAddress(port)).sync();
|
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();
|
serverChannelFuture.channel().closeFuture().sync();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
logger.error("启动NETTY TCP异常:", e);
|
logger.error("启动NETTY TCP异常:", e);
|
||||||
} finally {
|
} finally {
|
||||||
workerGroup.shutdownGracefully();
|
|
||||||
bossGroup.shutdownGracefully();
|
bossGroup.shutdownGracefully();
|
||||||
|
workerGroup.shutdownGracefully();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,11 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
|
|||||||
super.channelActive(ctx);
|
super.channelActive(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 当通道失效就会触发
|
||||||
|
* @param ctx
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||||
super.channelInactive(ctx);
|
super.channelInactive(ctx);
|
||||||
|
|||||||
@@ -28,6 +28,11 @@ public class ServerHandler extends ChannelInboundHandlerAdapter {
|
|||||||
messageService = SpringContextHolder.getBean("messageService");
|
messageService = SpringContextHolder.getBean("messageService");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通道就绪事件
|
||||||
|
* @param ctx
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
super.channelActive(ctx);
|
super.channelActive(ctx);
|
||||||
@@ -42,6 +47,10 @@ public class ServerHandler extends ChannelInboundHandlerAdapter {
|
|||||||
@Override
|
@Override
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
super.channelRead(ctx, msg);
|
super.channelRead(ctx, msg);
|
||||||
|
//taskQueue
|
||||||
|
// ctx.channel().eventLoop().execute()
|
||||||
|
//scheduleTaskQueue
|
||||||
|
// ctx.channel().eventLoop().schedule()
|
||||||
String clientIp = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getClientIp();
|
String clientIp = ctx.channel().attr(Constant.CHANNEL_PARAM).get().getClientIp();
|
||||||
try {
|
try {
|
||||||
messageService.receive(ctx.channel(), msg.toString());
|
messageService.receive(ctx.channel(), msg.toString());
|
||||||
|
|||||||
@@ -29,11 +29,13 @@ public class ServerProtocolInitializer extends ChannelInitializer<SocketChannel>
|
|||||||
protected void initChannel(SocketChannel ch){
|
protected void initChannel(SocketChannel ch){
|
||||||
ChannelPipeline pipeline = ch.pipeline();
|
ChannelPipeline pipeline = ch.pipeline();
|
||||||
logger.info("ServerProtocolInitializer");
|
logger.info("ServerProtocolInitializer");
|
||||||
|
// 通过指定的长度来标识整包的信息,这样就可以自动的处理粘包和半包的问题
|
||||||
pipeline.addFirst(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 4, 4, 0, 0));
|
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 ReadTimeoutHandler(READ_TIMEOUT));
|
||||||
pipeline.addLast(new MessageDecoder());
|
pipeline.addLast(new MessageDecoder());
|
||||||
pipeline.addLast(new MessageEncoder());
|
pipeline.addLast(new MessageEncoder());
|
||||||
|
// 心跳检测机制,通过调用触发下一个handler userEventTriggered 方法
|
||||||
|
pipeline.addLast(new IdleStateHandler(READ_TIME, WRITE_TIME,IDLE_TIME));
|
||||||
pipeline.addLast(new ServerHandler());
|
pipeline.addLast(new ServerHandler());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user