Netty内建对象

Handler

Handler

SimpleChannelInboundHandler

ChannelInboundHandlerAdapter which allows to explicit only handle a specific type of messages.

只允许显式地处理特定类型的消息

ChannelInboundHandlerAdapter

Pipeline

HttpServerCodec 请求解码和返回编码组合

A combination(组合) of HttpRequestDecoder and HttpResponseEncoder which enables easier server side HTTP implementation.

HttpRequestDecoder和HttpResponseEncoder的组合,使服务器端更容易实现HTTP

LengthFieldBasedFrameDecoder
LengthFieldPrepender
StringDecoder&StringEncoder
DelimiterBasedFrameDecoder 分隔符

A decoder that splits the received ByteBufs by one or more delimiters. It is particularly useful for decoding the frames which ends with a delimiter such as Delimiters or Delimiters newline characters.

解码器将收到的ByteBufs通过一个或者更多的分隔符进行切割。他对于解码结束的帧特别有用诸如分隔符和分隔换行符

IdleStateHandler 空闲检测

Triggers an IdleStateEvent when a Channel has not performed read, write, or both operation for a while.

当Channel一段时间内没有触发读和写操作触发IdleStateEvent

通过继承ChannelInboudHandlerAdapter重写userEventTriggered捕获异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 加入handler到pipeline
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
.addLast(new IdleStateHandler(5, 6, 10, TimeUnit.SECONDS))
.addLast(new CatchHandler()); // 事件处理handler
}
--------------------------------------------------------------
// 捕获异常
public class CatchHandler extends ChannelInboundHandlerAdapter {
/**
* Calls {@link ChannelHandlerContext#fireUserEventTriggered(Object)} to forward
* to the next {@link ChannelInboundHandler} in the {@link ChannelPipeline}.
*
* Sub-classes may override this method to change behavior.
*/
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
// super.userEventTriggered(ctx, evt); 默认将异常抛给下一个handler
if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt;
switch (event.state()) {
case READER_IDLE: // 到达时间不出现读
break;
case WRITER_IDLE: // 到达时间不出现写
break;
case ALL_IDLE: // // 到达时间不出现读 写
break;
}
}
}
}
LoggingHandler 日志框架

A ChannelHandler that logs all events using a logging framework. By default, all events are logged at DEBUG level.

使用日志框架记录所有事件日志的ChannelHandler。默认所有事件都使用debug等级

1
2
3
4
5
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
ChunkedWriteHandler
HttpObjectAggregator
WebSocketServerProtocolHandler

ProtocalType

HttpObject

WebSocketFrame

TextWebSocketFrame
PingWebSocketFrame
PongWebSocketFrame
CloseWebSocketFrame
ContinuationWebSocketFrame
BinaryWebSocketFrame
 上一篇

linux