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

Author Topic: How to avoid Selector.Wait() hanging mainloop?  (Read 3803 times)

0 Members and 1 Guest are viewing this topic.

Haikarainen

  • Guest
How to avoid Selector.Wait() hanging mainloop?
« on: April 21, 2011, 01:52:42 pm »
I decided to learn networking with SFML's networking package, by creating a server for a game i'm working on.

The serverpart is fine atm, and the client is not implemented in the game yet because im writing it in a separate project to debug the server before implementing the code into the game.

To the point: I have a mainloop on the client, wich simply recieves packets from the server throu a selector. But it freezes the mainloop at;
if(this->Selector.Wait()){ HERE IT FREEZES ... codeblabla. .. }

I tried making it without a selector first, then it freezed at Socket.Receive(), i basically just want to recieve packets while updating other code(in this example  updating a sf::RenderWindow).

I am not requesting that you write the code for me, i simply want a push in the right direction.

Code for Server: http://fredrik.haikarainen.se/server.cpp
Code for Client: http://fredrik.haikarainen.se/client.cpp

Thanks in advance, would love to know how to get around this !  :D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to avoid Selector.Wait() hanging mainloop?
« Reply #1 on: April 21, 2011, 02:17:49 pm »
Sockets are blocking by default, which means that a call to Receive() won't return until the socket receives some data.

You must either receive data in a separate thread, or switch your sockets to non-blocking mode.
Laurent Gomila - SFML developer

Haikarainen

  • Guest
Nevermind!
« Reply #2 on: April 21, 2011, 02:18:49 pm »
I got it, solved with setting blocket to 0 on all sockets (after connecting, wouldnt connect otherwise :S solution?), throwing away the selector and  :
Code: [Select]

sf::Packet p;
sf::Socket::Status status = this->Socket.Receive(p);
switch(status){
case sf::Socket::Disconnected:
std::cout << "Server disconnected.\n";
this->Connected = false;
return false;
break;
case sf::Socket::Error:
std::cout << "Connection error.\n";
this->Connected = false;
return false;
break;
case sf::Socket::NotReady:
break;
case sf::Socket::Done:
std::string ms;
p >> ms;
std::cout << "Server: " << ms << "\n";
break;
}
[/code]

Haikarainen

  • Guest
How to avoid Selector.Wait() hanging mainloop?
« Reply #3 on: April 21, 2011, 02:19:59 pm »
Quote from: "Laurent"
Sockets are blocking by default, which means that a call to Receive() won't return until the socket receives some data.

You must either receive data in a separate thread, or switch your sockets to non-blocking mode.


Thanks for the fast reply :) got it just as you wrote tho, thanks anyway.

And btw; kudos on your work with SFML, never seen such dedication and awesome code before! Thanks for teaching me basics of game/network-programming :D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
How to avoid Selector.Wait() hanging mainloop?
« Reply #4 on: April 21, 2011, 02:30:59 pm »
Quote
wouldnt connect otherwise :S solution?

Which version of SFML are you using?
Laurent Gomila - SFML developer

Haikarainen

  • Guest
How to avoid Selector.Wait() hanging mainloop?
« Reply #5 on: April 21, 2011, 02:34:52 pm »
Quote from: "Laurent"
Quote
wouldnt connect otherwise :S solution?

Which version of SFML are you using?


2.0