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

Author Topic: SocketSelector question  (Read 1797 times)

0 Members and 1 Guest are viewing this topic.

Silderan

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
SocketSelector question
« on: January 19, 2015, 12:06:23 am »
Hello.

Reading docs I see all times code like this to use SocketSelector.

while(1)
{
    if( selector.wait() )
    {
        if( selector.isReady(listener) )
            // add new client.
        else
            // Check rest of sockets to receive their data.
    }
}

Comes to me two questions:
1. Why don't check rest of sockets if new client connects?
2. Is it a must to fit this code into a separate thread or can I set sockets to be non-blocking so the selector will return inmediatly? Threading is a pain xD And not sure if protecting data at moving from socket to external (main thread) buffer makes me to loose the speed improvement of multi-threading.

Thanks!!!

Silderán.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: SocketSelector question
« Reply #1 on: January 19, 2015, 12:48:01 am »
1. It would do so in the next loop but I see what you mean. To change that, you could always remove the "else"...
2. http://www.sfml-dev.org/tutorials/2.2/network-socket.php#non-blocking-sockets
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Silderan

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: SocketSelector question
« Reply #2 on: January 19, 2015, 11:26:00 am »
Afer asking, I'd see the reason: If I remove "else", the loop will try to read data from "listener" socket. I could code some "if" to don't read from "listener". But could be another way; "listener" non-blocking. The problen is that "connect()" don't return a new socket (as the usual in such APIs) and I must privide it.

listener.setBlocking(false);
sf::TcpSocket *newClient = new sf::TcpSocket();
while(1)
{
    if (listener.accept(*newClient) == sf::Socket::Done)
    {
        // Add new client.
        newClient = new sf::tcpSocket(); // Preparing new socket for next client.
    }
    if( selector.wait(1) )// One Millisecond wait.
    {
        // Check clients sockets to receive their data.
    }
}

What do you think?
Maybe this changes doesn't care at all xD

Silderán.