SFML community forums

Help => Network => Topic started by: iride on January 22, 2014, 04:54:59 am

Title: sending one packet to multiple recipents
Post by: iride on January 22, 2014, 04:54:59 am
I noticed that sf::Tcp::Socket::send  takes a reference to  a sf::Packet , not const sf::Packet
Does this mean that when I send a packet , the contents of the packet get modified so I can't send the same packet to the other clients?
or in code
sf::Packet packet;
packet<<"hi";
for(sf::TcpSocket & s : sockets)
    s.send(packet);//is this ok?
 
Title: Re: sending one packet to multiple recipents
Post by: Azaral on January 22, 2014, 05:43:36 am
I made a rudimentary chat program and in it I just iterated through a vector of sf::TcpSocket* and did connection->send( packet ); and it worked for me. Granted, I was only connected with another person and the program excluded sending the message to the connection that sent it.
Title: Re: sending one packet to multiple recipents
Post by: binary1248 on January 22, 2014, 06:02:15 am
At the moment SFML does not modify the contents of the packet when sending it. The reason it is not passed as a const reference is because of the possibility for the user to derive from sf::Packet and implement their own transformation code (such as compression, encryption etc.) in onSend(). In that case it is desired that the packet be modified while sending although you would have to transform new data every single send instead of just reusing the same transformed data for multiple sends.

If you are really paranoid that your packet might be modified while iterating through all sockets, you can just explicitly pass a copy. It might not be as fast as passing by reference, but whatever makes you sleep better...
sf::Packet packet;
packet<<"hi";
for(sf::TcpSocket & s : sockets)
    s.send(sf::Packet(packet));//this is ok
 
Title: Re: sending one packet to multiple recipents
Post by: Nexus on January 22, 2014, 10:43:34 am
s.send(sf::Packet(packet));//this is ok
The problem is that SFML's sockets take packets by non-const reference (which is a bit strange). Thus, you cannot pass a temporary object.
sf::Packet copy = packet;
s.send(copy);