public async Task TestNotThrowBlockingOperationException() { IEventLoopGroup bossGroup = new MultithreadEventLoopGroup(1); IEventLoopGroup workerGroup = new MultithreadEventLoopGroup(1); IChannelGroup allChannels = new DefaultChannelGroup(); ServerBootstrap b = new ServerBootstrap(); b.Group(bossGroup, workerGroup); b.ChildHandler(new ChannelInboundHandlerAdapter0(allChannels)); b.Channel <TcpServerSocketChannel>(); var ch = await b.BindAsync(0); allChannels.Add(ch); await allChannels.CloseAsync(); await Task.WhenAll( bossGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)), workerGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(5)) ); await bossGroup.TerminationCompletion; await workerGroup.TerminationCompletion; }
public async Task TestBindMultiple() { DefaultChannelGroup channelGroup = new DefaultChannelGroup(); IEventLoopGroup group = new MultithreadEventLoopGroup(1); try { for (int i = 0; i < 100; i++) { Bootstrap udpBootstrap = new Bootstrap(); udpBootstrap .Group(group) .Channel <SocketDatagramChannel>() .Option(ChannelOption.SoBroadcast, true) .Handler(new ChannelInboundHandlerAdapter0()); var datagramChannel = await udpBootstrap .BindAsync(new IPEndPoint(IPAddress.Loopback, 0)); channelGroup.Add(datagramChannel); } Assert.Equal(100, channelGroup.Count); } finally { await channelGroup.CloseAsync(); await group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)); } }
private async Task RunServerAsync() { var bossGroup = new MultithreadEventLoopGroup(1); var workerGroup = new MultithreadEventLoopGroup(); try { var bootstrap = new ServerBootstrap(); bootstrap .Group(bossGroup, workerGroup) .Channel <TcpServerSocketChannel>() .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel => { var pipeline = channel.Pipeline; _channelGroup.Add(channel); _channels.Add(channel.Id.AsLongText(), channel); pipeline.AddLast(new LengthFieldBasedFrameDecoder(ByteOrder.BigEndian, ushort.MaxValue, 0, 2, 0, 2, true)); pipeline.AddLast(new LengthFieldPrepender(ByteOrder.BigEndian, 2, 0, false)); pipeline.AddLast(new SoupBinTcpMessageDecoder()); pipeline.AddLast(new SoupBinTcpMessageEncoder()); pipeline.AddLast("LoginRequestFilter", new LoginRequestFilterHandler()); pipeline.AddLast(new IdleStateHandler(15, 1, 0)); pipeline.AddLast(new ServerTimeoutHandler(Listener)); pipeline.AddLast("ServerHandshake", new ServerHandshakeHandler(Listener)); })); _serverChannel = await bootstrap.BindAsync(5500); await Listener.OnServerListening(); _cancellationToken.WaitHandle.WaitOne(); if (_serverChannel.Active) { await _channelGroup.WriteAndFlushAsync(new LogoutRequest()); await _serverChannel.CloseAsync(); } await Listener.OnServerDisconnect(); } finally { await Task.WhenAll( bossGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)), workerGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1))); } }
private async Task RunServerAsync() { var bossGroup = new MultithreadEventLoopGroup(1); var workerGroup = new MultithreadEventLoopGroup(); try { var bootstrap = new ServerBootstrap(); bootstrap .Group(bossGroup, workerGroup) .Channel <TcpServerSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Option(ChannelOption.SoKeepalive, true) .Option(ChannelOption.SoReuseaddr, true) .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel => { var pipeline = channel.Pipeline; _channelGroup.Add(channel); _channels.Add(channel.Id.AsLongText(), channel); pipeline.AddLast( new LengthFieldBasedFrameDecoder(byteOrder: ByteOrder.BigEndian, maxFrameLength: ushort.MaxValue, lengthFieldOffset: 0, lengthFieldLength: 2, lengthAdjustment: 0, initialBytesToStrip: 2, failFast: true)); pipeline.AddLast( new LengthFieldPrepender(byteOrder: ByteOrder.BigEndian, lengthFieldLength: 2, lengthAdjustment: 0, lengthFieldIncludesLengthFieldLength: false)); pipeline.AddLast(new SoupBinTcpMessageDecoder()); pipeline.AddLast(new SoupBinTcpMessageEncoder()); pipeline.AddLast("LoginRequestFilter", new LoginRequestFilterHandler()); pipeline.AddLast( new IdleStateHandler( readerIdleTimeSeconds: 15, writerIdleTimeSeconds: 1, allIdleTimeSeconds: 0)); pipeline.AddLast(new ServerTimeoutHandler(Listener)); pipeline.AddLast("ServerHandshake", new ServerHandshakeHandler(Listener)); })); _serverChannel = await bootstrap.BindAsync(5500); await Listener.OnServerListening(); // Wait here _cancellationToken.WaitHandle.WaitOne(); if (_serverChannel.Active) { await _channelGroup.WriteAndFlushAsync(new LogoutRequest()); await _serverChannel.CloseAsync(); } await Listener.OnServerDisconnect(); } finally { await Task.WhenAll( bossGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1)), workerGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1))); } }