Hello friends,
When I have non-blocking sf::TcpSockets updated in a loop, what are the correct ways to send and receive data?
This is how I've been doing:
Listening to connections:
sf::Status status;
sf::TcpSocket* socket = new sf::TcpSocket;
while ((status = listener.accept(*socket)) == sf::Socket::Done){
// store socket somewhere, then...
socket = new sf::TcpSocket;
}
delete socket;
Receiving data:
sf::Packet packet;
sf::Socket::Status status;
while ((status = socket->receive(packet)) == sf::Socket::Done){
// read packet
}
if (status == sf::Socket::Disconnected){
// delete socket, remove from storage
}
Sending data:
// when I want to send a packet, I don't send it right away, I store it in a queue
peer.packetsToSend.push_back(packet);
//...
// further in another moment I actually send it
if (!peer.packetsToSend.empty()){
sf::Packet& packet = peer.packetsToSend.front();
if ((status = socket->send(packet)) == sf::Socket::Done){
peer.packetsToSend.pop_front();
}
}
Question #1: is the packet send queue necessary? Do I need a receive queue too?
Question #2: can the send method return sf::Socket::Disconected as well? If so, do I have to handle it or you think it's fine handling it only on receive (considering I call receive 60 times per second)?
Question #3: how to proceed if send(...) returns an error? Should I try to send the whole thing again?
Question #4: how to proceed if receive(...) returns an error?
Thanks!