Azure / DotNetty

DotNetty project – a port of netty, event-driven asynchronous network application framework
Other
4.09k stars 977 forks source link

Shutdown TCP socket #383

Closed dmytro-gokun closed 6 years ago

dmytro-gokun commented 6 years ago

Hi all,

I have a server which process TCP connection. At some moment, the server may decide to close a client connection. But, somehow the client would not detect this for several minutes and continue sending packets to the server. Thus, those server are lost in transit.

I figured out that the correct way to handle this would be the following:

1) Shutdown the socket; 2) Read all remaining data in the buffer; 3) Close the socket.

However, i do not see any methods in DotNetty to shutdown a socket. Am I missing something?

ghost commented 6 years ago

Check out this example provided by DotNetty

https://github.com/Azure/DotNetty/blob/dev/examples/Echo.Server/Program.cs

dmytro-gokun commented 6 years ago

@jordanabrahambaws Thanks, I've seen that example. But I cannot see where exactly the things I'm talking about happen in this example. Where is socket shutdown happening, which line is that?

nayato commented 6 years ago

@dmytro-gokun we initially ported 5.0 alpha of netty which deprecated close-on-write behavior. As we're now back to aligning with 4.1, we're rediscovering these gems as we go. I believe this should address your concerns in next release: https://github.com/Azure/DotNetty/commit/c8252cb00d35168ad359eb9bce49818e810e420b

dmytro-gokun commented 6 years ago

@nayato So, just to clarify... If, at some point, in my application i want to shutdown read and/or write (w/o actually closing the socket, so I give my app a chance to process what is in the receive buffer)... what method(s) should I call and on which object(s) (let's say I have a reference to IChannelHandlerContext)?

Just to clarify. What I'm looking here is a DotNetty's way to do what this POSIX function does: https://www.unix.com/man-page/POSIX/3posix/shutdown/

StormHub commented 6 years ago

@dmytro-gokun Closing the channel would cause shutdown for TcpSocketChannel, https://github.com/Azure/DotNetty/blob/dev/src/DotNetty.Transport/Channels/Sockets/TcpSocketChannel.cs#L171 You can shutdown the channel output by ShutdownOutputAsync() https://github.com/Azure/DotNetty/blob/dev/src/DotNetty.Transport/Channels/Sockets/TcpSocketChannel.cs#L105

dmytro-gokun commented 6 years ago

@StormHub ShutdownOutputAsync() looks like just what I need. I will give it a try. Thanks!