Netty source code interpretation

New Connection access

Let’s continue with the previous chapter and take a closer look at the source code for new connection access. See io.net ty. Channel. Nio. AbstractNioMessageChannel. NioMessageUnsafe# read

public void read(a) {...try {
                try {
                    do {
                        Read SocketChannel into List readBuf
                        intlocalRead = doReadMessages(readBuf); . }while (allocHandle.continueReading());
                } catch (Throwable t) {
                    exception = t;
                }

                int size = readBuf.size();
                for (int i = 0; i < size; i ++) {
                    readPending = false;
                    SocketChannel = SocketChannel = SocketChannel = SocketChannelpipeline.fireChannelRead(readBuf.get(i)); }... }Copy the code

1 doReadMessages(readBuf)

See io.net ty. Channel. Socket. Nio. NioServerSocketChannel# doReadMessages

 @Override
    protected int doReadMessages(List<Object> buf) throws Exception {
        // Is this place very familiar? Again, the underlying Java NIO is called to create a SocketChannel through Accept
        SocketChannel ch = javaChannel().accept();

        try {
            if(ch ! =null) {
                //new a netty's own NioSocketChannel
                // From Java NIO to Netty (8
                // Take a look at the differences, especially the member variable readInterestOp
                buf.add(new NioSocketChannel(this, ch));
                return 1; }}catch (Throwable t) {
            ...
        }

        return 0;
    }
Copy the code

2 pipeline.fireChannelRead(readBuf.get(i));

I have adjusted some intermediate steps in this place, I suggest you debug follow up otherwise it will be hard to find. Finally see io.net ty. The bootstrap. ServerBootstrap. ServerBootstrapAcceptor# channelRead

public void channelRead(ChannelHandlerContext ctx, Object msg) {...try {
                // how to register NioServerSocketChannel from Java NIO to Netty
                // childGroup is a workerGroup
                // Register the NioSocketChannel we just created with an eventLoop in the workerGroup
                // Let's follow up here
                childGroup.register(child).addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        if(! future.isSuccess()) { forceClose(child, future.cause()); }}}); }catch(Throwable t) { forceClose(child, t); }}Copy the code

Childgroup. register(child) is a selector from Java NIO to Netty (8). See io.net ty. Channel. AbstractChannel. AbstractUnsafe# register0

private void register0(ChannelPromise promise) {
            try{...// isActive() determines whether the socket is bound to a port.
                // When NioServerSocketChannel is registered, this place is false and will not be executed,
                // But NioSocketChannel is registered
                if (isActive()) {
                    if (firstRegistration) {
                        //NioServerSocketChannel executes fireChannelActive after the port is bound
                        // So it will execute here and follow herepipeline.fireChannelActive(); }... }Copy the code

Pipeline. FireChannelActive () the execution of logic, are also from Java NIO to Netty (eight) “1.2.2, talked about, you can review again, the difference is

@Override
    protected void doBeginRead(a) throws Exception {...if ((interestOps & readInterestOp) == 0) {
            //NioServerSocketChannel assigns the selectionkey. OP_ACCEPT event to the readInterestOp
            // NioSocketChannel assigns the selectionkey. OP_READ event to the readInterestOpselectionKey.interestOps(interestOps | readInterestOp); }}Copy the code

So to summarize, what does new connection access do? Is it the same as Java NIO? Let’s recall the code from Java NIO to Netty (4)

ServerSocketChannel ServerSocketChannel = (ServerSocketChannel)key.channel();
/ / create a SocketChannel
SocketChannel clientChannel = ServerSocketChannel.accept();
// Set it to non-blocking
clientChannel.configureBlocking(false);
// Register selector
clientChannel.register(key.selector(), SelectionKey.OP_READ);
Copy the code

Is it the same?