Netty的基本使用
目录
[TOC]
引用
implementation 'io.netty:netty-all:4.1.6.Final'
Netty 4最后一个release版本是4.1.28,但是亲测这个版本启动就会报异常...........所以降级回4.1.6
使用
TCP
服务端
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boss, worker);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.option(ChannelOption.TCP_NODELAY, true);
bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline p = socketChannel.pipeline();
p.addLast("framedecoder", new LengthFieldBasedFrameDecoder(1024 * 1024 * 10, 0, 4, 0, 4));
p.addLast(new NettyServerHandler());
}
});
mChannelFuture = bootstrap.bind(mPort).sync();
if (mChannelFuture.isSuccess()) {
System.out.printf("start tcp server succeed,mPort : " + mPort);
}
mChannelFuture.channel().closeFuture().sync();
} catch (Exception e) {
System.out.printf("a exception happened while tcp server running");
e.printStackTrace();
} finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
mIsRunning = false;
}
其中
mChannelFuture.channel().closeFuture().sync();
会阻塞线程,所以这段代码应该放在一个线程中.
Channel
是连接服务端和客户端的通道.
ChannelPipeline p = socketChannel.pipeline();
ChannelPipeline
是一个处理器容器,当有新数据接收时会调用容器中的处理器.
NettyServerHandler
是处理最终接收到数据的地方
private class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
initTransport(ctx.channel());
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
WrapPacket packet = SocketUtils.decodeWrapPacketFromByteBufAfterSplit((ByteBuf) msg);
String channelId = ctx.channel().id().asLongText();
SubTransport transport = mChannelTransportMap.get(channelId);
if (transport == null) {
throw new RuntimeException("can not get transport form map , create a transport and put to map");
}
transport.onNewPacketReceived(packet);
}
}
LengthFieldBasedFrameDecoder
是基于包长度的包解析器,用来拆包和处理半包.还有其他的自带解析器,比如分割,固定包长度等,不过个人偏向于喜欢这个,这个可以将资源利用最大化,而且没有字符不能使用的限制,如果是分割符的解析器,则会占用掉一些字符组合不能使用.当然这个也会翻车,当别人恶意攻击时,或者协议设计有问题时,一个错了,那就接下来所有都错了.错一步,满盘皆输,也是渗人.当然也可以自定义包解析器喽.
客户端
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
bootstrap.group(eventLoopGroup);
bootstrap.remoteAddress(mAddress, mPort);
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast("framedecoder", new LengthFieldBasedFrameDecoder(1024 * 1024 * 10, 0, 4, 0, 0));
socketChannel.pipeline().addLast(new NettyClientHandler());
}
});
mChannelFuture = bootstrap.connect(mAddress, mPort).sync();
if (mChannelFuture.isSuccess()) {
Log.d("connect to mAddress : " + mAddress + ", mPort : " + mPort + " succeed");
}
mChannelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
Log.d("can not connect to mAddress : " + mAddress + " ,mPort : " + mPort);
} finally {
eventLoopGroup.shutdownGracefully();
}
同样也是阻塞的,和服务端差不多,便不再说明.
网友评论