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

Author Topic: Synchronous/asynchronous socket handling.  (Read 2002 times)

0 Members and 1 Guest are viewing this topic.

KryQ

  • Newbie
  • *
  • Posts: 1
    • View Profile
Synchronous/asynchronous socket handling.
« on: April 15, 2015, 06:44:00 pm »
Hello everybody :P
Few months ago I started to play with SMFL and i decided to make RPG like game simply for learning purposes.
And just because i like the concept of MMO i wanted to use simple client<->server communication using TCP sockets.

But after some time (and few hundreds of poorly written lines of code) i ran into one major obstacle that i think i overcomed but not very nicely. Thats why i'm asking you mighty Greybeards for some guidance.

To the point.

In my main client thread i have functions like this (code is written from head but i hope You get the idea):

sf::Vector2f CInputHandler::sendCmdGetPos()
{
 listening = false;
 std::string cmd;
 sf::Vector2f pos

 if (socket.recieve(packet) == sf::Socket::Done)
 {
  packet >> cmd;
  if(cmd=="playerPos")
  {
   packet >> pos;
   listening = true;
   return pos;
  }
 }
}
 

And everything would be fine and dandy but i also have separate thread for asynch calls from server (something like player "Kupa" logged in)

void CInputHandler::listen()
{
 while (1)
 {
  if(listening)
  {
   socket.setBlocking(false);
   if (socket.recieve(packet) == sf::Socket::Done)
   {
    packet >> cmd;
    if(cmd=="playerLoggedIn")
    {
     packet >> name;
     printf(name)
    }
   }
  }
  socket.setBlocking(true);
 }
}
 

And well it works but seems like not really clean way to solve the problem. Maybe somebody have better way to solve this?

Thanks in advance for responds, KryQ

 

anything