src/network/network_udp.cpp
author Tero Marttila <terom@fixme.fi>
Fri, 19 Dec 2008 02:13:39 +0200
changeset 10440 0a91ea45b0e8
parent 10310 ca2eb5811a07
permissions -rw-r--r--
adjust the random land gen a bit to work with mini-maps
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2153
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2153
diff changeset
     2
6121
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5918
diff changeset
     3
/**
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5918
diff changeset
     4
 * @file network_udp.cpp This file handles the UDP related communication.
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5918
diff changeset
     5
 *
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5918
diff changeset
     6
 * This is the GameServer <-> MasterServer and GameServer <-> GameClient
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5918
diff changeset
     7
 * communication before the game is being joined.
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5918
diff changeset
     8
 */
2aae24b0881f (svn r8857) -Documentation: Added some doxygen @file tags, repaired others (the @file tag MUST be found before any line of code, that includes preprocessor directives).
celestar
parents: 5918
diff changeset
     9
4826
6a545d194528 (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4344
diff changeset
    10
#ifdef ENABLE_NETWORK
6a545d194528 (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4344
diff changeset
    11
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    12
#include "../stdafx.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    13
#include "../debug.h"
8140
0d0d8c94f84b (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8139
diff changeset
    14
#include "../date_func.h"
8139
4e91c448c409 (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8132
diff changeset
    15
#include "../map_func.h"
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    16
#include "network_gamelist.h"
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
    17
#include "network_udp.h"
9428
1ba05b499957 (svn r13343) -Codechange: reorder/move variable/functions in the network headers so that nothing from the network directory needs to include basically all network headers.
rubidium
parents: 9420
diff changeset
    18
#include "network_internal.h"
9844
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents: 9659
diff changeset
    19
#include "core/host.h"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    20
#include "../variables.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    21
#include "../newgrf_config.h"
8132
f4c7a8e4f25a (svn r11694) -Codechange: move more endianness related stuff to endian_func.hpp.
rubidium
parents: 7931
diff changeset
    22
#include "../core/endian_func.hpp"
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents: 8140
diff changeset
    23
#include "../string_func.h"
10208
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    24
#include "../company_base.h"
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    25
#include "../company_func.h"
8270
e7c342f6b14c (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8254
diff changeset
    26
#include "../settings_type.h"
9451
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    27
#include "../rev.h"
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    28
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    29
#include "core/udp.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5380
diff changeset
    30
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
    31
enum {
2861
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
    32
	ADVERTISE_NORMAL_INTERVAL = 30000, // interval between advertising in ticks (15 minutes)
4344
7e123fec5b0b (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
    33
	ADVERTISE_RETRY_INTERVAL  =   300, // readvertise when no response after this many ticks (9 seconds)
7e123fec5b0b (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
    34
	ADVERTISE_RETRY_TIMES     =     3  // give up readvertising after this much failed retries
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
    35
};
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    36
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    37
NetworkUDPSocketHandler *_udp_client_socket; ///< udp client socket
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    38
NetworkUDPSocketHandler *_udp_server_socket; ///< udp server socket
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    39
NetworkUDPSocketHandler *_udp_master_socket; ///< udp master socket
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    40
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    41
///*** Communication with the masterserver ***/
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    42
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    43
class MasterNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    44
protected:
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    45
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_ACK_REGISTER);
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    46
public:
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    47
	virtual ~MasterNetworkUDPSocketHandler() {}
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    48
};
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    49
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    50
DEF_UDP_RECEIVE_COMMAND(Master, PACKET_UDP_MASTER_ACK_REGISTER)
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    51
{
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    52
	_network_advertise_retries = 0;
7372
6fac48ff7c4d (svn r10736) -Fix: Correct all mispellings of 'successful'.
peter1138
parents: 7331
diff changeset
    53
	DEBUG(net, 2, "[udp] advertising on master server successful");
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    54
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    55
	/* We are advertised, but we don't want to! */
9481
25ed9b0c242a (svn r13433) -Codechange: remove a now useless global variable.
rubidium
parents: 9451
diff changeset
    56
	if (!_settings_client.network.server_advertise) NetworkUDPRemoveAdvertise();
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    57
}
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    58
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    59
///*** Communication with clients (we are server) ***/
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    60
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    61
class ServerNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    62
protected:
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    63
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER);
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    64
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO);
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    65
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_GET_NEWGRFS);
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    66
public:
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    67
	virtual ~ServerNetworkUDPSocketHandler() {}
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    68
};
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    69
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    70
DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_FIND_SERVER)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    71
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    72
	// Just a fail-safe.. should never happen
9451
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    73
	if (!_network_udp_server) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    74
		return;
9451
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    75
	}
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    76
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    77
	NetworkGameInfo ngi;
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    78
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    79
	/* Update some game_info */
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    80
	ngi.clients_on     = _network_game_info.clients_on;
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    81
	ngi.start_date     = _network_game_info.start_date;
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    82
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    83
	ngi.server_lang    = _settings_client.network.server_lang;
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    84
	ngi.use_password   = !StrEmpty(_settings_client.network.server_password);
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    85
	ngi.clients_max    = _settings_client.network.max_clients;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9844
diff changeset
    86
	ngi.companies_on   = ActiveCompanyCount();
9451
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    87
	ngi.companies_max  = _settings_client.network.max_companies;
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    88
	ngi.spectators_on  = NetworkSpectatorCount();
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    89
	ngi.spectators_max = _settings_client.network.max_spectators;
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    90
	ngi.game_date      = _date;
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    91
	ngi.map_width      = MapSizeX();
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    92
	ngi.map_height     = MapSizeY();
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    93
	ngi.map_set        = _settings_game.game_creation.landscape;
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    94
	ngi.dedicated      = _network_dedicated;
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    95
	ngi.grfconfig      = _grfconfig;
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
    96
10310
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10208
diff changeset
    97
	strecpy(ngi.map_name, _network_game_info.map_name, lastof(ngi.map_name));
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10208
diff changeset
    98
	strecpy(ngi.server_name, _settings_client.network.server_name, lastof(ngi.server_name));
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10208
diff changeset
    99
	strecpy(ngi.server_revision, _openttd_revision, lastof(ngi.server_revision));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   100
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   101
	Packet packet(PACKET_UDP_SERVER_RESPONSE);
9451
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
   102
	this->Send_NetworkGameInfo(&packet, &ngi);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   103
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   104
	// Let the client know that we are here
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   105
	this->SendPacket(&packet, client_addr);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   106
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   107
	DEBUG(net, 2, "[udp] queried from '%s'", inet_ntoa(client_addr->sin_addr));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   108
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   109
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   110
DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_DETAIL_INFO)
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   111
{
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   112
	// Just a fail-safe.. should never happen
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   113
	if (!_network_udp_server) return;
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   114
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   115
	Packet packet(PACKET_UDP_SERVER_DETAIL_INFO);
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   116
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   117
	/* Send the amount of active companies */
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   118
	packet.Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9844
diff changeset
   119
	packet.Send_uint8 (ActiveCompanyCount());
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   120
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   121
	/* Fetch the latest version of everything */
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   122
	NetworkPopulateCompanyInfo();
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   123
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9844
diff changeset
   124
	Company *company;
9646
8c5f5d33027d (svn r13713) -Fix: possible crash on creating a network packet.
rubidium
parents: 9481
diff changeset
   125
	byte current = 0;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9844
diff changeset
   126
	/* Go through all the companies */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9844
diff changeset
   127
	FOR_ALL_COMPANIES(company) {
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   128
		current++;
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   129
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   130
		/* Send the information */
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   131
		packet.Send_uint8 (current);
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   132
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9844
diff changeset
   133
		packet.Send_string(_network_company_info[company->index].company_name);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9844
diff changeset
   134
		packet.Send_uint32(_network_company_info[company->index].inaugurated_year);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9844
diff changeset
   135
		packet.Send_uint64(_network_company_info[company->index].company_value);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9844
diff changeset
   136
		packet.Send_uint64(_network_company_info[company->index].money);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9844
diff changeset
   137
		packet.Send_uint64(_network_company_info[company->index].income);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9844
diff changeset
   138
		packet.Send_uint16(_network_company_info[company->index].performance);
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   139
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   140
		/* Send 1 if there is a passord for the company else send 0 */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9844
diff changeset
   141
		packet.Send_bool  (!StrEmpty(_network_company_info[company->index].password));
1019
6bae6c11e865 (svn r1520) Trim 134 (!) lines with trailing whitespace ):
tron
parents: 1011
diff changeset
   142
9646
8c5f5d33027d (svn r13713) -Fix: possible crash on creating a network packet.
rubidium
parents: 9481
diff changeset
   143
		for (int i = 0; i < NETWORK_VEHICLE_TYPES; i++) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9844
diff changeset
   144
			packet.Send_uint16(_network_company_info[company->index].num_vehicle[i]);
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   145
		}
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   146
9646
8c5f5d33027d (svn r13713) -Fix: possible crash on creating a network packet.
rubidium
parents: 9481
diff changeset
   147
		for (int i = 0; i < NETWORK_STATION_TYPES; i++) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 9844
diff changeset
   148
			packet.Send_uint16(_network_company_info[company->index].num_station[i]);
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   149
		}
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   150
	}
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   151
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   152
	this->SendPacket(&packet, client_addr);
638
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   153
}
cb9f5f50d3c7 (svn r1069) -Add: [Network] Added UDP-packet for detail info about a server for
truelight
parents: 629
diff changeset
   154
5339
96ac3f4933bb (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
/**
96ac3f4933bb (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
 * A client has requested the names of some NewGRFs.
96ac3f4933bb (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
 *
96ac3f4933bb (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
 * Replying this can be tricky as we have a limit of SEND_MTU bytes
96ac3f4933bb (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
 * in the reply packet and we can send up to 100 bytes per NewGRF
96ac3f4933bb (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
 * (GRF ID, MD5sum and NETWORK_GRF_NAME_LENGTH bytes for the name).
96ac3f4933bb (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
 * As SEND_MTU is _much_ less than 100 * NETWORK_MAX_GRF_COUNT, it
96ac3f4933bb (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
 * could be that a packet overflows. To stop this we only reply
96ac3f4933bb (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
 * with the first N NewGRFs so that if the first N + 1 NewGRFs
96ac3f4933bb (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
 * would be sent, the packet overflows.
96ac3f4933bb (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
 * in_reply and in_reply_count are used to keep a list of GRFs to
96ac3f4933bb (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
 * send in the reply.
96ac3f4933bb (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
 */
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   168
DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_GET_NEWGRFS)
5339
96ac3f4933bb (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
{
96ac3f4933bb (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
	uint8 num_grfs;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   171
	uint i;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   172
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   173
	const GRFConfig *in_reply[NETWORK_MAX_GRF_COUNT];
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   174
	uint8 in_reply_count = 0;
9146
dbe2317185eb (svn r13008) -Fix [FS#1997]: silence some MSVC x64 warnings
glx
parents: 9108
diff changeset
   175
	size_t packet_len = 0;
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   176
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   177
	DEBUG(net, 6, "[udp] newgrf data request from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   178
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   179
	num_grfs = p->Recv_uint8 ();
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   180
	if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   181
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   182
	for (i = 0; i < num_grfs; i++) {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   183
		GRFConfig c;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   184
		const GRFConfig *f;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   185
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   186
		this->Recv_GRFIdentifier(p, &c);
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   187
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   188
		/* Find the matching GRF file */
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   189
		f = FindGRFConfig(c.grfid, c.md5sum);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   190
		if (f == NULL) continue; // The GRF is unknown to this server
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   191
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   192
		/* If the reply might exceed the size of the packet, only reply
5341
02fb6f89fc8a (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
   193
		 * the current list and do not send the other data.
02fb6f89fc8a (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
   194
		 * The name could be an empty string, if so take the filename. */
02fb6f89fc8a (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
   195
		packet_len += sizeof(c.grfid) + sizeof(c.md5sum) +
5638
ee1871005c80 (svn r8097) Replace strlen() {==,!=,>} 0 by the more concise {,!}StrEmpty(). Additionally the test takes O(1) instead of O(n) now
tron
parents: 5624
diff changeset
   196
				min(strlen((f->name != NULL && !StrEmpty(f->name)) ? f->name : f->filename) + 1, (size_t)NETWORK_GRF_NAME_LENGTH);
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   197
		if (packet_len > SEND_MTU - 4) { // 4 is 3 byte header + grf count in reply
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   198
			break;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   199
		}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   200
		in_reply[in_reply_count] = f;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   201
		in_reply_count++;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   202
	}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   203
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   204
	if (in_reply_count == 0) return;
96ac3f4933bb (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
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   206
	Packet packet(PACKET_UDP_SERVER_NEWGRFS);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   207
	packet.Send_uint8(in_reply_count);
5339
96ac3f4933bb (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
	for (i = 0; i < in_reply_count; i++) {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   209
		char name[NETWORK_GRF_NAME_LENGTH];
5341
02fb6f89fc8a (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
   210
02fb6f89fc8a (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
   211
		/* The name could be an empty string, if so take the filename */
10310
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10208
diff changeset
   212
		strecpy(name, (in_reply[i]->name != NULL && !StrEmpty(in_reply[i]->name)) ?
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10208
diff changeset
   213
				in_reply[i]->name : in_reply[i]->filename, lastof(name));
6492
286a52449b54 (svn r9673) -Cleanup: remove spaces before tabs and replace non-indenting tabs with spaces.
rubidium
parents: 6424
diff changeset
   214
		this->Send_GRFIdentifier(&packet, in_reply[i]);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   215
		packet.Send_string(name);
5339
96ac3f4933bb (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
	}
96ac3f4933bb (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
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   218
	this->SendPacket(&packet, client_addr);
5339
96ac3f4933bb (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
}
96ac3f4933bb (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
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   221
///*** Communication with servers (we are client) ***/
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   222
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   223
class ClientNetworkUDPSocketHandler : public NetworkUDPSocketHandler {
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   224
protected:
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   225
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_RESPONSE);
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   226
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_MASTER_RESPONSE_LIST);
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   227
	DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_SERVER_NEWGRFS);
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   228
	virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config);
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   229
public:
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   230
	virtual ~ClientNetworkUDPSocketHandler() {}
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   231
};
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   232
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   233
DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_RESPONSE)
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   234
{
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   235
	NetworkGameList *item;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   236
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   237
	// Just a fail-safe.. should never happen
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   238
	if (_network_udp_server) return;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   239
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   240
	DEBUG(net, 4, "[udp] server response from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   241
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   242
	// Find next item
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   243
	item = NetworkGameListAddItem(inet_addr(inet_ntoa(client_addr->sin_addr)), ntohs(client_addr->sin_port));
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   244
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   245
	this->Recv_NetworkGameInfo(p, &item->info);
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   246
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   247
	item->info.compatible = true;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   248
	{
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   249
		/* Checks whether there needs to be a request for names of GRFs and makes
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   250
		 * the request if necessary. GRFs that need to be requested are the GRFs
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   251
		 * that do not exist on the clients system and we do not have the name
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   252
		 * resolved of, i.e. the name is still UNKNOWN_GRF_NAME_PLACEHOLDER.
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   253
		 * The in_request array and in_request_count are used so there is no need
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   254
		 * to do a second loop over the GRF list, which can be relatively expensive
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   255
		 * due to the string comparisons. */
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   256
		const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT];
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   257
		const GRFConfig *c;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   258
		uint in_request_count = 0;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   259
		struct sockaddr_in out_addr;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   260
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   261
		for (c = item->info.grfconfig; c != NULL; c = c->next) {
6229
695400602ab0 (svn r9031) -Codechange: Introduce grfconfig->status, and use it for states that are
maedhros
parents: 6178
diff changeset
   262
			if (c->status == GCS_NOT_FOUND) item->info.compatible = false;
8970
da7261f48b7e (svn r12762) -Fix: tabs after the first non-tab character are generally not okay (or lines starting with a space and then tabs).
rubidium
parents: 8826
diff changeset
   263
			if (c->status != GCS_NOT_FOUND || strcmp(c->name, UNKNOWN_GRF_NAME_PLACEHOLDER) != 0) continue;
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   264
			in_request[in_request_count] = c;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   265
			in_request_count++;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   266
		}
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   267
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   268
		if (in_request_count > 0) {
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   269
			/* There are 'unknown' GRFs, now send a request for them */
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   270
			uint i;
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   271
			Packet packet(PACKET_UDP_CLIENT_GET_NEWGRFS);
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   272
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   273
			packet.Send_uint8(in_request_count);
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   274
			for (i = 0; i < in_request_count; i++) {
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   275
				this->Send_GRFIdentifier(&packet, in_request[i]);
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   276
			}
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   277
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   278
			out_addr.sin_family      = AF_INET;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   279
			out_addr.sin_port        = htons(item->port);
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   280
			out_addr.sin_addr.s_addr = item->ip;
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   281
			this->SendPacket(&packet, &out_addr);
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   282
		}
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   283
	}
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   284
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   285
	if (item->info.hostname[0] == '\0')
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   286
		snprintf(item->info.hostname, sizeof(item->info.hostname), "%s", inet_ntoa(client_addr->sin_addr));
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   287
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   288
	/* Check if we are allowed on this server based on the revision-match */
6178
c29a7d37c3ce (svn r8949) -Codechange: only test the first NETWORK_REVISION_LENGTH - 1 characters when determining network compatability. This makes it possible to have 'long' branch names while still being able to play network games.
rubidium
parents: 6121
diff changeset
   289
	item->info.version_compatible = IsNetworkCompatibleVersion(item->info.server_revision);
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   290
	item->info.compatible &= item->info.version_compatible; // Already contains match for GRFs
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   291
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   292
	item->online = true;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   293
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   294
	UpdateNetworkGameWindow(false);
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   295
}
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   296
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   297
DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_MASTER_RESPONSE_LIST)
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   298
{
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   299
	int i;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   300
	struct in_addr ip;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   301
	uint16 port;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   302
	uint8 ver;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   303
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   304
	/* packet begins with the protocol version (uint8)
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   305
	 * then an uint16 which indicates how many
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   306
	 * ip:port pairs are in this packet, after that
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   307
	 * an uint32 (ip) and an uint16 (port) for each pair
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   308
	 */
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   309
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   310
	ver = p->Recv_uint8();
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   311
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   312
	if (ver == 1) {
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   313
		for (i = p->Recv_uint16(); i != 0 ; i--) {
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   314
			ip.s_addr = TO_LE32(p->Recv_uint32());
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   315
			port = p->Recv_uint16();
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   316
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   317
			/* Somehow we reached the end of the packet */
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   318
			if (this->HasClientQuit()) return;
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   319
			NetworkUDPQueryServer(inet_ntoa(ip), port);
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   320
		}
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   321
	}
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   322
}
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   323
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   324
/** The return of the client's request of the names of some NewGRFs */
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   325
DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_NEWGRFS)
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   326
{
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   327
	uint8 num_grfs;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   328
	uint i;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   329
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   330
	DEBUG(net, 6, "[udp] newgrf data reply from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port));
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   331
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   332
	num_grfs = p->Recv_uint8 ();
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   333
	if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   334
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   335
	for (i = 0; i < num_grfs; i++) {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   336
		char *unknown_name;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   337
		char name[NETWORK_GRF_NAME_LENGTH];
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   338
		GRFConfig c;
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   339
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   340
		this->Recv_GRFIdentifier(p, &c);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   341
		p->Recv_string(name, sizeof(name));
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   342
5341
02fb6f89fc8a (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
   343
		/* An empty name is not possible under normal circumstances
02fb6f89fc8a (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
   344
		 * and causes problems when showing the NewGRF list. */
5638
ee1871005c80 (svn r8097) Replace strlen() {==,!=,>} 0 by the more concise {,!}StrEmpty(). Additionally the test takes O(1) instead of O(n) now
tron
parents: 5624
diff changeset
   345
		if (StrEmpty(name)) continue;
5341
02fb6f89fc8a (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
   346
5339
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   347
		/* Finds the fake GRFConfig for the just read GRF ID and MD5sum tuple.
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   348
		 * If it exists and not resolved yet, then name of the fake GRF is
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   349
		 * overwritten with the name from the reply. */
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   350
		unknown_name = FindUnknownGRFName(c.grfid, c.md5sum, false);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   351
		if (unknown_name != NULL && strcmp(unknown_name, UNKNOWN_GRF_NAME_PLACEHOLDER) == 0) {
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   352
			ttd_strlcpy(unknown_name, name, NETWORK_GRF_NAME_LENGTH);
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   353
		}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   354
	}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   355
}
96ac3f4933bb (svn r7505) -Feature: show NewGRFs used on a game server, show which NewGRFs you do and do not have.
rubidium
parents: 5034
diff changeset
   356
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   357
void ClientNetworkUDPSocketHandler::HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   358
{
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   359
	/* Find the matching GRF file */
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   360
	const GRFConfig *f = FindGRFConfig(config->grfid, config->md5sum);
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   361
	if (f == NULL) {
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   362
		/* Don't know the GRF, so mark game incompatible and the (possibly)
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   363
		 * already resolved name for this GRF (another server has sent the
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   364
		 * name of the GRF already */
6424
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6247
diff changeset
   365
		config->name   = FindUnknownGRFName(config->grfid, config->md5sum, true);
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6247
diff changeset
   366
		config->status = GCS_NOT_FOUND;
3547
fea14ff86f9e (svn r4413) -Fix: fixed a bug which pushed the client back to the main menu when a
truelight
parents: 3456
diff changeset
   367
	} else {
6424
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6247
diff changeset
   368
		config->filename  = f->filename;
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6247
diff changeset
   369
		config->name      = f->name;
d410d4c31d15 (svn r9560) -Codechange: add support for multiple 'base' directories for newgrf searching.
rubidium
parents: 6247
diff changeset
   370
		config->info      = f->info;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   371
	}
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7372
diff changeset
   372
	SetBit(config->flags, GCF_COPY);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   373
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   374
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   375
// Close UDP connection
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6229
diff changeset
   376
void NetworkUDPCloseAll()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   377
{
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   378
	DEBUG(net, 1, "[udp] closed listeners");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   379
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   380
	_udp_server_socket->Close();
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   381
	_udp_master_socket->Close();
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   382
	_udp_client_socket->Close();
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   383
5521
53a834d67956 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5519
diff changeset
   384
	_network_udp_server = false;
53a834d67956 (svn r7825) -Codechange: make NetworkUDPClose close a single UDP socket. Use NetworkUDPStop to close all opened udp sockets (those were called NetworkUDPClose).
rubidium
parents: 5519
diff changeset
   385
	_network_udp_broadcast = 0;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   386
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   387
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   388
// Broadcast to all ips
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   389
static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   390
{
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   391
	uint i;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   392
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   393
	for (i = 0; _broadcast_list[i] != 0; i++) {
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   394
		Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4035
diff changeset
   395
		struct sockaddr_in out_addr;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   396
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   397
		out_addr.sin_family = AF_INET;
9420
8a38703928e8 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 9413
diff changeset
   398
		out_addr.sin_port = htons(_settings_client.network.server_port);
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4026
diff changeset
   399
		out_addr.sin_addr.s_addr = _broadcast_list[i];
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4026
diff changeset
   400
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   401
		DEBUG(net, 4, "[udp] broadcasting to %s", inet_ntoa(out_addr.sin_addr));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   402
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   403
		socket->SendPacket(&p, &out_addr);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   404
	}
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   405
}
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   406
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   407
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   408
// Request the the server-list from the master server
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6229
diff changeset
   409
void NetworkUDPQueryMasterServer()
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   410
{
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   411
	struct sockaddr_in out_addr;
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   412
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   413
	if (!_udp_client_socket->IsConnected()) {
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   414
		if (!_udp_client_socket->Listen(0, 0, true)) return;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   415
	}
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   416
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   417
	Packet p(PACKET_UDP_CLIENT_GET_LIST);
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   418
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   419
	out_addr.sin_family = AF_INET;
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   420
	out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT);
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   421
	out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST);
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   422
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   423
	// packet only contains protocol version
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   424
	p.Send_uint8(NETWORK_MASTER_SERVER_VERSION);
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   425
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   426
	_udp_client_socket->SendPacket(&p, &out_addr);
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   427
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   428
	DEBUG(net, 2, "[udp] master server queried at %s:%d", inet_ntoa(out_addr.sin_addr), ntohs(out_addr.sin_port));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   429
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   430
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   431
// Find all servers
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6229
diff changeset
   432
void NetworkUDPSearchGame()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   433
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   434
	// We are still searching..
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   435
	if (_network_udp_broadcast > 0) return;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   436
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   437
	// No UDP-socket yet..
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   438
	if (!_udp_client_socket->IsConnected()) {
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   439
		if (!_udp_client_socket->Listen(0, 0, true)) return;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   440
	}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   441
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   442
	DEBUG(net, 0, "[udp] searching server");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   443
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   444
	NetworkUDPBroadCast(_udp_client_socket);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   445
	_network_udp_broadcast = 300; // Stay searching for 300 ticks
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   446
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   447
5916
8931f4450f51 (svn r8543) -Codechange: make a real difference between querying the server via UDP and TCP.
rubidium
parents: 5900
diff changeset
   448
void NetworkUDPQueryServer(const char* host, unsigned short port, bool manually)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   449
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   450
	struct sockaddr_in out_addr;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   451
	NetworkGameList *item;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   452
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   453
	// No UDP-socket yet..
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   454
	if (!_udp_client_socket->IsConnected()) {
5916
8931f4450f51 (svn r8543) -Codechange: make a real difference between querying the server via UDP and TCP.
rubidium
parents: 5900
diff changeset
   455
		if (!_udp_client_socket->Listen(0, 0, true)) return;
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   456
	}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   457
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   458
	out_addr.sin_family = AF_INET;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   459
	out_addr.sin_port = htons(port);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   460
	out_addr.sin_addr.s_addr = NetworkResolveHost(host);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   461
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   462
	// Clear item in gamelist
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   463
	item = NetworkGameListAddItem(inet_addr(inet_ntoa(out_addr.sin_addr)), ntohs(out_addr.sin_port));
9108
9af955aa5b1b (svn r12968) -Fix: Don't segfault when adding a server if 0.0.0.0 was entered for the address.
maedhros
parents: 8970
diff changeset
   464
	if (item == NULL) return;
9af955aa5b1b (svn r12968) -Fix: Don't segfault when adding a server if 0.0.0.0 was entered for the address.
maedhros
parents: 8970
diff changeset
   465
7331
7e3503b13496 (svn r10694) -Codechange: update the server information in the game list every once in a while so the information stays fairly up-to-date when you are looking through the servers.
rubidium
parents: 7085
diff changeset
   466
	if (StrEmpty(item->info.server_name)) {
7e3503b13496 (svn r10694) -Codechange: update the server information in the game list every once in a while so the information stays fairly up-to-date when you are looking through the servers.
rubidium
parents: 7085
diff changeset
   467
		memset(&item->info, 0, sizeof(item->info));
10310
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10208
diff changeset
   468
		strecpy(item->info.server_name, host, lastof(item->info.server_name));
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10208
diff changeset
   469
		strecpy(item->info.hostname, host, lastof(item->info.hostname));
7331
7e3503b13496 (svn r10694) -Codechange: update the server information in the game list every once in a while so the information stays fairly up-to-date when you are looking through the servers.
rubidium
parents: 7085
diff changeset
   470
		item->online = false;
7e3503b13496 (svn r10694) -Codechange: update the server information in the game list every once in a while so the information stays fairly up-to-date when you are looking through the servers.
rubidium
parents: 7085
diff changeset
   471
	}
5916
8931f4450f51 (svn r8543) -Codechange: make a real difference between querying the server via UDP and TCP.
rubidium
parents: 5900
diff changeset
   472
	item->manually = manually;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   473
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   474
	// Init the packet
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   475
	Packet p(PACKET_UDP_CLIENT_FIND_SERVER);
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   476
	_udp_client_socket->SendPacket(&p, &out_addr);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   477
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   478
	UpdateNetworkGameWindow(false);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   479
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   480
765
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   481
/* Remove our advertise from the master-server */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6229
diff changeset
   482
void NetworkUDPRemoveAdvertise()
765
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   483
{
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   484
	struct sockaddr_in out_addr;
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   485
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   486
	/* Check if we are advertising */
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   487
	if (!_networking || !_network_server || !_network_udp_server) return;
765
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   488
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   489
	/* check for socket */
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   490
	if (!_udp_master_socket->IsConnected()) {
8826
8fbb67bcf292 (svn r12574) -Fix: binding to a specific IP could cause OpenTTD to not register properly with the masterserver if one has multiple external interfaces.
rubidium
parents: 8270
diff changeset
   491
		if (!_udp_master_socket->Listen(_network_server_bind_ip, 0, false)) return;
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   492
	}
765
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   493
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   494
	DEBUG(net, 1, "[udp] removing advertise from master server");
765
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   495
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   496
	/* Find somewhere to send */
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   497
	out_addr.sin_family = AF_INET;
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   498
	out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT);
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   499
	out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST);
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   500
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   501
	/* Send the packet */
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   502
	Packet p(PACKET_UDP_SERVER_UNREGISTER);
765
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   503
	/* Packet is: Version, server_port */
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   504
	p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION);
9420
8a38703928e8 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 9413
diff changeset
   505
	p.Send_uint16(_settings_client.network.server_port);
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   506
	_udp_master_socket->SendPacket(&p, &out_addr);
765
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   507
}
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
   508
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   509
/* Register us to the master server
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   510
     This function checks if it needs to send an advertise */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6229
diff changeset
   511
void NetworkUDPAdvertise()
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   512
{
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   513
	struct sockaddr_in out_addr;
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   514
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   515
	/* Check if we should send an advertise */
9481
25ed9b0c242a (svn r13433) -Codechange: remove a now useless global variable.
rubidium
parents: 9451
diff changeset
   516
	if (!_networking || !_network_server || !_network_udp_server || !_settings_client.network.server_advertise)
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   517
		return;
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   518
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   519
	/* check for socket */
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   520
	if (!_udp_master_socket->IsConnected()) {
8826
8fbb67bcf292 (svn r12574) -Fix: binding to a specific IP could cause OpenTTD to not register properly with the masterserver if one has multiple external interfaces.
rubidium
parents: 8270
diff changeset
   521
		if (!_udp_master_socket->Listen(_network_server_bind_ip, 0, false)) return;
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   522
	}
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   523
2861
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   524
	if (_network_need_advertise) {
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   525
		_network_need_advertise = false;
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   526
		_network_advertise_retries = ADVERTISE_RETRY_TIMES;
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   527
	} else {
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   528
		/* Only send once every ADVERTISE_NORMAL_INTERVAL ticks */
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   529
		if (_network_advertise_retries == 0) {
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   530
			if ((_network_last_advertise_frame + ADVERTISE_NORMAL_INTERVAL) > _frame_counter)
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   531
				return;
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   532
			_network_advertise_retries = ADVERTISE_RETRY_TIMES;
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   533
		}
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   534
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   535
		if ((_network_last_advertise_frame + ADVERTISE_RETRY_INTERVAL) > _frame_counter)
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   536
			return;
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   537
	}
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   538
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 738
diff changeset
   539
	_network_advertise_retries--;
2861
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2817
diff changeset
   540
	_network_last_advertise_frame = _frame_counter;
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   541
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   542
	/* Find somewhere to send */
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   543
	out_addr.sin_family = AF_INET;
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   544
	out_addr.sin_port = htons(NETWORK_MASTER_SERVER_PORT);
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   545
	out_addr.sin_addr.s_addr = NetworkResolveHost(NETWORK_MASTER_SERVER_HOST);
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   546
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5341
diff changeset
   547
	DEBUG(net, 1, "[udp] advertising to master server");
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   548
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   549
	/* Send the packet */
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   550
	Packet p(PACKET_UDP_SERVER_REGISTER);
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   551
	/* Packet is: WELCOME_MESSAGE, Version, server_port */
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   552
	p.Send_string(NETWORK_MASTER_SERVER_WELCOME_MESSAGE);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   553
	p.Send_uint8 (NETWORK_MASTER_SERVER_VERSION);
9420
8a38703928e8 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 9413
diff changeset
   554
	p.Send_uint16(_settings_client.network.server_port);
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5861
diff changeset
   555
	_udp_master_socket->SendPacket(&p, &out_addr);
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   556
}
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 638
diff changeset
   557
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6229
diff changeset
   558
void NetworkUDPInitialize()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   559
{
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   560
	_udp_client_socket = new ClientNetworkUDPSocketHandler();
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   561
	_udp_server_socket = new ServerNetworkUDPSocketHandler();
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   562
	_udp_master_socket = new MasterNetworkUDPSocketHandler();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   563
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   564
	_network_udp_server = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   565
	_network_udp_broadcast = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   566
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   567
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6229
diff changeset
   568
void NetworkUDPShutdown()
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   569
{
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   570
	NetworkUDPCloseAll();
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   571
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   572
	delete _udp_client_socket;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   573
	delete _udp_server_socket;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   574
	delete _udp_master_socket;
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   575
}
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   576
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   577
#endif /* ENABLE_NETWORK */