"Reprint" NiO client sequence diagram

Source: Internet
Author: User
Tags ack

Step One: Open Socketchannel, bind the client-local address (optionally, the default system will randomly assign an available local address), the sample code is as follows:

Socketchannel Clientchannel = Socketchannel.open ();

Step two: Set Socketchannel to non-blocking mode while setting the TCP parameters of the client connection, the sample code is as follows:

Clientchannel.configureblocking (FALSE);

Socket.setreuseaddress (TRUE);

Socket.setreceivebuffersize (buffer_size);

Socket.setsendbuffersize (buffer_size);

Step three: The asynchronous connection server side, the sample code is as follows:

boolean connected = clientChannel.connect(new InetSocketAddress(“ip”,port));

Step four: Determine if the connection is successful, if the connection is successful, then directly register the read status bit to the multiplexer, if there is no successful connection (asynchronous connection, return False, indicating that the client has sent the sync packet, the server does not return an ACK packet, the physical link has not yet been established), the sample code is as follows:
if (connected)
{
Clientchannel.register (selector, selectionkey.op_read, Iohandler);
}
Else
{
Clientchannel.register (selector, selectionkey.op_connect, Iohandler);
}

Step five: Register the Op_connect status bit with the reactor thread's multiplexer, and listen for the TCP ACK response on the server side, as shown in the sample code:

Clientchannel.register (selector, selectionkey.op_connect, Iohandler);

Step Six: Create a reactor thread, create a multiplexer, and start the thread with the following code:

Selector Selector = Selector.open ();
New Thread (New Reactortask ()). Start ();

Step Seven: The multiplexer threads the infinite loop of the Run method in vivo polling the Ready key, the code is as follows:
int num = Selector.select ();
Set Selectedkeys = Selector.selectedkeys ();
Iterator it = Selectedkeys.iterator ();
while (It.hasnext ()) {
if (key.isconnectable ())
Handlerconnect ();
}

Step nine: Determine the connection result, if the connection is successful, register the read event to the multiplexer, the sample code is as follows:

if (Channel.finishconnect ())
Registerread ();

Step Ten: Register the Read event to the multiplexer:

Clientchannel.register (selector, selectionkey.op_read, Iohandler);

Step 11: Asynchronously read the client request message to the buffer, the sample code is as follows:

int readnumber = Channel.read (Receivedbuffer);

Step 12: Encode the Bytebuffer, if there is a half-packet message receive buffer reset, continue to read the subsequent messages, the decoding of the successful message encapsulated into a task, posted to the business thread pool, business logic orchestration, the sample code is as follows:

Object message = NULL;
while (Buffer.hasremain ())
{
Bytebuffer.mark ();
Object message = decode (Bytebuffer);
if (message = = NULL)
{
Bytebuffer.reset ();
Break
}
Messagelist.add (message);
}
if (!bytebuffer.hasremain ())
Bytebuffer.clear ();
Else
Bytebuffer.compact ();
if (messagelist! = null &!messagelist.isempty ())
{
for (Object messagee:messagelist)
Handlertask (Messagee);
}

Step 13: Encode the Pojo object into Bytebuffer, call the asynchronous write interface of Socketchannel and send the message to the client asynchronously, with the sample code as follows:

Socketchannel.write (buffer);

The timing diagram created by the client is as follows:

The original link is as follows: http://ifeve.com/netty5-user-guide/

"Reprint" NiO client sequence diagram

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.