network_udp.c
author celestar
Mon, 01 Jan 2007 16:07:21 +0000
branchcustombridgeheads
changeset 5621 6ce400c0a2f4
parent 5568 75f13d7bfaed
permissions -rw-r--r--
(svn r7719) [cbh] - Codechange: KUDr and I have decided that our developemnt efforts went in the wrong direction, so revert all changes from 7686 onwards
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2153
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2153
diff changeset
     2
4826
63b1eb7c966b (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4344
diff changeset
     3
#ifdef ENABLE_NETWORK
63b1eb7c966b (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4344
diff changeset
     4
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1186
diff changeset
     6
#include "debug.h"
1317
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents: 1299
diff changeset
     7
#include "string.h"
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
     8
#include "network_data.h"
4261
2ec8f5a9747b (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4077
diff changeset
     9
#include "date.h"
850
e18648f7d5a4 (svn r1331) Update map size before sending game info
tron
parents: 805
diff changeset
    10
#include "map.h"
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    11
#include "network_gamelist.h"
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
    12
#include "network_udp.h"
2153
91e89aa8c299 (svn r2663) Include variables.h only in these files which need it, not globally via openttd.h
tron
parents: 1832
diff changeset
    13
#include "variables.h"
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    14
#include "newgrf_config.h"
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    15
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    16
//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    17
// This file handles all the LAN-stuff
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    18
// Stuff like:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    19
//   - UDP search over the network
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    20
//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    21
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    22
typedef enum {
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
    23
	PACKET_UDP_CLIENT_FIND_SERVER,
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    24
	PACKET_UDP_SERVER_RESPONSE,
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
    25
	PACKET_UDP_CLIENT_DETAIL_INFO,
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
    26
	PACKET_UDP_SERVER_DETAIL_INFO,   // Is not used in OpenTTD itself, only for external querying
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
    27
	PACKET_UDP_SERVER_REGISTER,      // Packet to register itself to the master server
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
    28
	PACKET_UDP_MASTER_ACK_REGISTER,  // Packet indicating registration has succedeed
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
    29
	PACKET_UDP_CLIENT_GET_LIST,      // Request for serverlist from master server
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
    30
	PACKET_UDP_MASTER_RESPONSE_LIST, // Response from master server with server ip's + port's
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
    31
	PACKET_UDP_SERVER_UNREGISTER,    // Request to be removed from the server-list
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    32
	PACKET_UDP_CLIENT_GET_NEWGRFS,   // Requests the name for a list of GRFs (GRF_ID and MD5)
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    33
	PACKET_UDP_SERVER_NEWGRFS,       // Sends the list of NewGRF's requested.
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    34
	PACKET_UDP_END
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    35
} PacketUDPType;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    36
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
    37
enum {
2861
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
    38
	ADVERTISE_NORMAL_INTERVAL = 30000, // interval between advertising in ticks (15 minutes)
4344
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
    39
	ADVERTISE_RETRY_INTERVAL  =   300, // readvertise when no response after this many ticks (9 seconds)
5d0e40cd67b9 (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
    40
	ADVERTISE_RETRY_TIMES     =     3  // give up readvertising after this much failed retries
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
    41
};
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    42
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    43
#define DEF_UDP_RECEIVE_COMMAND(type) void NetworkPacketReceive_ ## type ## _command(Packet *p, struct sockaddr_in *client_addr)
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2789
diff changeset
    44
static void NetworkSendUDP_Packet(SOCKET udp, Packet* p, struct sockaddr_in* recv);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    45
2789
75ce96e754a0 (svn r3337) Remove an unused variable and add 2 times static
tron
parents: 2775
diff changeset
    46
static NetworkClientState _udp_cs;
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
    47
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    48
/**
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    49
 * Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    50
 * @param p the packet to write the data to
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    51
 * @param c the configuration to write the GRF ID and MD5 checksum from
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    52
 */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    53
static void NetworkSend_GRFIdentifier(Packet *p, const GRFConfig *c)
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    54
{
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    55
	uint j;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    56
	NetworkSend_uint32(p, c->grfid);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    57
	for (j = 0; j < sizeof(c->md5sum); j++) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    58
		NetworkSend_uint8 (p, c->md5sum[j]);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    59
	}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    60
}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    61
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    62
/**
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    63
 * Deserializes the GRFIdentifier (GRF ID and MD5 checksum) from the packet
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    64
 * @param p the packet to read the data from
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    65
 * @param c the configuration to write the GRF ID and MD5 checksum to
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    66
 */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    67
static void NetworkRecv_GRFIdentifier(Packet *p, GRFConfig *c)
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    68
{
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    69
	uint j;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    70
	c->grfid = NetworkRecv_uint32(&_udp_cs, p);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    71
	for (j = 0; j < sizeof(c->md5sum); j++) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    72
		c->md5sum[j] = NetworkRecv_uint8(&_udp_cs, p);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    73
	}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    74
}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    75
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
    76
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    77
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    78
	Packet *packet;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    79
	// Just a fail-safe.. should never happen
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    80
	if (!_network_udp_server)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    81
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    82
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    83
	packet = NetworkSend_Init(PACKET_UDP_SERVER_RESPONSE);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    84
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    85
	// Update some game_info
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    86
	_network_game_info.game_date = _date;
850
e18648f7d5a4 (svn r1331) Update map size before sending game info
tron
parents: 805
diff changeset
    87
	_network_game_info.map_width = MapSizeX();
e18648f7d5a4 (svn r1331) Update map size before sending game info
tron
parents: 805
diff changeset
    88
	_network_game_info.map_height = MapSizeY();
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    89
	_network_game_info.map_set = _opt.landscape;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    90
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    91
	NetworkSend_uint8 (packet, NETWORK_GAME_INFO_VERSION);
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
    92
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    93
	/* NETWORK_GAME_INFO_VERSION = 4 */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    94
	{
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    95
		/* Only send the GRF Identification (GRF_ID and MD5 checksum) of
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    96
		 * the GRFs that are needed, i.e. the ones that the server has
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    97
		 * selected in the NewGRF GUI and not the ones that are used due
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    98
		 * to the fact that they are in [newgrf-static] in openttd.cfg */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
    99
		const GRFConfig *c;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   100
		uint i = 0;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   101
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   102
		/* Count number of GRFs to send information about */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   103
		for (c = _grfconfig; c != NULL; c = c->next) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   104
			if (!HASBIT(c->flags, GCF_STATIC)) i++;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   105
		}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   106
		NetworkSend_uint8 (packet, i); // Send number of GRFs
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   107
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   108
		/* Send actual GRF Identifications */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   109
		for (c = _grfconfig; c != NULL; c = c->next) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   110
			if (!HASBIT(c->flags, GCF_STATIC)) NetworkSend_GRFIdentifier(packet, c);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   111
		}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   112
	}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   113
4326
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   114
	/* NETWORK_GAME_INFO_VERSION = 3 */
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   115
	NetworkSend_uint32(packet, _network_game_info.game_date);
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   116
	NetworkSend_uint32(packet, _network_game_info.start_date);
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   117
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   118
	/* NETWORK_GAME_INFO_VERSION = 2 */
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   119
	NetworkSend_uint8 (packet, _network_game_info.companies_max);
2944
7c392e7b51c6 (svn r3500) - Workaround the inaccurate count of spectators/companies that can happen in certain border-cases. For now just dynamically get this value when requested so it is always right. To do properly all player/client creation/destruction needs a hook for networking.
Darkvater
parents: 2881
diff changeset
   120
	NetworkSend_uint8 (packet, ActivePlayerCount());
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   121
	NetworkSend_uint8 (packet, _network_game_info.spectators_max);
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   122
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   123
	/* NETWORK_GAME_INFO_VERSION = 1 */
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   124
	NetworkSend_string(packet, _network_game_info.server_name);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   125
	NetworkSend_string(packet, _network_game_info.server_revision);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   126
	NetworkSend_uint8 (packet, _network_game_info.server_lang);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   127
	NetworkSend_uint8 (packet, _network_game_info.use_password);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   128
	NetworkSend_uint8 (packet, _network_game_info.clients_max);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   129
	NetworkSend_uint8 (packet, _network_game_info.clients_on);
2944
7c392e7b51c6 (svn r3500) - Workaround the inaccurate count of spectators/companies that can happen in certain border-cases. For now just dynamically get this value when requested so it is always right. To do properly all player/client creation/destruction needs a hook for networking.
Darkvater
parents: 2881
diff changeset
   130
	NetworkSend_uint8 (packet, NetworkSpectatorCount());
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   131
	NetworkSend_string(packet, _network_game_info.map_name);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   132
	NetworkSend_uint16(packet, _network_game_info.map_width);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   133
	NetworkSend_uint16(packet, _network_game_info.map_height);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   134
	NetworkSend_uint8 (packet, _network_game_info.map_set);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   135
	NetworkSend_uint8 (packet, _network_game_info.dedicated);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   136
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   137
	// Let the client know that we are here
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   138
	NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   139
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   140
	free(packet);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   141
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   142
	DEBUG(net, 2, "[udp] queried from '%s'", inet_ntoa(client_addr->sin_addr));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   143
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   144
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   145
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   146
{
2881
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2879
diff changeset
   147
	extern const char _openttd_revision[];
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   148
	NetworkGameList *item;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   149
	byte game_info_version;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   150
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   151
	// Just a fail-safe.. should never happen
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   152
	if (_network_udp_server)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   153
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   154
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   155
	game_info_version = NetworkRecv_uint8(&_udp_cs, p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   156
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   157
	if (_udp_cs.has_quit) return;
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   158
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   159
	DEBUG(net, 4, "[udp] server response from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   160
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   161
	// Find next item
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   162
	item = NetworkGameListAddItem(inet_addr(inet_ntoa(client_addr->sin_addr)), ntohs(client_addr->sin_port));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   163
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   164
	item->info.compatible = true;
2881
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2879
diff changeset
   165
	/* Please observer the order. In the order in which packets are sent
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   166
	 * they are to be received */
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   167
	switch (game_info_version) {
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   168
		case 4: {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   169
			GRFConfig *c, **dst = &item->info.grfconfig;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   170
			const GRFConfig *f;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   171
			uint i;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   172
			uint num_grfs = NetworkRecv_uint8(&_udp_cs, p);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   173
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   174
			for (i = 0; i < num_grfs; i++) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   175
				c = calloc(1, sizeof(*c));
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   176
				NetworkRecv_GRFIdentifier(p, c);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   177
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   178
				/* Find the matching GRF file */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   179
				f = FindGRFConfig(c->grfid, c->md5sum);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   180
				if (f == NULL) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   181
					/* Don't know the GRF, so mark game incompatible and the (possibly)
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   182
					 * already resolved name for this GRF (another server has sent the
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   183
					 * name of the GRF already */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   184
					item->info.compatible = false;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   185
					c->name     = FindUnknownGRFName(c->grfid, c->md5sum, true);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   186
					SETBIT(c->flags, GCF_NOT_FOUND);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   187
				} else {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   188
					c->filename = f->filename;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   189
					c->name     = f->name;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   190
					c->info     = f->info;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   191
				}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   192
				SETBIT(c->flags, GCF_COPY);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   193
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   194
				/* Append GRFConfig to the list */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   195
				*dst = c;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   196
				dst = &c->next;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   197
			}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   198
		} /* Fallthrough */
4326
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   199
		case 3:
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   200
			item->info.game_date     = NetworkRecv_uint32(&_udp_cs, p);
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   201
			item->info.start_date    = NetworkRecv_uint32(&_udp_cs, p);
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   202
			/* Fallthrough */
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   203
		case 2:
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   204
			item->info.companies_max = NetworkRecv_uint8(&_udp_cs, p);
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   205
			item->info.companies_on = NetworkRecv_uint8(&_udp_cs, p);
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   206
			item->info.spectators_max = NetworkRecv_uint8(&_udp_cs, p);
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   207
			/* Fallthrough */
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   208
		case 1:
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   209
			NetworkRecv_string(&_udp_cs, p, item->info.server_name, sizeof(item->info.server_name));
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   210
			NetworkRecv_string(&_udp_cs, p, item->info.server_revision, sizeof(item->info.server_revision));
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   211
			item->info.server_lang   = NetworkRecv_uint8(&_udp_cs, p);
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   212
			item->info.use_password  = NetworkRecv_uint8(&_udp_cs, p);
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   213
			item->info.clients_max   = NetworkRecv_uint8(&_udp_cs, p);
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   214
			item->info.clients_on    = NetworkRecv_uint8(&_udp_cs, p);
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   215
			item->info.spectators_on = NetworkRecv_uint8(&_udp_cs, p);
4326
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   216
			if (game_info_version < 3) { // 16 bits dates got scrapped and are read earlier
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   217
				item->info.game_date     = NetworkRecv_uint16(&_udp_cs, p) + DAYS_TILL_ORIGINAL_BASE_YEAR;
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   218
				item->info.start_date    = NetworkRecv_uint16(&_udp_cs, p) + DAYS_TILL_ORIGINAL_BASE_YEAR;
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   219
			}
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   220
			NetworkRecv_string(&_udp_cs, p, item->info.map_name, sizeof(item->info.map_name));
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   221
			item->info.map_width     = NetworkRecv_uint16(&_udp_cs, p);
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   222
			item->info.map_height    = NetworkRecv_uint16(&_udp_cs, p);
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   223
			item->info.map_set       = NetworkRecv_uint8(&_udp_cs, p);
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   224
			item->info.dedicated     = NetworkRecv_uint8(&_udp_cs, p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   225
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   226
			if (item->info.server_lang >= NETWORK_NUM_LANGUAGES) item->info.server_lang = 0;
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   227
			if (item->info.map_set >= NUM_LANDSCAPE ) item->info.map_set = 0;
2775
d3ed38a97250 (svn r3322) - Fix: Network window crash when it receives invalid information for example from the integrated nightly, so validate the network-input when it is received
Darkvater
parents: 2524
diff changeset
   228
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   229
			if (item->info.hostname[0] == '\0')
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   230
				snprintf(item->info.hostname, sizeof(item->info.hostname), "%s", inet_ntoa(client_addr->sin_addr));
2881
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2879
diff changeset
   231
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2879
diff changeset
   232
			/* Check if we are allowed on this server based on the revision-match */
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   233
			item->info.version_compatible =
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   234
				strcmp(item->info.server_revision, _openttd_revision) == 0 ||
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   235
				strcmp(item->info.server_revision, NOREV_STRING) == 0;
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   236
			item->info.compatible &= item->info.version_compatible; // Already contains match for GRFs
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   237
			break;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   238
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   239
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   240
	{
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   241
		/* Checks whether there needs to be a request for names of GRFs and makes
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   242
		 * the request if necessary. GRFs that need to be requested are the GRFs
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   243
		 * that do not exist on the clients system and we do not have the name
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   244
		 * resolved of, i.e. the name is still UNKNOWN_GRF_NAME_PLACEHOLDER.
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   245
		 * The in_request array and in_request_count are used so there is no need
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   246
		 * to do a second loop over the GRF list, which can be relatively expensive
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   247
		 * due to the string comparisons. */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   248
		const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT];
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   249
		const GRFConfig *c;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   250
		uint in_request_count = 0;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   251
		struct sockaddr_in out_addr;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   252
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   253
		for (c = item->info.grfconfig; c != NULL; c = c->next) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   254
			if (!HASBIT(c->flags, GCF_NOT_FOUND) || strcmp(c->name, UNKNOWN_GRF_NAME_PLACEHOLDER) != 0) continue;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   255
			in_request[in_request_count] = c;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   256
			in_request_count++;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   257
		}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   258
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   259
		if (in_request_count > 0) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   260
			/* There are 'unknown' GRFs, now send a request for them */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   261
			uint i;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   262
			Packet *packet = NetworkSend_Init(PACKET_UDP_CLIENT_GET_NEWGRFS);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   263
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   264
			NetworkSend_uint8 (packet, in_request_count);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   265
			for (i = 0; i < in_request_count; i++) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   266
				NetworkSend_GRFIdentifier(packet, in_request[i]);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   267
			}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   268
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   269
			out_addr.sin_family      = AF_INET;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   270
			out_addr.sin_port        = htons(item->port);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   271
			out_addr.sin_addr.s_addr = item->ip;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   272
			NetworkSendUDP_Packet(_udp_client_socket, packet, &out_addr);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   273
			free(packet);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   274
		}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   275
	}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   276
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   277
	item->online = true;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   278
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   279
	UpdateNetworkGameWindow(false);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   280
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   281
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   282
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO)
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   283
{
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 668
diff changeset
   284
	NetworkClientState *cs;
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   285
	NetworkClientInfo *ci;
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   286
	Packet *packet;
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   287
	Player *player;
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   288
	byte current = 0;
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   289
	int i;
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   290
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   291
	// Just a fail-safe.. should never happen
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   292
	if (!_network_udp_server) return;
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   293
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   294
	packet = NetworkSend_Init(PACKET_UDP_SERVER_DETAIL_INFO);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   295
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   296
	/* Send the amount of active companies */
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   297
	NetworkSend_uint8 (packet, NETWORK_COMPANY_INFO_VERSION);
2944
7c392e7b51c6 (svn r3500) - Workaround the inaccurate count of spectators/companies that can happen in certain border-cases. For now just dynamically get this value when requested so it is always right. To do properly all player/client creation/destruction needs a hook for networking.
Darkvater
parents: 2881
diff changeset
   298
	NetworkSend_uint8 (packet, ActivePlayerCount());
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   299
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   300
	/* Fetch the latest version of everything */
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   301
	NetworkPopulateCompanyInfo();
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   302
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   303
	/* Go through all the players */
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   304
	FOR_ALL_PLAYERS(player) {
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   305
		/* Skip non-active players */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   306
		if (!player->is_active) continue;
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   307
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   308
		current++;
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   309
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   310
		/* Send the information */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   311
		NetworkSend_uint8(packet, current);
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   312
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   313
		NetworkSend_string(packet, _network_player_info[player->index].company_name);
4326
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   314
		NetworkSend_uint32(packet, _network_player_info[player->index].inaugurated_year);
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   315
		NetworkSend_uint64(packet, _network_player_info[player->index].company_value);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   316
		NetworkSend_uint64(packet, _network_player_info[player->index].money);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   317
		NetworkSend_uint64(packet, _network_player_info[player->index].income);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   318
		NetworkSend_uint16(packet, _network_player_info[player->index].performance);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   319
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   320
		/* Send 1 if there is a passord for the company else send 0 */
1011
2587838541bf (svn r1510) -Add: Improved Network Lobby GUI: (bociusz)
truelight
parents: 985
diff changeset
   321
		if (_network_player_info[player->index].password[0] != '\0') {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   322
			NetworkSend_uint8(packet, 1);
1011
2587838541bf (svn r1510) -Add: Improved Network Lobby GUI: (bociusz)
truelight
parents: 985
diff changeset
   323
		} else {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   324
			NetworkSend_uint8(packet, 0);
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1011
diff changeset
   325
		}
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1011
diff changeset
   326
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   327
		for (i = 0; i < NETWORK_VEHICLE_TYPES; i++)
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   328
			NetworkSend_uint16(packet, _network_player_info[player->index].num_vehicle[i]);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   329
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   330
		for (i = 0; i < NETWORK_STATION_TYPES; i++)
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   331
			NetworkSend_uint16(packet, _network_player_info[player->index].num_station[i]);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   332
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   333
		/* Find the clients that are connected to this player */
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   334
		FOR_ALL_CLIENTS(cs) {
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   335
			ci = DEREF_CLIENT_INFO(cs);
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4826
diff changeset
   336
			if (ci->client_playas == player->index) {
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   337
				/* The uint8 == 1 indicates that a client is following */
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   338
				NetworkSend_uint8(packet, 1);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   339
				NetworkSend_string(packet, ci->client_name);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   340
				NetworkSend_string(packet, ci->unique_id);
4326
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   341
				NetworkSend_uint32(packet, ci->join_date);
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   342
			}
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   343
		}
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   344
		/* Also check for the server itself */
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   345
		ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4826
diff changeset
   346
		if (ci->client_playas == player->index) {
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   347
			/* The uint8 == 1 indicates that a client is following */
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   348
			NetworkSend_uint8(packet, 1);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   349
			NetworkSend_string(packet, ci->client_name);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   350
			NetworkSend_string(packet, ci->unique_id);
4326
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   351
			NetworkSend_uint32(packet, ci->join_date);
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   352
		}
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   353
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   354
		/* Indicates end of client list */
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   355
		NetworkSend_uint8(packet, 0);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   356
	}
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   357
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   358
	/* And check if we have any spectators */
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   359
	FOR_ALL_CLIENTS(cs) {
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   360
		ci = DEREF_CLIENT_INFO(cs);
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4826
diff changeset
   361
		if (!IsValidPlayer(ci->client_playas)) {
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   362
			/* The uint8 == 1 indicates that a client is following */
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   363
			NetworkSend_uint8(packet, 1);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   364
			NetworkSend_string(packet, ci->client_name);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   365
			NetworkSend_string(packet, ci->unique_id);
4326
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   366
			NetworkSend_uint32(packet, ci->join_date);
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   367
		}
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   368
	}
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   369
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   370
	/* Also check for the server itself */
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   371
	ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4826
diff changeset
   372
	if (!IsValidPlayer(ci->client_playas)) {
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   373
		/* The uint8 == 1 indicates that a client is following */
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   374
		NetworkSend_uint8(packet, 1);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   375
		NetworkSend_string(packet, ci->client_name);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   376
		NetworkSend_string(packet, ci->unique_id);
4326
c2ae4dbc1074 (svn r5999) -Feature: change the original date format to a 32 bits format based at the year 0.
rubidium
parents: 4261
diff changeset
   377
		NetworkSend_uint32(packet, ci->join_date);
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   378
	}
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   379
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   380
	/* Indicates end of client list */
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   381
	NetworkSend_uint8(packet, 0);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   382
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   383
	NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr);
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   384
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   385
	free(packet);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   386
}
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   387
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   388
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   389
{
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   390
	int i;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   391
	struct in_addr ip;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   392
	uint16 port;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   393
	uint8 ver;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   394
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   395
	/* packet begins with the protocol version (uint8)
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   396
	 * then an uint16 which indicates how many
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   397
	 * ip:port pairs are in this packet, after that
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   398
	 * an uint32 (ip) and an uint16 (port) for each pair
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   399
	 */
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   400
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   401
	ver = NetworkRecv_uint8(&_udp_cs, p);
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   402
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   403
	if (_udp_cs.has_quit) return;
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   404
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   405
	if (ver == 1) {
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   406
		for (i = NetworkRecv_uint16(&_udp_cs, p); i != 0 ; i--) {
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   407
			ip.s_addr = TO_LE32(NetworkRecv_uint32(&_udp_cs, p));
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   408
			port = NetworkRecv_uint16(&_udp_cs, p);
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   409
			NetworkUDPQueryServer(inet_ntoa(ip), port);
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   410
		}
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   411
	}
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   412
}
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   413
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   414
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   415
{
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   416
	_network_advertise_retries = 0;
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   417
	DEBUG(net, 2, "[udp] advertising on master server successfull");
765
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   418
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   419
	/* We are advertised, but we don't want to! */
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   420
	if (!_network_advertise) NetworkUDPRemoveAdvertise();
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   421
}
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   422
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   423
/**
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   424
 * A client has requested the names of some NewGRFs.
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   425
 *
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   426
 * Replying this can be tricky as we have a limit of SEND_MTU bytes
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   427
 * in the reply packet and we can send up to 100 bytes per NewGRF
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   428
 * (GRF ID, MD5sum and NETWORK_GRF_NAME_LENGTH bytes for the name).
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   429
 * As SEND_MTU is _much_ less than 100 * NETWORK_MAX_GRF_COUNT, it
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   430
 * could be that a packet overflows. To stop this we only reply
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   431
 * with the first N NewGRFs so that if the first N + 1 NewGRFs
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   432
 * would be sent, the packet overflows.
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   433
 * in_reply and in_reply_count are used to keep a list of GRFs to
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   434
 * send in the reply.
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   435
 */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   436
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS)
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   437
{
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   438
	uint8 num_grfs;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   439
	uint i;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   440
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   441
	const GRFConfig *in_reply[NETWORK_MAX_GRF_COUNT];
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   442
	Packet *packet;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   443
	uint8 in_reply_count = 0;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   444
	uint packet_len = 0;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   445
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   446
	/* Just a fail-safe.. should never happen */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   447
	if (_udp_cs.has_quit) return;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   448
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   449
	DEBUG(net, 6, "[udp] newgrf data request from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   450
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   451
	num_grfs = NetworkRecv_uint8 (&_udp_cs, p);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   452
	if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   453
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   454
	for (i = 0; i < num_grfs; i++) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   455
		GRFConfig c;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   456
		const GRFConfig *f;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   457
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   458
		NetworkRecv_GRFIdentifier(p, &c);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   459
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   460
		/* Find the matching GRF file */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   461
		f = FindGRFConfig(c.grfid, c.md5sum);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   462
		if (f == NULL) continue; // The GRF is unknown to this server
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   463
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   464
		/* If the reply might exceed the size of the packet, only reply
5341
df905c2cbaae (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   465
		 * the current list and do not send the other data.
df905c2cbaae (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   466
		 * The name could be an empty string, if so take the filename. */
df905c2cbaae (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   467
		packet_len += sizeof(c.grfid) + sizeof(c.md5sum) +
df905c2cbaae (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   468
				min(strlen((f->name != NULL && strlen(f->name) > 0) ? f->name : f->filename) + 1, NETWORK_GRF_NAME_LENGTH);
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   469
		if (packet_len > SEND_MTU - 4) { // 4 is 3 byte header + grf count in reply
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   470
			break;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   471
		}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   472
		in_reply[in_reply_count] = f;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   473
		in_reply_count++;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   474
	}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   475
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   476
	if (in_reply_count == 0) return;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   477
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   478
	packet = NetworkSend_Init(PACKET_UDP_SERVER_NEWGRFS);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   479
	NetworkSend_uint8 (packet, in_reply_count);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   480
	for (i = 0; i < in_reply_count; i++) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   481
		char name[NETWORK_GRF_NAME_LENGTH];
5341
df905c2cbaae (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   482
df905c2cbaae (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   483
		/* The name could be an empty string, if so take the filename */
df905c2cbaae (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   484
		ttd_strlcpy(name, (in_reply[i]->name != NULL && strlen(in_reply[i]->name) > 0) ?
df905c2cbaae (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   485
				in_reply[i]->name : in_reply[i]->filename, sizeof(name));
df905c2cbaae (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   486
	 	NetworkSend_GRFIdentifier(packet, in_reply[i]);
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   487
		NetworkSend_string(packet, name);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   488
	}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   489
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   490
	NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   491
	free(packet);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   492
}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   493
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   494
/** The return of the client's request of the names of some NewGRFs */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   495
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS)
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   496
{
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   497
	uint8 num_grfs;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   498
	uint i;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   499
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   500
	/* Just a fail-safe.. should never happen */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   501
	if (_udp_cs.has_quit) return;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   502
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   503
	DEBUG(net, 6, "[udp] newgrf data reply from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   504
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   505
	num_grfs = NetworkRecv_uint8 (&_udp_cs, p);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   506
	if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   507
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   508
	for (i = 0; i < num_grfs; i++) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   509
		char *unknown_name;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   510
		char name[NETWORK_GRF_NAME_LENGTH];
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   511
		GRFConfig c;
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   512
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   513
		NetworkRecv_GRFIdentifier(p, &c);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   514
		NetworkRecv_string(&_udp_cs, p, name, sizeof(name));
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   515
5341
df905c2cbaae (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   516
		/* An empty name is not possible under normal circumstances
df905c2cbaae (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   517
		 * and causes problems when showing the NewGRF list. */
df905c2cbaae (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   518
		if (strlen(name) == 0) continue;
df905c2cbaae (svn r7507) -Fix (7505): the name of a GRF could be "", which causes a segmentation fault. So take the filename, which cannot be "", when the of the GRF name is "". Also check for "" length when receiving GRF names.
rubidium
parents: 5339
diff changeset
   519
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   520
		/* Finds the fake GRFConfig for the just read GRF ID and MD5sum tuple.
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   521
		 * If it exists and not resolved yet, then name of the fake GRF is
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   522
		 * overwritten with the name from the reply. */
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   523
		unknown_name = FindUnknownGRFName(c.grfid, c.md5sum, false);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   524
		if (unknown_name != NULL && strcmp(unknown_name, UNKNOWN_GRF_NAME_PLACEHOLDER) == 0) {
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   525
			ttd_strlcpy(unknown_name, name, NETWORK_GRF_NAME_LENGTH);
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   526
		}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   527
	}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   528
}
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   529
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   530
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   531
// The layout for the receive-functions by UDP
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   532
typedef void NetworkUDPPacket(Packet *p, struct sockaddr_in *client_addr);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   533
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   534
static NetworkUDPPacket* const _network_udp_packet[] = {
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   535
	RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER),
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   536
	RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE),
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   537
	RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO),
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   538
	NULL,
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   539
	NULL,
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   540
	RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER),
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   541
	NULL,
765
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   542
	RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST),
5339
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   543
	NULL,
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   544
	RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS),
7573f179efe8 (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   545
	RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS),
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   546
};
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   547
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   548
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   549
// If this fails, check the array above with network_data.h
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   550
assert_compile(lengthof(_network_udp_packet) == PACKET_UDP_END);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   551
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   552
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2789
diff changeset
   553
static void NetworkHandleUDPPacket(Packet* p, struct sockaddr_in* client_addr)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   554
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   555
	byte type;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   556
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   557
	/* Fake a client, so we can see when there is an illegal packet */
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   558
	_udp_cs.socket = INVALID_SOCKET;
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   559
	_udp_cs.has_quit = false;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   560
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   561
	type = NetworkRecv_uint8(&_udp_cs, p);
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   562
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   563
	if (type < PACKET_UDP_END && _network_udp_packet[type] != NULL && !_udp_cs.has_quit) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   564
		_network_udp_packet[type](p, client_addr);
3547
a4584d709460 (svn r4413) -Fix: fixed a bug which pushed the client back to the main menu when a
truelight
parents: 3456
diff changeset
   565
	} else {
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   566
		if (!_udp_cs.has_quit) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   567
			DEBUG(net, 0, "[udp] received invalid packet type %d", type);
3547
a4584d709460 (svn r4413) -Fix: fixed a bug which pushed the client back to the main menu when a
truelight
parents: 3456
diff changeset
   568
		} else {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   569
			DEBUG(net, 0, "[udp] received illegal packet");
3547
a4584d709460 (svn r4413) -Fix: fixed a bug which pushed the client back to the main menu when a
truelight
parents: 3456
diff changeset
   570
		}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   571
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   572
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   573
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   574
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   575
// Send a packet over UDP
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2789
diff changeset
   576
static void NetworkSendUDP_Packet(SOCKET udp, Packet* p, struct sockaddr_in* recv)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   577
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   578
	int res;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   579
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   580
	// Put the length in the buffer
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   581
	p->buffer[0] = p->size & 0xFF;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   582
	p->buffer[1] = p->size >> 8;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   583
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   584
	// Send the buffer
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   585
	res = sendto(udp, p->buffer, p->size, 0, (struct sockaddr *)recv, sizeof(*recv));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   586
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   587
	// Check for any errors, but ignore it otherwise
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   588
	if (res == -1) DEBUG(net, 1, "[udp] sendto failed with: %i", GET_LAST_ERROR());
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   589
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   590
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   591
// Start UDP listener
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   592
bool NetworkUDPListen(SOCKET *udp, uint32 host, uint16 port, bool broadcast)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   593
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   594
	struct sockaddr_in sin;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   595
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   596
	// Make sure socket is closed
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   597
	closesocket(*udp);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   598
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   599
	*udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   600
	if (*udp == INVALID_SOCKET) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   601
		DEBUG(net, 0, "[udp] failed to start UDP listener");
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   602
		return false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   603
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   604
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   605
	// set nonblocking mode for socket
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   606
	{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   607
		unsigned long blocking = 1;
2497
1c8460570c0d (svn r3023) -Fix [BeOS] fixed compilation on BeOS R5 (MYOB)
bjarni
parents: 2186
diff changeset
   608
#ifndef BEOS_NET_SERVER
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   609
		ioctlsocket(*udp, FIONBIO, &blocking);
2497
1c8460570c0d (svn r3023) -Fix [BeOS] fixed compilation on BeOS R5 (MYOB)
bjarni
parents: 2186
diff changeset
   610
#else
2524
5919ff778fde (svn r3053) -Fix: [ 1326604 ] Fixed typo and hang for BeOS Networking (MYOB)
truelight
parents: 2497
diff changeset
   611
		setsockopt(*udp, SOL_SOCKET, SO_NONBLOCK, &blocking, NULL);
2497
1c8460570c0d (svn r3023) -Fix [BeOS] fixed compilation on BeOS R5 (MYOB)
bjarni
parents: 2186
diff changeset
   612
#endif
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   613
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   614
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   615
	sin.sin_family = AF_INET;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   616
	// Listen on all IPs
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   617
	sin.sin_addr.s_addr = host;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   618
	sin.sin_port = htons(port);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   619
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   620
	if (bind(*udp, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   621
		DEBUG(net, 0, "[udp] bind failed on %s:%i", inet_ntoa(*(struct in_addr *)&host), port);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   622
		return false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   623
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   624
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   625
	if (broadcast) {
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   626
		/* Enable broadcast */
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   627
		unsigned long val = 1;
2497
1c8460570c0d (svn r3023) -Fix [BeOS] fixed compilation on BeOS R5 (MYOB)
bjarni
parents: 2186
diff changeset
   628
#ifndef BEOS_NET_SERVER // will work around this, some day; maybe.
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   629
		setsockopt(*udp, SOL_SOCKET, SO_BROADCAST, (char *) &val , sizeof(val));
2497
1c8460570c0d (svn r3023) -Fix [BeOS] fixed compilation on BeOS R5 (MYOB)
bjarni
parents: 2186
diff changeset
   630
#endif
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   631
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   632
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   633
	DEBUG(net, 1, "[udp] listening on port %s:%d", inet_ntoa(*(struct in_addr *)&host), port);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   634
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   635
	return true;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   636
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   637
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   638
// Close UDP connection
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   639
void NetworkUDPClose(void)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   640
{
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   641
	DEBUG(net, 1, "[udp] closed listeners");
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   642
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   643
	if (_network_udp_server) {
985
ef5ba72aca89 (svn r1483) -Fix: don't close a socket if it is already closed (windows doesn't like
truelight
parents: 903
diff changeset
   644
		if (_udp_server_socket != INVALID_SOCKET) {
ef5ba72aca89 (svn r1483) -Fix: don't close a socket if it is already closed (windows doesn't like
truelight
parents: 903
diff changeset
   645
			closesocket(_udp_server_socket);
ef5ba72aca89 (svn r1483) -Fix: don't close a socket if it is already closed (windows doesn't like
truelight
parents: 903
diff changeset
   646
			_udp_server_socket = INVALID_SOCKET;
ef5ba72aca89 (svn r1483) -Fix: don't close a socket if it is already closed (windows doesn't like
truelight
parents: 903
diff changeset
   647
		}
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   648
985
ef5ba72aca89 (svn r1483) -Fix: don't close a socket if it is already closed (windows doesn't like
truelight
parents: 903
diff changeset
   649
		if (_udp_master_socket != INVALID_SOCKET) {
ef5ba72aca89 (svn r1483) -Fix: don't close a socket if it is already closed (windows doesn't like
truelight
parents: 903
diff changeset
   650
			closesocket(_udp_master_socket);
ef5ba72aca89 (svn r1483) -Fix: don't close a socket if it is already closed (windows doesn't like
truelight
parents: 903
diff changeset
   651
			_udp_master_socket = INVALID_SOCKET;
ef5ba72aca89 (svn r1483) -Fix: don't close a socket if it is already closed (windows doesn't like
truelight
parents: 903
diff changeset
   652
		}
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   653
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   654
		_network_udp_server = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   655
		_network_udp_broadcast = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   656
	} else {
985
ef5ba72aca89 (svn r1483) -Fix: don't close a socket if it is already closed (windows doesn't like
truelight
parents: 903
diff changeset
   657
		if (_udp_client_socket != INVALID_SOCKET) {
ef5ba72aca89 (svn r1483) -Fix: don't close a socket if it is already closed (windows doesn't like
truelight
parents: 903
diff changeset
   658
			closesocket(_udp_client_socket);
ef5ba72aca89 (svn r1483) -Fix: don't close a socket if it is already closed (windows doesn't like
truelight
parents: 903
diff changeset
   659
			_udp_client_socket = INVALID_SOCKET;
ef5ba72aca89 (svn r1483) -Fix: don't close a socket if it is already closed (windows doesn't like
truelight
parents: 903
diff changeset
   660
		}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   661
		_network_udp_broadcast = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   662
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   663
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   664
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   665
// Receive something on UDP level
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   666
void NetworkUDPReceive(SOCKET udp)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   667
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   668
	struct sockaddr_in client_addr;
1343
481847e32573 (svn r1847) Adjustment for MorphOS to unbreak the build there and removal of some now obsolete preprocessor magic
tron
parents: 1329
diff changeset
   669
	socklen_t client_len;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   670
	int nbytes;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   671
	static Packet *p = NULL;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   672
	int packet_len;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   673
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   674
	// If p is NULL, malloc him.. this prevents unneeded mallocs
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   675
	if (p == NULL) p = malloc(sizeof(*p));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   676
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   677
	packet_len = sizeof(p->buffer);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   678
	client_len = sizeof(client_addr);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   679
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   680
	// Try to receive anything
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   681
	nbytes = recvfrom(udp, p->buffer, packet_len, 0, (struct sockaddr *)&client_addr, &client_len);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   682
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   683
	// We got some bytes.. just asume we receive the whole packet
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   684
	if (nbytes > 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   685
		// Get the size of the buffer
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   686
		p->size = (uint16)p->buffer[0];
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   687
		p->size += (uint16)p->buffer[1] << 8;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   688
		// Put the position on the right place
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   689
		p->pos = 2;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   690
		p->next = NULL;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   691
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   692
		// Handle the packet
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   693
		NetworkHandleUDPPacket(p, &client_addr);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   694
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   695
		// Free the packet
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   696
		free(p);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   697
		p = NULL;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   698
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   699
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   700
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   701
// Broadcast to all ips
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2789
diff changeset
   702
static void NetworkUDPBroadCast(SOCKET udp)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   703
{
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   704
	Packet* p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   705
	uint i;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   706
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   707
	for (i = 0; _broadcast_list[i] != 0; i++) {
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   708
		struct sockaddr_in out_addr;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   709
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   710
		out_addr.sin_family = AF_INET;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   711
		out_addr.sin_port = htons(_network_server_port);
4034
091963647651 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4026
diff changeset
   712
		out_addr.sin_addr.s_addr = _broadcast_list[i];
091963647651 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4026
diff changeset
   713
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   714
		DEBUG(net, 4, "[udp] broadcasting to %s", inet_ntoa(out_addr.sin_addr));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   715
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   716
		NetworkSendUDP_Packet(udp, p, &out_addr);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   717
	}
4035
f468417d21b9 (svn r5294) -Fix: Plug a memory leak
tron
parents: 4034
diff changeset
   718
f468417d21b9 (svn r5294) -Fix: Plug a memory leak
tron
parents: 4034
diff changeset
   719
	free(p);
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   720
}
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   721
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   722
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   723
// Request the the server-list from the master server
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   724
void NetworkUDPQueryMasterServer(void)
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   725
{
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   726
	struct sockaddr_in out_addr;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   727
	Packet *p;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   728
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   729
	if (_udp_client_socket == INVALID_SOCKET)
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   730
		if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true))
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   731
			return;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   732
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   733
	p = NetworkSend_Init(PACKET_UDP_CLIENT_GET_LIST);
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   734
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   735
	out_addr.sin_family = AF_INET;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   736
	out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT);
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   737
	out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST);
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   738
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   739
	// packet only contains protocol version
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   740
	NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION);
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   741
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   742
	NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr);
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   743
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   744
	DEBUG(net, 2, "[udp] master server queried at %s:%d", inet_ntoa(out_addr.sin_addr),ntohs(out_addr.sin_port));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   745
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   746
	free(p);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   747
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   748
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   749
// Find all servers
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   750
void NetworkUDPSearchGame(void)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   751
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   752
	// We are still searching..
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   753
	if (_network_udp_broadcast > 0) return;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   754
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   755
	// No UDP-socket yet..
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   756
	if (_udp_client_socket == INVALID_SOCKET)
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   757
		if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true))
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   758
			return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   759
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   760
	DEBUG(net, 0, "[udp] searching server");
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   761
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   762
	NetworkUDPBroadCast(_udp_client_socket);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   763
	_network_udp_broadcast = 300; // Stay searching for 300 ticks
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   764
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   765
1329
a8a0d60b0a8e (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   766
NetworkGameList *NetworkUDPQueryServer(const char* host, unsigned short port)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   767
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   768
	struct sockaddr_in out_addr;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   769
	Packet *p;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   770
	NetworkGameList *item;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   771
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   772
	// No UDP-socket yet..
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   773
	if (_udp_client_socket == INVALID_SOCKET)
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   774
		if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true))
738
0b2fb79e64fc (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 716
diff changeset
   775
			return NULL;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   776
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   777
	out_addr.sin_family = AF_INET;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   778
	out_addr.sin_port = htons(port);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   779
	out_addr.sin_addr.s_addr = NetworkResolveHost(host);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   780
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   781
	// Clear item in gamelist
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   782
	item = NetworkGameListAddItem(inet_addr(inet_ntoa(out_addr.sin_addr)), ntohs(out_addr.sin_port));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   783
	memset(&item->info, 0, sizeof(item->info));
3550
5887de7bd4ee (svn r4416) Remove a pointless buffer copy and use strlcpy() instead of snprintf("%s")
tron
parents: 3547
diff changeset
   784
	ttd_strlcpy(item->info.server_name, host, lengthof(item->info.server_name));
5887de7bd4ee (svn r4416) Remove a pointless buffer copy and use strlcpy() instead of snprintf("%s")
tron
parents: 3547
diff changeset
   785
	ttd_strlcpy(item->info.hostname, host, lengthof(item->info.hostname));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   786
	item->online = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   787
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   788
	// Init the packet
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   789
	p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   790
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   791
	NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   792
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   793
	free(p);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   794
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   795
	UpdateNetworkGameWindow(false);
738
0b2fb79e64fc (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 716
diff changeset
   796
	return item;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   797
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   798
765
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   799
/* Remove our advertise from the master-server */
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   800
void NetworkUDPRemoveAdvertise(void)
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   801
{
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   802
	struct sockaddr_in out_addr;
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   803
	Packet *p;
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   804
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   805
	/* Check if we are advertising */
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   806
	if (!_networking || !_network_server || !_network_udp_server) return;
765
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   807
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   808
	/* check for socket */
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   809
	if (_udp_master_socket == INVALID_SOCKET)
805
f41ff15b4881 (svn r1276) -Fix: [Network] Bug in bind system. Advertising failed on systems with
truelight
parents: 765
diff changeset
   810
		if (!NetworkUDPListen(&_udp_master_socket, _network_server_bind_ip, 0, false))
765
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   811
			return;
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   812
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   813
	DEBUG(net, 1, "[udp] removing advertise from master server");
765
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   814
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   815
	/* Find somewhere to send */
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   816
	out_addr.sin_family = AF_INET;
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   817
	out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT);
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   818
	out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST);
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   819
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   820
	/* Send the packet */
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   821
	p = NetworkSend_Init(PACKET_UDP_SERVER_UNREGISTER);
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   822
	/* Packet is: Version, server_port */
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   823
	NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION);
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   824
	NetworkSend_uint16(p, _network_server_port);
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   825
	NetworkSendUDP_Packet(_udp_master_socket, p, &out_addr);
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   826
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   827
	free(p);
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   828
}
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   829
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   830
/* Register us to the master server
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   831
     This function checks if it needs to send an advertise */
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 668
diff changeset
   832
void NetworkUDPAdvertise(void)
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   833
{
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   834
	struct sockaddr_in out_addr;
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   835
	Packet *p;
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   836
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   837
	/* Check if we should send an advertise */
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   838
	if (!_networking || !_network_server || !_network_udp_server || !_network_advertise)
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   839
		return;
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   840
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   841
	/* check for socket */
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   842
	if (_udp_master_socket == INVALID_SOCKET)
805
f41ff15b4881 (svn r1276) -Fix: [Network] Bug in bind system. Advertising failed on systems with
truelight
parents: 765
diff changeset
   843
		if (!NetworkUDPListen(&_udp_master_socket, _network_server_bind_ip, 0, false))
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   844
			return;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   845
2861
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   846
	if (_network_need_advertise) {
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   847
		_network_need_advertise = false;
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   848
		_network_advertise_retries = ADVERTISE_RETRY_TIMES;
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   849
	} else {
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   850
		/* Only send once every ADVERTISE_NORMAL_INTERVAL ticks */
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   851
		if (_network_advertise_retries == 0) {
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   852
			if ((_network_last_advertise_frame + ADVERTISE_NORMAL_INTERVAL) > _frame_counter)
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   853
				return;
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   854
			_network_advertise_retries = ADVERTISE_RETRY_TIMES;
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   855
		}
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   856
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   857
		if ((_network_last_advertise_frame + ADVERTISE_RETRY_INTERVAL) > _frame_counter)
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   858
			return;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   859
	}
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   860
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   861
	_network_advertise_retries--;
2861
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   862
	_network_last_advertise_frame = _frame_counter;
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   863
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   864
	/* Find somewhere to send */
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   865
	out_addr.sin_family = AF_INET;
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   866
	out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT);
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   867
	out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST);
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   868
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   869
	DEBUG(net, 1, "[udp] advertising to master server");
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   870
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   871
	/* Send the packet */
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   872
	p = NetworkSend_Init(PACKET_UDP_SERVER_REGISTER);
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   873
	/* Packet is: WELCOME_MESSAGE, Version, server_port */
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   874
	NetworkSend_string(p, NETWORK_MASTER_SERVER_WELCOME_MESSAGE);
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   875
	NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION);
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   876
	NetworkSend_uint16(p, _network_server_port);
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   877
	NetworkSendUDP_Packet(_udp_master_socket, p, &out_addr);
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   878
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   879
	free(p);
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   880
}
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   881
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   882
void NetworkUDPInitialize(void)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   883
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   884
	_udp_client_socket = INVALID_SOCKET;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   885
	_udp_server_socket = INVALID_SOCKET;
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   886
	_udp_master_socket = INVALID_SOCKET;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   887
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   888
	_network_udp_server = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   889
	_network_udp_broadcast = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   890
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   891
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   892
#endif /* ENABLE_NETWORK */