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

Author Topic: conflicting use of packet  (Read 2106 times)

0 Members and 1 Guest are viewing this topic.

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
conflicting use of packet
« on: November 18, 2015, 07:11:43 pm »
should i have more than one packet in a class?
for example in chat app.

theres a:
- list of people in the chat room
     - people who joins the room
     - people who leaves the room
- messaging system


so i only have one sf::Packet in my class
class ClientManager
{
    public:
         void sendPacket();
         void receivePacket()
         {
                socket.receive(packet);
         }
         sf::Packet packet;
}


class MainWindow
{
     public:
 
          void leavesTheChatRoom()
          {
                 client.packet << "my name"; // send it, so everybody knows who leaves the room
                 client.sendPacket();
          }
          void sendMessage()
          {
                 client.packet << "my message"; // send the message to everybody
                 client.sendPacket();
          }
          void receiveMessage()
          {
                client.receivePacket();
          }
    private:
          ClientManager client;

}

as you can see in the code, 3 functions can use the packet at the same time
- you send a message at the same time someone leaves the room
- you receive a message at the same time you send a message
- etc.

so if thats happens, the packet will have your message and the message of other client and the name in it
« Last Edit: November 18, 2015, 07:26:46 pm by lorence30 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: conflicting use of packet
« Reply #1 on: November 18, 2015, 09:14:23 pm »
Use local sf::Packet instances just when you need to send/receive data. There's no point to store and reuse a sf::Packet instance.
Laurent Gomila - SFML developer

 

anything