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

Author Topic: Good idea for a network thread?  (Read 3572 times)

0 Members and 1 Guest are viewing this topic.

s3rius

  • Newbie
  • *
  • Posts: 21
    • View Profile
Good idea for a network thread?
« on: April 18, 2011, 04:51:52 pm »
I'm tinkering with creating Multiplayer.
I was thinking about a good architecture to manage sending/receiving stuff and the best I've come up with is this:

So that would only be the Server-side part.

Code: [Select]


//TCP.h
class TCP
{
public:
TCP(void);
~TCP(void);

sf::SocketTCP m_listener;
sf::SocketTCP m_client;

sf::Thread *m_networkLoop;

};

//TCP.cpp

//Thread functions handling the Socket.
void NetworkThread( void* UserData ){
TCP* tt = static_cast<TCP*>(UserData);

sf::Packet packet;
sf::SocketTCP someClient;

while(true){

//Look for incoming connections and add them to a list
while( tt->m_listener.Accept( someClient ) == sf::Socket::Done ){
//Add someClient to m_clientsList
}

//Looks for incoming packets from m_client.
//Will be changed so it iterates through all sockets in m_clientsList.
while (tt->m_client.Receive( packet ) == sf::Socket::Done){
//Do stuff with packet
}

//If the local machine has some thing to send ..
while( gotSomethingToSend() ){
tt->m_client.Send( packet ); //Would be changed to send it to all sockets in m_clientsList
}

sf::Sleep(0.1f);
}
}

TCP::TCP(void)
{
m_listener.Listen(1234);

m_listener.SetBlocking( false );
m_client.SetBlocking( false );

m_networkLoop = new sf::Thread(&NetworkThread, this);
m_networkLoop->Launch();
}


TCP::~TCP(void)
{
delete m_networkLoop;
}




Now, I understand that there's a lot of problems in this code in regard of how to properly handle Sockets/Threads (like not closing them).

But my main question is whether the concept itself would work.

e.g.
That received packets and incoming connections can be taken from the Socket like events from the event queue.
And that I can use the same socket to send and receive.

Or whether this is a horribly bad idea ( wouldn't be my first one! :D ).

GatorQue

  • Newbie
  • *
  • Posts: 36
    • View Profile
Networking suggestion
« Reply #1 on: April 19, 2011, 06:28:51 am »
I'm not sure that I have reviewed your idea completely.  But I did want to point you to a website where you can read some more about networking in computer games.  Your idea of treating networking packets like events has merit, and this website should hopefully give you some additional ideas about your approach.  While not SFML specific, the concepts should be easily ported over.

http://gafferongames.com/networking-for-game-programmers/

I'm also hoping to have a multiplayer example done by the end of summer, but that might be a bit too ambitious a goal for me, we shall see.

s3rius

  • Newbie
  • *
  • Posts: 21
    • View Profile
Good idea for a network thread?
« Reply #2 on: April 19, 2011, 04:33:03 pm »
Well, my problem is more about how sockets work in SFML.

The website tells me how to open winsock ports (which I don't need due to SFML) and more advanced stuff like Protocol Ids / Client-side prediction (which I also don't need).

Interesting site nontheless, but doesn't exactly help me  :P

PS:
Well, I got some help from a friend and it seems I can use my approach :)

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Good idea for a network thread?
« Reply #3 on: April 20, 2011, 09:12:27 am »
I'm interested in how using a while loop to use the receive function is different than an if statement.

s3rius

  • Newbie
  • *
  • Posts: 21
    • View Profile
Good idea for a network thread?
« Reply #4 on: April 20, 2011, 02:49:15 pm »
Quote from: "Wizzard"
I'm interested in how using a while loop to use the receive function is different than an if statement.


Because I made the Socket non-blocking.

Code: [Select]

     socket.Receive( blah );


This stops the code and waits for the socket to receive something, much like std::getline does.

But if you set it to non-blocking it will return immediately. If no packet to receive is found it'll return NotReady, I believe.

So instead of waiting for the socket until it receives something I am periodically asking it whether it has received something.

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Good idea for a network thread?
« Reply #5 on: April 21, 2011, 04:09:57 am »
Can a socket be ready more than once in the while loop?
Code: [Select]
while (socket.Receive(blah, blah, blah)) // or socket.Receive(blah)
    Blah();

s3rius

  • Newbie
  • *
  • Posts: 21
    • View Profile
Good idea for a network thread?
« Reply #6 on: April 21, 2011, 02:48:33 pm »
Quote from: "Wizzard"
Can a socket be ready more than once in the while loop?
Code: [Select]
while (socket.Receive(blah, blah, blah)) // or socket.Receive(blah)
    Blah();


I guess if it received two packets since the last call then yes.
But I'm guessing here :D

However, it works for me so what do I care :3

 

anything