Bytetomessagedecoder1trigger on socket removal, last read data processing @Override Public Final voidHandlerremoved (Channelhandlercontext ctx)throwsException {bytebuf buf=Internalbuffer (); if(Buf.isreadable ()) {bytebuf bytes=buf.readbytes (Buf.readablebytes ()); Buf.release (); Ctx.firechannelread (bytes); } cumulation=NULL; Ctx.firechannelreadcomplete (); HandlerRemoved0 (CTX); } 2read the data bytebuf Release,discardsomereadbytes method after the study Public voidChannelread (Channelhandlercontext ctx, Object msg)throwsException {recyclablearraylist out= Recyclablearraylist.newinstance ();//creating an array that has been wrapped Try { if(msginstanceofbytebuf) {BYTEBUF Data=(BYTEBUF) msg; if(cumulation = =NULL) {//whether to continue reading datacumulation =data; Try{Calldecode (CTX, cumulation, out); } finally { if(cumulation! =NULL&&!cumulation.isreadable ()) {cumulation.release (); Cumulation=NULL; } } } Else{//continue read processing Try { //Create a new space if there is not enough space left to open if(Cumulation.writerindex () > Cumulation.maxcapacity ()-data.readablebytes ()) {Bytebuf oldcumulation=cumulation; Cumulation= Ctx.alloc (). Buffer (Oldcumulation.readablebytes () +data.readablebytes ()); Cumulation.writebytes (oldcumulation); Oldcumulation.release (); } cumulation.writebytes (data); Calldecode (CTX, cumulation, out); } finally { if(cumulation! =NULL) { if(!cumulation.isreadable ()) {cumulation.release (); Cumulation=NULL; } Else{cumulation.discardsomereadbytes (); }} data.release (); } } } Else{out.add (msg); } } Catch(decoderexception e) {Throwe; } Catch(Throwable t) {Throw Newdecoderexception (t); } finally { intSize =out.size (); if(Size = = 0) {Decodewasnull=true; } Else { for(inti = 0; i < size; i + +) {Ctx.firechannelread (Out.get (i)); }} out.recycle (); } } //In fact, as long as the core code decode call business processing protected voidCalldecode (Channelhandlercontext ctx, bytebuf in, list<object>Out ) { Try { //Looping through data while(In.isreadable ()) {intoutsize =out.size (); intOldinputlength =in.readablebytes (); Decode (CTX, in, out);//Business Expansion Processing//Check If this handler is removed before continuing the loop. //If It was removed, it's not safe for continue to operate on the buffer. // // Seehttps://github.com/netty/netty/issues/1664 //If handler is deleted, then the data is not read if(ctx.isremoved ()) { Break; } //The following is not clearly written ..... if(Outsize = =out.size ()) { if(Oldinputlength = =in.readablebytes ()) { Break; } Else { Continue; } } if(Oldinputlength = =in.readablebytes ()) { Throw Newdecoderexception (Stringutil.simpleclassname (GetClass ())+ ". Decode () did not read anything but decoded a message."); } if(Issingledecode ()) { Break; } } } Catch(decoderexception e) {Throwe; } Catch(Throwable cause) {Throw Newdecoderexception (cause); } }
Netty Bytetomessagedecoder Analysis