Netty New Connection Access

Source: Internet
Author: User

Problem

Where is the 1.Netty detection of new connection access?

A: The first process of the boss thread rotation the accept event, and then the second process of the boss thread creates the connection through the Accept method of the channel at the bottom of the JDK.

2. How is the new connection registered to the Nioeventloop thread?

A: The assignment of the new connection Nioeventloop and the selector registration can be answered. The boss thread calls Chooser's Next method, gets a nioeventloop, and registers the connection with Nioeventloop selector.

Netty New connection Access processing logic

(1) Detect new connection: New Connection selector polling the Accept event via the server-side channel-bound

(2) Create Niosocketchannel: Jdknio-based Channle creates a Netty Niosocketchannel, which is the client channel

(3) Assign a thread and register Selector:netty to the client channel to assign a nioeventloop, and register this channel with the nioeventloop corresponding selector, The subsequent reading and writing of this channel is thus Nioeventloop managed

(4) Register the read event to selector: The process of registration and the server-side initiate registration event are reused for the same logic.

(1) Detecting new connections

Processselectedkey (Key,channel) [Entry]: Add breakpoint

Niomessageunsafe.read ()

Doreadmessages () [While Loop]

Javachannel (). Accept ()

    • Run Server.java's main function to start the server
    • In terminal, enter Telnet 127.0.0.1 8888 (if Telnet is not an internal or external command ...) Then in Control Panel-programs-enable or disable Windows features, tick the Telnet client)
    • One-step, one-step breakpoint debugging
(2) Create Niosocketchannel

New Niosocketchannel (PARENT,CH) [entrance]

Abstractniobytechannel (P,ch,op-read)

Configureblocking (False) &save op

Create Id,unsafe,pipeline

New Niosocketchannelconfig ()

Settcpnodelay (True) disables the Nagle algorithm, small packets are emitted to reduce latency

(3) Assigning threads and registering selector

The pipeline composition of channel of service end

Head->serverbootstrapacceptor->tail

Among them serverbootstrapacceptor do the following things:

    • Add Childhandler
    • Set options and Attrs
    • Select Nioeventloop and register selector
(4) Register to selector read the classification of channel in the event Netty
    • Nioserversocketchannel: Service-side channel
    • Niosocketchannel: Client Channel
    • Unsafe

Different points of the server channel and client channel:

(a) client channel (Abstractniobytechannel) registers a read event with the Abstractniochannel

Service-side channel (Abstractniomessagechannel) registers an accept event with the Abstractniochannel

The common parts of the service-side channel and the client channel are implemented by Abstractniochannel

(b) Different types of unsafe at the lower levels of the channel

Client Channel Correspondence Niobyteunsafe: Read IO data

Service-Side channel correspondence Niomessageunsafe: Read a connection

Netty New Connection Access

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.