这篇文章(http://san-yun.iteye.com/blog/1689608)分析了部分netty代码,下面我对netty的核心类做了记录:
DefaultChannelPipeline
public void sendUpstream(ChannelEvent e);
public void sendDownstream(ChannelEvent e){
try {
((ChannelDownstreamHandler) ctx.getHandler()).handleDownstream(ctx, e);
} catch (Throwable t) {
notifyHandlerException(e, t);
}
}
SimpleChannelHandler
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e){
if (e instanceof MessageEvent) {
messageReceived(ctx, (MessageEvent) e);
} else if (e instanceof WriteCompletionEvent) {
WriteCompletionEvent evt = (WriteCompletionEvent) e;
writeComplete(ctx, evt);
} else if (e instanceof ChildChannelStateEvent) {
ChildChannelStateEvent evt = (ChildChannelStateEvent) e;
if (evt.getChildChannel().isOpen()) {
childChannelOpen(ctx, evt);
} else {
childChannelClosed(ctx, evt);
}
}else if (e instanceof ExceptionEvent) {
exceptionCaught(ctx, (ExceptionEvent) e);
} else {
ctx.sendUpstream(e);
}
}
public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)
throws Exception {
if (e instanceof MessageEvent) {
writeRequested(ctx, (MessageEvent) e);
} else if (e instanceof ChannelStateEvent) {
//省略细节代码....
} else {
ctx.sendDownstream(e);
}
Channels.write()
1.DefaultChannelPipeline.sendDownstream(e);
2.SimpleChannelHandler.handleDownstream(e);
3.SimpleChannelHandler.writeRequested(ctx,e);
4.OioServerSocketPipelineSink.eventSunk(pipeline,e);
5.OioServerSocketPipelineSink.handleAcceptedSocket(e);
6.OioWorker.write(channel, future, message)
MessageEvent
DownstreamMessageEvent
UpstreamMessageEvent