欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Netty對Protocol Buffer的支持

一.簡(jiǎn)介

  在上一篇博文中筆者已經(jīng)介紹了google的Protocol Buffer的使用,那么本文筆者就開(kāi)始介紹netty對Protocol Buffer的支持。

二.編碼的實(shí)現

2.1 服務(wù)端啟動(dòng)代碼

public class ServerTest {    public static void main(String[] args) throws Exception {        EventLoopGroup bossGroup = new NioEventLoopGroup();        EventLoopGroup workerGroup = new NioEventLoopGroup();                try{            ServerBootstrap serverBootstrap = new ServerBootstrap();                        serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)                           .handler(new LoggingHandler(LogLevel.INFO))                           .childHandler(new ServerChannelInitilizer());                        ChannelFuture channelFuture = serverBootstrap.bind(8989).sync();            channelFuture.channel().closeFuture().sync();        }finally{            bossGroup.shutdownGracefully();            workerGroup.shutdownGracefully();        }    }}

2.2 服務(wù)端管道初始化代碼

public class ServerChannelInitilizer extends ChannelInitializer<SocketChannel>{    @Override    protected void initChannel(SocketChannel ch) throws Exception {        ChannelPipeline pipeline = ch.pipeline();                /**         * 采用Base 128 Varints進(jìn)行編碼,在消息頭上加上32個(gè)整數,來(lái)標注數據的長(cháng)度。         */        pipeline.addLast("protobufVarint32FrameDecoder", new ProtobufVarint32FrameDecoder());        pipeline.addLast("protobufDecoder", new ProtobufDecoder(PersonsBook.AddressBook.getDefaultInstance()));                /**         * 對采用Base 128 Varints進(jìn)行編碼的數據解碼         */        pipeline.addLast("protobufVarint32LengthFieldPrepender", new ProtobufVarint32LengthFieldPrepender());        pipeline.addLast("protobufEncoder", new ProtobufEncoder());                        pipeline.addLast("serverHandler", new ServerHandler());    }}

2.3 服務(wù)端Handler處理

public class ServerHandler extends SimpleChannelInboundHandler<AddressBook>{    @Override    protected void channelRead0(ChannelHandlerContext ctx, AddressBook msg) throws Exception {        List<Person> list = msg.getPersonList();        list.forEach(p -> System.out.println(p.getName() + ";;" + p.getId() + ";;" + p.getEmail()));    }}

2.4 客戶(hù)端啟動(dòng)程序

public class ClientTest {    public static void main(String[] args) throws Exception {        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();                try{            Bootstrap bootstrap = new Bootstrap();            bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)                     .handler(new ClientChannelInitializer());                        ChannelFuture channelFuture = bootstrap.connect("localhost", 8989).sync();            channelFuture.channel().closeFuture().sync();        }finally{            eventLoopGroup.shutdownGracefully();        }    }}

2.5客戶(hù)端通道初始化

public class ClientChannelInitializer extends ChannelInitializer<SocketChannel>{    @Override    protected void initChannel(SocketChannel ch) throws Exception {        ChannelPipeline pipeline = ch.pipeline();                pipeline.addLast("protobufVarint32FrameDecoder", new ProtobufVarint32FrameDecoder());        pipeline.addLast("protobufDecoder", new ProtobufDecoder(PersonsBook.AddressBook.getDefaultInstance()));        pipeline.addLast("protobufVarint32LengthFieldPrepender", new ProtobufVarint32LengthFieldPrepender());        pipeline.addLast("protobufEncoder", new ProtobufEncoder());                pipeline.addLast("clientHandler", new ClientHandler());    }}

2.6 客戶(hù)端Handler處理代碼

public class ClientHandler extends SimpleChannelInboundHandler<AddressBook>{    @Override    protected void channelRead0(ChannelHandlerContext ctx, AddressBook msg) throws Exception {            }        @Override    public void channelActive(ChannelHandlerContext ctx) throws Exception {        AddressBook ab = AddressBook.newBuilder()                    .addPerson(Person.newBuilder().setEmail("123@qq.com").setId(23).setName("zhangsan"))                    .addPerson(Person.newBuilder().setEmail("789@163.com").setId(45).setName("lisi"))                    .build();                ctx.writeAndFlush(ab);    }}

三.程序運行

  先運行服務(wù)端啟動(dòng)代碼,在運行客戶(hù)端啟動(dòng)代碼。

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
從netty
3個(gè)netty5的例子,簡(jiǎn)單介紹netty的用法
netty + protobuf 傳輸多個(gè)類(lèi)
事件管道模型
教你正確地利用Netty建立連接池
Netty實(shí)戰入門(mén)詳解——讓你徹底記住什么是Netty(看不懂你來(lái)找我)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久