CppServer
CppServer copied to clipboard
DisconnectAsync
Fisrt of all, great stuff. Many thanks.
Doing some tests with an async client and using the function:
void onSent(size_t sent, size_t pending) override{ if (pending == 0){ // Sleep here DisconnectAsync(); } }
I had the idea of disconnecting the client as soon as pending==0. If I use DisconnectAsync() nothing happens (Disconnect () does the trick). I have checked the status of the connection and it is still connected. Is this an expected behaviour?
I also had to introduce a delay before disconnecting, otherwise some tx data is lost. I guess this is how the sockets and the OS kernel work but is there a way to know that there is indeed no remaining data to transmit?
Many thanks again
In your case the best choose is to call DisconnectAsync() and your connection will be disconnected asynchronously very soon and all data will be transferred to the peer. If you call Disconnect() it will close the socket immediately with possible data lost. Delay with Sleep() will not work because you need to leave onSent() handler and let working loop do its job (wait to send all data and then disconnect).
Call Disconnect() only in socket synchronous scenarios with socket blocking methods call (Connect()/Send()/Receive())