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

Author Topic: TCP and handling sf::Socket::Partial  (Read 2585 times)

0 Members and 1 Guest are viewing this topic.

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
TCP and handling sf::Socket::Partial
« on: September 01, 2016, 04:21:55 pm »
Hello again,

I read from the online tutorial (http://www.sfml-dev.org/tutorials/2.4/network-socket.php bottom):

Quote
If sf::Socket::Partial is returned, you must make sure to handle the partial send properly or
else data corruption will occur.

When sending sf::Packets, the byte offset is saved within the sf::Packet itself. In this case,
you must make sure to keep attempting to send the exact same unmodified sf::Packet object
over and over until a status other than sf::Socket::Partial is returned.
Constructing a new sf::Packet object and filling it with the same data will not work,
it must be the same object that was previously sent.

Does this mean I should implement something in this style (and yes it doesn't work, that's why I'm here):
EDIT: The code works now, my previous version wasn't working due to my embarrassing lack of C++ knowledge conserning the remove-erase idiom (I thought remove_if would actually erase the elements).
// edited code, was nonsense
class Server {
// ...
private:
    void send(const sf::Packet& packet);
    void handle_outgoing_packets();
// ...
    std::vector<sf::Packet> pending_packets;
};

void Server::send(const sf::Packet& packet)
{
    pending_packets.push_back(packet);
}

void Server::handle_outgoing_packets()
{
    auto iter = std::remove_if(pending_packets.begin(), pending_packets.end(), [this] (auto& p) {
        return clientSocket.send(p) == sf::Socket::Done;
    });
    pending_packets.erase(iter, pending_packets.end());
}
 

I know this doesn't work properly (because my packets are getting stuck in the "queue"). However, I am more interested in the basic notion of such a mechanism to handle sf::Socket::Partial, or can I just "hit send and be done with it", despite what he documentation says.

P.S.: I am using non-blocking sockets and have a server loop. If I forgot any important information please let me know.

Kind Regards,
Raincode
« Last Edit: September 01, 2016, 06:08:41 pm by Raincode »