Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Non-blocking usage and Status handling  (Read 2503 times)

0 Members and 1 Guest are viewing this topic.

Tukimitzu

  • Full Member
  • ***
  • Posts: 117
  • Anti-Hero Member
    • View Profile
Non-blocking usage and Status handling
« on: April 10, 2016, 08:09:21 am »
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!

nicox11

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Non-blocking usage and Status handling
« Reply #1 on: April 11, 2016, 10:32:55 am »
Question #1:
It depends on your architecture. But, I think you could simply gather the data you have to send and send them directly. If you want to send data at a single rate, maybe a queue can do the job.

Question #2:
If a client gets disconnected at the time you are sending a packet, I don't see any reason the send method wouldn't return sf::Socket::Disconnected. You can implement a timeout system, in order to detect users that suddenly can't reach the server (disconnected).

Question #3:
I'd simply build a fixed time rate loop that sends, let's say 30 times per seconds, the actual state of the game to the clients. I'll not bother if the client is ready or not, receiving or not. But, at the same time, i'd implement a time out system to detect clients that are not receiving or sending by removing them.

Question #4:
Same as #3

Tukimitzu

  • Full Member
  • ***
  • Posts: 117
  • Anti-Hero Member
    • View Profile
Re: Non-blocking usage and Status handling
« Reply #2 on: April 14, 2016, 03:47:56 am »
Thx nicox11!