java - Using Protocol Buffers and Netty 4.1.6 -
i have following server , client initializers (both have extremely similar code sch
changes cch
client, both representing respective handlers).
@override public void initchannel(socketchannel ch) throws exception { ch.pipeline().addlast("handler", sch); ch.pipeline().addlast(new commonclasshandler()); ch.pipeline().addlast("framedecoder", new protobufvarint32framedecoder()); ch.pipeline().addlast("protobufdecoder", new protobufdecoder(server.mymessage.getdefaultinstance())); ch.pipeline().addlast("frameencoder", new protobufvarint32lengthfieldprepender()); ch.pipeline().addlast("protobufencoder", new protobufencoder()); }
i wish use binary format when sending commands/actions client or server, therefore, i'm using google's protocol buffers.
here create builder
when dealing client's input:
while (channel.isopen()) { client.mymessage.builder builder = client.mymessage.newbuilder(); string input = in.readline(); // save console input builder.setkeyword(input); // set value of keyword said input channel.writeandflush(builder.build()); // send build server }
and here method automatically called when server / client receive message:
@override public void channelread(channelhandlercontext ctx, object msg) { bytebuf bb = (bytebuf)msg; string order = bb.tostring(charset.defaultcharset()); system.out.println(order); // displays received data server.mymessage.builder builder = server.mymessage.newbuilder(); builder.setkeyword("301"); ctx.writeandflush(builder.build()); }
1) when displaying contents of bytebuf
displays 2 unknown characters , "\n" prior message; maybe should handle received data way have normal display?
2) after displaying received data, server should send answer "301" client, no use, client not display (the method not called in client handler), there apparent reason?
please excuse questions, there little documentation concerning use of protocol buffers netty 4.1.6.
you adding handler @ start of pipeline ch.pipeline().addlast("handler", sch);
, should putting @ end of pipeline, after protobufdecoder.
once make change should start receiving mymessage
msg
instead of bytebuf
. i'm guessing unknown characters seeing right frame lengths stripped frame decoder have, won't run until after handler way have things setup right now.
Comments
Post a Comment