zoukankan      html  css  js  c++  java
  • Netty(7-2)传List

    ObjectEchoServer

    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new ObjectEncoder());
                        p.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                        p.addLast(new ObjectEchoServerHandler());
                    }

    ObjectEchoServerHandler

    public class ObjectEchoServerHandler extends ChannelInboundHandlerAdapter {
    
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            ctx.write(msg);
        }
    
        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            ctx.flush();
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            cause.printStackTrace();
            ctx.close();
        }
    
    }

    ObjectEchoClient

    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new ObjectEncoder());
                        p.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                        p.addLast(new ObjectEchoClientHandler());
                    }

    ObjectEchoClientHandler

    public class ObjectEchoClientHandler extends ChannelInboundHandlerAdapter {
        private final List<Integer> firstMessage;
        
        public ObjectEchoClientHandler() {//每个channel(connection)都会new该handler
            firstMessage = new ArrayList<Integer>(ObjectEchoClient.SIZE);
            for (int i = 0; i < ObjectEchoClient.SIZE; i++) {
                firstMessage.add(Integer.valueOf(i));
            }
        }
    
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            ctx.writeAndFlush(firstMessage);
        }
    
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            //Echo back the received object to the server.
            ctx.write(msg);
        }
    
        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            ctx.flush();
        }
    
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            cause.printStackTrace();
            ctx.close();
        }
    
    }
  • 相关阅读:
    小球(总结sort和cmp函数、结构体排序)
    垃圾装袋(标记法)【标记思想】
    种树(标记思想)【贪心算法】
    PHP 配置文件
    最大前驱路径
    PHP代码片段
    PHP 中的Trait
    BootStrapTable 错误
    工作两周总结
    工作一周总结
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/9749936.html
Copyright © 2011-2022 走看看