mgravell / Pipelines.Sockets.Unofficial

.NET managed sockets wrapper using the new "Pipelines" API
Other
412 stars 54 forks source link

Unable to wait for socket closure #53

Closed andrewjw1995 closed 4 years ago

andrewjw1995 commented 4 years ago

As far as I can tell, there's no way to receive a callback when the socket used by SocketConnection is closed or disposed. I'd like to make a proxy server which accepts connections using a proprietary protocol, unpacks the messages from that protocol, and sends the content on to another server. I'll end up with two SocketConnections, and a class which is translating between them. When one SocketConnection is closed, I need to close the other one to ensure I'm not leaking connections.

Ideally, I'd like to register a callback, or be able to await a task, so I can run some cleanup code once either of the two sockets has disconnected.

xqrzd commented 4 years ago

You'll be notified of this in your main parsing loop, when PipeReader.ReadAsync() returns a status of canceled, completed, or throws an exception.

andrewjw1995 commented 4 years ago

@xqrzd that makes more sense - I was waiting for ReadAsync() to return a status of canceled or completed, but it was throwing an exception instead. The documentation for PipeReader does not indicate that this method can throw an exception, so I was not handling it. I think perhaps the readme for this repo could be updated to show how to make a basic proxy, as it's a very simple use case and it would demonstrate where network exceptions need to be caught, for both PipeReader and PipeWriter