Netty Bytetomessagedecoder Analysis

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.