static async Task RunHttpServerAsync(int port) { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency; } bossGroup = new MultithreadEventLoopGroup(1); workerGroup = new MultithreadEventLoopGroup(); try { var bootstrap = new ServerBootstrap(); bootstrap.Group(bossGroup, workerGroup); bootstrap.Channel <TcpServerSocketChannel>(); bootstrap.Option(ChannelOption.SoBacklog, 8192); bootstrap.ChildHandler(new ActionChannelInitializer <DotNetty.Transport.Channels.IChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast(new HttpServerCodec()); pipeline.AddLast(new HttpObjectAggregator(65536 * 5)); pipeline.AddLast(new HttpDecoder()); })); bootstrapChannel = await bootstrap.BindAsync(port); LOGGER.Info("start http server success. listener port:[{}]", port); } catch (Exception e) { throw new Exception("start http server ERROR! \n" + e.ToString()); } }
protected override async Task DoOpenAsync() { _bootstrap = new Bootstrap(); _bootstrap.Channel <TcpSocketChannel>() .Group(new MultithreadEventLoopGroup(1)) .Option(ChannelOption.SoKeepalive, true) .Option(ChannelOption.TcpNodelay, true) .Option(ChannelOption.ConnectTimeout, TimeSpan.FromSeconds(3)) //.Option(ChannelOption.Allocator, PooledByteBufferAllocator.Default) .Handler(new ActionChannelInitializer <ISocketChannel>(c => { var nettyClientHandler = new NettyClientHandler(Url, this); var codec = new DubboCountCodec(); var nettyAdapter = new NettyCodecAdapter(codec, Url, this); var pipeline = c.Pipeline; pipeline.AddLast("decoder", nettyAdapter.GetDecoder()); pipeline.AddLast("encoder", nettyAdapter.GetEncoder()); pipeline.AddLast("handler", nettyClientHandler); })); try { _channel = await _bootstrap.ConnectAsync(Url.Ip, Url.Port); } catch (Exception e) { Console.WriteLine(e); throw; } }
public NettyChannel(ISerializer serializer, DotNetty.Transport.Channels.IChannel channel) { _serializer = serializer; _channel = channel; _pipeline = _channel.Pipeline; }
static async Task RunServerAsync(int port, bool useLibuv) { if (useLibuv) { DispatcherEventLoopGroup dispatcher = new DispatcherEventLoopGroup(); bossGroup = dispatcher; workerGroup = new WorkerEventLoopGroup(dispatcher); } else { bossGroup = new MultithreadEventLoopGroup(1); workerGroup = new MultithreadEventLoopGroup(); //默认为CPU核数*2 } try { var bootstrap = new ServerBootstrap(); bootstrap.Group(bossGroup, workerGroup); if (useLibuv) { bootstrap.Channel <TcpServerChannel>(); } else { bootstrap.Channel <TcpServerSocketChannel>(); } bootstrap.Option(ChannelOption.SoBacklog, 65535) .Option(ChannelOption.RcvbufAllocator, new AdaptiveRecvByteBufAllocator()) .Option(ChannelOption.Allocator, PooledByteBufferAllocator.Default) .ChildOption(ChannelOption.Allocator, PooledByteBufferAllocator.Default) .ChildOption(ChannelOption.SoKeepalive, true) .ChildOption(ChannelOption.TcpNodelay, true) .ChildHandler(new ActionChannelInitializer <DotNetty.Transport.Channels.IChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast("IdleChecker", new IdleStateHandler(50, 50, 0)); // 消息编码解码器 分发handler //每个channel channelpipeline 都会new一次,即每个客户端 pipeline.AddLast(new TcpServerEncoder(), new TcpServerDecoder(), new TcpServerHandler()); })); bootstrapChannel = await bootstrap.BindAsync(port); LOGGER.Info("start tcp server success. listener port:[{}]", port); } catch (Exception e) { LOGGER.Error(e, e.Message); throw new Exception("start tcp server ERROR! \n" + e.StackTrace); } }
protected override void Encode(IChannelHandlerContext context, object message, IByteBuffer output) { DotNetty.Transport.Channels.IChannel ch = context.Channel; NettyChannel channel = NettyChannel.GetOrAddChannel(ch, _url, _handler); try { //Console.WriteLine("encode:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); _codec.Encode(channel, output, message); //Console.WriteLine("encoded:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); } finally { NettyChannel.RemoveChannelIfDisconnected(ch); } }