Results 1 to 19 of 19

Thread: Need help with more advanced netgame concepts

  1. #1

    Default Need help with more advanced netgame concepts

    Hello,
    I am looking to help myself understand networking more by making a program that moves a sprite around both clientside and serverside. Up to this point I have tried reading through the netgame example code, but I am stuck on a few concepts. I understand how to do things as demonstrated in this tutorial, but I see that in the actual example, the code uses things like NetgameEventDispatchers, and SlotContainers. I may end up just making something I can understand with the TCP classes, but before I embark on a project like that, I'd like to try to better understand the preexisting netgame engine better. Here is what I think I know so far and what I need help with:

    NetgameEventDispatchers seem similar to the if-else statements in the tutorial, except they directly (somehow) call functions with a type (or types) of argument(s) specified when they are created like this:
    Code:
    CL_NetGameEventDispatcher_v1<ServerUser*>  //This would specify a dispatcher that takes 1 ServerUser* argument, correct?
    By using dispatchers, you only ever need to pass the name of the event and the correct type of argument(s) and the dispatcher will decide which function to send it to. I'm pretty sure about this, but could somebody please confirm this, and also explain how I would go about initializing dispatchers with more than 1 argument?

    Slot containers are similar to dispatchers in the sense that they call functions, but unlike dispatchers, slot containers respond whenever they receive signals. However, I can't figure out how slot containers pass arguments on to the functions they are calling. Could somebody please help me?

    I know that this is already a lot to ask, but I have one final question. How would you go about passing custom classes (for example, a coordinates class) as an argument in a NetGameEvent? In the example it converts the class to a NetGameEventValue, but is that necessary? What even is a NetGameEventValue?

    Thank you guys so much for even reading this post. I know it's a lot to ask, but the reference docs aren't meant for people like me who don't even understand what the basic function of each class is.

    Once again, many thanks,
    igpay

  2. #2
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    554

    Default

    The link you provided points to the old documentation. It was recently moved to a wiki and that version had been updated with a section describing the dispatcher class: http://clanlib.org/wiki/MainDocs:NetGame_Engine

    I am not sure how good a description the above article provides for the event dispatcher, but basically just imagine that its a more convenient way of typing:

    Code:
    if (event.get_name() == "login")
      on_login(event, customdata);
    else if (event.get_name() == "start")
      on_start(event, customdata);
    else if ..
    About the slot container, that class is described in http://clanlib.org/wiki/MainDocs:Template_Callbacks

    Hope that helps.

  3. #3

    Default

    Thank you so much. I knew that that page had been updated recently (I even searched google for a cached version, just in case someone had deleted the extra info at the bottom).

    What you have told me has confirmed what I thought I knew about dispatchers, and told me how to make one with more than one argument (which for future reference, would look like this):
    Code:
    CL_NetGameEventDispatcher_v2 <int, int>
    I'm still a bit iffy on how slots pass arguments to the functions they are calling, but I'll probably be able to figure that out now. Now I just need to figure out how to go about passing custom classes to a NetGameEvent.

    Thanks so much for your help!


    edit:
    I've got it now. The slots don't directly pass the arguments to the functions. Instead, both arguments are in the signal itself, which then passes the arguments to the functions. Additionally, the slots themselves can have fixed parameters that are passed to the functions after signal's arguments.

  4. #4

    Default

    I have a question about NetGame as well. I scanned the code about, and it seems that it uses TCP, is this correct?

    I may not have a complete understanding of TCP, but is it not possible that, if one user doesn't respond for long enough (Say, they disconnect their internet without first closing the TCP connection), that the buffer would eventually fill, and function calls which try to send game events will start to block the thread?

    If this is the case, has ClanLib considered this problem? If so, what does ClanLib do in the event that your sent game event would block the thread? Does it cause the connection to fail? Does it simply drop that one event?

  5. #5

    Default

    Some background:
    I want to make a server where many users can connect and see each other in the same virtual space. If there's a lot of movement happening, there might be many events happening in a short period of time, and if someone's bad connection is unable to keep up, I want them to be disconnected, rather than blocking and waiting for them (as I believe TCP streams are wont to do)

  6. #6
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    554

    Default

    The NetGame implementation uses a separate thread to dispatch the messages, so the main thread will never block while netevents are being delivered.

    The server also uses individual queues for each client, so one client cannot delay or block the communication with other clients.

    Both the API and the implementation lacks code to disconnect a client that does not process the events at all or processes them slower than they are queued. In this situation the events will continue to queue up until the client disconnects or the server runs out of memory.

    You have two options on avoiding this problem. The simplest would be to occasionally send a 'ping' event to the client that you except a 'pong' reply to within a reasonable time frame. If you do not get the pong reply you forcefully disconnect the client. Implementing this would also give your game a ping value for each client.

    The alternative solution is to add the support in the API for automatic disconnection if the queue grows too large. Doing this is in principle easy enough, but deciding the allowed amount of messages to be queued before disconnection is not that easy to decide. It would be unfortunate if the server started disconnecting well-behaving clients simply because some game decided to send 100 small netevents simultaneously.

  7. #7
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    554

    Default

    Oh and yes, it does use TCP for delivery.

    The main advantage of this approach is that you are guaranteed that messages are received in exactly the same order they are sent, with no messages dropped or arriving late. This makes it far less likely to have bugs hiding where some piece of code does not correctly deal with dropped or re-arranged messages.

    The main disadvantage of using TCP is that once a message has been dispatched it WILL arrive and in the order it was sent. For example, this means that if a player position update packet is lost, it will be retransmitted even though the message following it wasn't lost and contains more up-to-date data.

    It is common for some games to use UDP instead to avoid this problem. But I'm not personally fully convinced that UDP is a better approach given the complexity involved in writing a high-performance retransmit of lost packets (for those that you DO want to always arrive), not to mention that UDP doesn't guarantee that the packets arrive in the same order they were dispatched. Either way, games like World of Warcraft uses only TCP and illustrates that at least for that genre its quite possible to send your player position updates without problems with TCP.

  8. #8

    Default

    Awesome! Thanks, that information is super helpful

    I suppose there's nothing stopping me from using TCP and UDP at the same time, too

    But I don't think it'll be necessary. The game I'm making doesn't rely on players timing their movements or anything like that.

    By the way, about your suggestion to do ping checks on the clients and disconnect them if they're too slow, how do you do that with NetGame?

  9. #9
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Bergen, Norway
    Posts
    588

    Default

    I once posted some code that demonstrates some ping-pong event handling using NetGame:

    http://www.rtsoft.com/forums/showthr...ll=1#post10816

  10. #10

    Default

    Ok, I'm back again with another question. After creating the majority of my program, I am beginning to wonder if it was all for naught. My entire system was based around the concept that you could send classes that you had created through the client/server connection, but apparently I was wrong. Is there some way I could static_cast the class I want to send, and then convert it back on the other side? I may just make some other version of the netgame engine that I can understand using TCP.

  11. #11

    Default

    Quote Originally Posted by sphair View Post
    I once posted some code that demonstrates some ping-pong event handling using NetGame:

    http://www.rtsoft.com/forums/showthr...ll=1#post10816
    That code doesn't seem to disconnect the user if they don't reply in a certain amount of time.

    How do you disconnect a user?

  12. #12

    Default

    I'm not sure if this is optimal, but if you trust the clients, you could always send them an event that is handled by them calling CL_NetGameClient::disconnect()

    I don't think that there is any way to kick clients serverside.

  13. #13
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    I have never used NetGame before, but comparing 2.1.1 code to 2.2.0,
    "CL_NetGameConnection::connection_main()" contains

    In 2.1.1:
    Code:
    for (unsigned int i = 0; i < new_send_queue.size(); i++)
    {
    	CL_NetGameNetworkData::send_data(connection, new_send_queue[i]);
    }
    In 2.2.0:
    Code:
    for (unsigned int i = 0; i < new_send_queue.size(); i++)
    {
    	if (new_send_queue[i].type == Message::type_message)
    	{
    		CL_NetGameNetworkData::send_data(connection, new_send_queue[i].event);
    	}
    	else if (new_send_queue[i].type == Message::type_disconnect)
    	{
    		connection.disconnect_graceful();
    		site->add_network_event(CL_NetGameNetworkEvent(this, CL_NetGameNetworkEvent::client_disconnected));
    		return;
    	}
    }
    It is possible ClanLib 2.1.2 (SVN) contains this code, but I have not checked.

  14. #14

    Default

    Unfortunately I'm thinking of disconnecting them if they stop responding to prevent messages from queuing up forever (or until the connection times out in the kernel, which can be a long time)

    If they're not responding, I can't ask them to disconnect.

  15. #15

    Default

    Thanks, rombust. I'll take a look at svn and possibly use 2.1.2 or 2.2.0

  16. #16

    Default

    Doesn't appear to be in 2.1.2

  17. #17

    Default

    Hm... Does anyone know if there's an ETA for Clanlib 2.2?

    If I develop my game in Clanlib 2.1, what's the percentage likelihood that it will be easy to port to 2.2 when it's released to gain access to its new features?

  18. #18
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Bergen, Norway
    Posts
    588

    Default

    http://clanlib.org/wiki/BreakingChanges

    This contains all the breaking changes between 2.1 and 2.2. Shouldnt take long to upgrade from 2.1.

  19. #19

    Default

    Okay cool then. I suspect this will only become an issue during large scale production, which is a bridge best crossed when I come to it.

    Thanks for the help everyone. I think ClanLib should meet my needs

Similar Threads

  1. Sending an array with NetGame
    By CrisisSDK in forum Official ClanLib SDK Forums
    Replies: 3
    Last Post: 12-16-2009, 05:08 PM
  2. questions about netgame event value
    By ppheathead in forum Official ClanLib SDK Forums
    Replies: 2
    Last Post: 07-07-2009, 07:10 AM
  3. problem of raw data parameter passing netgame engine
    By ppheathead in forum Official ClanLib SDK Forums
    Replies: 3
    Last Post: 07-06-2009, 12:11 PM
  4. NetGame channeling?
    By catch22 in forum Official ClanLib SDK Forums
    Replies: 2
    Last Post: 05-29-2009, 06:43 AM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •