private void OnDisconnect() { if (m_Listener != null) { Logger.Debug("OnDisconnect"); Dispose(); m_Listener.OnDisconnect(this); } }
private async Task RunClientAsync() { var group = new MultithreadEventLoopGroup(); try { var bootstrap = new Bootstrap(); bootstrap .Group(group) .Channel <TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Handler(new ActionChannelInitializer <ISocketChannel>(channel => { var pipeline = channel.Pipeline; 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(new IdleStateHandler(15, 1, 0)); pipeline.AddLast(new ClientTimeoutHandler()); pipeline.AddLast(new ClientHandler(_loginDetails, _listener)); })); _clientChannel = await bootstrap.ConnectAsync(new IPEndPoint(_ipAddress, _port)); _cancellationToken.WaitHandle.WaitOne(); if (_clientChannel.Active) { await _clientChannel.WriteAndFlushAsync(new LogoutRequest()); await _clientChannel.CloseAsync(); await _listener.OnDisconnect(); } } finally { await group.ShutdownGracefullyAsync(); } }
public override void ChannelInactive(IChannelHandlerContext context) { _listener.OnDisconnect(); }
private async Task RunClientAsync() { var group = new MultithreadEventLoopGroup(); try { var bootstrap = new Bootstrap(); bootstrap .Group(group) .Channel <TcpSocketChannel>() .Option(ChannelOption.TcpNodelay, true) .Option(ChannelOption.SoKeepalive, true) .Option(ChannelOption.SoReuseaddr, true) .Handler(new ActionChannelInitializer <ISocketChannel>(channel => { var pipeline = channel.Pipeline; 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( new IdleStateHandler( readerIdleTimeSeconds: 15, writerIdleTimeSeconds: 1, allIdleTimeSeconds: 0)); pipeline.AddLast(new ClientTimeoutHandler()); pipeline.AddLast(new ClientHandler(_loginDetails, _listener)); })); _clientChannel = await bootstrap.ConnectAsync(new IPEndPoint(_ipAddress, _port)); // Wait here _cancellationToken.WaitHandle.WaitOne(); if (_clientChannel.Active) { await _clientChannel.WriteAndFlushAsync(new LogoutRequest()); await _clientChannel.CloseAsync(); await _listener.OnDisconnect(); } } finally { await group.ShutdownGracefullyAsync(); } }