Netty source code parsing (a) : start

Netty Channel: Netty Channel

Netty source code parsing (three) : Netty Future and Promise

Netty source code parsing (four) : Netty’s ChannelPipeline

Netty source code parsing (five) : Netty thread pool analysis

Netty source code parsing (six) : Channel register operation

NioEventLoop (NioEventLoop

Return to Channel register operation

Netty source code parsing (nine) : Connect process and bind process analysis


Today! Mr. Lighthouse told us:

Netty Channel

In this section, we’ll look at how NioSocketChannel is related to the underlying SocketChannel in the JDK in a one-to-one relationship. NioServerSocketChannel and ServerSocketChannel have the same one-to-one relationship.



Channel (…) is called during Bootstrap (client) and ServerBootstrap (server) startup. Methods:

Next, let’s look at channel(…) Method source:

// AbstractBootstrap
public B channel(Class<? extends C> channelClass) {
    if (channelClass == null) {
        throw new NullPointerException("channelClass");
    }
    return channelFactory(new ReflectiveChannelFactory<C>(channelClass));
}Copy the code

As you can see, this method is just an instance of channelFactory as ReflectiveChannelFactory, and let’s see what the channelFactory is:

newChannel()Method is the only method in the ChannelFactory interface,
The factory patternYou’re all familiar with it. And we can see,
ReflectiveChannelFactory#newChannel()The ChannelFactory newChannel() method is used to create a Channel using the no-argument constructor that reflects a call to the Channel.


  • For NioSocketChannel, because it functions as a client, it is created at connect(…). When;

  • For NioServerSocketChannel, which acts as a server-side function, it is created on the binding port bind(…). From time to time.

Let’s briefly trace the creation of a NioSocketChannel in Bootstrap as a client and see how NioSocketChannel is associated with SocketChannel in JDK:

1// Bootstrap
2public ChannelFuture connect(String inetHost, int inetPort) {
3    return connect(InetSocketAddress.createUnresolved(inetHost, inetPort));
4}Copy the code

Then look inside and see this method:

1public ChannelFuture connect(SocketAddress remoteAddress) {
2    if (remoteAddress == null) {
3        throw new NullPointerException("remoteAddress"); 5 validate(); 6return doResolveAndConnect(remoteAddress, config.localAddress());
7}Copy the code

Continue to:

1 private ChannelFuture 2 private ChannelFuturedoResolveAndConnect(final SocketAddress remoteAddress, final SocketAddress local4 Final ChannelFuture regFuture = initAndRegister(); 5 final Channel channel = regFuture.channel(); 6... 7}Copy the code

And then, let’s see
initAndRegister()Methods:

1final ChannelFuture initAndRegister() { 2 Channel channel = null; 3 the try {4 said, / / in front of us here will provide the Channel instantiation 5 Channel = channelFactory. NewChannel (); 6 init(channel); 7 } catch (Throwable t) { 8 ... 9} 10... 11return regFuture;
12}Copy the code

We found it.
channel = channelFactory.newChannel()This line of code, as mentioned earlier, calls the no-argument constructor for the corresponding Channel.
Then we can look at the NioSocketChannel constructor:

1public NioSocketChannelThis (DEFAULT_SELECTOR_PROVIDER); this(DEFAULT_SELECTOR_PROVIDER); 4} 5 6public socketChannel (SelectorProvider provider) {7 The newSocket(provider) method creates JDK SocketChannel 8 this(newSocket(provider)); 9}Copy the code

We can see that a SocketChannel instance of the JDK NIO is created when newSocket(provider) is called:

1private static SocketChannel newSocket(SelectorProvider provider) {2 try {3 // create SocketChannel instance 4return provider.openSocketChannel();
5    } catch (IOException e) {
6throw new ChannelException("Failed to open a socket.", e); 8 7}}Copy the code

NioServerSocketChannel is also very simple from
ServerBootstrap#bind(...)The way to do it is to click all the way in.
NioSocketChannel instantiates the underlying JDK SocketChannel first. NioServerSocketChannel instantiates ServerSocketChannel first:


Socketchannel constructor (NioSocketChannel constructor)

1public NioSocketChannel(SelectorProvider provider) {
2    this(newSocket(provider));
3}Copy the code


We just saw that newSocket(provider) creates the underlying SocketChannel instance. Let’s look at the constructor:

1public NioSocketChannel(Channel parent, SocketChannel socket) {
2    super(parent, socket);
3 config = new NioSocketChannelConfig(this, socket.socket());
4}Copy the code

There are two lines of code above. The second is a simple instantiation of the internal NioSocketChannelConfig instance, which is used to hold the configuration information for the channel. There is nothing here that we need to worry about now, so we will skip it.
The first line calls the parent constructor, setting the SocketChannel’s non-blocking mode in addition to its properties:

1protected AbstractNioByteChannel(Channel parent, SelectableChannel) {2 3 super(parent, CH, selectionKey.op_read); 7protected AbstractNioChannel(Channel parent, SelectableChannel ch, intreadInterestOp) { 8 super(parent); 9 this.ch = ch; OP_READ = selectionkey. OP_READ = 11 this.readinterestop =readInterestOp; 12 try {13 // ****** set channel non-blocking mode ****** 14 ch.configureblocking (false); 15 } catch (IOException e) { 16 ...... 18 17}}Copy the code

NioServerSocketChannel is constructed similarly, also set to non-blocking, and then set the selectionkey. OP_ACCEPT event that the server cares about:

1public ServerSocketChannel(ServerSocketChannel channel) {2 public ServerSocketChannel(ServerSocketChannel channel) { 3 super(null, channel, selectionkey.op_accept); 4 config = new NioServerSocketChannelConfig(this, javaChannel().socket()); 5}Copy the code

We instantiate SocketChannel or ServerSocketChannel in the JDK layer and set it to non-blocking mode.