Latest  | Search | Go
Edit this page   |   Attach file 

  Home | Tutorials | Technical Reference | Runtime | API Documentation | ClientNetworking  


Client-Data Networking

Author:Jeremy Stieglitz


What is Client-Data Networking?

Client-Data Networking is a set of functions you can use to get Data from the Clients to the Server. You write Data on the Client-side, which is then sent to the Server-side for Reading. These Data packets are checked on the Server for buffer overrun, underrun, and other malformed possibilities. However, it's up to you to ensure that you only use Client-Data Networking for networking which will retain a Server-authoritative World state, to avoid cheating. Namely Client-Data Networking is very useful for networking information such as arbritrary Client-side GUI input, but you would not want to use Client-Data Networking to send information such as Ammo or Damage amounts.

Writing Data on the Client

Let's look at the simple set of functions used to write Data on the Client. First, you'll want to define the Client Data Message Types (unsigned chars) you're going to use in your application's networking. It's best to do this with some public static variables located in an easily accessible class, such as your Module's GameCore. The EvalKit defines the following Client Data Message Type in its GameCore:

public class GameCore : LogicCore
{
   public const byte MSGID_CLIENTDATA_JOIN_TEAM = 1;
...

Now whenever you want to send a Data message from the Client, you'll always want to begin by writing the MessageType. Then proceed to write the rest of the Message's Data, one variable at a time, like so:

      MClient.WriteMessageTypeClientData(GameCore.MSGID_CLIENTDATA_JOIN_TEAM);
      MClient.WriteIntClientData(1);

That's all there is to writing Client Data. At the end of each frame, all Client Data queued for sending will be automatically batched and sent to the Server. Just make sure you begin each Message block with MClient.WriteMessageTypeClientData!

Reading Data on the Server

The Server will receive each MessageType that the Client wrote in the "GameCore." and can then read out the corresponding variables in the order which the Client wrote them. Let's see how this is done:

GameCore.cs:
---
public override void Server_ProcessClientMessage(MNetworkClient client, byte msgType)
{
      switch (msgType)
      {
         case MSGID_CLIENTDATA_JOIN_TEAM:
            int team = MServer.ReadIntClientData();
                                if(team < 1)
                                   team = 1;
                                else if(team > 2)
                                   team = 2;
            AddPlayer(client, team);
            break;
         default:
            break;
      }
}

You don't need to worry about error-handling invalid packets from the Client; the read functions will simply just return null values in the case of malformed packet buffer. Just ensure you're ready to handle the whole range of possible values for a given data type; manually clamp the values if necessary! And remember, to ensure the security of your game, it is best to only use Client-Data Networking for input-related systems like abstract GUI functionality such as team selection or item purchasing. Furthermore, action-keypresses are more effectively networked via the GameInput class as described in NetworkingSystem.

ClientNetworking   Edit | Attach | Ref-By | Printable | Diffs | r1.1 | More