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

Author Topic: Managing clients using selectors ?  (Read 3064 times)

0 Members and 1 Guest are viewing this topic.

Haikarainen

  • Guest
Managing clients using selectors ?
« on: July 21, 2011, 03:12:50 pm »
I have a hard time to understand this, say we have a really simple server.

It listens on a sf::TcpListener that is added to a sf::TcpSelector.
Every frame it iterates through all of the selectors ready sockets, If the current ready socket equals the selector, it accepts the new connection and adds that socket to the listener.

But how would i manage the clients outside the selector itself?

Say we have a class named Client. Wich contains the clients Socket, and some other data(like Name , Xpos, Ypos etc), And that we have an "std::vector<Client> Clients" that holds the connected clients.

How would i properly add clients to this vector using the same sf::TcpSocket that is accepted and added to the selector? Would the Clients socket be a pointer to the one in the selector? How would i then get a pointer to that socket?

Could someone please help me understand how to do this? Im not looking for someone to code a working example(even if that would be nice), but the knowledge on how to do this myself.

Thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Managing clients using selectors ?
« Reply #1 on: July 21, 2011, 03:28:06 pm »
You must do the opposite (the selector is not a container, it's not supposed to own the sockets): create your client, assign it its socket, push it to the array of clients, and then add the client's socket to the selector so that you can be notified when it's ready to receive.
Laurent Gomila - SFML developer

Haikarainen

  • Guest
Managing clients using selectors ?
« Reply #2 on: July 21, 2011, 03:32:12 pm »
Quote from: "Laurent"
You must do the opposite (the selector is not a container, it's not supposed to own the sockets): create your client, assign it its socket, push it to the array of clients, and then add the client's socket to the selector so that you can be notified when it's ready to receive.


Oh, of course :P

If i do this without using pointers (wich i think is my only option), would the socket in the selector be "synced" to teh socket in the clients?

Thanks  alot for the quick reply ! :)

EDIT:
Code: [Select]
if(CurrentSocket == this->Listener){
// We have a new client !
sf::IPAddress NewCAddress;
sf::SocketTCP NewClient;

this->Listener.Accept(NewClient, &NewCAddress);
std::cout << "Client connected ! (" << Address << ")" << std::endl;

// Add it to the selector and clients
this->Selector.Add(NewClient);
this->Clients.push_back(new cClient(NewClient, NewCAdress))
}


Would this work? Adding the sockets as nonpointers, and the sockets will still "work togheter" as if they were "the same" ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Managing clients using selectors ?
« Reply #3 on: July 21, 2011, 04:02:41 pm »
Yes, in SFML 1.6 sockets have value semantics. Which means that it doesn't matter which instance you use, it's the carried value (the socket identifier) which is important. So you can have several Socket instances all refering to the same physical socket.

This is no longer true in SFML 2.0.
Laurent Gomila - SFML developer

Haikarainen

  • Guest
Managing clients using selectors ?
« Reply #4 on: July 21, 2011, 04:05:44 pm »
Quote from: "Laurent"
Yes, in SFML 1.6 sockets have value semantics. Which means that it doesn't matter which instance you use, it's the carried value (the socket identifier) which is important. So you can have several Socket instances all refering to the same physical socket.

This is no longer true in SFML 2.0.


Hm ok, the thing is im using sfml 2.0, how would this go about in the new version? Creating a pointer from the NewClient, wich is temporary, wouldnt be a great idea.. Or am i wrong?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Managing clients using selectors ?
« Reply #5 on: July 21, 2011, 04:09:10 pm »
Quote
Hm ok, the thing is im using sfml 2.0

Hum? Your code uses SFML 1.6.

Quote
how would this go about in the new version?

You need to allocate and store the socket in your client object, and then pass around a pointer to it (to the selector).
Laurent Gomila - SFML developer

Haikarainen

  • Guest
Managing clients using selectors ?
« Reply #6 on: July 21, 2011, 04:17:15 pm »
Quote from: "Laurent"
Hum? Your code uses SFML 1.6.

Lol darn it, im currently reworking some network code since i figured out i was totally lost in how it all worked. So i took some example code from the tutorials and forgot to adapt them to 2.0


Quote from: "Laurent"
You need to allocate and store the socket in your client object, and then pass around a pointer to it (to the selector).


Oh, so tcpselectors in 2.0 take pointers ? :)

Edit : Nvrmnd found usage example in documentation, will go on from here, Thanks alot for your help!