src/network/network_client.cpp
author rubidium
Tue, 30 Sep 2008 20:51:04 +0000
changeset 10208 72c00af5c95d
parent 10207 c291a21b304e
child 10310 ca2eb5811a07
permissions -rw-r--r--
(svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 9103
diff changeset
     3
/** @file network_client.cpp Client part of the network protocol. */
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 9103
diff changeset
     4
4826
6a545d194528 (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4512
diff changeset
     5
#ifdef ENABLE_NETWORK
6a545d194528 (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4512
diff changeset
     6
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: 5432
diff changeset
     7
#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: 5432
diff changeset
     8
#include "../debug.h"
7266
b16e67e992b4 (svn r10587) -Codechange: move the string/dparam related stuff from variables.h to strings.h
rubidium
parents: 7222
diff changeset
     9
#include "../openttd.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
    10
#include "network_internal.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: 5432
diff changeset
    11
#include "core/tcp.h"
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    12
#include "network_client.h"
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    13
#include "network_gamelist.h"
4512
823dfd4aa413 (svn r6297) -Codechange: Disentangle the query window mess a bit: Move the network game password handling somewhere were it belongs to
tron
parents: 4428
diff changeset
    14
#include "network_gui.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: 5432
diff changeset
    15
#include "../saveload.h"
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8114
diff changeset
    16
#include "../command_func.h"
9336
6baad5b3033d (svn r13228) -Codechange: split console.h.
rubidium
parents: 9111
diff changeset
    17
#include "../console_func.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: 5432
diff changeset
    18
#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: 5432
diff changeset
    19
#include "../ai/ai.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8116
diff changeset
    20
#include "../core/alloc_func.hpp"
10039
1f236afd6cd1 (svn r14199) -Codechange: split fileio.h into fileio_type.h and fileio_func.h so not everything that includes saveload.h needs to include everything else too.
rubidium
parents: 9896
diff changeset
    21
#include "../fileio_func.h"
7999
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    22
#include "../md5.h"
8114
dd6d21dc99c1 (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8106
diff changeset
    23
#include "../strings_func.h"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
    24
#include "../window_func.h"
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
    25
#include "../string_func.h"
10208
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    26
#include "../company_func.h"
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    27
#include "../company_base.h"
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    28
#include "../company_gui.h"
9085
8b3897a75aed (svn r12944) -Codechange: use rev.h instead of externs at many places
smatz
parents: 8973
diff changeset
    29
#include "../rev.h"
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    30
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8254
diff changeset
    31
#include "table/strings.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8254
diff changeset
    32
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    33
// This file handles all the client-commands
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    34
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    35
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    36
// So we don't make too much typos ;)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    37
#define MY_CLIENT DEREF_CLIENT(0)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    38
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    39
static uint32 last_ack_frame;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    40
7999
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    41
/** One bit of 'entropy' used to generate a salt for the company passwords. */
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    42
static uint32 _password_game_seed;
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    43
/** The other bit of 'entropy' used to generate a salt for the company passwords. */
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    44
static char _password_server_unique_id[NETWORK_UNIQUE_ID_LENGTH];
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    45
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    46
/** Make sure the unique ID length is the same as a md5 hash. */
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    47
assert_compile(NETWORK_UNIQUE_ID_LENGTH == 16 * 2 + 1);
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    48
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    49
/**
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    50
 * Generates a hashed password for the company name.
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    51
 * @param password the password to 'encrypt'.
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    52
 * @return the hashed password.
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    53
 */
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    54
static const char *GenerateCompanyPasswordHash(const char *password)
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    55
{
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    56
	if (StrEmpty(password)) return password;
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    57
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    58
	char salted_password[NETWORK_UNIQUE_ID_LENGTH];
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    59
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    60
	memset(salted_password, 0, sizeof(salted_password));
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    61
	snprintf(salted_password, sizeof(salted_password), "%s", password);
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    62
	/* Add the game seed and the server's unique ID as the salt. */
8252
69bfb7671f97 (svn r11816) -Fix: forgot clearing one byte, causing the passwords to differ slightly on different platforms.
rubidium
parents: 8214
diff changeset
    63
	for (uint i = 0; i < NETWORK_UNIQUE_ID_LENGTH - 1; i++) salted_password[i] ^= _password_server_unique_id[i] ^ (_password_game_seed >> i);
7999
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    64
8133
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8131
diff changeset
    65
	Md5 checksum;
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8131
diff changeset
    66
	uint8 digest[16];
7999
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    67
	static char hashed_password[NETWORK_UNIQUE_ID_LENGTH];
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    68
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    69
	/* Generate the MD5 hash */
8252
69bfb7671f97 (svn r11816) -Fix: forgot clearing one byte, causing the passwords to differ slightly on different platforms.
rubidium
parents: 8214
diff changeset
    70
	checksum.Append((const uint8*)salted_password, sizeof(salted_password) - 1);
8133
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8131
diff changeset
    71
	checksum.Finish(digest);
7999
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    72
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    73
	for (int di = 0; di < 16; di++) sprintf(hashed_password + di * 2, "%02x", digest[di]);
8252
69bfb7671f97 (svn r11816) -Fix: forgot clearing one byte, causing the passwords to differ slightly on different platforms.
rubidium
parents: 8214
diff changeset
    74
	hashed_password[lengthof(hashed_password) - 1] = '\0';
7999
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    75
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    76
	return hashed_password;
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    77
}
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    78
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    79
/**
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: 10039
diff changeset
    80
 * Hash the current company password; used when the server 'company' sets his/her password.
7999
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    81
 */
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    82
void HashCurrentCompanyPassword()
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    83
{
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: 10039
diff changeset
    84
	if (StrEmpty(_network_company_info[_local_company].password)) return;
7999
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    85
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
    86
	_password_game_seed = _settings_game.game_creation.generation_seed;
9420
8a38703928e8 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 9413
diff changeset
    87
	ttd_strlcpy(_password_server_unique_id, _settings_client.network.network_id, sizeof(_password_server_unique_id));
7999
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    88
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: 10039
diff changeset
    89
	const char *new_pw = GenerateCompanyPasswordHash(_network_company_info[_local_company].password);
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: 10039
diff changeset
    90
	ttd_strlcpy(_network_company_info[_local_company].password, new_pw, sizeof(_network_company_info[_local_company].password));
7999
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    91
}
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    92
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
    93
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    94
// **********
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    95
// Sending functions
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    96
//   DEF_CLIENT_SEND_COMMAND has no parameters
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    97
// **********
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    98
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    99
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_COMPANY_INFO)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   100
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   101
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   102
	// Packet: CLIENT_COMPANY_INFO
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   103
	// Function: Request company-info (in detail)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   104
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   105
	//    <none>
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   106
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   107
	Packet *p;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   108
	_network_join_status = NETWORK_JOIN_STATUS_GETTING_COMPANY_INFO;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   109
	InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   110
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   111
	p = NetworkSend_Init(PACKET_CLIENT_COMPANY_INFO);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   112
	MY_CLIENT->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   113
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   114
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   115
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_JOIN)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   116
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   117
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   118
	// Packet: CLIENT_JOIN
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   119
	// Function: Try to join the server
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   120
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   121
	//    String: OpenTTD Revision (norev000 if no revision)
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: 10039
diff changeset
   122
	//    String: Client Name (max NETWORK_NAME_LENGTH)
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: 10039
diff changeset
   123
	//    uint8:  Play as Company id (1..MAX_COMPANIES)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   124
	//    uint8:  Language ID
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: 10039
diff changeset
   125
	//    String: Unique id to find the client back in server-listing
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   126
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   127
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   128
	Packet *p;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   129
	_network_join_status = NETWORK_JOIN_STATUS_AUTHORIZING;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   130
	InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   131
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   132
	p = NetworkSend_Init(PACKET_CLIENT_JOIN);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   133
	p->Send_string(_openttd_revision);
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: 10039
diff changeset
   134
	p->Send_string(_settings_client.network.client_name); // Client name
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   135
	p->Send_uint8 (_network_playas);      // PlayAs
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   136
	p->Send_uint8 (NETLANG_ANY);          // Language
9420
8a38703928e8 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 9413
diff changeset
   137
	p->Send_string(_settings_client.network.network_id);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   138
	MY_CLIENT->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   139
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   140
5872
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   141
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   142
{
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   143
	//
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   144
	// Packet: CLIENT_NEWGRFS_CHECKED
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   145
	// Function: Tell the server that we have the required GRFs
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   146
	// Data:
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   147
	//
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   148
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   149
	Packet *p = NetworkSend_Init(PACKET_CLIENT_NEWGRFS_CHECKED);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   150
	MY_CLIENT->Send_Packet(p);
5872
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   151
}
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   152
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   153
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_PASSWORD)(NetworkPasswordType type, const char *password)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   154
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   155
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   156
	// Packet: CLIENT_PASSWORD
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   157
	// Function: Send a password to the server to authorize
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   158
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   159
	//    uint8:  NetworkPasswordType
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   160
	//    String: Password
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   161
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   162
	Packet *p = NetworkSend_Init(PACKET_CLIENT_PASSWORD);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   163
	p->Send_uint8 (type);
7999
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
   164
	p->Send_string(type == NETWORK_GAME_PASSWORD ? password : GenerateCompanyPasswordHash(password));
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   165
	MY_CLIENT->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   166
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   167
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   168
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_GETMAP)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   169
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   170
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   171
	// Packet: CLIENT_GETMAP
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   172
	// Function: Request the map from the server
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   173
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   174
	//    <none>
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   175
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   176
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   177
	Packet *p = NetworkSend_Init(PACKET_CLIENT_GETMAP);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   178
	MY_CLIENT->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   179
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   180
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   181
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_MAP_OK)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   182
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   183
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   184
	// Packet: CLIENT_MAP_OK
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   185
	// Function: Tell the server that we are done receiving/loading the map
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   186
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   187
	//    <none>
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   188
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   189
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   190
	Packet *p = NetworkSend_Init(PACKET_CLIENT_MAP_OK);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   191
	MY_CLIENT->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   192
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   193
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   194
DEF_CLIENT_SEND_COMMAND(PACKET_CLIENT_ACK)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   195
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   196
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   197
	// Packet: CLIENT_ACK
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   198
	// Function: Tell the server we are done with this frame
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   199
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   200
	//    uint32: current FrameCounter of the client
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   201
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   202
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   203
	Packet *p = NetworkSend_Init(PACKET_CLIENT_ACK);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   204
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   205
	p->Send_uint32(_frame_counter);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   206
	MY_CLIENT->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   207
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   208
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   209
// Send a command packet to the server
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   210
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_COMMAND)(CommandPacket *cp)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   211
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   212
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   213
	// Packet: CLIENT_COMMAND
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   214
	// Function: Send a DoCommand to the Server
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   215
	// Data:
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: 10039
diff changeset
   216
	//    uint8:  CompanyID (0..MAX_COMPANIES-1)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   217
	//    uint32: CommandID (see command.h)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   218
	//    uint32: P1 (free variables used in DoCommand)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   219
	//    uint32: P2
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   220
	//    uint32: Tile
1820
d03c56850dc2 (svn r2324) Introduce _cmd_text for passing strings with a command instead of abusing _decode_parameters as text buffer. This should prevent several possible buffer overruns and is a bit cleaner to use. As bonus it reduces the size of most command packets by 79 bytes.
tron
parents: 1797
diff changeset
   221
	//    string: text
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   222
	//    uint8:  CallBackID (see callback_table.c)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   223
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   224
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   225
	Packet *p = NetworkSend_Init(PACKET_CLIENT_COMMAND);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   226
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: 10039
diff changeset
   227
	p->Send_uint8 (cp->company);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   228
	p->Send_uint32(cp->cmd);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   229
	p->Send_uint32(cp->p1);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   230
	p->Send_uint32(cp->p2);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   231
	p->Send_uint32((uint32)cp->tile);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   232
	p->Send_string(cp->text);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   233
	p->Send_uint8 (cp->callback);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   234
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   235
	MY_CLIENT->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   236
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   237
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   238
// Send a chat-packet over the network
4906
935e6280c093 (svn r6876) -Codechange: Change the naming of _PLAYER in DESTTYPE_PLAYER/NETWORK_ACTION_CHAT_PLAYER
Darkvater
parents: 4880
diff changeset
   239
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_CHAT)(NetworkAction action, DestType type, int dest, const char *msg)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   240
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   241
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   242
	// Packet: CLIENT_CHAT
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   243
	// Function: Send a chat-packet to the serve
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   244
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   245
	//    uint8:  ActionID (see network_data.h, NetworkAction)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   246
	//    uint8:  Destination Type (see network_data.h, DestType);
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: 10039
diff changeset
   247
	//    uint16: Destination Company/Client
9896
19aa91881726 (svn r14045) -Codechange: move the network's limitation to chat messages to a more logical location and give it a more consistent name.
rubidium
parents: 9664
diff changeset
   248
	//    String: Message (max NETWORK_CHAT_LENGTH)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   249
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   250
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   251
	Packet *p = NetworkSend_Init(PACKET_CLIENT_CHAT);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   252
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   253
	p->Send_uint8 (action);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   254
	p->Send_uint8 (type);
6531
4445b25832c4 (svn r9719) -Fix: in-game private messages did not work for clients with a Client ID > 255.
rubidium
parents: 6492
diff changeset
   255
	p->Send_uint16(dest);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   256
	p->Send_string(msg);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   257
	MY_CLIENT->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   258
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   259
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   260
// Send an error-packet over the network
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   261
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_ERROR)(NetworkErrorCode errorno)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   262
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   263
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   264
	// Packet: CLIENT_ERROR
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   265
	// Function: The client made an error and is quiting the game
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   266
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   267
	//    uint8:  ErrorID (see network_data.h, NetworkErrorCode)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   268
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   269
	Packet *p = NetworkSend_Init(PACKET_CLIENT_ERROR);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   270
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   271
	p->Send_uint8(errorno);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   272
	MY_CLIENT->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   273
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   274
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   275
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_SET_PASSWORD)(const char *password)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   276
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   277
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   278
	// Packet: PACKET_CLIENT_SET_PASSWORD
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   279
	// Function: Set the password for the clients current company
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   280
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   281
	//    String: Password
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   282
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   283
	Packet *p = NetworkSend_Init(PACKET_CLIENT_SET_PASSWORD);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   284
7999
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
   285
	p->Send_string(GenerateCompanyPasswordHash(password));
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   286
	MY_CLIENT->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   287
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   288
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   289
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_SET_NAME)(const char *name)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   290
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   291
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   292
	// Packet: PACKET_CLIENT_SET_NAME
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: 10039
diff changeset
   293
	// Function: Gives the client a new name
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   294
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   295
	//    String: Name
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   296
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   297
	Packet *p = NetworkSend_Init(PACKET_CLIENT_SET_NAME);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   298
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   299
	p->Send_string(name);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   300
	MY_CLIENT->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   301
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   302
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   303
// Send an quit-packet over the network
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   304
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_QUIT)(const char *leavemsg)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   305
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   306
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   307
	// Packet: CLIENT_QUIT
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   308
	// Function: The client is quiting the game
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   309
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   310
	//    String: leave-message
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   311
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   312
	Packet *p = NetworkSend_Init(PACKET_CLIENT_QUIT);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   313
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   314
	p->Send_string(leavemsg);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   315
	MY_CLIENT->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   316
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   317
1026
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   318
DEF_CLIENT_SEND_COMMAND_PARAM(PACKET_CLIENT_RCON)(const char *pass, const char *command)
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   319
{
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   320
	Packet *p = NetworkSend_Init(PACKET_CLIENT_RCON);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   321
	p->Send_string(pass);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   322
	p->Send_string(command);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   323
	MY_CLIENT->Send_Packet(p);
1026
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   324
}
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   325
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   326
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   327
// **********
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   328
// Receiving functions
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   329
//   DEF_CLIENT_RECEIVE_COMMAND has parameter: Packet *p
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   330
// **********
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   331
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6531
diff changeset
   332
extern bool SafeSaveOrLoad(const char *filename, int mode, int newgm, Subdirectory subdir);
8973
749e572f9378 (svn r12765) -Codechange: move some stuff out of variables.h that required including other headers in variables.h.
rubidium
parents: 8264
diff changeset
   333
extern StringID _switch_mode_errorstr;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   334
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   335
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_FULL)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   336
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   337
	// We try to join a server which is full
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   338
	_switch_mode_errorstr = STR_NETWORK_ERR_SERVER_FULL;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   339
	DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   340
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   341
	return NETWORK_RECV_STATUS_SERVER_FULL;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   342
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   343
841
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 779
diff changeset
   344
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_BANNED)
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 779
diff changeset
   345
{
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 779
diff changeset
   346
	// We try to join a server where we are banned
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 779
diff changeset
   347
	_switch_mode_errorstr = STR_NETWORK_ERR_SERVER_BANNED;
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 779
diff changeset
   348
	DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 779
diff changeset
   349
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 779
diff changeset
   350
	return NETWORK_RECV_STATUS_SERVER_BANNED;
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 779
diff changeset
   351
}
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 779
diff changeset
   352
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   353
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   354
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   355
	byte company_info_version;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   356
	int i;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   357
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   358
	company_info_version = p->Recv_uint8();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   359
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   360
	if (!MY_CLIENT->has_quit && company_info_version == NETWORK_COMPANY_INFO_VERSION) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   361
		byte total;
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: 10039
diff changeset
   362
		CompanyID current;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   363
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   364
		total = p->Recv_uint8();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   365
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   366
		// There is no data at all..
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   367
		if (total == 0) return NETWORK_RECV_STATUS_CLOSE_QUERY;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   368
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   369
		current = (Owner)p->Recv_uint8();
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: 10039
diff changeset
   370
		if (current >= MAX_COMPANIES) return NETWORK_RECV_STATUS_CLOSE_QUERY;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   371
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: 10039
diff changeset
   372
		p->Recv_string(_network_company_info[current].company_name, sizeof(_network_company_info[current].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: 10039
diff changeset
   373
		_network_company_info[current].inaugurated_year = p->Recv_uint32();
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: 10039
diff changeset
   374
		_network_company_info[current].company_value    = p->Recv_uint64();
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: 10039
diff changeset
   375
		_network_company_info[current].money            = p->Recv_uint64();
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: 10039
diff changeset
   376
		_network_company_info[current].income           = p->Recv_uint64();
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: 10039
diff changeset
   377
		_network_company_info[current].performance      = p->Recv_uint16();
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: 10039
diff changeset
   378
		_network_company_info[current].use_password     = p->Recv_bool();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   379
		for (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: 10039
diff changeset
   380
			_network_company_info[current].num_vehicle[i] = p->Recv_uint16();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   381
		for (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: 10039
diff changeset
   382
			_network_company_info[current].num_station[i] = p->Recv_uint16();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   383
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: 10039
diff changeset
   384
		p->Recv_string(_network_company_info[current].clients, sizeof(_network_company_info[current].clients));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   385
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   386
		InvalidateWindow(WC_NETWORK_WINDOW, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   387
734
f4ad2f5805fd (svn r1186) -Fix: [Network] You can now join a company on a server where a company
truelight
parents: 722
diff changeset
   388
		return NETWORK_RECV_STATUS_OKAY;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   389
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   390
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   391
	return NETWORK_RECV_STATUS_CLOSE_QUERY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   392
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   393
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   394
// This packet contains info about the client (playas and name)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   395
//  as client we save this in NetworkClientInfo, linked via 'index'
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   396
//  which is always an unique number on a server.
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   397
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   398
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   399
	NetworkClientInfo *ci;
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   400
	uint16 index = p->Recv_uint16();
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: 10039
diff changeset
   401
	CompanyID playas = (CompanyID)p->Recv_uint8();
662
22708a1cbba9 (svn r1096) -Fix: [Network] PlayAs is now registered correctly
truelight
parents: 659
diff changeset
   402
	char name[NETWORK_NAME_LENGTH];
22708a1cbba9 (svn r1096) -Fix: [Network] PlayAs is now registered correctly
truelight
parents: 659
diff changeset
   403
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   404
	p->Recv_string(name, sizeof(name));
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   405
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   406
	if (MY_CLIENT->has_quit) return NETWORK_RECV_STATUS_CONN_LOST;
662
22708a1cbba9 (svn r1096) -Fix: [Network] PlayAs is now registered correctly
truelight
parents: 659
diff changeset
   407
22708a1cbba9 (svn r1096) -Fix: [Network] PlayAs is now registered correctly
truelight
parents: 659
diff changeset
   408
	/* Do we receive a change of data? Most likely we changed playas */
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   409
	if (index == _network_own_client_index) _network_playas = playas;
2684
4df784e4be66 (svn r3226) -Fix: GPMI implementation had minor glitches
truelight
parents: 2373
diff changeset
   410
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   411
	ci = NetworkFindClientInfoFromIndex(index);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   412
	if (ci != NULL) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   413
		if (playas == ci->client_playas && strcmp(name, ci->client_name) != 0) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   414
			// Client name changed, display the change
9337
ab6e0234bacc (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 9336
diff changeset
   415
			NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, "%s", name);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   416
		} else if (playas != ci->client_playas) {
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: 10039
diff changeset
   417
			// The client changed from client-player..
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   418
			// Do not display that for now
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   419
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   420
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   421
		ci->client_playas = playas;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   422
		ttd_strlcpy(ci->client_name, name, sizeof(ci->client_name));
662
22708a1cbba9 (svn r1096) -Fix: [Network] PlayAs is now registered correctly
truelight
parents: 659
diff changeset
   423
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   424
		InvalidateWindow(WC_CLIENT_LIST, 0);
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   425
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   426
		return NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   427
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   428
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   429
	// We don't have this index yet, find an empty index, and put the data there
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   430
	ci = NetworkFindClientInfoFromIndex(NETWORK_EMPTY_INDEX);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   431
	if (ci != NULL) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   432
		ci->client_index = index;
662
22708a1cbba9 (svn r1096) -Fix: [Network] PlayAs is now registered correctly
truelight
parents: 659
diff changeset
   433
		ci->client_playas = playas;
22708a1cbba9 (svn r1096) -Fix: [Network] PlayAs is now registered correctly
truelight
parents: 659
diff changeset
   434
22708a1cbba9 (svn r1096) -Fix: [Network] PlayAs is now registered correctly
truelight
parents: 659
diff changeset
   435
		ttd_strlcpy(ci->client_name, name, sizeof(ci->client_name));
22708a1cbba9 (svn r1096) -Fix: [Network] PlayAs is now registered correctly
truelight
parents: 659
diff changeset
   436
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   437
		InvalidateWindow(WC_CLIENT_LIST, 0);
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   438
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   439
		return NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   440
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   441
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   442
	// Here the program should never ever come.....
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   443
	return NETWORK_RECV_STATUS_MALFORMED_PACKET;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   444
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   445
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   446
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   447
{
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   448
	NetworkErrorCode error = (NetworkErrorCode)p->Recv_uint8();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   449
2879
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   450
	switch (error) {
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   451
		/* We made an error in the protocol, and our connection is closed.... */
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   452
		case NETWORK_ERROR_NOT_AUTHORIZED:
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   453
		case NETWORK_ERROR_NOT_EXPECTED:
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: 10039
diff changeset
   454
		case NETWORK_ERROR_COMPANY_MISMATCH:
2879
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   455
			_switch_mode_errorstr = STR_NETWORK_ERR_SERVER_ERROR;
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   456
			break;
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   457
		case NETWORK_ERROR_FULL:
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   458
			_switch_mode_errorstr = STR_NETWORK_ERR_SERVER_FULL;
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   459
			break;
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   460
		case NETWORK_ERROR_WRONG_REVISION:
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   461
			_switch_mode_errorstr = STR_NETWORK_ERR_WRONG_REVISION;
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   462
			break;
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   463
		case NETWORK_ERROR_WRONG_PASSWORD:
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   464
			_switch_mode_errorstr = STR_NETWORK_ERR_WRONG_PASSWORD;
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   465
			break;
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   466
		case NETWORK_ERROR_KICKED:
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   467
			_switch_mode_errorstr = STR_NETWORK_ERR_KICKED;
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   468
			break;
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   469
		case NETWORK_ERROR_CHEATER:
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   470
			_switch_mode_errorstr = STR_NETWORK_ERR_CHEATER;
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   471
			break;
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   472
		default:
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   473
			_switch_mode_errorstr = STR_NETWORK_ERR_LOSTCONNECTION;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   474
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   475
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   476
	DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
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
	return NETWORK_RECV_STATUS_SERVER_ERROR;
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
5872
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   481
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHECK_NEWGRFS)
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   482
{
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   483
	uint grf_count = p->Recv_uint8();
5872
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   484
	NetworkRecvStatus ret = NETWORK_RECV_STATUS_OKAY;
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   485
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   486
	/* Check all GRFs */
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   487
	for (; grf_count > 0; grf_count--) {
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   488
		GRFConfig c;
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   489
		MY_CLIENT->Recv_GRFIdentifier(p, &c);
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   490
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   491
		/* Check whether we know this GRF */
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   492
		const GRFConfig *f = FindGRFConfig(c.grfid, c.md5sum);
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   493
		if (f == NULL) {
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   494
			/* We do not know this GRF, bail out of initialization */
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   495
			char buf[sizeof(c.md5sum) * 2 + 1];
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   496
			md5sumToString(buf, lastof(buf), c.md5sum);
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   497
			DEBUG(grf, 0, "NewGRF %08X not found; checksum %s", BSWAP32(c.grfid), buf);
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   498
			ret = NETWORK_RECV_STATUS_NEWGRF_MISMATCH;
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   499
		}
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   500
	}
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   501
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   502
	if (ret == NETWORK_RECV_STATUS_OKAY) {
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   503
		/* Start receiving the map */
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   504
		SEND_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)();
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   505
	} else {
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   506
		/* NewGRF mismatch, bail out */
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   507
		_switch_mode_errorstr = STR_NETWORK_ERR_NEWGRF_MISMATCH;
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   508
	}
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   509
	return ret;
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   510
}
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   511
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   512
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEED_PASSWORD)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   513
{
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   514
	NetworkPasswordType type = (NetworkPasswordType)p->Recv_uint8();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   515
4512
823dfd4aa413 (svn r6297) -Codechange: Disentangle the query window mess a bit: Move the network game password handling somewhere were it belongs to
tron
parents: 4428
diff changeset
   516
	switch (type) {
7999
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
   517
		case NETWORK_COMPANY_PASSWORD:
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
   518
			/* Initialize the password hash salting variables. */
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
   519
			_password_game_seed = p->Recv_uint32();
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
   520
			p->Recv_string(_password_server_unique_id, sizeof(_password_server_unique_id));
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
   521
			if (MY_CLIENT->has_quit) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
   522
4512
823dfd4aa413 (svn r6297) -Codechange: Disentangle the query window mess a bit: Move the network game password handling somewhere were it belongs to
tron
parents: 4428
diff changeset
   523
		case NETWORK_GAME_PASSWORD:
823dfd4aa413 (svn r6297) -Codechange: Disentangle the query window mess a bit: Move the network game password handling somewhere were it belongs to
tron
parents: 4428
diff changeset
   524
			ShowNetworkNeedPassword(type);
823dfd4aa413 (svn r6297) -Codechange: Disentangle the query window mess a bit: Move the network game password handling somewhere were it belongs to
tron
parents: 4428
diff changeset
   525
			return NETWORK_RECV_STATUS_OKAY;
823dfd4aa413 (svn r6297) -Codechange: Disentangle the query window mess a bit: Move the network game password handling somewhere were it belongs to
tron
parents: 4428
diff changeset
   526
823dfd4aa413 (svn r6297) -Codechange: Disentangle the query window mess a bit: Move the network game password handling somewhere were it belongs to
tron
parents: 4428
diff changeset
   527
		default: return NETWORK_RECV_STATUS_MALFORMED_PACKET;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   528
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   529
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   530
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   531
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_WELCOME)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   532
{
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   533
	_network_own_client_index = p->Recv_uint16();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   534
7999
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
   535
	/* Initialize the password hash salting variables, even if they were previously. */
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
   536
	_password_game_seed = p->Recv_uint32();
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
   537
	p->Recv_string(_password_server_unique_id, sizeof(_password_server_unique_id));
bb3a1508fd71 (svn r11557) -Codechange: send and store the passwords a little more secure to/in the servers.
rubidium
parents: 7807
diff changeset
   538
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   539
	// Start receiving the map
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   540
	SEND_COMMAND(PACKET_CLIENT_GETMAP)();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   541
	return NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   542
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   544
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_WAIT)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   545
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   546
	_network_join_status = NETWORK_JOIN_STATUS_WAITING;
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   547
	_network_join_waiting = p->Recv_uint8();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   548
	InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   549
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   550
	// We are put on hold for receiving the map.. we need GUI for this ;)
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5376
diff changeset
   551
	DEBUG(net, 1, "The server is currently busy sending the map to someone else, please wait..." );
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5376
diff changeset
   552
	DEBUG(net, 1, "There are %d clients in front of you", _network_join_waiting);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   553
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   554
	return NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   555
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   556
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   557
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_MAP)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   558
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   559
	static FILE *file_pointer;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   560
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   561
	byte maptype;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   562
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   563
	maptype = p->Recv_uint8();
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   564
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   565
	if (MY_CLIENT->has_quit) return NETWORK_RECV_STATUS_CONN_LOST;
543
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
	// First packet, init some stuff
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   568
	if (maptype == MAP_PACKET_START) {
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6531
diff changeset
   569
		file_pointer = FioFOpenFile("network_client.tmp", "wb", AUTOSAVE_DIR);;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   570
		if (file_pointer == NULL) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   571
			_switch_mode_errorstr = STR_NETWORK_ERR_SAVEGAMEERROR;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   572
			return NETWORK_RECV_STATUS_SAVEGAME;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   573
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   574
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   575
		_frame_counter = _frame_counter_server = _frame_counter_max = p->Recv_uint32();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   576
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   577
		_network_join_kbytes = 0;
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   578
		_network_join_kbytes_total = p->Recv_uint32() / 1024;
5703
755268acddb6 (svn r8167) -Fix (FS#556): a network client crashes, due to a division by zero, when the connection gets lost at the right moment or when the packet is malformed (server sends size 0 for the map).
rubidium
parents: 5624
diff changeset
   579
755268acddb6 (svn r8167) -Fix (FS#556): a network client crashes, due to a division by zero, when the connection gets lost at the right moment or when the packet is malformed (server sends size 0 for the map).
rubidium
parents: 5624
diff changeset
   580
		/* If the network connection has been closed due to loss of connection
755268acddb6 (svn r8167) -Fix (FS#556): a network client crashes, due to a division by zero, when the connection gets lost at the right moment or when the packet is malformed (server sends size 0 for the map).
rubidium
parents: 5624
diff changeset
   581
		 * or when _network_join_kbytes_total is 0, the join status window will
755268acddb6 (svn r8167) -Fix (FS#556): a network client crashes, due to a division by zero, when the connection gets lost at the right moment or when the packet is malformed (server sends size 0 for the map).
rubidium
parents: 5624
diff changeset
   582
		 * do a division by zero. When the connection is lost, we just return
755268acddb6 (svn r8167) -Fix (FS#556): a network client crashes, due to a division by zero, when the connection gets lost at the right moment or when the packet is malformed (server sends size 0 for the map).
rubidium
parents: 5624
diff changeset
   583
		 * that. If kbytes_total is 0, the packet must be malformed as a
755268acddb6 (svn r8167) -Fix (FS#556): a network client crashes, due to a division by zero, when the connection gets lost at the right moment or when the packet is malformed (server sends size 0 for the map).
rubidium
parents: 5624
diff changeset
   584
		 * savegame less than 1 kilobyte is practically impossible. */
755268acddb6 (svn r8167) -Fix (FS#556): a network client crashes, due to a division by zero, when the connection gets lost at the right moment or when the packet is malformed (server sends size 0 for the map).
rubidium
parents: 5624
diff changeset
   585
		if (MY_CLIENT->has_quit) return NETWORK_RECV_STATUS_CONN_LOST;
755268acddb6 (svn r8167) -Fix (FS#556): a network client crashes, due to a division by zero, when the connection gets lost at the right moment or when the packet is malformed (server sends size 0 for the map).
rubidium
parents: 5624
diff changeset
   586
		if (_network_join_kbytes_total == 0) return NETWORK_RECV_STATUS_MALFORMED_PACKET;
755268acddb6 (svn r8167) -Fix (FS#556): a network client crashes, due to a division by zero, when the connection gets lost at the right moment or when the packet is malformed (server sends size 0 for the map).
rubidium
parents: 5624
diff changeset
   587
755268acddb6 (svn r8167) -Fix (FS#556): a network client crashes, due to a division by zero, when the connection gets lost at the right moment or when the packet is malformed (server sends size 0 for the map).
rubidium
parents: 5624
diff changeset
   588
		_network_join_status = NETWORK_JOIN_STATUS_DOWNLOADING;
6492
286a52449b54 (svn r9673) -Cleanup: remove spaces before tabs and replace non-indenting tabs with spaces.
rubidium
parents: 6247
diff changeset
   589
		InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   590
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   591
		// The first packet does not contain any more data
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   592
		return NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   593
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   594
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   595
	if (maptype == MAP_PACKET_NORMAL) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   596
		// We are still receiving data, put it to the file
9103
95b1766c87dd (svn r12963) -Fix (r12960): loading some NewGRFs could cause an infinite loop.
rubidium
parents: 9100
diff changeset
   597
		if (fwrite(p->buffer + p->pos, 1, p->size - p->pos, file_pointer) != (size_t)(p->size - p->pos)) {
9100
3ad0412f8ddf (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 9097
diff changeset
   598
			_switch_mode_errorstr = STR_NETWORK_ERR_SAVEGAMEERROR;
3ad0412f8ddf (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 9097
diff changeset
   599
			return NETWORK_RECV_STATUS_SAVEGAME;
3ad0412f8ddf (svn r12960) -Codechange: handle return values of (some) file system related functions.
rubidium
parents: 9097
diff changeset
   600
		}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   601
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   602
		_network_join_kbytes = ftell(file_pointer) / 1024;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   603
		InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   604
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   605
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   606
	// Check if this was the last packet
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   607
	if (maptype == MAP_PACKET_END) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   608
		fclose(file_pointer);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   609
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   610
		_network_join_status = NETWORK_JOIN_STATUS_PROCESSING;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   611
		InvalidateWindow(WC_NETWORK_STATUS_WINDOW, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   612
5413
21f75917d589 (svn r7616) -Cleanup:
Darkvater
parents: 5380
diff changeset
   613
		/* The map is done downloading, load it */
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6531
diff changeset
   614
		if (!SafeSaveOrLoad("network_client.tmp", SL_LOAD, GM_NORMAL, AUTOSAVE_DIR)) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   615
			DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   616
			_switch_mode_errorstr = STR_NETWORK_ERR_SAVEGAMEERROR;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   617
			return NETWORK_RECV_STATUS_SAVEGAME;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   618
		}
5432
5940d6a253c5 (svn r7638) -Codechange: Remove special window ShowJoinStatusWindowAfterJoin and shuffle around
Darkvater
parents: 5413
diff changeset
   619
		/* If the savegame has successfully loaded, ALL windows have been removed,
5940d6a253c5 (svn r7638) -Codechange: Remove special window ShowJoinStatusWindowAfterJoin and shuffle around
Darkvater
parents: 5413
diff changeset
   620
		 * only toolbar/statusbar and gamefield are visible */
1500
a66721629bc0 (svn r2004) - Fix: [ 1149487 ] Autosave ignoring settings
Darkvater
parents: 1317
diff changeset
   621
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   622
		// Say we received the map and loaded it correctly!
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   623
		SEND_COMMAND(PACKET_CLIENT_MAP_OK)();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   624
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: 10039
diff changeset
   625
		/* New company/spectator (invalid company) or company we want to join is not active
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: 10039
diff changeset
   626
		 * Switch local company to spectator and await the server's judgement */
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: 10039
diff changeset
   627
		if (_network_playas == COMPANY_NEW_COMPANY || !IsValidCompanyID(_network_playas)) {
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: 10039
diff changeset
   628
			SetLocalCompany(COMPANY_SPECTATOR);
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   629
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: 10039
diff changeset
   630
			if (_network_playas != COMPANY_SPECTATOR) {
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: 10039
diff changeset
   631
				/* We have arrived and ready to start playing; send a command to make a new company;
1797
fa156be69235 (svn r2301) - CodeChange: prettyify ChangeOwnerShipOfPlayerItems() a bit
Darkvater
parents: 1500
diff changeset
   632
				 * the server will give us a client-id and let us in */
5432
5940d6a253c5 (svn r7638) -Codechange: Remove special window ShowJoinStatusWindowAfterJoin and shuffle around
Darkvater
parents: 5413
diff changeset
   633
				_network_join_status = NETWORK_JOIN_STATUS_REGISTERING;
5940d6a253c5 (svn r7638) -Codechange: Remove special window ShowJoinStatusWindowAfterJoin and shuffle around
Darkvater
parents: 5413
diff changeset
   634
				ShowJoinStatusWindow();
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: 10039
diff changeset
   635
				NetworkSend_Command(0, 0, 0, CMD_COMPANY_CTRL, NULL);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   636
			}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   637
		} else {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   638
			// take control over an existing company
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: 10039
diff changeset
   639
			SetLocalCompany(_network_playas);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   640
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   641
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   642
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   643
	return NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   644
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   645
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   646
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_FRAME)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   647
{
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   648
	_frame_counter_server = p->Recv_uint32();
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   649
	_frame_counter_max = p->Recv_uint32();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   650
#ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   651
	// Test if the server supports this option
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   652
	//  and if we are at the frame the server is
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   653
	if (p->pos < p->size) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   654
		_sync_frame = _frame_counter_server;
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   655
		_sync_seed_1 = p->Recv_uint32();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   656
#ifdef NETWORK_SEND_DOUBLE_SEED
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   657
		_sync_seed_2 = p->Recv_uint32();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   658
#endif
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   659
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   660
#endif
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5376
diff changeset
   661
	DEBUG(net, 5, "Received FRAME %d", _frame_counter_server);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   662
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   663
	// Let the server know that we received this frame correctly
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   664
	//  We do this only once per day, to save some bandwidth ;)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   665
	if (!_network_first_time && last_ack_frame < _frame_counter) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   666
		last_ack_frame = _frame_counter + DAY_TICKS;
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5376
diff changeset
   667
		DEBUG(net, 4, "Sent ACK at %d", _frame_counter);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   668
		SEND_COMMAND(PACKET_CLIENT_ACK)();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   669
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   670
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   671
	return NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   672
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   673
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   674
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_SYNC)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   675
{
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   676
	_sync_frame = p->Recv_uint32();
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   677
	_sync_seed_1 = p->Recv_uint32();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   678
#ifdef NETWORK_SEND_DOUBLE_SEED
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   679
	_sync_seed_2 = p->Recv_uint32();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   680
#endif
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   681
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   682
	return NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   683
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   684
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   685
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_COMMAND)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   686
{
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
   687
	CommandPacket *cp = MallocT<CommandPacket>(1);
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: 10039
diff changeset
   688
	cp->company  = (CompanyID)p->Recv_uint8();
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   689
	cp->cmd      = p->Recv_uint32();
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   690
	cp->p1       = p->Recv_uint32();
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   691
	cp->p2       = p->Recv_uint32();
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   692
	cp->tile     = p->Recv_uint32();
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   693
	p->Recv_string(cp->text, sizeof(cp->text));
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   694
	cp->callback = p->Recv_uint8();
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   695
	cp->frame    = p->Recv_uint32();
7222
d46753675bf7 (svn r10501) -Fix [FS#1015]: error dialog was sometimes shown on all clients when a command failed instead of only the client that actually did the command.
rubidium
parents: 7060
diff changeset
   696
	cp->my_cmd   = p->Recv_bool();
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   697
	cp->next     = NULL;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   698
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   699
	// The server did send us this command..
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   700
	//  queue it in our own queue, so we can handle it in the upcoming frame!
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   701
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   702
	if (_local_command_queue == NULL) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   703
		_local_command_queue = cp;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   704
	} else {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   705
		// Find last packet
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   706
		CommandPacket *c = _local_command_queue;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   707
		while (c->next != NULL) c = c->next;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   708
		c->next = cp;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   709
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   710
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   711
	return NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   712
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   713
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   714
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_CHAT)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   715
{
9896
19aa91881726 (svn r14045) -Codechange: move the network's limitation to chat messages to a more logical location and give it a more consistent name.
rubidium
parents: 9664
diff changeset
   716
	char name[NETWORK_NAME_LENGTH], msg[NETWORK_CHAT_LENGTH];
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   717
	const NetworkClientInfo *ci = NULL, *ci_to;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   718
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   719
	NetworkAction action = (NetworkAction)p->Recv_uint8();
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   720
	uint16 index = p->Recv_uint16();
5918
f06b73812d5f (svn r8546) -Codechange: add a seperate (wrapper) functions to send/receive booleans.
rubidium
parents: 5902
diff changeset
   721
	bool self_send = p->Recv_bool();
9896
19aa91881726 (svn r14045) -Codechange: move the network's limitation to chat messages to a more logical location and give it a more consistent name.
rubidium
parents: 9664
diff changeset
   722
	p->Recv_string(msg, NETWORK_CHAT_LENGTH);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   723
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   724
	ci_to = NetworkFindClientInfoFromIndex(index);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   725
	if (ci_to == NULL) return NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   726
4946
9c6fe6ede9d2 (svn r6934) -Cleanup: Some minor cleanup for network_client and removing 'stupid' comment
Darkvater
parents: 4945
diff changeset
   727
	/* Did we initiate the action locally? */
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   728
	if (self_send) {
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   729
		switch (action) {
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   730
			case NETWORK_ACTION_CHAT_CLIENT:
4946
9c6fe6ede9d2 (svn r6934) -Cleanup: Some minor cleanup for network_client and removing 'stupid' comment
Darkvater
parents: 4945
diff changeset
   731
				/* For speaking to client we need the client-name */
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   732
				snprintf(name, sizeof(name), "%s", ci_to->client_name);
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   733
				ci = NetworkFindClientInfoFromIndex(_network_own_client_index);
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   734
				break;
4945
d20e68851e70 (svn r6933) -Feature: Allow spectators to speak to eachother (team)
Darkvater
parents: 4912
diff changeset
   735
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: 10039
diff changeset
   736
			/* For speaking to company or giving money, we need the company-name */
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   737
			case NETWORK_ACTION_GIVE_MONEY:
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: 10039
diff changeset
   738
				if (!IsValidCompanyID(ci_to->client_playas)) return NETWORK_RECV_STATUS_OKAY;
4945
d20e68851e70 (svn r6933) -Feature: Allow spectators to speak to eachother (team)
Darkvater
parents: 4912
diff changeset
   739
				/* fallthrough */
d20e68851e70 (svn r6933) -Feature: Allow spectators to speak to eachother (team)
Darkvater
parents: 4912
diff changeset
   740
			case NETWORK_ACTION_CHAT_COMPANY: {
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: 10039
diff changeset
   741
				StringID str = IsValidCompanyID(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS;
7060
4b811c8460cf (svn r10325) -Fix (r10323): Missed 3 company name references
peter1138
parents: 6929
diff changeset
   742
				SetDParam(0, ci_to->client_playas);
4878
4d4f76a898fd (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
   743
4945
d20e68851e70 (svn r6933) -Feature: Allow spectators to speak to eachother (team)
Darkvater
parents: 4912
diff changeset
   744
				GetString(name, str, lastof(name));
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   745
				ci = NetworkFindClientInfoFromIndex(_network_own_client_index);
4945
d20e68851e70 (svn r6933) -Feature: Allow spectators to speak to eachother (team)
Darkvater
parents: 4912
diff changeset
   746
			} break;
4946
9c6fe6ede9d2 (svn r6934) -Cleanup: Some minor cleanup for network_client and removing 'stupid' comment
Darkvater
parents: 4945
diff changeset
   747
9c6fe6ede9d2 (svn r6934) -Cleanup: Some minor cleanup for network_client and removing 'stupid' comment
Darkvater
parents: 4945
diff changeset
   748
			default: NOT_REACHED(); break;
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   749
		}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   750
	} else {
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   751
		/* Display message from somebody else */
602
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 571
diff changeset
   752
		snprintf(name, sizeof(name), "%s", ci_to->client_name);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   753
		ci = ci_to;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   754
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   755
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   756
	if (ci != NULL)
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: 10039
diff changeset
   757
		NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), self_send, name, "%s", msg);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   758
	return NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   759
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   760
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   761
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   762
{
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   763
	char str[100];
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   764
	uint16 index;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   765
	NetworkClientInfo *ci;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   766
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   767
	index = p->Recv_uint16();
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   768
	GetNetworkErrorMsg(str, (NetworkErrorCode)p->Recv_uint8(), lastof(str));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   769
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   770
	ci = NetworkFindClientInfoFromIndex(index);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   771
	if (ci != NULL) {
9337
ab6e0234bacc (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 9336
diff changeset
   772
		NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, "%s", str);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   773
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   774
		// The client is gone, give the NetworkClientInfo free
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   775
		ci->client_index = NETWORK_EMPTY_INDEX;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   776
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   777
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   778
	InvalidateWindow(WC_CLIENT_LIST, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   779
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   780
	return NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   781
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   782
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   783
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_QUIT)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   784
{
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   785
	char str[100];
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   786
	uint16 index;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   787
	NetworkClientInfo *ci;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   788
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   789
	index = p->Recv_uint16();
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   790
	p->Recv_string(str, lengthof(str));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   791
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   792
	ci = NetworkFindClientInfoFromIndex(index);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   793
	if (ci != NULL) {
9337
ab6e0234bacc (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 9336
diff changeset
   794
		NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, ci->client_name, "%s", str);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   795
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   796
		// The client is gone, give the NetworkClientInfo free
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   797
		ci->client_index = NETWORK_EMPTY_INDEX;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   798
	} else {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5376
diff changeset
   799
		DEBUG(net, 0, "Unknown client (%d) is leaving the game", index);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   800
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   801
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   802
	InvalidateWindow(WC_CLIENT_LIST, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   803
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   804
	// If we come here it means we could not locate the client.. strange :s
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   805
	return NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   806
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   807
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   808
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_JOIN)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   809
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   810
	uint16 index;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   811
	NetworkClientInfo *ci;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   812
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   813
	index = p->Recv_uint16();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   814
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   815
	ci = NetworkFindClientInfoFromIndex(index);
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 717
diff changeset
   816
	if (ci != NULL)
9337
ab6e0234bacc (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 9336
diff changeset
   817
		NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, ci->client_name, "");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   818
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   819
	InvalidateWindow(WC_CLIENT_LIST, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   820
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   821
	return NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   822
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   823
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   824
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_SHUTDOWN)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   825
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   826
	_switch_mode_errorstr = STR_NETWORK_SERVER_SHUTDOWN;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   827
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   828
	return NETWORK_RECV_STATUS_SERVER_ERROR;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   829
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   830
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   831
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_NEWGAME)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   832
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   833
	// To trottle the reconnects a bit, every clients waits
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: 10039
diff changeset
   834
	//  his _local_company value before reconnecting
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: 10039
diff changeset
   835
	// COMPANY_SPECTATOR is currently 255, so to avoid long wait periods
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   836
	//  set the max to 10.
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: 10039
diff changeset
   837
	_network_reconnect = min(_local_company + 1, 10);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   838
	_switch_mode_errorstr = STR_NETWORK_SERVER_REBOOT;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   839
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   840
	return NETWORK_RECV_STATUS_SERVER_ERROR;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   841
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   842
1026
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   843
DEF_CLIENT_RECEIVE_COMMAND(PACKET_SERVER_RCON)
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   844
{
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   845
	char rcon_out[NETWORK_RCONCOMMAND_LENGTH];
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   846
9337
ab6e0234bacc (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 9336
diff changeset
   847
	ConsoleColour color_code = (ConsoleColour)p->Recv_uint16();
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   848
	p->Recv_string(rcon_out, sizeof(rcon_out));
1026
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   849
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   850
	IConsolePrint(color_code, rcon_out);
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   851
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   852
	return NETWORK_RECV_STATUS_OKAY;
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   853
}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   854
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   855
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   856
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   857
// The layout for the receive-functions by the client
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   858
typedef NetworkRecvStatus NetworkClientPacket(Packet *p);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   859
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   860
// This array matches PacketType. At an incoming
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   861
//  packet it is matches against this array
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   862
//  and that way the right function to handle that
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   863
//  packet is found.
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   864
static NetworkClientPacket* const _network_client_packet[] = {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   865
	RECEIVE_COMMAND(PACKET_SERVER_FULL),
841
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 779
diff changeset
   866
	RECEIVE_COMMAND(PACKET_SERVER_BANNED),
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   867
	NULL, /*PACKET_CLIENT_JOIN,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   868
	RECEIVE_COMMAND(PACKET_SERVER_ERROR),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   869
	NULL, /*PACKET_CLIENT_COMPANY_INFO,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   870
	RECEIVE_COMMAND(PACKET_SERVER_COMPANY_INFO),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   871
	RECEIVE_COMMAND(PACKET_SERVER_CLIENT_INFO),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   872
	RECEIVE_COMMAND(PACKET_SERVER_NEED_PASSWORD),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   873
	NULL, /*PACKET_CLIENT_PASSWORD,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   874
	RECEIVE_COMMAND(PACKET_SERVER_WELCOME),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   875
	NULL, /*PACKET_CLIENT_GETMAP,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   876
	RECEIVE_COMMAND(PACKET_SERVER_WAIT),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   877
	RECEIVE_COMMAND(PACKET_SERVER_MAP),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   878
	NULL, /*PACKET_CLIENT_MAP_OK,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   879
	RECEIVE_COMMAND(PACKET_SERVER_JOIN),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   880
	RECEIVE_COMMAND(PACKET_SERVER_FRAME),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   881
	RECEIVE_COMMAND(PACKET_SERVER_SYNC),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   882
	NULL, /*PACKET_CLIENT_ACK,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   883
	NULL, /*PACKET_CLIENT_COMMAND,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   884
	RECEIVE_COMMAND(PACKET_SERVER_COMMAND),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   885
	NULL, /*PACKET_CLIENT_CHAT,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   886
	RECEIVE_COMMAND(PACKET_SERVER_CHAT),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   887
	NULL, /*PACKET_CLIENT_SET_PASSWORD,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   888
	NULL, /*PACKET_CLIENT_SET_NAME,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   889
	NULL, /*PACKET_CLIENT_QUIT,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   890
	NULL, /*PACKET_CLIENT_ERROR,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   891
	RECEIVE_COMMAND(PACKET_SERVER_QUIT),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   892
	RECEIVE_COMMAND(PACKET_SERVER_ERROR_QUIT),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   893
	RECEIVE_COMMAND(PACKET_SERVER_SHUTDOWN),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   894
	RECEIVE_COMMAND(PACKET_SERVER_NEWGAME),
1026
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   895
	RECEIVE_COMMAND(PACKET_SERVER_RCON),
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1013
diff changeset
   896
	NULL, /*PACKET_CLIENT_RCON,*/
5872
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   897
	RECEIVE_COMMAND(PACKET_SERVER_CHECK_NEWGRFS),
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5703
diff changeset
   898
	NULL, /*PACKET_CLIENT_NEWGRFS_CHECKED,*/
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   899
};
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   900
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   901
// If this fails, check the array above with network_data.h
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   902
assert_compile(lengthof(_network_client_packet) == PACKET_END);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   903
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   904
// Is called after a client is connected to the server
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5918
diff changeset
   905
void NetworkClient_Connected()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   906
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   907
	// Set the frame-counter to 0 so nothing happens till we are ready
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   908
	_frame_counter = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   909
	_frame_counter_server = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   910
	last_ack_frame = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   911
	// Request the game-info
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   912
	SEND_COMMAND(PACKET_CLIENT_JOIN)();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   913
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   914
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   915
// Reads the packets from the socket-stream, if available
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: 5609
diff changeset
   916
NetworkRecvStatus NetworkClient_ReadPackets(NetworkTCPSocketHandler *cs)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   917
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   918
	Packet *p;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   919
	NetworkRecvStatus res = NETWORK_RECV_STATUS_OKAY;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   920
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   921
	while (res == NETWORK_RECV_STATUS_OKAY && (p = cs->Recv_Packet(&res)) != NULL) {
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   922
		byte type = p->Recv_uint8();
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   923
		if (type < PACKET_END && _network_client_packet[type] != NULL && !MY_CLIENT->has_quit) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   924
			res = _network_client_packet[type](p);
2026
567e3bc9af72 (svn r2535) Tabs
tron
parents: 1962
diff changeset
   925
		} else {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   926
			res = NETWORK_RECV_STATUS_MALFORMED_PACKET;
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5376
diff changeset
   927
			DEBUG(net, 0, "[client] received invalid packet type %d", type);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   928
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   929
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: 5872
diff changeset
   930
		delete p;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   931
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   932
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   933
	return res;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   934
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   935
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
   936
void NetworkClientSendRcon(const char *password, const char *command)
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
   937
{
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
   938
	SEND_COMMAND(PACKET_CLIENT_RCON)(password, command);
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
   939
}
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
   940
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: 10039
diff changeset
   941
void NetworkUpdateClientName()
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
   942
{
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
   943
	NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(_network_own_client_index);
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
   944
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
   945
	if (ci == NULL) return;
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
   946
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
   947
	/* Don't change the name if it is the same as the old name */
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: 10039
diff changeset
   948
	if (strcmp(ci->client_name, _settings_client.network.client_name) != 0) {
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
   949
		if (!_network_server) {
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: 10039
diff changeset
   950
			SEND_COMMAND(PACKET_CLIENT_SET_NAME)(_settings_client.network.client_name);
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
   951
		} else {
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: 10039
diff changeset
   952
			if (NetworkFindName(_settings_client.network.client_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: 10039
diff changeset
   953
				NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, "%s", _settings_client.network.client_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: 10039
diff changeset
   954
				ttd_strlcpy(ci->client_name, _settings_client.network.client_name, sizeof(ci->client_name));
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
   955
				NetworkUpdateClientInfo(NETWORK_SERVER_INDEX);
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
   956
			}
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
   957
		}
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
   958
	}
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
   959
}
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
   960
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
   961
void NetworkClientSendChat(NetworkAction action, DestType type, int dest, const char *msg)
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
   962
{
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
   963
	SEND_COMMAND(PACKET_CLIENT_CHAT)(action, type, dest, msg);
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
   964
}
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
   965
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
   966
void NetworkClientSetPassword()
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
   967
{
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: 10039
diff changeset
   968
	SEND_COMMAND(PACKET_CLIENT_SET_PASSWORD)(_network_company_info[_local_company].password);
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
   969
}
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
   970
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   971
#endif /* ENABLE_NETWORK */