onDisconnect
Closed this issue · 4 comments
Having a way to check if a socket has disconnected would be very nice. Maybe something like an onDisconnect
subprocedure or a socket is connected $
method.
What do you think?
According to Internet scholars uncovered by extensive Google-based research:
The only way to reliably detect if a socket is still connected is to periodically try to send data.
Beej's guide seems to confirm this, so I've just added ERRORCODE
to SEND
and READ
as well as a way to see how many bytes were sent or received. I think a proper library that builds on top of ldpl-socket (like an IRC client) could provide the onDisconnect
hook for their users, but I don't know if it makes sense in this library unless you have an idea for how to implement it?
So basically, code using this library should check ERRORCODE
after trying to send or receive to detect if there was a disconnect.
Wonderful! However, if you were listening for data in blocking mode and the client disconnected, what would happen? Would it hang reading forever?
if you were listening for data in blocking mode and the client disconnected, what would happen? Would it hang reading forever?
As soon as it disconnects the function will return. read()
(and friends like recv()
) handle this natively, you'll get back 0 bytes. ldpl-socket detects this and sets ERRORCODE
to 1
: https://github.com/dvkt/ldpl-socket/blob/0627a53ba3104eb42c11d9ae998a44ab6f3619c6/ldpl_socket/ldpl_socket.cpp#L126-L131
Ah! Wonderful! Thank you!