The previous article covered the startup process of the Netty server, mainly explaining the creation of NioeventLoop and what bootstrap.bind(9999) does. This section explains netty inbound. We already know that when the server starts up, the for(;) in NioEventLoop It’s been an endless cycle, mainly doing those three things. If you are careful, you can find that you already know that there is such an inbound operation in step2 processSelectedKeys of the server startup source codeTo prepare the debug environment, close all breakpoints, start server debug first, and then make a breakpoint as shown in the following figureHere, I write a Netty client to establish a connection with the server, and the client sends a message to the server, the purpose of which is to let the server listen to accept events and read events, so as to facilitate the subsequent debugging of the inbound process. Start the client (don’t start the client with debug)You can see that the selectedKeys collection has one element and that the current NioEventLoop is a bossGroup. Then you go all the way down into the processSelectedKey method and you see a readOps, which must be a connection event.Ops is 16, which goes into the read method unsafe.readThis readBuf collection puts NioSocketChannel. Let’s go to the pipeline. FireChannelRead (readbuf.get (I)) methodThen go down into the invokeChannelRead methodThen enter invokeChannelRead (m);We see that this method has a channelRead(this, MSG); Go to channelRead and see what’s going on insideWe see only ctx.fireChannleread () entering the channelRead methodThis method has an invokeChannelRead(findContextInbound(MASK_CHANNEL_READ), MSG) method. We can take a look insideThis mask is an inbound identifier, and then constantly next to find matching nodes. Finally, the CTX (which is serverBootstrpAcceptor, an inbound handler) is returned. Exiting the findContextInbound method is followed by the invokeChannelRead method, which is then called next. InvokeChannelRead (m). Please see belowEnter the invokeChannelRead methodIf you go to the channelRead(this, MSG) method, you can see that the channelRead class is an inner class of ServerBootstrp, which inherits the inBoundHandlerAdapter. If we look at this code, we see that MSG is a channel, then we get a pipeline based on the channel, and add a childHandler to itSo what is childHandler, we can look at thatThis childHandler is the channel initialization object that we put in The NettyServer. This channel initialization object is the handler that handles our custom business processing on the server sideLet’s go further and look at childGroup, which is actually a workGroup, and then there is an important step to register the server channel with the workGroup.Continue into the register method and go to eventloop.execute. The eventLoop is the workGroup Execute (eventLoop. Execute), queue (startThread), if (eventLoop. Execute), queue (startThread) A cycle ofLet’s go to the startThread method and see We see that there’s a run method, so let’s follow through. You can see the for(;;) in run. Step1 step2 step3 is described in the netty thread model diagram.The important thing to know here is that the loop is a workerGroupFollowing the code, we go to step2 processSelectedKeys, not step1 because there are no eventsFind that Step2 also has no selectedKey to process (because the workGroup has not registered the channel at this point), and exit to enter Step3, which enters finally.After obtaining the task, access safeExecute. So let’s go into the safeExecute method and see what we’re doing, okayDo the channel registration in the run method, enter register0, and see that there is a doRegister method.Go to the doRegister methodLet’s look at what the javaChannel here gets. Is a socketChannel, previously registered bossGroup is ServerSocketChannel. Then we get a socketChannel and register it with the workGroup’s Select selector.Continue to go down, the execution pipeline. InvokeHandlerAddedIfNeeded ();Let’s go into the method and go down Prepare to execute a task. Enter the executeAdd the handlerEnter the CTX. CallHandlerAdded ()And then you go downEnter the channel to initialize the objectCall the initChannel method, find the channel initialization object back to our NettyServer, then execute the initChannel method inside, and add our custom message inbound handler.Go all the way down F8 and you’re done, and then you’re done with the message inbound process. That’s the end of the client connection event.
Read event flow:
Next we need to handle the event of the message read (which we mentioned at the beginning of this article handled the 2 event)Go to the processSelectedKey method of step2 at F8 and see that the Ops event is OP_READEntering the read method brings you to the method of this class and reads the number of message bytes (30 bytes) sent by the client.Next, the processing read event is published. Is this the first inbound operation of the message?The method to enter the beginning of fireKeep going downExecute the next. InvokeChannelRead method and enter the channelRead method Step down into the invokeChannelRead method Next. InvokeChannelRead (m);Entering this channelRead method brings us to this channelRead method of our NettyServerHandler Then F8 goes down and you can see that the event for the read message is done by our workerGroupOkay, so this articleTo this endThank you for your patience.