Objective
TCP mode before the transmission of PROTOCOLBUF mode, the back of a websocket transport protocolbuf business, the current basic has been stabilized, now the codec part of the record.
"Assembly of Server Parts"
The core idea is that the protocolbuf is a byte[] stream, and the WebSocket object is an object in Netty's own codec, where the data part is the byte stream after the PB serialization (that is, the content of WS is a binary byte array), so you need to do the conversion on the encoder and decoder.
protected void Initchannel (Channel ch) throws Exception {
ch.pipeline (). AddLast ("ReadTimeout", new Readtimeouthandler (45)); Long time does not write will break
Ch.pipeline (). AddLast ("Httpservercodec", New Httpservercodec ());
Ch.pipeline (). AddLast ("Chunkedwriter", New Chunkedwritehandler ());
Ch.pipeline (). AddLast ("Httpaggregator", New Httpobjectaggregator (65535));
Ch.pipeline (). AddLast ("Wsprotocoldecoder",
new Websocketserverprotocolhandler (Websocket_path, NULL, true));
Ch.pipeline (). AddLast ("Wsbinarydecoder", New Websocketframedecoder ()); WS decodes into bytes
ch.pipeline (). AddLast ("Wsencoder", New Websocketframeprepender ());//bytes encoded as WS
Ch.pipeline (). AddLast ("Protobufencoder", New Protobufencoder ()); BP is encoded into byte
ch.pipeline (). AddLast ("Protobufdecoder", New Protobufdecoder (Protocol.getdefaultinstance ()));
Ch.pipeline (). AddLast (New Protobufhandler ());
}
Core is
Ch.pipeline (). AddLast ("Wsbinarydecoder", New Websocketframedecoder ()); WS decoded into bytes
Ch.pipeline (). AddLast ("Wsencoder", New Websocketframeprepender ()); Bytes encoded into WS Two pieces of code, he above is Netty the WebSocket protocol codec, the following is the netty of the PB protocol with the codec, the role is to convert the code
"Decoding part"
Package com.seeplant.netty;
Import java.util.List;
Import Io.netty.buffer.ByteBuf;
Import Io.netty.buffer.PooledByteBufAllocator;
Import Io.netty.channel.ChannelHandlerContext;
Import Io.netty.handler.codec.MessageToMessageDecoder;
Import Io.netty.handler.codec.http.websocketx.WebSocketFrame;
/**
* from the WS Bytebuf, passed to the next layer (Pbdecoder), the next layer with BYTEBUF organized into BP/public
class Websocketframedecoder extends messagetomessagedecoder<websocketframe> {
@Override
protected void decode (Channelhandlercontext CTX, Websocketframe msg, list<object> out) throws Exception {bytebuf buff
= msg.content ();
byte[] messagebytes = new byte[buff.readablebytes ()];
Buff.readbytes (messagebytes);
Direct Memory Care
bytebuf bytebuf = PooledByteBufAllocator.DEFAULT.buffer ();//Direct Memory
bytebuf.writebytes ( messagebytes);
Out.add (Bytebuf.retain ());
}
Note that when the next decoder is passed, the Bytebuf object needs to be retain or the Bytebuf object will be destroyed by the decoder.
"Coding section"
Package com.seeplant.netty;
Import java.util.List;
Import Io.netty.buffer.ByteBuf;
Import Io.netty.channel.ChannelHandlerContext;
Import Io.netty.handler.codec.MessageToMessageEncoder;
Import Io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
Import Io.netty.handler.codec.http.websocketx.WebSocketFrame;
/**
* is packaged from bytes into websocket/public
class Websocketframeprepender extends messagetomessageencoder< bytebuf> {
@Override
protected void encode (Channelhandlercontext ctx, Bytebuf msg, list<object> out ) throws Exception {
Websocketframe websocketframe = new Binarywebsocketframe (msg);
Out.add (Websocketframe);
}
The encoder is very simple, that is, the PB byte flow out of the websocketframe, and is the binary form of the Wsframe