上一篇文章分析了NioServerSocketChannel,在这篇文章分析一下另外一种SocketChannel,其实也是调用accept之后创建的channel。。。
还是按照惯例来,先看一下它的继承体系吧:
其实和ServerSocketChannel差不太多,只不过这里直接继承的是AbstractNioByteChannel,而不是AbstractNioMessageChannel,也就是说NioSocketChannel的数据是以byte为单位的,而ServerSocketChannel是以对象为单位的。。。
首先我们来看看AbstractByteChannel的定义吧,主要还是里面的unsafe对象,先来看看read方法:
public void read() {
assert eventLoop().inEventLoop();
final SelectionKey key = selectionKey();
if (!config().isAutoRead()) {
// only remove readInterestOp if needed
key.interestOps(key.interestOps() & ~readInterestOp);
}
//获取当前channel的pipeline
final ChannelPipeline pipeline = pipeline();
//获取当前存数据的buffer,这里是基于字节的buffer
final ByteBuf byteBuf = pipeline.inboundByteBuffer();
boolean closed = false;
boolean read = false;
boolean firedChannelReadSuspended = false;
try {