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

Author Topic: sending one packet to multiple recipents  (Read 2288 times)

0 Members and 1 Guest are viewing this topic.

iride

  • Jr. Member
  • **
  • Posts: 88
    • View Profile
    • Email
sending one packet to multiple recipents
« 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?
 
« Last Edit: January 22, 2014, 04:57:01 am by iride »

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: sending one packet to multiple recipents
« Reply #1 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.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: sending one packet to multiple recipents
« Reply #2 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
 
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sending one packet to multiple recipents
« Reply #3 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);
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything