一、前言

  前面介绍了ServerCnxn,下面开始学习NIOServerCnxn。

二、NIOServerCnxn源码分析

  2.1 类的继承关系

public class NIOServerCnxn extends ServerCnxn {}

  说明:NIOServerCnxn继承了ServerCnxn抽象类,使用NIO来处理与客户端之间的通信,使用单线程处理。

  2.2 类的内部类

  1. SendBufferWriter类 

    private class SendBufferWriter extends Writer {
        private StringBuffer sb = new StringBuffer();
        
        /**
         * Check if we are ready to send another chunk.
         * @param force force sending, even if not a full chunk
         */
        // 是否准备好发送另一块
        private void checkFlush(boolean force) {
            if ((force && sb.length() > 0) || sb.length() > 2048) { // 当强制发送并且sb大小大于0,或者sb大小大于2048即发送缓存
                sendBufferSync(ByteBuffer.wrap(sb.toString().getBytes()));
                // clear our internal buffer
                sb.setLength(0);
            }
        }

        @Override
        public void close() throws IOException {
            if (sb == null) return;
            // 关闭之前需要强制性发送缓存
            checkFlush(true);
            sb = null; // clear out the ref to ensure no reuse
        }

        @Override
        public void flush() throws IOException {
            checkFlush(true);
        }

        @Override
        public void write(char[] cbuf, int off, int len) throws IOException {
            sb.append(cbuf, off, len);
            checkFlush(false);
        }
    }
SendBufferWriter

相关文章:

  • 2022-01-14
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2021-06-27
  • 2021-12-19
  • 2022-01-05
  • 2021-09-21
猜你喜欢
  • 2021-10-25
  • 2021-10-19
  • 2021-07-20
  • 2022-12-23
  • 2021-05-19
  • 2021-07-31
  • 2021-07-15
相关资源
相似解决方案