Template Workshop 2: Lanlist Template
In this Workshop you will learn how you can search for game servers inside
a lan. The templates for searching for servers inside a lan and over the
internet are combined. Both have nearly the same interface and are easy to use.
The internet template will be explained in detail in the next workshop.
Functions of the internet serverlist template start with
ITemp_ and lan serverlist function start with LanTemp_.
The whole template is open source and can be easily adapted to your needs.
If you find bugs or possible improvements, share them with us by
sending me a mail or post a forum entry on the Gamestudio forum, please!
The theory behind
Beside the normal game server (which is started through
enet_init_server()) a small server that listens on an other udp
port is started. If players are searching for game servers, they will
broadcast (that is sending to everybody) a "search" packet. Every game
server that has this small udp server running will response with server
related informations like the server name, number of players,...
The player that sent the "search" packet will wait some time and collect
all packets that are comming from the game servers. The server informations
are then listed. After that, the whole list can
be displayed on the screen and one of the servers can be selected to
connect to.
Because the player should only find servers of the same game,
the search packet contains a string that determines from which game the
packet was sent. The first part of the string is the user_name
the Gamestudio edition was registered to and the second part is userdefined.
It's recommended to use the game's name here. So the whole string could
look like this for example: "Peter SoxbergerCoolest ANet Game 2".
The time the servers need to response to the search packet is captured.
The measured value is stored in the ping field (in ms) of the
server list entry.
The example project
The template comes with an example project which is located in the "Example"
folder. Start the application "LanTemp_Example.exe".
Press [A] to start a server. Now start the application again (don't
close the current running one) and press [G]. After 3 seconds, the
server should appear in a list. Now switch back to the server application
and press [I] a few times. This increases the number of players.
Switch back to the second application and press [G] again. The number
of players should have changed now. If you have more than one computer
at home and they are inside the same lan, you can start the server also
on a different computer.
If you press [R], the server will be stopped and can't be found
anymore.
Please notice that it's not possible to have a server running
and doing a searching for servers in the same application!
How to include the template into a project?
First of all you have to copy the script files into your project main folder. The script files are located in the "Lite-C" folder.
Then add this line #include "Serverlist.h" after this line #include "anet.h".
Now you have full access to the template's functions.
A short overview
At first the template always has to be initialized by using this function:
The ServerListEntry struct:
This struct represents an entry in the server list. If you want to open
a server that is added to the list, fill the struct with the informations about your
server and share it through the function LanTemp_AddServer() (lan template) or ITemp_AddServer() (internet template).
The ServerList struct:
This struct represents the whole server list and is filled by
LanTemp_GetList() or ITemp_GetList().
If you need an example code look into LanTemp_Example.c. For more
informations about the template functions look into Serverlist.h.
Have fun!