Netty source code learning-Class 1

Source: Internet
Author: User
Document directory
  • Client
  • Server
  • Model-1
  • Model-2
  • Model-3
  • Model-netty
  • Interestops, selectionkey
  • Upstream:
  • Downstream:
  • To be continued...
Demo Client

 

Server

 

 

 

NIO model-1

 

Model-2

 

Model-3

 

 

Model-netty

Boss

Worker

 

Channel Design

 

 

Scenario

Connect-> Read-> write-> close

Interestops, selectionkey

Op_read readable and writable. neither read nor write operation has been susponded.

Op_write is unreadable and cannot be written. Both has been susponded.

Op_read_write readable cannot be written. write operation has been susponded.

Op_none is not readable and writable. Read operation has been susponded.

 

 

Pipeline Design upstream:

L huhuudecoder

L messagereceived, exceptioncaught, channelopen, channelclosed, channelbound, channelunbound, channelconnected, writecomplte, channeldisconnected, changeinterestchanged

Downstream:

L huhuuencoder

L write, bind, unbind, connect, disconnect, close

 

 

 

Bytebuffer design to be continued...

 

Network Abstract: netty workflow analysis

The following uses the echo example in netty to track the process and briefly

Server startup> client connection> server processing client data <> client processing server data

 

1: Client connection:

 

Let's look at this line of code:

Bootstrap. Connect (NewInetsocketaddress (host, Port ));

Connect to the server using the clientbootstrap help class.

Debug source code to find that a channel class performs the connect operation.

How does this channel come from? It is actually obtained from the previous channelfactory and channelpipelinefactory.

Channel. Connect-> abstractchannel. Connect-> channels. Connect (...);

Channels is a channel help class that encapsulates some common operations. During encapsulation operations, events are basically triggered.

Initiate a downstream event of connectd.

 

All events are thrown to channelpipeline for management. channelpipeline uses the responsibility chain mode to send the event to channelhandler registered in pipeline, which is processed by channelhandler. If all the channelhandler is traversed, it is handed over to the channelsink for processing. The channelsink performs different processing based on different events. For the connect event, after the channelsink sends the connection operation, it registers the channel to nioworker. Any subsequent events are processed by nioworker (encapsulation selector operation.

 

The client connection process is as follows:

Clientbootstrap. Connect-> channel. Connect->

Abstractchannel. Connect-> channels. Connect (...)

-> Send the connect event-> channelsink-> initiate the actual connection operation-> register the channel to nioworker

 

2: server startup:

Bootstrap. BIND (...) -> Trigger serversocketchannel.Open() Event

-> Capture Open events, channel. Bind-> channels. BIND (...) -> Initiate the BIND command-> pipelinesink for processing-> use socket for bind and wait for the connection event.

 

3: servers process connections:

After the server is started, nioserversocketpipelinesink. Boss. Run () listens to the accept event-> captures the accept event-> registers nioworker for niosocketchannel

-> Register the op_read listener with Java. NiO. socketchannel.

 

4: the client starts to send data to the server:

After the client connects to the server, it will initiate the connected upstream event-> process through pipeline-> simplechannelupstreamhandler. handleupstream ()->

Echoclienthandler. channelconnected ()

 

5. The server receives and processes data.

Receive data:

Nioworker. Run ()-> nioworker. processselectedkeys ()->

Nioworker. Read () encapsulates the data read from the socketchannel

Channelbuffer-> send upstream event:Firemessagereceived(Channel, buffer)-> processed by hanlder registered to the pipeline: echoserverhandler. messagereceived (...)

 

Send data:

E. getchannel (). Write (E. getmessage ();-> channels. Write ()->

Initiate a downstream event-> nioserversocketpipelinesink. handleacceptedsocket () puts the event to the channel and sends it through nioworker. writefromusercode.

6. Client: The process of the client is similar to that of the server.

 

Summary:

1: netty encapsulates operations into events. For example, when a connection is initiated, a connect downstream event is generated. After the connection is completed, an upstream connect event is generated.

2: all events are transmitted in the pipeline. During the transfer process, the events may be registered with the handler in the pipeline for processing.

3: After pipeline is transmitted, all requests must be processed through the channelsink. By default, sink processes trivial operations, such as connection and read/write.

4: channels: almost all operations can be found here. Of course, Channels generally send events.

5: nioworker: Core class for handling Io events, and undertakes the distribution responsibility.

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.