quasar
quasar copied to clipboard
FiberSocketChannel 's close event?
When I write a socks5 proxy, I want the FiberSocketChannel can notify me when a remote-socket is closed. But I cannot find a onClose() method or a Exception which can replace onClose event。 Because a proxy have a client as downstream and a server as upstream. Client ---- Proxy ---- Server When server the a picture to a client, it will close the socket when the reached the picture-file's end. But the FiberSocketChannel cannot notify users.
The concrete class is AsyncFiberSocketChannel
based upon JDK's AIO AsynchronousSocketChannel
, whose read
and write
operations are asynchronous and receive a CompletionHandler
callback. Quasar converts them into fiber-blocking calls that either return a result or throw an exception after the operation has completed.
If the socket is closed during an operation I think you should see an exception being thrown, isn't this what actually happens?
@circlespainter If I close a socket, I will received a exception when I do some op on the socket. But I want receive a notify not wait until I do something. Because of my server is a proxy.
Why do you need to receive a notification? If I understand correctly your case the proxy will receive the picture on behalf of the client so it will read
the response from the channel connected to the server. Since the server will close the channel when the binary transfer is finished, the proxy will perform a read that will get the final chunk of data and then, when it attempts to fetch more data through another read
, it will receive an exception instead and in this way it will know that the transfer has been completed.
when it attempts to fetch more data through another read, it will receive an exception
How do I know which one is the final block? So I must wait for a read timeout. e.g. socketA.read(buffer, N , TimeUnit.seconds); But how much seconds do I wait for timeout ? It depends the size of the buffer and the speed of the server.
How do I know which one is the final block? ... But how much seconds do I wait for timeout ?
I think the final block will be the one before a further read
raises an exception about the connection having been closed and that should happen very quickly, or else it means the parties and/or the network are clogged; but you'll need timeouts anyway, in every read.
Have you tried already and it doesn't work like this? If so, can you share a small sample project?