Netty4 Connectivity Steps

Source: Internet
Author: User

Reprint: http://xw-z1985.iteye.com/blog/1973205

The steps that occur in sequence on the server

  1. Set up the service-side monitoring socket serversocketchannel, and the corresponding pipeline pipeline;
  2. Starts the boss thread, registers the Serversocketchannel with the selector that the boss thread holds, and assigns the Selectionkey returned by the registration to the Serversocketchannel associated selectionkey variable;
  3. Triggering the Channelregistered event in the serversocketchannel corresponding pipeline;
  4. Binding IP and Ports
  5. The Channelactive event is triggered and the op_accept location of the Selectionkey associated with the Serversocketchannel is 1.
  6. After the client initiates a connect request, the Select loop that the boss thread is running detects that the Serversocketchannel's accept event is ready, and a connected socket Socketchannel is established through the Accept system call. and create a corresponding pipeline for it;
  7. Triggering the Channelread event on the service-side listener socket corresponding to the pipeline;
  8. The Channelread event is responded by the Channelread method of the Serverbootstrapacceptor: adding a Channelinitializer processor to the pipe that corresponds to the connected socket, starting a worker thread, and joins the registered task of the connected socket into the task queue of the worker thread;
  9. The worker thread performs the registration task of the connected socket: registers the connected socket with the selector held by the worker thread and assigns the Selectionkey returned by the registration to the Selectionkey variable associated with the connected socket To trigger the Channelregistered event in a pipe that corresponds to a connected socket The Channelregistered event is responded by Channelinitializer's Channelregistered method: Adding a custom processor (such as Echoserverhandler) to a pipe that corresponds to a connected socket ; The Channelactive event is triggered in the pipeline that corresponds to the connected socket; The Channelactive event is responded to by the Channelactive method of the inbound processor in the pipeline that corresponds to the connected socket The op_read location of the Selectionkey associated with the connected socket is 1, so that the selector associated with the worker thread starts listening on the read event of the connected socket.
  10. While the worker thread is running, the boss thread then triggers the Channelreadcomplete event in the pipeline that corresponds to the service-side listener socket.
  11. After the client sends a message to the server, the selector loop that the worker thread is running detects that the read event is ready for the connected socket. The message is read from the receive buffer of the socket to the cache allocated by the Adaptiverecvbytebufallocator (which can adaptively adjust the allocated cache size) through the read system call;
  12. Trigger the Channelread event in the pipeline that corresponds to the connected socket;
  13. The Channelread event is responded by the Channelread method of the Echoserverhandler processor: Performs a write operation to store the message in Channeloutboundbuffer;
  14. Trigger the Channelreadcomplete event in the pipeline that corresponds to the connected socket;
  15. The Channelreadcomplete event is responded by the Channelreadcomplete method of the Echoserverhandler processor: The flush operation flush the message from Channeloutboundbuffer to the send buffer of the socket ;

Steps that the client takes in sequence

    1. Set up socket socketchannel, and corresponding pipe pipeline;
    2. Starts the client thread, registers the Socketchannel with the client thread-held selector, and assigns the Selectionkey returned by the registration to the Socketchannel associated selectionkey variable;
    3. Triggering channelregistered events;
    4. The Channelregistered event is responded by Channelinitializer's Channelregistered method: Adding the client-defined processor (such as Echoclienthandler) sequentially to the pipeline;
    5. Initiate a connect request to the server and the Op_connect location of the socketchannel associated Selectionkey is 1;
    6. Starting three handshake, the client thread is running a select loop that detects that the Socketchannel connect event is ready, then the associated Selectionkey's op_connect location is 0, And then by calling Finishconnect to complete the establishment of the connection;
    7. Triggering channelactive events;
    8. The Channelactive event is responded by the Echoclienthandler channelactive method, and the message is sent to the server by calling the Ctx.writeandflush method;
    9. The message is stored in channeloutboundbuffer first, (if the size of all non-flush messages stored by Channeloutboundbuffer exceeds the high watermark Writebufferhighwatermark (the default value is 64 * 1024), the Channelwritabilitychanged event is triggered)
    10. The message is then flush from the channeloutboundbuffer to the socket's send buffer, or if the size of all non-flush messages stored by Channeloutboundbuffer is smaller than the low watermark, Will trigger the Channelwritabilitychanged event)

Netty4 Connectivity Steps

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.