LAN Server Discovery
LAN Server Discovery
LAn server discovery is teh easiest way to organize a game over a network.
- First of all, a port needs to be set for a server to listen on for incoming connections.
- To discover game servers, a client sends out a broadcast message (ID_PING) over a local network.
- After receiving this message, a server that is currently running sends the response (ID_PONG) back to the client if it matches the filter. As a filter (game_type_info) a game version is usually used.
- A server can send additional information in its ID_PONG message (for example, it can be a map name or the current number of players).
Moreover:
- A server can send its updates to a client via network.advertiseServer().
- A request to a specific machine can be sent via network.ping().
The following events are used to handle network messages for LAN server discovery:
- For ID_PONG message, NETWORK_ON_PONG_RECEIVED network message is used.
- For server updates, NETWORK_ON_LAN_SERVER_ADVERTISE network message is used.
LAN Server Discovery / Offline Ping Functions
- int network.pingAvailableLANServers(int listen_port,int server_port,string game_type_info) — sends a request to discover servers in the LAN.
- listen_port — the number of a port used by the client to receive response from servers
- server_port — the number of a port to direct request to (i.e. a broadcast request will be sent to the specified port)
- game_type_info — string containing game information. It used to filter out server responses (in order to receive responses only from necessary servers).
This function returns 1 if the request was successfully sent; otherwise, 0.
- int network.advertiseServer(int listen_port) — sends information about the server into the LAN.
- listen_port — the number of a port to direct packets to
This function returns 1 if the request was successfully sent; otherwise, 0.
- listen_port — the number of a port to direct packets to
- int network.ping(NetworkAddress network_address) — sends off-line ping to the specified address. (It is used to send ping to network node when no connection is established).
- network_address — address of the target network node
This function returns 1 if the request was successfully sent; otherwise, 0.
- network_address — address of the target network node
- void network.setGameTypeInfo(string game_type_info) — sets information about the game. By this information clients will find necessary servers in the LAN and filter out all the rest.
- game_type_info — string containing game information.
- string network.getGameTypeInfo() — gets a string that contains information about the game and is used to filter out responses from servers in the LAN.
- void network.setCustomInfo(string custom_info) — sets additional information that will be sent in response to a server discovery request or to an offline ping one.
- string network.getCustomInfo() — gets additional information that will be sent in response to the server discovery request or to an offline ping one.
Samples
- Example of server discovery can be found under <UnigineSDK>/data/network/samples/land_discovery folder.
- Example of restoring connection and changing to another server can be found under <UnigineSDK>/data/network/samples/server_reelection folder.
Last update: 2017-07-03
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)