zoukankan      html  css  js  c++  java
  • 10

    方法 职责
    writeInbound(Object... msgs) 将入站消息写入到EmbeddedChannel中
    readInbound() 从EmbeddedChannel中读取一个入站消息,任何返回的消息都穿过了整个ChannelPipeLine
    writeOutbound(Object... msgs) 将出站消息写入到EmbeddedChannel中
    readOutbound() 从EmbeddedChannel中读取一个出站消息,任何返回的消息都穿过了整个ChannelPipeLine

    10.1 示例

    //测试结果
    Received message:0
    Received Finished!
    Received message:1
    Received Finished!
    Received message:2
    Received Finished!
    embeddedChannel readInbound:0
    embeddedChannel readInbound:1
    embeddedChannel readInbound:2
    
    public class EmBeddedChannelTest {
        public static void main(String[] args) {
            ByteBuf byteBuf = Unpooled.buffer();
            for (int i = 0; i < 3; i++) {
                byteBuf.writeInt(i);
            }
    
            EmbeddedChannel embeddedChannel = new EmbeddedChannel();
    		
    		//获取channelPipeLine
            ChannelPipeline channelPipeline = embeddedChannel.pipeline();
            channelPipeline.addLast(new SimpleChannelInBoundHandlerTest());
            channelPipeline.addFirst(new DecodeTest());
    
    		//写入测试数据
            embeddedChannel.writeInbound(byteBuf);
    		
            System.out.println("embeddedChannel readInbound:"+embeddedChannel.readInbound());
            System.out.println("embeddedChannel readInbound:"+embeddedChannel.readInbound());
            System.out.println("embeddedChannel readInbound:"+embeddedChannel.readInbound());
    
        }
    
    }
    
    //解码器
    class DecodeTest extends ByteToMessageDecoder {
        @Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
            if( in.readableBytes()>=4 ){
                out.add(in.readInt());
            }
        }
    }
    
    //channelHandler
    class  SimpleChannelInBoundHandlerTest extends SimpleChannelInboundHandler {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("Received message:"+msg);
            System.out.println("Received Finished!");
            ctx.fireChannelRead(msg);
        }
    }
    
  • 相关阅读:
    Python数据分析与机器学习-Matplot_2
    Python数据分析与机器学习-Matplot_1
    1008. 数组元素循环右移问题 (20)
    Latex小技巧
    执行PowerShell脚本的时候出现"在此系 统上禁止运行脚本"错误
    Linux使用MentoHust联网线上校园网, 回到普通有线网络却连不上?
    Re:uxul
    Linux下nautilus的右键快捷菜单项设置
    从入门到入狱——搭讪技巧
    Latex命令
  • 原文地址:https://www.cnblogs.com/Desneo/p/7326393.html
Copyright © 2011-2022 走看看