netty:读取大块的有分界数据

服务端和客户端的共用代码

【ConnectionInfo.java】

@Getter
@Setter
public class ConnectionInfo
{
    private StringBuffer readBuffer = new StringBuffer();
    private AtomicInteger readBufferIndex = new AtomicInteger(0);
    private AtomicInteger blockCounter = new AtomicInteger(0);
}

【ConnectionInfos.java】

public class ConnectionInfos
{
    private static ConcurrentHashMap<Channel, ConnectionInfo> connections = new ConcurrentHashMap<>();

    public static ConnectionInfo put(Channel channel, ConnectionInfo connectionInfo) {
        return connections.put(channel, connectionInfo);
    }

    public static ConnectionInfo get(Channel channel) {
        return connections.get(channel);
    }

    public static ConnectionInfo remove(Channel channel) {
        return connections.remove(channel);
    }
}

【Constants.java】

public class Constants
{
    public static final String spliterator = "----------------------end-----------------------";
}

服务端

pipeline
// out handler
socketChannel.pipeline().addLast(new StringEncoder());
// in handler
socketChannel.pipeline().addLast(new StringDecoder());
socketChannel.pipeline().addLast(new MyServerChannelInBoundHandler1());
socketChannel.pipeline().addLast(new MyServerChannelInBoundHandler2());

【MyServerChannelInBoundHandler1.java】

@Slf4j
public class MyServerChannelInBoundHandler1 extends SimpleChannelInboundHandler<String>
{
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("MyServerChannelInBoundHandler1.channelActive[" + ctx.channel().remoteAddress() + "]: ");
        ctx.writeAndFlush("MyServerChannelInBoundHandler1: I'm server, thanks for connected!\r\n");
        ConnectionInfos.put(ctx.channel(), new ConnectionInfo());  // 连接建立的时候放入一个连接对象
        super.channelActive(ctx);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) {
        log.info("MyServerChannelInBoundHandler1.channelRead0[" + ctx.channel().remoteAddress() + "]:" + msg);
        ConnectionInfo connectionInfo = ConnectionInfos.get(ctx.channel());
        StringBuffer stringBuffer = connectionInfo.getReadBuffer();
        stringBuffer.append(msg);
        AtomicReference<String> targetHolder = new AtomicReference<>();
        while(true){
            // 每读取到新数据就判断一下是否有完成的数据到达
            if( getTargetData(stringBuffer, true, targetHolder, connectionInfo.getReadBufferIndex()) ){
                ctx.fireChannelRead(targetHolder.get());    // 读取到完整大块数据之后传到下一个handler
                targetHolder.set(null);
                continue;
            } else {
                break;
            }
        }
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info("MyServerChannelInBoundHandler1.channelInactive[" + ctx.channel().remoteAddress() + "]: ");
        ConnectionInfos.remove(ctx.channel());
        super.channelInactive(ctx);
    }

    // 根据分隔符读取完整数据块
    private static boolean getTargetData(StringBuffer stringBuffer, 
                                         boolean modified, 
                                         AtomicReference<String> targetHolder, 
                                         AtomicInteger indexHolder)
    {
        int spliteratorIndex = stringBuffer.indexOf(Constants.spliterator, indexHolder.get());
        if( spliteratorIndex<0 ){
            indexHolder.set(Math.min(0, stringBuffer.length() - Constants.spliterator.length() - 2));
            return false;
        }

        String targetData = stringBuffer.substring(0, spliteratorIndex);
        if( modified ){
            stringBuffer.delete(0, spliteratorIndex + Constants.spliterator.length() + 2);
            indexHolder.set(0);
        } else {
            indexHolder.set(spliteratorIndex);
        }
        if( targetHolder!=null ){
            targetHolder.set(targetData);
        }
        return true;
    }
}

【MyServerChannelInBoundHandler2.java】

@Slf4j
public class MyServerChannelInBoundHandler2 extends SimpleChannelInboundHandler<String>
{
    // 参数【msg】就是从上一个handler传过来的完整的大块数据
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) {
        ConnectionInfo connectionInfo = ConnectionInfos.get(ctx.channel());
        AtomicInteger blockCounter = connectionInfo.getBlockCounter();
        blockCounter.getAndIncrement();
        log.info("MyServerChannelInBoundHandler2.channelRead0[" + ctx.channel().remoteAddress() + "]:" + msg);
        ctx.writeAndFlush(String.format("received [%d] blocks\r\n", blockCounter.get()));
    }
}

客户端

【MyClientInBoundHandler.java】

@Slf4j
public class MyClientInBoundHandler extends ChannelInboundHandlerAdapter
{
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("channelActive[" + ctx.channel().remoteAddress() + "]");

        ctx.write("Hello, Server, I'm client 1!\r\n");
        ctx.write(Constants.spliterator+"\r\n");    // 发送每1个大块分隔符
        
        ctx.write("Hello, Server, I'm client 2!\r\n");
        ctx.write(Constants.spliterator+"\r\n");    // 发送每2个大块分隔符
        ctx.flush();

        for( int i=0; i<100; i++ ){
            ctx.writeAndFlush(String.format("[%010d]\r\n", i));
            Thread.sleep(1L);
        }
        ctx.writeAndFlush(Constants.spliterator+"\r\n");    // 发送每3个大块分隔符

        for( int i=0; i<100; i++ ){
            ctx.writeAndFlush(String.format("[%010d]\r\n", i));
            Thread.sleep(1L);
        }
        ctx.writeAndFlush(Constants.spliterator+"\r\n");    // 发送每4个大块分隔符
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        log.info("channelReadComplete[" + ctx.channel().remoteAddress() + "]: " );
        super.channelReadComplete(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("channelRead[" + ctx.channel().remoteAddress() + "]: " + msg);
        super.channelRead(ctx, msg);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈鸿圳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值