netty源码解解析(4.0)-2 Chanel的接口设计
- 辅助方法。
- outbound方法
- inbound方法
方法
|
说明
|
EventLoop eventLoop()
|
得到EventLoop实例,每个Channel实例都会被注册到一个EventLoop中,这个EventLoop实例就是Channel注册的实例。
|
Channel parent()
|
得到父Channel实例。如: channelB = channelA.accept(), 那么channelA就是channelB的parent。
|
ChannelConfig config()
|
得到Channel实例的配置信息。
|
boolean isOpen()
|
channel是否处于open状态。netty为每个channel定义了四种状态open->registered->active->closed。一个新创建的channel处于open状态,随后他被注册到一个eventloop中它处于open+registered状态,当这个channel上的连接建立成功后它处于open+registered+active状态,被关闭后处于closed状态。
|
boolean isRegistered()
|
channel是否处于registered状态。
|
boolean isActive()
|
channel是否处于active状态。
|
SocketAddress localAddress()
|
channel的本地bind的地址。
|
SocketAddress remoteAddress()
|
channel连接的远程channel的地址。
|
boolean isWritable()
|
channel的I/O线程是否可以立即执操作。
|
Unsafe unsafe()
|
得到channel内部的Unsafe实例。
|
ChannelPipeline pipeline()
|
得到channel内部的ChannelPipeline实例。
|
ByteBufAllocator alloc()
|
channel持有的buffer分配器。
|
方法
|
说明
|
ChannelFuture bind(SocketAddress localAddress)
ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise)
|
让channel绑定的指定的本地地址(localAddress)上。这个方法会触发ChannelOutboundHandler#bind(ChannelHandlerContext, SocketAddress, ChannelPromise)方法的调用。
|
ChannelFuture connect(SocketAddress remoteAddress)
ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress)
ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise)
|
连接到远程地址(remoteAddress), 这个方法会触发ChannelOutboundHandler#connect(ChannelHandlerContext, SocketAddress, SocketAddress, ChannelPromise)方法的调用。
|
ChannelFuture disconnect()
ChannelFuture disconnect(ChannelPromise promise);
|
断开连接, 这个方法会触发ChannelOutboundHandler#disconnect(ChannelHandlerContext, ChannelPromise)的调用。
|
ChannelFuture close()
ChannelFuture close(ChannelPromise promise)
|
关闭channel. 这个方法会触发ChannelOutboundHandler#close(ChannelHandlerContext, ChannelPromise)的调用。
|
ChannelFuture deregister()
ChannelFuture deregister(ChannelPromise promise)
|
从eventloop中注销这个channel,这个方法会触发ChannelOutboundHandler#deregister(ChannelHandlerContext, ChannelPromise)的调用。
|
ChannelFuture write(Object msg)
ChannelFuture write(Object msg, ChannelPromise promise)
|
向channel写入数据,这个操作不会导致真正写操作,只会把数据追加到输出缓冲区中。它会触发ChannelOutboundHandler#write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)调用。
|
Channel flush()
|
对输出缓冲区中的数据执行真正的写操作,调用这个方法后连接的另一端才能收到write的数据,它会触发ChannelOutboundHandler#flush(ChannelHandlerContext ctx)调用。
|
ChannelFuture writeAndFlush(Object msg, ChannelPromise promise)
ChannelFuture writeAndFlush(Object msg)
|
效果和先调用write然后调用flush一样。
|
方法
|
说明
|
Channel read()
|
从channel中读取数据,把数据放到输入缓冲区中,然后触发ChannelInboundHandler#channelRead(ChannelHandlerContext, Object)和ChannelInboundHandler#channelReadComplete(ChannelHandlerContext)调用,如果之前已经有一个read操作正在执行或等待执行,这个方法不会有任何影响。
|
方法
|
说明
|
SocketAddress localAddress()
|
同Channel
|
SocketAddress remoteAddress()
|
同Channel
|
void register(EventLoop eventLoop, ChannelPromise promise)
|
同Channel,
|
void bind(SocketAddress localAddress, ChannelPromise promise)
|
同Channel, 必须在I/O线程中执行
|
void connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise)
|
同Channel, 必须在I/O线程中执行
|
void disconnect(ChannelPromise promise)
|
同Channel, 必须在I/O线程中执行
|
void close(ChannelPromise promise)
|
同Channel, 必须在I/O线程中执行
|
void closeForcibly()
|
立即关闭channel,并且不触发任何事件。
|
void deregister(ChannelPromise promise)
|
同Channel, 必须在I/O线程中执行
|
void beginRead()
|
为channel触发read事件做准备。如:把read事件注册到NIO 的selector上。 必须在I/O线程中执行 必须在I/O线程中执行
|
void write(Object msg, ChannelPromise promise)
|
同Channel, 必须在I/O线程中执行
|
void flush()
|
同Channel, 必须在I/O线程中执行
|
ChannelOutboundBuffer outboundBuffer()
|
得到channel的输出缓冲区,write的数据就是追加到这个缓冲区中。 必须在I/O线程中执行
|
netty源码解解析(4.0)-2 Chanel的接口设计的更多相关文章
- netty源码解解析(4.0)-10 ChannelPipleline的默认实现--事件传递及处理
事件触发.传递.处理是DefaultChannelPipleline实现的另一个核心能力.在前面在章节中粗略地讲过了事件的处理流程,本章将会详细地分析其中的所有关键细节.这些关键点包括: 事件触发接口 ...
- netty源码解解析(4.0)-11 Channel NIO实现-概览
结构设计 Channel的NIO实现位于io.netty.channel.nio包和io.netty.channel.socket.nio包中,其中io.netty.channel.nio是抽象实 ...
- netty源码解解析(4.0)-17 ChannelHandler: IdleStateHandler实现
io.netty.handler.timeout.IdleStateHandler功能是监测Channel上read, write或者这两者的空闲状态.当Channel超过了指定的空闲时间时,这个Ha ...
- netty源码解解析(4.0)-18 ChannelHandler: codec--编解码框架
编解码框架和一些常用的实现位于io.netty.handler.codec包中. 编解码框架包含两部分:Byte流和特定类型数据之间的编解码,也叫序列化和反序列化.不类型数据之间的转换. 下图是编解码 ...
- netty源码解解析(4.0)-20 ChannelHandler: 自己实现一个自定义协议的服务器和客户端
本章不会直接分析Netty源码,而是通过使用Netty的能力实现一个自定义协议的服务器和客户端.通过这样的实践,可以更深刻地理解Netty的相关代码,同时可以了解,在设计实现自定义协议的过程中需要解决 ...
- netty源码解解析(4.0)-15 Channel NIO实现:写数据
写数据是NIO Channel实现的另一个比较复杂的功能.每一个channel都有一个outboundBuffer,这是一个输出缓冲区.当调用channel的write方法写数据时,这个数据被一系列C ...
- netty源码解解析(4.0)-5 线程模型-EventExecutorGroup框架
上一章讲了EventExecutorGroup的整体结构和原理,这一章我们来探究一下它的具体实现. EventExecutorGroup和EventExecutor接口 io.netty.util.c ...
- netty源码解解析(4.0)-8 ChannelPipeline的设计
io.netty.channel.ChannelPipeline 设计原理 上图中,为了更直观地展示事件处理顺序, 故意有规律地放置两种handler的顺序,实际上ChannelInboundHa ...
- netty源码解解析(4.0)-16 ChannelHandler概览
本章开始分析ChannelHandler实现代码.ChannelHandler是netty为开发者提供的实现定制业务的主要接口,开发者在使用netty时,最主要的工作就是实现自己的ChannelHan ...
随机推荐
- [Algorithm]Algorithm章1 排序算法
1.冒泡排序-相邻交换 (1)算法描述 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也 ...
- c++ stl源码剖析学习笔记(一)uninitialized_copy()函数
template <class InputIterator, class ForwardIterator>inline ForwardIterator uninitialized_copy ...
- MySQL—查询某时间范围的数据
-- 查询今天的数据 select * from `user` where to_days(birthday) = to_days(CURDATE()); -- 查询昨天的数据 select * fr ...
- 必须知道的Spring Boot中的一些Controller注解
这篇文章是抄其他人的,原址:https://cloud.tencent.com/developer/article/1082720 本文旨在向你介绍在Spring Boot中controller中最基 ...
- AX_UserInfo
UserInfoHelp::userInUserGroup(curuserid(), "Admin") EmplTable::userId2EmplId(curuserid()) ...
- Nodejs之路:非I/O的异步API
本篇主要介绍setTimeout,setInterval,setImmediate和process.nextTick. 1,定时器 Node中的定时器和浏览器中用法一致.区别在于:在Node中,执行到 ...
- ABP框架系列之十五:(Caching-缓存)
Introduction ASP.NET Boilerplate provides an abstraction for caching. It internally uses this cache ...
- 源码管理工具Git-客户端GitBash常用命令
1.配置用户名和邮箱地址(第一次启动程序时配置,以后使用不用配置)git config --global user.name "dolen"git config --global ...
- php循环
while 例子: /* example 1 */ $a = 0; while (true) { $a++; echo $a.'<br>'; if($a >= 10){ break; ...
- 【转】Map 与 Unordered_map
map和unordered_map的差别和使用 map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/b ...