public class MyChannelInitializer extends {
public void InitChannel( channel) {
channel.Pipeline().AddLast("myHandler", new MyHandler());
}
}
bootstrap = ...;
...
bootstrap.childHandler(new MyChannelInitializer());
...
I/O Request
via or
{@link ChannelHandlerContext}
|
+---------------------------------------------------+---------------+
| ChannelPipeline | |
| \|/ |
| +----------------------------------------------+----------+ |
| | ChannelHandler N | |
| +----------+-----------------------------------+----------+ |
| /|\ | |
| | \|/ |
| +----------+-----------------------------------+----------+ |
| | ChannelHandler N-1 | |
| +----------+-----------------------------------+----------+ |
| /|\ . |
| . . |
| ChannelHandlerContext.fireIN_EVT() ChannelHandlerContext.OUT_EVT()|
| [method call] [method call] |
| . . |
| . \|/ |
| +----------+-----------------------------------+----------+ |
| | ChannelHandler 2 | |
| +----------+-----------------------------------+----------+ |
| /|\ | |
| | \|/ |
| +----------+-----------------------------------+----------+ |
| | ChannelHandler 1 | |
| +----------+-----------------------------------+----------+ |
| /|\ | |
+---------------+-----------------------------------+---------------+
| \|/
+---------------+-----------------------------------+---------------+
| | | |
| [ Socket.read() ] [ Socket.write() ] |
| |
| Netty Internal I/O Threads (Transport Implementation) |
+-------------------------------------------------------------------+
public class MyInboundHandler :
{
public override void ChannelActive( ctx)
{
Console.WriteLine("Connected!");
ctx.FireChannelActive();
}
}
public class MyOutboundHandler :
{
public override async Task CloseAsync( ctx)
{
Console.WriteLine("Closing...");
await ctx.CloseAsync();
}
}
static readonly group = new ();
...
pipeline = ch.Pipeline;
pipeline.AddLast("decoder", new MyProtocolDecoder());
pipeline.AddLast("encoder", new MyProtocolEncoder());
// Tell the pipeline to run MyBusinessLogicHandler's event handler methods
// in a different thread than an I/O thread so that the I/O thread is not blocked by
// a time-consuming task.
// If your business logic is fully asynchronous or finished very quickly, you don't
// need to specify a group.
pipeline.AddLast(group, "handler", new MyBusinessLogicHandler());
This may be used by