network_udp.c
author rubidium
Fri, 10 Aug 2007 17:53:12 +0000
branch0.5
changeset 5530 15f701e93291
parent 5438 024eb27ab119
permissions -rw-r--r--
(svn r10841) [0.5] -Backport from trunk (r10835, r10593, r10500, r10497, r10410, r10357, r10199):
- Fix: [Windows] Do not try to minimise or restore the window when closing OpenTTD [FS#998] (r10835)
- Fix: One could not remove locks that were build in a (very) old version of OpenTTD [FS#1038] (r10593)
- Fix: One cannot navigate using arrow keys in the game name text box [FS#1038] (r10500)
- Fix: Ship's maximum speed wrongly shown [FS#1013] (r10497)
- Fix: [OSX] Of the resolution is changed to something that is too high for the monitor, then it is reduced to fit the monitor size, solving several crashes and graphical glitches [FS#458] (r10410)
- Fix: NPF was leaking memory each time it got initialized, except for the first time (r10357)
- Fix: [YAPF] 'target_seen' flag that is set prematurely in some cases (1 tile long cached segment followed by target station) which caused asserts to trigger [FS#884] (r10199)
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
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
    48
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    49
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    50
	Packet *packet;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    51
	// Just a fail-safe.. should never happen
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    52
	if (!_network_udp_server)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    53
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    54
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    55
	packet = NetworkSend_Init(PACKET_UDP_SERVER_RESPONSE);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    56
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    57
	// Update some game_info
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    58
	_network_game_info.game_date = _date;
850
e18648f7d5a4 (svn r1331) Update map size before sending game info
tron
parents: 805
diff changeset
    59
	_network_game_info.map_width = MapSizeX();
e18648f7d5a4 (svn r1331) Update map size before sending game info
tron
parents: 805
diff changeset
    60
	_network_game_info.map_height = MapSizeY();
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    61
	_network_game_info.map_set = _opt.landscape;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    62
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    63
	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
    64
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
    65
	/* 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
    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
		/* 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
    68
		 * 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
    69
		 * 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
    70
		 * 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
    71
		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
    72
		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
    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
		/* 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
    75
		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
    76
			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
    77
		}
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
    78
		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
    79
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
    80
		/* 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
    81
		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
    82
			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
    83
		}
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
    84
	}
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
    85
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
    86
	/* 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
    87
	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
    88
	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
    89
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
    90
	/* 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
    91
	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
    92
	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
    93
	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
    94
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
    95
	/* NETWORK_GAME_INFO_VERSION = 1 */
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    96
	NetworkSend_string(packet, _network_game_info.server_name);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    97
	NetworkSend_string(packet, _network_game_info.server_revision);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    98
	NetworkSend_uint8 (packet, _network_game_info.server_lang);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    99
	NetworkSend_uint8 (packet, _network_game_info.use_password);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   100
	NetworkSend_uint8 (packet, _network_game_info.clients_max);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   101
	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
   102
	NetworkSend_uint8 (packet, NetworkSpectatorCount());
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   103
	NetworkSend_string(packet, _network_game_info.map_name);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   104
	NetworkSend_uint16(packet, _network_game_info.map_width);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   105
	NetworkSend_uint16(packet, _network_game_info.map_height);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   106
	NetworkSend_uint8 (packet, _network_game_info.map_set);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   107
	NetworkSend_uint8 (packet, _network_game_info.dedicated);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   108
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   109
	// 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
   110
	NetworkSendUDP_Packet(_udp_server_socket, packet, client_addr);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   111
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   112
	free(packet);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   113
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   114
	DEBUG(net, 2)("[NET][UDP] Queried from %s", inet_ntoa(client_addr->sin_addr));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   115
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   116
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   117
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   118
{
2881
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2879
diff changeset
   119
	extern const char _openttd_revision[];
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   120
	NetworkGameList *item;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   121
	byte game_info_version;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   122
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   123
	// Just a fail-safe.. should never happen
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   124
	if (_network_udp_server)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   125
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   126
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   127
	game_info_version = NetworkRecv_uint8(&_udp_cs, p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   128
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   129
	if (_udp_cs.has_quit) return;
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   130
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   131
	DEBUG(net, 6)("[NET][UDP] Server response from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_port));
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   132
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   133
	// Find next item
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   134
	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
   135
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
   136
	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
   137
	/* 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
   138
	 * 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
   139
	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
   140
		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
   141
			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
   142
			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
   143
			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
   144
			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
   145
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
   146
			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
   147
				c = calloc(1, sizeof(*c));
5424
f09762e44457 (svn r8489) [0.5] -Backport from trunk (8459, 8461): check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5341
diff changeset
   148
				NetworkRecv_GRFIdentifier(&_udp_cs, p, c);
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
   149
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
   150
				/* 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
   151
				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
   152
				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
   153
					/* 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
   154
					 * 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
   155
					 * 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
   156
					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
   157
					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
   158
					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
   159
				} 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
   160
					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
   161
					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
   162
					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
   163
				}
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
				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
   165
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
   166
				/* 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
   167
				*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
   168
				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
   169
			}
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
		} /* 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
   171
		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
   172
			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
   173
			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
   174
			/* 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
   175
		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
   176
			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
   177
			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
   178
			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
   179
			/* 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
   180
		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
   181
			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
   182
			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
   183
			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
   184
			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
   185
			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
   186
			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
   187
			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
   188
			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
   189
				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
   190
				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
   191
			}
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
   192
			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
   193
			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
   194
			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
   195
			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
   196
			item->info.dedicated     = NetworkRecv_uint8(&_udp_cs, p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   197
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
   198
			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
   199
			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
   200
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
   201
			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
   202
				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
   203
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2879
diff changeset
   204
			/* 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
   205
			item->info.version_compatible =
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   206
				strcmp(item->info.server_revision, _openttd_revision) == 0 ||
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   207
				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
   208
			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
   209
			break;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   210
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   211
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
   212
	{
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
   213
		/* 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
   214
		 * 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
   215
		 * 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
   216
		 * 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
   217
		 * 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
   218
		 * 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
   219
		 * 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
   220
		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
   221
		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
   222
		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
   223
		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
   224
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
   225
		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
   226
			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
   227
			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
   228
			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
   229
		}
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
   230
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
   231
		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
   232
			/* 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
   233
			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
   234
			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
   235
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
			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
   237
			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
   238
				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
   239
			}
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
			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
   242
			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
   243
			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
   244
			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
   245
			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
   246
		}
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
	}
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
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   249
	item->online = true;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   250
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   251
	UpdateNetworkGameWindow(false);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   252
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   253
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   254
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
   255
{
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 668
diff changeset
   256
	NetworkClientState *cs;
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   257
	NetworkClientInfo *ci;
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   258
	Packet *packet;
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   259
	Player *player;
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   260
	byte current = 0;
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   261
	int i;
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   262
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   263
	// Just a fail-safe.. should never happen
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   264
	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
   265
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   266
	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
   267
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   268
	/* 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
   269
	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
   270
	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
   271
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   272
	/* 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
   273
	NetworkPopulateCompanyInfo();
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   274
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   275
	/* 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
   276
	FOR_ALL_PLAYERS(player) {
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   277
		/* Skip non-active players */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   278
		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
   279
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   280
		current++;
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   281
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   282
		/* Send the information */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   283
		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
   284
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   285
		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
   286
		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
   287
		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
   288
		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
   289
		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
   290
		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
   291
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   292
		/* 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
   293
		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
   294
			NetworkSend_uint8(packet, 1);
1011
2587838541bf (svn r1510) -Add: Improved Network Lobby GUI: (bociusz)
truelight
parents: 985
diff changeset
   295
		} else {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   296
			NetworkSend_uint8(packet, 0);
1019
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1011
diff changeset
   297
		}
6363b8a4273e (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1011
diff changeset
   298
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   299
		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
   300
			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
   301
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   302
		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
   303
			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
   304
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   305
		/* 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
   306
		FOR_ALL_CLIENTS(cs) {
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   307
			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
   308
			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
   309
				/* 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
   310
				NetworkSend_uint8(packet, 1);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   311
				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
   312
				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
   313
				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
   314
			}
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   315
		}
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   316
		/* 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
   317
		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
   318
		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
   319
			/* 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
   320
			NetworkSend_uint8(packet, 1);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   321
			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
   322
			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
   323
			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
   324
		}
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   325
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   326
		/* 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
   327
		NetworkSend_uint8(packet, 0);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   328
	}
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
	/* 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
   331
	FOR_ALL_CLIENTS(cs) {
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   332
		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
   333
		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
   334
			/* 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
   335
			NetworkSend_uint8(packet, 1);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   336
			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
   337
			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
   338
			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
   339
		}
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   340
	}
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   341
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   342
	/* 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
   343
	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
   344
	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
   345
		/* 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
   346
		NetworkSend_uint8(packet, 1);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   347
		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
   348
		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
   349
		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
   350
	}
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   351
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   352
	/* 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
   353
	NetworkSend_uint8(packet, 0);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   354
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   355
	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
   356
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   357
	free(packet);
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   358
}
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   359
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   360
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   361
{
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   362
	int i;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   363
	struct in_addr ip;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   364
	uint16 port;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   365
	uint8 ver;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   366
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   367
	/* packet begins with the protocol version (uint8)
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   368
	 * then an uint16 which indicates how many
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   369
	 * 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
   370
	 * 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
   371
	 */
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   372
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   373
	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
   374
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   375
	if (_udp_cs.has_quit) return;
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   376
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   377
	if (ver == 1) {
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   378
		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
   379
			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
   380
			port = NetworkRecv_uint16(&_udp_cs, p);
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   381
			NetworkUDPQueryServer(inet_ntoa(ip), port);
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   382
		}
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   383
	}
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   384
}
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   385
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   386
DEF_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER)
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   387
{
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   388
	_network_advertise_retries = 0;
765
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   389
	DEBUG(net, 2)("[NET][UDP] We are advertised on the master-server!");
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   390
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   391
	if (!_network_advertise) {
765
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   392
		/* We are advertised, but we don't want to! */
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   393
		NetworkUDPRemoveAdvertise();
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   394
	}
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   395
}
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   396
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
   397
/**
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
   398
 * 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
   399
 *
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
   400
 * 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
   401
 * 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
   402
 * (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
   403
 * 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
   404
 * 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
   405
 * 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
   406
 * 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
   407
 * 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
   408
 * 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
   409
 */
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
   410
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
   411
{
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
   412
	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
   413
	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
   414
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
   415
	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
   416
	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
   417
	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
   418
	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
   419
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
   420
	/* 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
   421
	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
   422
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
	DEBUG(net, 6)("[NET][UDP] NewGRF data request from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_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
   424
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
	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
   426
	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
   427
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
	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
   429
		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
   430
		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
   431
5424
f09762e44457 (svn r8489) [0.5] -Backport from trunk (8459, 8461): check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5341
diff changeset
   432
		NetworkRecv_GRFIdentifier(&_udp_cs, p, &c);
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
   433
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
		/* 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
   435
		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
   436
		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
   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
		/* 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
   439
		 * 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
   440
		 * 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
   441
		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
   442
				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
   443
		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
   444
			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
   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
		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
   447
		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
   448
	}
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
   449
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
	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
   451
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
	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
   453
	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
   454
	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
   455
		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
   456
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
   457
		/* 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
   458
		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
   459
				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
   460
	 	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
   461
		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
   462
	}
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
	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
   465
	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
   466
}
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
   467
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
   468
/** 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
   469
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
   470
{
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
	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
   472
	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
   473
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
	/* 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
   475
	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
   476
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
	DEBUG(net, 6)("[NET][UDP] NewGRF data reply from %s:%d", inet_ntoa(client_addr->sin_addr),ntohs(client_addr->sin_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
   478
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
	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
   480
	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
   481
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
   482
	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
   483
		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
   484
		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
   485
		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
   486
5424
f09762e44457 (svn r8489) [0.5] -Backport from trunk (8459, 8461): check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5341
diff changeset
   487
		NetworkRecv_GRFIdentifier(&_udp_cs, p, &c);
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
   488
		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
   489
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
   490
		/* 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
   491
		 * 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
   492
		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
   493
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
   494
		/* 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
   495
		 * 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
   496
		 * 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
   497
		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
   498
		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
   499
			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
   500
		}
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
	}
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
}
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
   503
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   504
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   505
// The layout for the receive-functions by UDP
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   506
typedef void NetworkUDPPacket(Packet *p, struct sockaddr_in *client_addr);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   507
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   508
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
   509
	RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER),
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   510
	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
   511
	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
   512
	NULL,
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   513
	NULL,
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   514
	RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER),
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   515
	NULL,
765
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   516
	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
   517
	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
   518
	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
   519
	RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS),
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   520
};
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   521
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   522
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   523
// 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
   524
assert_compile(lengthof(_network_udp_packet) == PACKET_UDP_END);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   525
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   526
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2789
diff changeset
   527
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
   528
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   529
	byte type;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   530
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   531
	/* 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
   532
	_udp_cs.socket = INVALID_SOCKET;
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   533
	_udp_cs.has_quit = false;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   534
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 850
diff changeset
   535
	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
   536
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   537
	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
   538
		_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
   539
	} else {
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   540
		if (!_udp_cs.has_quit) {
5425
1a9efec6fd29 (svn r8490) [0.5] -Add: logging of the IP address and port of invalid/illegal UDP packets.
rubidium
parents: 5424
diff changeset
   541
			DEBUG(net, 0)("[NET][UDP] Received invalid packet type %d from %s:%d",
1a9efec6fd29 (svn r8490) [0.5] -Add: logging of the IP address and port of invalid/illegal UDP packets.
rubidium
parents: 5424
diff changeset
   542
					type, inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
3547
a4584d709460 (svn r4413) -Fix: fixed a bug which pushed the client back to the main menu when a
truelight
parents: 3456
diff changeset
   543
		} else {
5425
1a9efec6fd29 (svn r8490) [0.5] -Add: logging of the IP address and port of invalid/illegal UDP packets.
rubidium
parents: 5424
diff changeset
   544
			DEBUG(net, 0)("[NET][UDP] Received illegal packet from %s:%d",
1a9efec6fd29 (svn r8490) [0.5] -Add: logging of the IP address and port of invalid/illegal UDP packets.
rubidium
parents: 5424
diff changeset
   545
					inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
3547
a4584d709460 (svn r4413) -Fix: fixed a bug which pushed the client back to the main menu when a
truelight
parents: 3456
diff changeset
   546
		}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   547
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   548
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   549
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   550
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   551
// Send a packet over UDP
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2789
diff changeset
   552
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
   553
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   554
	int res;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   555
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   556
	// Put the length in the buffer
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   557
	p->buffer[0] = p->size & 0xFF;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   558
	p->buffer[1] = p->size >> 8;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   559
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   560
	// Send the buffer
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   561
	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
   562
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   563
	// Check for any errors, but ignore it for the rest
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   564
	if (res == -1) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   565
		DEBUG(net, 1)("[NET][UDP] Send error: %i", GET_LAST_ERROR());
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   566
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   567
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   568
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   569
// Start UDP listener
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   570
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
   571
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   572
	struct sockaddr_in sin;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   573
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   574
	// Make sure socket is closed
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   575
	closesocket(*udp);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   576
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   577
	*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
   578
	if (*udp == INVALID_SOCKET) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   579
		DEBUG(net, 1)("[NET][UDP] Failed to start UDP support");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   580
		return false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   581
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   582
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   583
	// set nonblocking mode for socket
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   584
	{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   585
		unsigned long blocking = 1;
2497
1c8460570c0d (svn r3023) -Fix [BeOS] fixed compilation on BeOS R5 (MYOB)
bjarni
parents: 2186
diff changeset
   586
#ifndef BEOS_NET_SERVER
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   587
		ioctlsocket(*udp, FIONBIO, &blocking);
2497
1c8460570c0d (svn r3023) -Fix [BeOS] fixed compilation on BeOS R5 (MYOB)
bjarni
parents: 2186
diff changeset
   588
#else
2524
5919ff778fde (svn r3053) -Fix: [ 1326604 ] Fixed typo and hang for BeOS Networking (MYOB)
truelight
parents: 2497
diff changeset
   589
		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
   590
#endif
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   591
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   592
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   593
	sin.sin_family = AF_INET;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   594
	// Listen on all IPs
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   595
	sin.sin_addr.s_addr = host;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   596
	sin.sin_port = htons(port);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   597
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   598
	if (bind(*udp, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
629
ce914eb4023d (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 543
diff changeset
   599
		DEBUG(net, 1) ("[NET][UDP] error: 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
   600
		return false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   601
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   602
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   603
	if (broadcast) {
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   604
		/* Enable broadcast */
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   605
		unsigned long val = 1;
2497
1c8460570c0d (svn r3023) -Fix [BeOS] fixed compilation on BeOS R5 (MYOB)
bjarni
parents: 2186
diff changeset
   606
#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
   607
		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
   608
#endif
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   609
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   610
629
ce914eb4023d (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 543
diff changeset
   611
	DEBUG(net, 1)("[NET][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
   612
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   613
	return true;
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
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   616
// Close UDP connection
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   617
void NetworkUDPClose(void)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   618
{
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   619
	DEBUG(net, 1) ("[NET][UDP] Closed listeners");
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   620
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   621
	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
   622
		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
   623
			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
   624
			_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
   625
		}
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   626
985
ef5ba72aca89 (svn r1483) -Fix: don't close a socket if it is already closed (windows doesn't like
truelight
parents: 903
diff changeset
   627
		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
   628
			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
   629
			_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
   630
		}
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   631
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   632
		_network_udp_server = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   633
		_network_udp_broadcast = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   634
	} 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
   635
		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
   636
			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
   637
			_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
   638
		}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   639
		_network_udp_broadcast = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   640
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   641
}
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
// Receive something on UDP level
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   644
void NetworkUDPReceive(SOCKET udp)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   645
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   646
	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
   647
	socklen_t client_len;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   648
	int nbytes;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   649
	static Packet *p = NULL;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   650
	int packet_len;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   651
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   652
	// 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
   653
	if (p == NULL) p = malloc(sizeof(*p));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   654
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   655
	packet_len = sizeof(p->buffer);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   656
	client_len = sizeof(client_addr);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   657
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   658
	// Try to receive anything
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   659
	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
   660
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   661
	// 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
   662
	if (nbytes > 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   663
		// Get the size of the buffer
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   664
		p->size = (uint16)p->buffer[0];
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   665
		p->size += (uint16)p->buffer[1] << 8;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   666
		// Put the position on the right place
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   667
		p->pos = 2;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   668
		p->next = NULL;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   669
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   670
		// Handle the packet
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   671
		NetworkHandleUDPPacket(p, &client_addr);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   672
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   673
		// Free the packet
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   674
		free(p);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   675
		p = NULL;
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
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   678
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   679
// Broadcast to all ips
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2789
diff changeset
   680
static void NetworkUDPBroadCast(SOCKET udp)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   681
{
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   682
	Packet* p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   683
	uint i;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   684
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   685
	for (i = 0; _broadcast_list[i] != 0; i++) {
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   686
		struct sockaddr_in out_addr;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   687
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   688
		out_addr.sin_family = AF_INET;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   689
		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
   690
		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
   691
091963647651 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4026
diff changeset
   692
		DEBUG(net, 6)("[NET][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
   693
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   694
		NetworkSendUDP_Packet(udp, p, &out_addr);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   695
	}
4035
f468417d21b9 (svn r5294) -Fix: Plug a memory leak
tron
parents: 4034
diff changeset
   696
f468417d21b9 (svn r5294) -Fix: Plug a memory leak
tron
parents: 4034
diff changeset
   697
	free(p);
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   698
}
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   699
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   700
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   701
// 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
   702
void NetworkUDPQueryMasterServer(void)
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   703
{
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   704
	struct sockaddr_in out_addr;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   705
	Packet *p;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   706
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   707
	if (_udp_client_socket == INVALID_SOCKET)
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   708
		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
   709
			return;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   710
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   711
	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
   712
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   713
	out_addr.sin_family = AF_INET;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   714
	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
   715
	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
   716
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   717
	// packet only contains protocol version
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   718
	NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION);
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   719
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   720
	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
   721
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   722
	DEBUG(net, 2)("[NET][UDP] Queried Master Server 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
   723
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   724
	free(p);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   725
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   726
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   727
// Find all servers
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   728
void NetworkUDPSearchGame(void)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   729
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   730
	// We are still searching..
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   731
	if (_network_udp_broadcast > 0)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   732
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   733
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   734
	// No UDP-socket yet..
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   735
	if (_udp_client_socket == INVALID_SOCKET)
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   736
		if (!NetworkUDPListen(&_udp_client_socket, 0, 0, true))
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   737
			return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   738
5438
024eb27ab119 (svn r8650) [0.5] -Fix: 'Searching Server' is not worthy of debug level 0, as this opens an extra window on MorphOS for no reason.
rubidium
parents: 5425
diff changeset
   739
	DEBUG(net, 2)("[NET][UDP] Searching server");
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   740
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   741
	NetworkUDPBroadCast(_udp_client_socket);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   742
	_network_udp_broadcast = 300; // Stay searching for 300 ticks
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   743
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   744
1329
a8a0d60b0a8e (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   745
NetworkGameList *NetworkUDPQueryServer(const char* host, unsigned short port)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   746
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   747
	struct sockaddr_in out_addr;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   748
	Packet *p;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   749
	NetworkGameList *item;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   750
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   751
	// No UDP-socket yet..
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   752
	if (_udp_client_socket == INVALID_SOCKET)
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   753
		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
   754
			return NULL;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   755
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   756
	out_addr.sin_family = AF_INET;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   757
	out_addr.sin_port = htons(port);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   758
	out_addr.sin_addr.s_addr = NetworkResolveHost(host);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   759
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   760
	// Clear item in gamelist
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   761
	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
   762
	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
   763
	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
   764
	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
   765
	item->online = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   766
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   767
	// Init the packet
638
a26f83ba23f3 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   768
	p = NetworkSend_Init(PACKET_UDP_CLIENT_FIND_SERVER);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   769
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   770
	NetworkSendUDP_Packet(_udp_client_socket, p, &out_addr);
543
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
	free(p);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   773
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   774
	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
   775
	return item;
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
765
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   778
/* 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
   779
void NetworkUDPRemoveAdvertise(void)
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   780
{
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   781
	struct sockaddr_in out_addr;
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   782
	Packet *p;
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   783
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   784
	/* Check if we are advertising */
1832
84c9fb08f7eb (svn r2337) -Fix: NetworkUDPRemoveAdvertise wasn't fully correct (which made Darkvater go crazy ;))
truelight
parents: 1343
diff changeset
   785
	if (!_networking || !_network_server || !_network_udp_server)
765
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   786
		return;
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   787
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   788
	/* check for socket */
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   789
	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
   790
		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
   791
			return;
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   792
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   793
	DEBUG(net, 2)("[NET][UDP] Removing advertise..");
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   794
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   795
	/* Find somewhere to send */
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   796
	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
   797
	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
   798
	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
   799
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   800
	/* Send the packet */
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   801
	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
   802
	/* Packet is: Version, server_port */
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   803
	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
   804
	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
   805
	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
   806
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   807
	free(p);
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   808
}
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   809
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   810
/* Register us to the master server
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   811
     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
   812
void NetworkUDPAdvertise(void)
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   813
{
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   814
	struct sockaddr_in out_addr;
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   815
	Packet *p;
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   816
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   817
	/* Check if we should send an advertise */
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   818
	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
   819
		return;
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   820
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   821
	/* check for socket */
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   822
	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
   823
		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
   824
			return;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   825
2861
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   826
	if (_network_need_advertise) {
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   827
		_network_need_advertise = false;
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   828
		_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
   829
	} else {
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   830
		/* 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
   831
		if (_network_advertise_retries == 0) {
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   832
			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
   833
				return;
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   834
			_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
   835
		}
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   836
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   837
		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
   838
			return;
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   839
	}
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   840
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   841
	_network_advertise_retries--;
2861
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   842
	_network_last_advertise_frame = _frame_counter;
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   843
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   844
	/* Find somewhere to send */
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   845
	out_addr.sin_family = AF_INET;
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   846
	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
   847
	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
   848
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   849
	DEBUG(net, 1)("[NET][UDP] Advertising to master server");
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   850
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   851
	/* Send the packet */
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   852
	p = NetworkSend_Init(PACKET_UDP_SERVER_REGISTER);
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   853
	/* Packet is: WELCOME_MESSAGE, Version, server_port */
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   854
	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
   855
	NetworkSend_uint8(p, NETWORK_MASTER_SERVER_VERSION);
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   856
	NetworkSend_uint16(p, _network_server_port);
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   857
	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
   858
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   859
	free(p);
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   860
}
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   861
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   862
void NetworkUDPInitialize(void)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   863
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   864
	_udp_client_socket = INVALID_SOCKET;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   865
	_udp_server_socket = INVALID_SOCKET;
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   866
	_udp_master_socket = INVALID_SOCKET;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   867
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   868
	_network_udp_server = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   869
	_network_udp_broadcast = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   870
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   871
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   872
#endif /* ENABLE_NETWORK */