public void channelOpen(
ChannelHandlerContext ctx, ChannelStateEvent evt) {
try {
// 内部类可以访问外部类的方法 ,如 getPipelineFactory()。
evt.getChannel().getConfig().setPipelineFactory(getPipelineFactory());
// Split options into two categories: parent and child.
Map<String, Object> allOptions = getOptions();
Map<String, Object> parentOptions = new HashMap<String,
Object>();
for (Entry<String, Object> e: allOptions.entrySet())
{
if (e.getKey().startsWith( "child."))
{
childOptions.put( e.getKey().substring(6), e.getValue());
} else if (!"pipelineFactory" .equals(e.getKey()))
{
parentOptions.put(e.getKey(), e.getValue());
}
}
// Apply parent options.
evt.getChannel().getConfig().setOptions(parentOptions);
} finally {
ctx.sendUpstream(evt);
}
//============ 真正的绑定套接字 =======
evt.getChannel().bind( localAddress).addListener( new ChannelFutureListener()
{
public void operationComplete(ChannelFuture
future) throws Exception {
if (future.isSuccess()) {
// 这里触发了 bind的成功
bindFuture.setSuccess();
} else {
bindFuture.setFailure(future.getCause());
}
}
});
}