public static Task<IChannel> CreateConnection(Role role, IPEndPoint socketAddress, int poolSize, IChannelHandler upstreamHandler)
{
if (role == Role.Client)
{
var connection = new ClientBootstrap()
.Channel<TcpSocketChannel>()
.Option(ChannelOption.TcpNodelay, true)
.Group(GetClientWorkerPool(poolSize))
.Handler(new ActionChannelInitializer<TcpSocketChannel>(channel =>
{
ApplyChannelPipeline(channel, upstreamHandler);
}));
return connection.ConnectAsync(socketAddress);
}
else //server
{
var connection = new ServerBootstrap()
.Group(GetServerPool(poolSize), GetServerWorkerPool(poolSize))
.Channel<TcpServerSocketChannel>()
.ChildOption(ChannelOption.TcpNodelay, true)
.ChildHandler(new ActionChannelInitializer<TcpSocketChannel>(channel =>
{
ApplyChannelPipeline(channel, upstreamHandler);
}));
return connection.BindAsync(socketAddress);
}
}