Thursday, April 29, 2010



Chapter 2: Server

[Prev: Requirements] [Next: Client]


No matter what networking game it is (RTS, FPS, RPG, etc.), in order for it to work on a network, there must be at least 1 computer as the server. When making a call on the phone, someone must be on the other side to pick up -- the same thing for the internet. Do not think of a server as this highly powerful untouchable thing, it isn't. The main job of a server is to "listen to the phone" waiting for people to connect to it, and transfer data between people.

Imagine a local bar, does the waitress go door to door selling liquor? No, this is similar to what a server does, the client must walk "into the bar" (connect) to get liquor, and it also allows people to see each other. This is just a crude analogy.

Let's go ahead and start making some code. We will begin with initialization, like most things, variables need to be set properly before they are used, 39dll is no different. Start by loading the Dllscripts.gm6 file, create a persistent object called "obj_dll", and in the create event put:
dllinit(0, true, false);

The first argument "0" means use "39dll.dll" file, if you rename the dll file, you would put the string here. The second argument loads our network functions, the third loads utility functions (which we won't use in this tutorial). You only need to place 1 of these objects in the first room and you're done. Save this file as base.gmk because we will be modifying it further as we go through the other chapters.

*note: From here on I will be mentioning code and in what module to add it to with a short description to make learning quicker. If more information is needed, I will put it.

base.gmk object obj_dll create event
listen = tcplisten(12000, 4, 1);
if (listen < 0) game_end();

This makes the program begin listening to port 12000. When the server is busy, it can not always connect people right away, this is called a queue, and the "4" is how many in that queue are allowed. If the queue is full, any new clients will be ignored, 4 is sufficient for now. The "1" means "non-blocking", it deals with parallelism, which is what we want, so just set it. The function returns a "socket" required by other functions or "-1" if it failed (a socket is the connection between your program and the network card containing information about the virtual connection between the two computers, the complex actions are seemlessly done by the simple functions supplied by 39dll). We need an object to hold client connections, so create persistent object "obj_client".

obj_client create event
client = -1;

obj_dll step event
client = tcpaccept(listen, 1);
if (client >= 0)
{
    o = instance_create(0, 0, obj_client);
    o.client = client;
}

When clients try to connect, the server must accept their connection (which means the underlying socket will store their IP and port information for you). It will grab the first in the queue and create a connection and return the socket (or "-1" if none were in the queue). After a connection has been made, we create the client object and give it the socket. Save this file as server.gmk.


[Prev: Requirements] [Next: Client]

No comments:

Post a Comment