src/network/network_server.cpp
author smatz
Tue, 04 Nov 2008 18:20:41 +0000
changeset 10319 987cb9dc47b4
parent 10315 b3c652b3a316
permissions -rw-r--r--
(svn r14564) -Fix (r14555): one more incorrect use of lastof()
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: 8785
diff changeset
     3
/** @file network_server.cpp Server 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: 8785
diff changeset
     4
4826
6a545d194528 (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4717
diff changeset
     5
#ifdef ENABLE_NETWORK
6a545d194528 (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4717
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: 5381
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: 5381
diff changeset
     8
#include "../openttd.h" // XXX StringID
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: 5381
diff changeset
     9
#include "../debug.h"
8114
dd6d21dc99c1 (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 7999
diff changeset
    10
#include "../strings_func.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
    11
#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: 5381
diff changeset
    12
#include "core/tcp.h"
8280
bb1cd1c23f4b (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8270
diff changeset
    13
#include "../vehicle_base.h"
bb1cd1c23f4b (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8270
diff changeset
    14
#include "../vehicle_func.h"
8140
0d0d8c94f84b (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8131
diff changeset
    15
#include "../date_func.h"
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    16
#include "network_server.h"
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 667
diff changeset
    17
#include "network_udp.h"
9336
6baad5b3033d (svn r13228) -Codechange: split console.h.
rubidium
parents: 9111
diff changeset
    18
#include "../console_func.h"
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8114
diff changeset
    19
#include "../command_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: 5381
diff changeset
    20
#include "../saveload.h"
8785
871586967963 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 8354
diff changeset
    21
#include "../station_base.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: 5381
diff changeset
    22
#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: 5381
diff changeset
    23
#include "../genworld.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8116
diff changeset
    24
#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
    25
#include "../fileio_func.h"
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents: 8144
diff changeset
    26
#include "../string_func.h"
10208
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_func.h"
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    29
#include "../company_gui.h"
8270
e7c342f6b14c (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8264
diff changeset
    30
#include "../settings_type.h"
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    31
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8254
diff changeset
    32
#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
    33
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    34
// This file handles all the server-commands
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    35
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
    36
static void NetworkHandleCommandQueue(NetworkTCPSocketHandler* cs);
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 692
diff changeset
    37
543
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
// Sending functions
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
    40
//   DEF_SERVER_SEND_COMMAND has parameter: NetworkTCPSocketHandler *cs
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    41
// **********
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    42
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
    43
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CLIENT_INFO)(NetworkTCPSocketHandler *cs, NetworkClientInfo *ci)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    44
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    45
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    46
	// Packet: SERVER_CLIENT_INFO
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    47
	// Function: Sends info about a client
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    48
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    49
	//    uint16:  The index of the client (always unique on a server. 1 = 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
    50
	//    uint8:  As which company the client is playing
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    51
	//    String: The name of the client
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    52
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    53
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    54
	if (ci->client_index != NETWORK_EMPTY_INDEX) {
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
    55
		Packet *p = NetworkSend_Init(PACKET_SERVER_CLIENT_INFO);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
    56
		p->Send_uint16(ci->client_index);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
    57
		p->Send_uint8 (ci->client_playas);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
    58
		p->Send_string(ci->client_name);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    59
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
    60
		cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    61
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    62
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    63
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    64
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_COMPANY_INFO)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    65
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    66
//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    67
	// Packet: SERVER_COMPANY_INFO
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    68
	// Function: Sends info about the companies
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    69
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    70
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    71
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    72
	int i;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    73
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
    74
	Company *company;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    75
	Packet *p;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    76
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
    77
	byte active = ActiveCompanyCount();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    78
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    79
	if (active == 0) {
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
    80
		p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    81
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
    82
		p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
    83
		p->Send_uint8 (active);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    84
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
    85
		cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    86
		return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    87
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    88
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    89
	NetworkPopulateCompanyInfo();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    90
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
    91
	FOR_ALL_COMPANIES(company) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    92
		p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    93
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
    94
		p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
    95
		p->Send_uint8 (active);
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
    96
		p->Send_uint8 (company->index);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    97
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
    98
		p->Send_string(_network_company_info[company->index].company_name);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10039
diff changeset
    99
		p->Send_uint32(_network_company_info[company->index].inaugurated_year);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10039
diff changeset
   100
		p->Send_uint64(_network_company_info[company->index].company_value);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10039
diff changeset
   101
		p->Send_uint64(_network_company_info[company->index].money);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10039
diff changeset
   102
		p->Send_uint64(_network_company_info[company->index].income);
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10039
diff changeset
   103
		p->Send_uint16(_network_company_info[company->index].performance);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   104
1011
933efc472c62 (svn r1510) -Add: Improved Network Lobby GUI: (bociusz)
truelight
parents: 970
diff changeset
   105
		/* Send 1 if there is a passord for the company else send 0 */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10039
diff changeset
   106
		p->Send_bool(!StrEmpty(_network_company_info[company->index].password));
1011
933efc472c62 (svn r1510) -Add: Improved Network Lobby GUI: (bociusz)
truelight
parents: 970
diff changeset
   107
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   108
		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
   109
			p->Send_uint16(_network_company_info[company->index].num_vehicle[i]);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   110
		}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   111
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   112
		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
   113
			p->Send_uint16(_network_company_info[company->index].num_station[i]);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   114
		}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   115
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
   116
		if (StrEmpty(_network_company_info[company->index].clients)) {
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   117
			p->Send_string("<none>");
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   118
		} 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
   119
			p->Send_string(_network_company_info[company->index].clients);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   120
		}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   121
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   122
		cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   123
	}
734
f4ad2f5805fd (svn r1186) -Fix: [Network] You can now join a company on a server where a company
truelight
parents: 733
diff changeset
   124
f4ad2f5805fd (svn r1186) -Fix: [Network] You can now join a company on a server where a company
truelight
parents: 733
diff changeset
   125
	p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO);
f4ad2f5805fd (svn r1186) -Fix: [Network] You can now join a company on a server where a company
truelight
parents: 733
diff changeset
   126
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   127
	p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   128
	p->Send_uint8 (0);
734
f4ad2f5805fd (svn r1186) -Fix: [Network] You can now join a company on a server where a company
truelight
parents: 733
diff changeset
   129
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   130
	cs->Send_Packet(p);
543
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
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
   133
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(NetworkTCPSocketHandler *cs, NetworkErrorCode error)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   134
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   135
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   136
	// Packet: SERVER_ERROR
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   137
	// Function: The client made an error
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   138
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   139
	//    uint8:  ErrorID (see network_data.h, NetworkErrorCode)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   140
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   141
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   142
	char str[100];
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   143
	Packet *p = NetworkSend_Init(PACKET_SERVER_ERROR);
3417
acded0892dad (svn r4241) - Fix: Perform validation on the error number that a server receives from a client. An invalid value may cause the server to terminate.
peter1138
parents: 3283
diff changeset
   144
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   145
	p->Send_uint8(error);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   146
	cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   147
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4906
diff changeset
   148
	GetNetworkErrorMsg(str, error, lastof(str));
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
   149
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   150
	// Only send when the current client was in game
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   151
	if (cs->status > STATUS_AUTH) {
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
   152
		NetworkTCPSocketHandler *new_cs;
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   153
		char client_name[NETWORK_CLIENT_NAME_LENGTH];
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   154
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   155
		NetworkGetClientName(client_name, sizeof(client_name), cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   156
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
   157
		DEBUG(net, 1, "'%s' made an error and has been disconnected. Reason: '%s'", client_name, str);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   158
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
   159
		NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "%s", str);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   160
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   161
		FOR_ALL_CLIENTS(new_cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   162
			if (new_cs->status > STATUS_AUTH && new_cs != cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   163
				// Some errors we filter to a more general error. Clients don't have to know the real
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   164
				//  reason a joining failed.
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   165
				if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED || error == NETWORK_ERROR_WRONG_REVISION)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   166
					error = NETWORK_ERROR_ILLEGAL_PACKET;
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
				SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, error);
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
	} else {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
   172
		DEBUG(net, 1, "Client %d made an error and has been disconnected. Reason: '%s'", cs->index, str);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   173
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   174
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   175
	cs->has_quit = true;
543
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
	// Make sure the data get's there before we close the connection
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   178
	cs->Send_Packets();
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
	// The client made a mistake, so drop his connection now!
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 692
diff changeset
   181
	NetworkCloseClient(cs);
543
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
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: 5854
diff changeset
   184
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CHECK_NEWGRFS)(NetworkTCPSocketHandler *cs)
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: 5854
diff changeset
   185
{
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: 5854
diff changeset
   186
	//
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: 5854
diff changeset
   187
	// Packet: 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: 5854
diff changeset
   188
	// Function: Sends info about the used GRFs to the client
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: 5854
diff changeset
   189
	// 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: 5854
diff changeset
   190
	//      uint8:  Amount of 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: 5854
diff changeset
   191
	//    And then for each 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: 5854
diff changeset
   192
	//      uint32: GRF ID
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: 5854
diff changeset
   193
	// 16 * uint8:  MD5 checksum of the 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: 5854
diff changeset
   194
	//
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: 5854
diff changeset
   195
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: 5854
diff changeset
   196
	Packet *p = NetworkSend_Init(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: 5854
diff changeset
   197
	const 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: 5854
diff changeset
   198
	uint grf_count = 0;
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: 5854
diff changeset
   199
7140
31e79bb57c5b (svn r10414) -Fix: the network protocol check for required newgrfs sent static newgrfs too.
rubidium
parents: 7060
diff changeset
   200
	for (c = _grfconfig; c != NULL; c = c->next) {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7807
diff changeset
   201
		if (!HasBit(c->flags, GCF_STATIC)) grf_count++;
7140
31e79bb57c5b (svn r10414) -Fix: the network protocol check for required newgrfs sent static newgrfs too.
rubidium
parents: 7060
diff changeset
   202
	}
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: 5854
diff changeset
   203
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   204
	p->Send_uint8 (grf_count);
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: 5854
diff changeset
   205
	for (c = _grfconfig; c != NULL; c = c->next) {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7807
diff changeset
   206
		if (!HasBit(c->flags, GCF_STATIC)) cs->Send_GRFIdentifier(p, c);
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: 5854
diff changeset
   207
	}
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: 5854
diff changeset
   208
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   209
	cs->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: 5854
diff changeset
   210
}
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: 5854
diff changeset
   211
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
   212
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_NEED_PASSWORD)(NetworkTCPSocketHandler *cs, NetworkPasswordType type)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   213
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   214
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   215
	// Packet: SERVER_NEED_PASSWORD
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   216
	// Function: Indication to the client that the server needs a password
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   217
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   218
	//    uint8:  Type of password
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   219
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   220
6235
a4ea32acd914 (svn r9038) -Fix [FS#115]: inactive connections are not automatically kicked, i.e. people who only open a telnet (or similar) connection to a server.
rubidium
parents: 6178
diff changeset
   221
	/* Invalid packet when status is AUTH or higher */
a4ea32acd914 (svn r9038) -Fix [FS#115]: inactive connections are not automatically kicked, i.e. people who only open a telnet (or similar) connection to a server.
rubidium
parents: 6178
diff changeset
   222
	if (cs->status >= STATUS_AUTH) return;
a4ea32acd914 (svn r9038) -Fix [FS#115]: inactive connections are not automatically kicked, i.e. people who only open a telnet (or similar) connection to a server.
rubidium
parents: 6178
diff changeset
   223
a4ea32acd914 (svn r9038) -Fix [FS#115]: inactive connections are not automatically kicked, i.e. people who only open a telnet (or similar) connection to a server.
rubidium
parents: 6178
diff changeset
   224
	cs->status = STATUS_AUTHORIZING;
a4ea32acd914 (svn r9038) -Fix [FS#115]: inactive connections are not automatically kicked, i.e. people who only open a telnet (or similar) connection to a server.
rubidium
parents: 6178
diff changeset
   225
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   226
	Packet *p = NetworkSend_Init(PACKET_SERVER_NEED_PASSWORD);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   227
	p->Send_uint8(type);
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
   228
	p->Send_uint32(_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
   229
	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
   230
	cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   231
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   232
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   233
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WELCOME)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   234
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   235
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   236
	// Packet: SERVER_WELCOME
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   237
	// Function: The client is joined and ready to receive his map
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   238
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   239
	//    uint16:  Own ClientID
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 *p;
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
   243
	NetworkTCPSocketHandler *new_cs;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   244
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   245
	// Invalid packet when status is AUTH or higher
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
   246
	if (cs->status >= STATUS_AUTH) return;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   247
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   248
	cs->status = STATUS_AUTH;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   249
	_network_game_info.clients_on++;
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
	p = NetworkSend_Init(PACKET_SERVER_WELCOME);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   252
	p->Send_uint16(cs->index);
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
   253
	p->Send_uint32(_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
   254
	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
   255
	cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   256
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   257
		// Transmit info about all the active clients
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   258
	FOR_ALL_CLIENTS(new_cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   259
		if (new_cs != cs && new_cs->status > STATUS_AUTH)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   260
			SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, DEREF_CLIENT_INFO(new_cs));
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   261
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   262
	// Also send the info of the server
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   263
	SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX));
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   264
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   265
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   266
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WAIT)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   267
{
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: PACKET_SERVER_WAIT
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   270
	// Function: The client can not receive the map at the moment because
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   271
	//             someone else is already receiving the map
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   272
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   273
	//    uint8:  Clients awaiting map
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
	int waiting = 0;
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
   276
	NetworkTCPSocketHandler *new_cs;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   277
	Packet *p;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   278
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
   279
	// Count how many clients are waiting in the queue
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   280
	FOR_ALL_CLIENTS(new_cs) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   281
		if (new_cs->status == STATUS_MAP_WAIT) waiting++;
543
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
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   284
	p = NetworkSend_Init(PACKET_SERVER_WAIT);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   285
	p->Send_uint8(waiting);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   286
	cs->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
// This sends the map to the client
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   290
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_MAP)
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
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   293
	// Packet: SERVER_MAP
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   294
	// Function: Sends the map to the client, or a part of it (it is splitted in
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   295
	//   a lot of multiple packets)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   296
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   297
	//    uint8:  packet-type (MAP_PACKET_START, MAP_PACKET_NORMAL and MAP_PACKET_END)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   298
	//  if MAP_PACKET_START:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   299
	//    uint32: The current FrameCounter
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   300
	//  if MAP_PACKET_NORMAL:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   301
	//    piece of the map (till max-size of packet)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   302
	//  if MAP_PACKET_END:
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
   303
	//    nothing
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   304
	//
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
	static FILE *file_pointer;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   307
	static uint sent_packets; // How many packets we did send succecfully last time
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   308
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   309
	if (cs->status < STATUS_AUTH) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   310
		// Illegal call, return error and ignore the packet
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   311
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   312
		return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   313
	}
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   314
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   315
	if (cs->status == STATUS_AUTH) {
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6836
diff changeset
   316
		const char *filename = "network_server.tmp";
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   317
		Packet *p;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   318
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   319
		// Make a dump of the current game
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9451
diff changeset
   320
		if (SaveOrLoad(filename, SL_SAVE, AUTOSAVE_DIR) != SL_OK) usererror("network savedump failed");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   321
6929
56470c1b8a66 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 6836
diff changeset
   322
		file_pointer = FioFOpenFile(filename, "rb", AUTOSAVE_DIR);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   323
		fseek(file_pointer, 0, SEEK_END);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   324
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9451
diff changeset
   325
		if (ftell(file_pointer) == 0) usererror("network savedump failed - zero sized savegame?");
5705
7509e8810d5a (svn r8171) -Fix (FS#556): return SL_ERROR when unthreaded saves failed, to make sure we do not try to send zero-byte savegames.
rubidium
parents: 5638
diff changeset
   326
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   327
		// Now send the _frame_counter and how many packets are coming
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   328
		p = NetworkSend_Init(PACKET_SERVER_MAP);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   329
		p->Send_uint8 (MAP_PACKET_START);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   330
		p->Send_uint32(_frame_counter);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   331
		p->Send_uint32(ftell(file_pointer));
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   332
		cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   333
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   334
		fseek(file_pointer, 0, SEEK_SET);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   335
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   336
		sent_packets = 4; // We start with trying 4 packets
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   337
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   338
		cs->status = STATUS_MAP;
1602
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   339
		/* Mark the start of download */
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   340
		cs->last_frame = _frame_counter;
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   341
		cs->last_frame_server = _frame_counter;
543
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
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   344
	if (cs->status == STATUS_MAP) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   345
		uint i;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   346
		int res;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   347
		for (i = 0; i < sent_packets; i++) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   348
			Packet *p = NetworkSend_Init(PACKET_SERVER_MAP);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   349
			p->Send_uint8(MAP_PACKET_NORMAL);
4321
958a8e9c012b (svn r5974) -Codechange: added casts all around the place to make Windows 64bit happy (michi_cc)
truelight
parents: 4300
diff changeset
   350
			res = (int)fread(p->buffer + p->size, 1, SEND_MTU - p->size, file_pointer);
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   351
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9451
diff changeset
   352
			if (ferror(file_pointer)) usererror("Error reading temporary network savegame!");
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   353
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   354
			p->size += res;
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   355
			cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   356
			if (feof(file_pointer)) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   357
				// Done reading!
3121
72fb0554f9f1 (svn r3726) - [6/6] Finalize conversion, finally save the patches struct.
Darkvater
parents: 3117
diff changeset
   358
				Packet *p = NetworkSend_Init(PACKET_SERVER_MAP);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   359
				p->Send_uint8(MAP_PACKET_END);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   360
				cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   361
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   362
				// Set the status to DONE_MAP, no we will wait for the client
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   363
				//  to send it is ready (maybe that happens like never ;))
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   364
				cs->status = STATUS_DONE_MAP;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   365
				fclose(file_pointer);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   366
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   367
				{
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
   368
					NetworkTCPSocketHandler *new_cs;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   369
					bool new_map_client = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   370
					// Check if there is a client waiting for receiving the map
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   371
					//  and start sending him the map
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   372
					FOR_ALL_CLIENTS(new_cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   373
						if (new_cs->status == STATUS_MAP_WAIT) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   374
							// Check if we already have a new client to send the map to
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   375
							if (!new_map_client) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   376
								// If not, this client will get the map
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   377
								new_cs->status = STATUS_AUTH;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   378
								new_map_client = true;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   379
								SEND_COMMAND(PACKET_SERVER_MAP)(new_cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   380
							} else {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   381
								// Else, send the other clients how many clients are in front of them
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   382
								SEND_COMMAND(PACKET_SERVER_WAIT)(new_cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   383
							}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   384
						}
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
				}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   387
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   388
				// There is no more data, so break the for
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   389
				break;
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
		}
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
		// Send all packets (forced) and check if we have send it all
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   394
		cs->Send_Packets();
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   395
		if (cs->IsPacketQueueEmpty()) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   396
			// All are sent, increase the sent_packets
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   397
			sent_packets *= 2;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   398
		} else {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   399
			// Not everything is sent, decrease the sent_packets
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   400
			if (sent_packets > 1) sent_packets /= 2;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   401
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   402
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   403
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   404
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
   405
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_JOIN)(NetworkTCPSocketHandler *cs, uint16 client_index)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   406
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   407
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   408
	// Packet: SERVER_JOIN
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   409
	// Function: A client is joined (all active clients receive this after a
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   410
	//     PACKET_CLIENT_MAP_OK) Mostly what directly follows is a
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   411
	//     PACKET_SERVER_CLIENT_INFO
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   412
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   413
	//    uint16:  Client-Index
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   414
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   415
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   416
	Packet *p = NetworkSend_Init(PACKET_SERVER_JOIN);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   417
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   418
	p->Send_uint16(client_index);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   419
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   420
	cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   421
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   422
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   423
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   424
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_FRAME)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   425
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   426
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   427
	// Packet: SERVER_FRAME
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   428
	// Function: Sends the current frame-counter to the client
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   429
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   430
	//    uint32: Frame Counter
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   431
	//    uint32: Frame Counter Max (how far may the client walk before the server?)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   432
	//    [uint32: general-seed-1]
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   433
	//    [uint32: general-seed-2]
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   434
	//      (last two depends on compile-settings, and are not default settings)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   435
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   436
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   437
	Packet *p = NetworkSend_Init(PACKET_SERVER_FRAME);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   438
	p->Send_uint32(_frame_counter);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   439
	p->Send_uint32(_frame_counter_max);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   440
#ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   441
	p->Send_uint32(_sync_seed_1);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   442
#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
   443
	p->Send_uint32(_sync_seed_2);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   444
#endif
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   445
#endif
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   446
	cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   447
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   448
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   449
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_SYNC)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   450
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   451
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   452
	// Packet: SERVER_SYNC
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   453
	// Function: Sends a sync-check to the client
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   454
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   455
	//    uint32: Frame Counter
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   456
	//    uint32: General-seed-1
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   457
	//    [uint32: general-seed-2]
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   458
	//      (last one depends on compile-settings, and are not default settings)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   459
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   460
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   461
	Packet *p = NetworkSend_Init(PACKET_SERVER_SYNC);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   462
	p->Send_uint32(_frame_counter);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   463
	p->Send_uint32(_sync_seed_1);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   464
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   465
#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
   466
	p->Send_uint32(_sync_seed_2);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   467
#endif
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   468
	cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   469
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   470
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
   471
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_COMMAND)(NetworkTCPSocketHandler *cs, CommandPacket *cp)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   472
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   473
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   474
	// Packet: SERVER_COMMAND
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   475
	// Function: Sends a DoCommand to the client
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   476
	// 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
   477
	//    uint8:  CompanyID (0..MAX_COMPANIES-1)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   478
	//    uint32: CommandID (see command.h)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   479
	//    uint32: P1 (free variables used in DoCommand)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   480
	//    uint32: P2
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   481
	//    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: 1805
diff changeset
   482
	//    string: text
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   483
	//    uint8:  CallBackID (see callback_table.c)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   484
	//    uint32: Frame of execution
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   485
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   486
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   487
	Packet *p = NetworkSend_Init(PACKET_SERVER_COMMAND);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   488
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
   489
	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
   490
	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
   491
	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
   492
	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
   493
	p->Send_uint32(cp->tile);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   494
	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
   495
	p->Send_uint8 (cp->callback);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   496
	p->Send_uint32(cp->frame);
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: 7140
diff changeset
   497
	p->Send_bool  (cp->my_cmd);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   498
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   499
	cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   500
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   501
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
   502
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CHAT)(NetworkTCPSocketHandler *cs, NetworkAction action, uint16 client_index, bool self_send, const char *msg)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   503
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   504
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   505
	// Packet: SERVER_CHAT
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   506
	// Function: Sends a chat-packet to the client
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   507
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   508
	//    uint8:  ActionID (see network_data.h, NetworkAction)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   509
	//    uint16:  Client-index
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: 9676
diff changeset
   510
	//    String: Message (max NETWORK_CHAT_LENGTH)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   511
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   512
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   513
	Packet *p = NetworkSend_Init(PACKET_SERVER_CHAT);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   514
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   515
	p->Send_uint8 (action);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   516
	p->Send_uint16(client_index);
5918
f06b73812d5f (svn r8546) -Codechange: add a seperate (wrapper) functions to send/receive booleans.
rubidium
parents: 5902
diff changeset
   517
	p->Send_bool  (self_send);
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   518
	p->Send_string(msg);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   519
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   520
	cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   521
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   522
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
   523
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR_QUIT)(NetworkTCPSocketHandler *cs, uint16 client_index, NetworkErrorCode errorno)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   524
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   525
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   526
	// Packet: SERVER_ERROR_QUIT
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   527
	// Function: One of the clients made an error and is quiting the game
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   528
	//      This packet informs the other clients of that.
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   529
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   530
	//    uint16:  Client-index
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   531
	//    uint8:  ErrorID (see network_data.h, NetworkErrorCode)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   532
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   533
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   534
	Packet *p = NetworkSend_Init(PACKET_SERVER_ERROR_QUIT);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   535
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   536
	p->Send_uint16(client_index);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   537
	p->Send_uint8 (errorno);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   538
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   539
	cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   540
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   541
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
   542
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_QUIT)(NetworkTCPSocketHandler *cs, uint16 client_index, const char *leavemsg)
543
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
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   545
	// Packet: SERVER_ERROR_QUIT
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   546
	// Function: A client left the game, and this packets informs the other clients
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   547
	//      of that.
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   548
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   549
	//    uint16:  Client-index
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   550
	//    String: leave-message
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   551
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   552
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   553
	Packet *p = NetworkSend_Init(PACKET_SERVER_QUIT);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   554
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   555
	p->Send_uint16(client_index);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   556
	p->Send_string(leavemsg);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   557
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   558
	cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   559
}
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
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_SHUTDOWN)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   562
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   563
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   564
	// Packet: SERVER_SHUTDOWN
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   565
	// Function: Let the clients know that the server is closing
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   566
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   567
	//     <none>
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   568
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   569
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   570
	Packet *p = NetworkSend_Init(PACKET_SERVER_SHUTDOWN);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   571
	cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   572
}
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
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_NEWGAME)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   575
{
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
	// Packet: PACKET_SERVER_NEWGAME
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   578
	// Function: Let the clients know that the server is loading a new map
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   579
	// Data:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   580
	//     <none>
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   581
	//
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   582
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   583
	Packet *p = NetworkSend_Init(PACKET_SERVER_NEWGAME);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   584
	cs->Send_Packet(p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   585
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   586
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
   587
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_RCON)(NetworkTCPSocketHandler *cs, uint16 color, const char *command)
1026
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
   588
{
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
   589
	Packet *p = NetworkSend_Init(PACKET_SERVER_RCON);
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
   590
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   591
	p->Send_uint16(color);
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   592
	p->Send_string(command);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
   593
	cs->Send_Packet(p);
1026
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
   594
}
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
   595
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   596
// **********
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   597
// Receiving functions
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
   598
//   DEF_SERVER_RECEIVE_COMMAND has parameter: NetworkTCPSocketHandler *cs, Packet *p
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   599
// **********
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   600
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   601
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   602
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   603
	SEND_COMMAND(PACKET_SERVER_COMPANY_INFO)(cs);
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
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: 5854
diff changeset
   606
DEF_SERVER_RECEIVE_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: 5854
diff changeset
   607
{
9474
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
   608
	if (cs->status != STATUS_INACTIVE) {
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
   609
		/* Illegal call, return error and ignore the packet */
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
   610
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
   611
		return;
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
   612
	}
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
   613
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: 5854
diff changeset
   614
	NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
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: 5854
diff changeset
   615
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: 5854
diff changeset
   616
	/* We now want a password from the client else we do not allow him in! */
9451
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
   617
	if (!StrEmpty(_settings_client.network.server_password)) {
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: 5854
diff changeset
   618
		SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_GAME_PASSWORD);
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: 5854
diff changeset
   619
	} 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
   620
		if (IsValidCompanyID(ci->client_playas) && _network_company_info[ci->client_playas].password[0] != '\0') {
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: 5854
diff changeset
   621
			SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD);
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: 5854
diff changeset
   622
		} 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: 5854
diff changeset
   623
			SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
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: 5854
diff changeset
   624
		}
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: 5854
diff changeset
   625
	}
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: 5854
diff changeset
   626
}
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: 5854
diff changeset
   627
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   628
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   629
{
9474
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
   630
	if (cs->status != STATUS_INACTIVE) {
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
   631
		/* Illegal call, return error and ignore the packet */
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
   632
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
   633
		return;
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
   634
	}
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
   635
3623
9b612a4bbd39 (svn r4521) - Fix: be consistent about the size of the player-name in MP. This hopefully fixes a crash on lesser OS's (eg Win98). Use ttd_strlcpy() to just copy over strings and properly terminate them because different implementations of snprintf() behave differently. Courtesy of TrueLight
Darkvater
parents: 3437
diff changeset
   636
	char name[NETWORK_CLIENT_NAME_LENGTH];
7807
b10fc6c5d081 (svn r11357) -Fix: NetworkUniqueID could be 80 chars, while the max size we generate is 32. So reduce the size a bit. Pointed out by dihedral, so give him a big hug ;)
truelight
parents: 7495
diff changeset
   637
	char unique_id[NETWORK_UNIQUE_ID_LENGTH];
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   638
	NetworkClientInfo *ci;
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
	CompanyID playas;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   640
	NetworkLanguage client_lang;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   641
	char client_revision[NETWORK_REVISION_LENGTH];
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   642
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   643
	p->Recv_string(client_revision, sizeof(client_revision));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   644
659
a9733f856cd0 (svn r1093) -Fix: Hopefully fixed windows revision issues once and for all. Removed globalness of _openttd_revision and put all such ifdefs into one place. If server has a revision only the same revisions can join; if the server has no revision everyone can join. I reckon this should be a server-side option to allow people to join or not to join.
darkvater
parents: 656
diff changeset
   645
	// Check if the client has revision control enabled
6178
c29a7d37c3ce (svn r8949) -Codechange: only test the first NETWORK_REVISION_LENGTH - 1 characters when determining network compatability. This makes it possible to have 'long' branch names while still being able to play network games.
rubidium
parents: 5918
diff changeset
   646
	if (!IsNetworkCompatibleVersion(client_revision)) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   647
		// Different revisions!!
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   648
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_REVISION);
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   649
		return;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   650
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   651
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   652
	p->Recv_string(name, sizeof(name));
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   653
	playas = (Owner)p->Recv_uint8();
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   654
	client_lang = (NetworkLanguage)p->Recv_uint8();
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   655
	p->Recv_string(unique_id, sizeof(unique_id));
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   656
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   657
	if (cs->has_quit) return;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   658
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
   659
	// join another company does not affect these values
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
   660
	switch (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
   661
		case COMPANY_NEW_COMPANY: /* New company */
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
   662
			if (ActiveCompanyCount() >= _settings_client.network.max_companies) {
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
   663
				SEND_COMMAND(PACKET_SERVER_ERROR)(cs, 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
   664
				return;
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
   665
			}
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
   666
			break;
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
   667
		case COMPANY_SPECTATOR: /* Spectator */
9451
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
   668
			if (NetworkSpectatorCount() >= _settings_client.network.max_spectators) {
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
   669
				SEND_COMMAND(PACKET_SERVER_ERROR)(cs, 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
   670
				return;
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
   671
			}
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
   672
			break;
4878
4d4f76a898fd (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
   673
		default: /* Join another company (companies 1-8 (index 0-7)) */
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
   674
			if (!IsValidCompanyID(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
   675
				SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_COMPANY_MISMATCH);
4861
2119f5d46e41 (svn r6787) -Codechange: Use PLAYER_NEW_COMPANY as a player identifier wishing to become a
Darkvater
parents: 4860
diff changeset
   676
				return;
2119f5d46e41 (svn r6787) -Codechange: Use PLAYER_NEW_COMPANY as a player identifier wishing to become a
Darkvater
parents: 4860
diff changeset
   677
			}
2119f5d46e41 (svn r6787) -Codechange: Use PLAYER_NEW_COMPANY as a player identifier wishing to become a
Darkvater
parents: 4860
diff changeset
   678
			break;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   679
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   680
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
   681
	// We need a valid name.. make it Player
10310
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10208
diff changeset
   682
	if (StrEmpty(name)) strecpy(name, "Player", lastof(name));
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
   683
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
   684
	if (!NetworkFindName(name)) { // Change name if duplicate
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
   685
		// We could not create a name for this client
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   686
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NAME_IN_USE);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   687
		return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   688
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   689
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   690
	ci = DEREF_CLIENT_INFO(cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   691
10310
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10208
diff changeset
   692
	strecpy(ci->client_name, name, lastof(ci->client_name));
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10208
diff changeset
   693
	strecpy(ci->unique_id, unique_id, lastof(ci->unique_id));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   694
	ci->client_playas = playas;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   695
	ci->client_lang = client_lang;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   696
4878
4d4f76a898fd (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
   697
	/* Make sure companies to which people try to join are not autocleaned */
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
   698
	if (IsValidCompanyID(playas)) _network_company_info[playas].months_empty = 0;
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: 5854
diff changeset
   699
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: 5854
diff changeset
   700
	if (_grfconfig == 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: 5854
diff changeset
   701
		RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)(cs, 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: 5854
diff changeset
   702
	} 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: 5854
diff changeset
   703
		SEND_COMMAND(PACKET_SERVER_CHECK_NEWGRFS)(cs);
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: 5854
diff changeset
   704
	}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   705
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   706
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   707
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   708
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   709
	NetworkPasswordType type;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   710
	char password[NETWORK_PASSWORD_LENGTH];
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   711
	const NetworkClientInfo *ci;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   712
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   713
	type = (NetworkPasswordType)p->Recv_uint8();
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   714
	p->Recv_string(password, sizeof(password));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   715
6252
691c889970d6 (svn r9061) -Fix [r9038, FS#668]: passwords are send when the server is in the 'authorizing' state, not 'inactive'.
rubidium
parents: 6247
diff changeset
   716
	if (cs->status == STATUS_AUTHORIZING && type == NETWORK_GAME_PASSWORD) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   717
		// Check game-password
9451
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
   718
		if (strcmp(password, _settings_client.network.server_password) != 0) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   719
			// Password is invalid
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   720
			SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   721
			return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   722
		}
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 = DEREF_CLIENT_INFO(cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   725
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
   726
		if (IsValidCompanyID(ci->client_playas) && _network_company_info[ci->client_playas].password[0] != '\0') {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   727
			SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   728
			return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   729
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   730
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   731
		// Valid password, allow user
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   732
		SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   733
		return;
6252
691c889970d6 (svn r9061) -Fix [r9038, FS#668]: passwords are send when the server is in the 'authorizing' state, not 'inactive'.
rubidium
parents: 6247
diff changeset
   734
	} else if (cs->status == STATUS_AUTHORIZING && type == NETWORK_COMPANY_PASSWORD) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   735
		ci = DEREF_CLIENT_INFO(cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   736
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
   737
		if (strcmp(password, _network_company_info[ci->client_playas].password) != 0) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   738
			// Password is invalid
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   739
			SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   740
			return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   741
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   742
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   743
		SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   744
		return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   745
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   746
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   747
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   748
	SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   749
	return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   750
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   751
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   752
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_GETMAP)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   753
{
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
   754
	NetworkTCPSocketHandler *new_cs;
543
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
	// The client was never joined.. so this is impossible, right?
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   757
	//  Ignore the packet, give the client a warning, and close his connection
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   758
	if (cs->status < STATUS_AUTH || cs->has_quit) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   759
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   760
		return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   761
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   762
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   763
	// Check if someone else is receiving the map
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   764
	FOR_ALL_CLIENTS(new_cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   765
		if (new_cs->status == STATUS_MAP) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   766
			// Tell the new client to wait
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   767
			cs->status = STATUS_MAP_WAIT;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   768
			SEND_COMMAND(PACKET_SERVER_WAIT)(cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   769
			return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   770
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   771
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   772
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   773
	// We receive a request to upload the map.. give it to the client!
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   774
	SEND_COMMAND(PACKET_SERVER_MAP)(cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   775
}
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
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   778
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   779
	// Client has the map, now start syncing
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   780
	if (cs->status == STATUS_DONE_MAP && !cs->has_quit) {
793
7bee9006a465 (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
   781
		char client_name[NETWORK_CLIENT_NAME_LENGTH];
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
   782
		NetworkTCPSocketHandler *new_cs;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   783
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   784
		NetworkGetClientName(client_name, sizeof(client_name), cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   785
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
   786
		NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, client_name, "");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   787
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   788
		// Mark the client as pre-active, and wait for an ACK
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   789
		//  so we know he is done loading and in sync with us
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   790
		cs->status = STATUS_PRE_ACTIVE;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   791
		NetworkHandleCommandQueue(cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   792
		SEND_COMMAND(PACKET_SERVER_FRAME)(cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   793
		SEND_COMMAND(PACKET_SERVER_SYNC)(cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   794
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   795
		// This is the frame the client receives
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   796
		//  we need it later on to make sure the client is not too slow
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   797
		cs->last_frame = _frame_counter;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   798
		cs->last_frame_server = _frame_counter;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   799
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   800
		FOR_ALL_CLIENTS(new_cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   801
			if (new_cs->status > STATUS_AUTH) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   802
				SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(new_cs, DEREF_CLIENT_INFO(cs));
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   803
				SEND_COMMAND(PACKET_SERVER_JOIN)(new_cs, cs->index);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   804
			}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   805
		}
1602
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   806
9420
8a38703928e8 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 9413
diff changeset
   807
		if (_settings_client.network.pause_on_join) {
1602
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   808
			/* Now pause the game till the client is in sync */
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   809
			DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   810
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
   811
			NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game paused (incoming client)", NETWORK_SERVER_INDEX);
1602
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   812
		}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   813
	} else {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   814
		// Wrong status for this packet, give a warning to client, and close connection
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   815
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   816
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   817
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   818
1804
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   819
/** Enforce the command flags.
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   820
 * Eg a server-only command can only be executed by a server, etc.
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   821
 * @param *cp the commandpacket that is going to be checked
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   822
 * @param *ci client information for debugging output to console
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   823
 */
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   824
static bool CheckCommandFlags(const CommandPacket *cp, const NetworkClientInfo *ci)
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   825
{
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   826
	byte flags = GetCommandFlags(cp->cmd);
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   827
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   828
	if (flags & CMD_SERVER && ci->client_index != NETWORK_SERVER_INDEX) {
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
   829
		IConsolePrintF(CC_ERROR, "WARNING: server only command from client %d (IP: %s), kicking...", ci->client_index, GetClientIP(ci));
1804
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   830
		return false;
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   831
	}
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   832
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   833
	if (flags & CMD_OFFLINE) {
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
		IConsolePrintF(CC_ERROR, "WARNING: offline only command from client %d (IP: %s), kicking...", ci->client_index, GetClientIP(ci));
1804
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   835
		return false;
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   836
	}
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   837
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
   838
	if (cp->cmd != CMD_COMPANY_CTRL && !IsValidCompanyID(cp->company) && ci->client_index != NETWORK_SERVER_INDEX) {
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
   839
		IConsolePrintF(CC_ERROR, "WARNING: spectator issueing command from client %d (IP: %s), kicking...", ci->client_index, GetClientIP(ci));
7495
70dbeb7e2f7a (svn r11006) -Fix: spectators are not allowed to issue commands. Issue spotted by SmatZ.
rubidium
parents: 7222
diff changeset
   840
		return false;
70dbeb7e2f7a (svn r11006) -Fix: spectators are not allowed to issue commands. Issue spotted by SmatZ.
rubidium
parents: 7222
diff changeset
   841
	}
70dbeb7e2f7a (svn r11006) -Fix: spectators are not allowed to issue commands. Issue spotted by SmatZ.
rubidium
parents: 7222
diff changeset
   842
1804
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   843
	return true;
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   844
}
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   845
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   846
/** The client has done a command and wants us to handle it
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   847
 * @param *cs the connected client that has sent the command
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   848
 * @param *p the packet in which the command was sent
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   849
 */
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   850
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   851
{
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
   852
	NetworkTCPSocketHandler *new_cs;
1804
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   853
	const NetworkClientInfo *ci;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   854
	byte callback;
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
	// The client was never joined.. so this is impossible, right?
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   857
	//  Ignore the packet, give the client a warning, and close his connection
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   858
	if (cs->status < STATUS_DONE_MAP || cs->has_quit) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   859
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   860
		return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   861
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   862
6836
c8f93252c5d0 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 6621
diff changeset
   863
	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
   864
	cp->company = (CompanyID)p->Recv_uint8();
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
   865
	cp->cmd     = 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
   866
	cp->p1      = 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
   867
	cp->p2      = 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
   868
	cp->tile    = p->Recv_uint32();
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   869
	p->Recv_string(cp->text, lengthof(cp->text));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   870
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   871
	callback = p->Recv_uint8();
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   872
6836
c8f93252c5d0 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 6621
diff changeset
   873
	if (cs->has_quit) {
c8f93252c5d0 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 6621
diff changeset
   874
		free(cp);
c8f93252c5d0 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 6621
diff changeset
   875
		return;
c8f93252c5d0 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 6621
diff changeset
   876
	}
1804
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   877
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   878
	ci = DEREF_CLIENT_INFO(cs);
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   879
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   880
	/* Check if cp->cmd is valid */
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   881
	if (!IsValidCommand(cp->cmd)) {
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
   882
		IConsolePrintF(CC_ERROR, "WARNING: invalid command from client %d (IP: %s).", ci->client_index, GetClientIP(ci));
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   883
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
6836
c8f93252c5d0 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 6621
diff changeset
   884
		free(cp);
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   885
		return;
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   886
	}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   887
1804
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   888
	if (!CheckCommandFlags(cp, ci)) {
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   889
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_KICKED);
6836
c8f93252c5d0 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 6621
diff changeset
   890
		free(cp);
1804
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   891
		return;
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   892
	}
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   893
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
   894
	/** Only CMD_COMPANY_CTRL is always allowed, for the rest, playas needs
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
   895
	 * to match the company in the packet. If it doesn't, the client has done
1804
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   896
	 * something pretty naughty (or a bug), and will be kicked
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   897
	 */
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
   898
	if (!(cp->cmd == CMD_COMPANY_CTRL && cp->p1 == 0 && ci->client_playas == COMPANY_NEW_COMPANY) && ci->client_playas != cp->company) {
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
   899
		IConsolePrintF(CC_ERROR, "WARNING: client %d (IP: %s) tried to execute a command as company %d, kicking...",
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
   900
		               ci->client_playas + 1, GetClientIP(ci), cp->company + 1);
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
   901
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_COMPANY_MISMATCH);
6836
c8f93252c5d0 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 6621
diff changeset
   902
		free(cp);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   903
		return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   904
	}
748
fa61195ee8a8 (svn r1204) -Add: [Network] Added some cheaters-protection (money-cheat mostly)
truelight
parents: 734
diff changeset
   905
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
   906
	/** @todo CMD_COMPANY_CTRL with p1 = 0 announces a new company to the server. To give the
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
   907
	 * company the correct ID, the server injects p2 and executes the command. Any other p1
1804
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   908
	 * is prohibited. Pretty ugly and should be redone together with its function.
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
   909
	 * @see CmdCompanyCtrl()
1804
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   910
	 */
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
   911
	if (cp->cmd == CMD_COMPANY_CTRL) {
1804
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   912
		if (cp->p1 != 0) {
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   913
			SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_CHEATER);
6836
c8f93252c5d0 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 6621
diff changeset
   914
			free(cp);
1804
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   915
			return;
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   916
		}
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   917
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
   918
		/* XXX - Execute the command as a valid company. Normally this would be done by a
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   919
		 * spectator, but that is not allowed any commands. So do an impersonation. The drawback
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   920
		 * of this is that the first company's last_built_tile is also updated... */
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
   921
		cp->company = OWNER_BEGIN;
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
   922
		cp->p2 = cs - _clients; // XXX - UGLY! p2 is mis-used to get the client-id in CmdCompanyCtrl
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   923
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   924
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   925
	// The frame can be executed in the same frame as the next frame-packet
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   926
	//  That frame just before that frame is saved in _frame_counter_max
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   927
	cp->frame = _frame_counter_max + 1;
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   928
	cp->next  = NULL;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   929
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   930
	// Queue the command for the clients (are send at the end of the frame
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   931
	//   if they can handle it ;))
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   932
	FOR_ALL_CLIENTS(new_cs) {
5381
1f4ef7e8330f (svn r7566) -Fix: only send commands to the new client from the map, that is going to be transfered to the new client, is saved on the server and not while waiting for the 'map download slot' as that will add commands for already passed frames in the command queue of the new client.
rubidium
parents: 5380
diff changeset
   933
		if (new_cs->status >= STATUS_MAP) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   934
			// Callbacks are only send back to the client who sent them in the
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   935
			//  first place. This filters that out.
1804
fbe724472bb4 (svn r2308) - Fix: enforce server-only and/or offline commands by giving them flags in the process table. This also fixes bug "[ 1190944 ] Many commands not checked for security"
Darkvater
parents: 1795
diff changeset
   936
			cp->callback = (new_cs != cs) ? 0 : callback;
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: 7140
diff changeset
   937
			cp->my_cmd = (new_cs == cs);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   938
			NetworkAddCommandQueue(new_cs, cp);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   939
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   940
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   941
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   942
	cp->callback = 0;
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: 7140
diff changeset
   943
	cp->my_cmd = false;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   944
	// Queue the command on the server
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   945
	if (_local_command_queue == NULL) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   946
		_local_command_queue = cp;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   947
	} else {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   948
		// Find last packet
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   949
		CommandPacket *c = _local_command_queue;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   950
		while (c->next != NULL) c = c->next;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   951
		c->next = cp;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   952
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   953
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   954
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   955
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ERROR)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   956
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   957
	// This packets means a client noticed an error and is reporting this
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   958
	//  to us. Display the error and report it to the other clients
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
   959
	NetworkTCPSocketHandler *new_cs;
3437
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 3419
diff changeset
   960
	char str[100];
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   961
	char client_name[NETWORK_CLIENT_NAME_LENGTH];
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
   962
	NetworkErrorCode errorno = (NetworkErrorCode)p->Recv_uint8();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   963
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   964
	// The client was never joined.. thank the client for the packet, but ignore it
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   965
	if (cs->status < STATUS_DONE_MAP || cs->has_quit) {
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   966
		cs->has_quit = true;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   967
		return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   968
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   969
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   970
	NetworkGetClientName(client_name, sizeof(client_name), cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   971
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4906
diff changeset
   972
	GetNetworkErrorMsg(str, errorno, lastof(str));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   973
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
   974
	DEBUG(net, 2, "'%s' reported an error and is closing its connection (%s)", client_name, str);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   975
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
   976
	NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "%s", str);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   977
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   978
	FOR_ALL_CLIENTS(new_cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   979
		if (new_cs->status > STATUS_AUTH) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   980
			SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, errorno);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   981
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   982
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   983
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   984
	cs->has_quit = true;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   985
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   986
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   987
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_QUIT)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   988
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   989
	// The client wants to leave. Display this and report it to the other
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   990
	//  clients.
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
   991
	NetworkTCPSocketHandler *new_cs;
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   992
	char str[100];
793
7bee9006a465 (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
   993
	char client_name[NETWORK_CLIENT_NAME_LENGTH];
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   994
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   995
	// The client was never joined.. thank the client for the packet, but ignore it
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   996
	if (cs->status < STATUS_DONE_MAP || cs->has_quit) {
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   997
		cs->has_quit = true;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   998
		return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   999
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1000
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
  1001
	p->Recv_string(str, lengthof(str));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1002
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1003
	NetworkGetClientName(client_name, sizeof(client_name), cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1004
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
  1005
	NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "%s", str);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1006
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1007
	FOR_ALL_CLIENTS(new_cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1008
		if (new_cs->status > STATUS_AUTH) {
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1009
			SEND_COMMAND(PACKET_SERVER_QUIT)(new_cs, cs->index, str);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1010
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1011
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1012
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1013
	cs->has_quit = true;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1014
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1015
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1016
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ACK)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1017
{
9474
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1018
	if (cs->status < STATUS_AUTH) {
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1019
		/* Illegal call, return error and ignore the packet */
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1020
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED);
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1021
		return;
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1022
	}
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1023
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
  1024
	uint32 frame = p->Recv_uint32();
1602
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1025
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1026
	/* The client is trying to catch up with the server */
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1027
	if (cs->status == STATUS_PRE_ACTIVE) {
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1028
		/* The client is not yet catched up? */
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1029
		if (frame + DAY_TICKS < _frame_counter) return;
1602
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1030
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1031
		/* Now he is! Unpause the game */
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1032
		cs->status = STATUS_ACTIVE;
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1033
9420
8a38703928e8 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 9413
diff changeset
  1034
		if (_settings_client.network.pause_on_join) {
1602
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1035
			DoCommandP(0, 0, 0, NULL, CMD_PAUSE);
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
  1036
			NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused (client connected)", NETWORK_SERVER_INDEX);
1602
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1037
		}
4713
29313f34d770 (svn r6625) - Feature: Add the ability for servers to execute a script just after a client has connected, e.g. for a MOTD, etc.
peter1138
parents: 4428
diff changeset
  1038
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
  1039
		CheckMinActiveClients();
4717
a614b951ed57 (svn r6629) - Fix (r6628): One check for the number of players got misplaced by patch...
peter1138
parents: 4716
diff changeset
  1040
4713
29313f34d770 (svn r6625) - Feature: Add the ability for servers to execute a script just after a client has connected, e.g. for a MOTD, etc.
peter1138
parents: 4428
diff changeset
  1041
		/* Execute script for, e.g. MOTD */
29313f34d770 (svn r6625) - Feature: Add the ability for servers to execute a script just after a client has connected, e.g. for a MOTD, etc.
peter1138
parents: 4428
diff changeset
  1042
		IConsoleCmdExec("exec scripts/on_server_connect.scr 0");
1602
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1043
	}
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1044
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1045
	// The client received the frame, make note of it
1602
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1046
	cs->last_frame = frame;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1047
	// With those 2 values we can calculate the lag realtime
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1048
	cs->last_frame_server = _frame_counter;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1049
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1050
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1051
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1052
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
  1053
void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, const char *msg, uint16 from_index)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1054
{
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
  1055
	NetworkTCPSocketHandler *cs;
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1056
	const NetworkClientInfo *ci, *ci_own, *ci_to;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1057
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1058
	switch (desttype) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1059
	case DESTTYPE_CLIENT:
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1060
		/* Are we sending to the server? */
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1061
		if (dest == NETWORK_SERVER_INDEX) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1062
			ci = NetworkFindClientInfoFromIndex(from_index);
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1063
			/* Display the text locally, and that is it */
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1064
			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
  1065
				NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), false, ci->client_name, "%s", msg);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1066
		} else {
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1067
			/* Else find the client to send the message to */
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1068
			FOR_ALL_CLIENTS(cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1069
				if (cs->index == dest) {
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1070
					SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_index, false, msg);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1071
					break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1072
				}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1073
			}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1074
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1075
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1076
		// Display the message locally (so you know you have sent it)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1077
		if (from_index != dest) {
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1078
			if (from_index == NETWORK_SERVER_INDEX) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1079
				ci = NetworkFindClientInfoFromIndex(from_index);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1080
				ci_to = NetworkFindClientInfoFromIndex(dest);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1081
				if (ci != NULL && ci_to != 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
  1082
					NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), true, ci_to->client_name, "%s", msg);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1083
			} else {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1084
				FOR_ALL_CLIENTS(cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1085
					if (cs->index == from_index) {
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1086
						SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, dest, true, msg);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1087
						break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1088
					}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1089
				}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1090
			}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1091
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1092
		break;
4906
935e6280c093 (svn r6876) -Codechange: Change the naming of _PLAYER in DESTTYPE_PLAYER/NETWORK_ACTION_CHAT_PLAYER
Darkvater
parents: 4880
diff changeset
  1093
	case DESTTYPE_TEAM: {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1094
		bool show_local = true; // If this is false, the message is already displayed
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1095
														// on the client who did sent it.
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
  1096
		/* Find all clients that belong to this company */
733
ac3d7e1b786e (svn r1185) -Fix: [Network] Send to team now results in correct company on the sender
truelight
parents: 722
diff changeset
  1097
		ci_to = NULL;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1098
		FOR_ALL_CLIENTS(cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1099
			ci = DEREF_CLIENT_INFO(cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1100
			if (ci->client_playas == dest) {
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1101
				SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_index, false, msg);
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1102
				if (cs->index == from_index) show_local = false;
733
ac3d7e1b786e (svn r1185) -Fix: [Network] Send to team now results in correct company on the sender
truelight
parents: 722
diff changeset
  1103
				ci_to = ci; // Remember a client that is in the company for company-name
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1104
			}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1105
		}
733
ac3d7e1b786e (svn r1185) -Fix: [Network] Send to team now results in correct company on the sender
truelight
parents: 722
diff changeset
  1106
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1107
		ci = NetworkFindClientInfoFromIndex(from_index);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1108
		ci_own = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1109
		if (ci != NULL && ci_own != NULL && ci_own->client_playas == dest) {
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
  1110
			NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), false, ci->client_name, "%s", msg);
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1111
			if (from_index == NETWORK_SERVER_INDEX) show_local = false;
1834
52de5b434670 (svn r2339) - Fix: on a dedicated server, after the joining of a player, the player was set to player 0 instead of remaining OWNER_SPECTATOR. Fix this, and allow the dedicated server to execute commands (so 'patch <value>') now works and not only when a client is connected.
Darkvater
parents: 1820
diff changeset
  1112
			ci_to = ci_own;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1113
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1114
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
  1115
		/* There is no such client */
1834
52de5b434670 (svn r2339) - Fix: on a dedicated server, after the joining of a player, the player was set to player 0 instead of remaining OWNER_SPECTATOR. Fix this, and allow the dedicated server to execute commands (so 'patch <value>') now works and not only when a client is connected.
Darkvater
parents: 1820
diff changeset
  1116
		if (ci_to == NULL) break;
733
ac3d7e1b786e (svn r1185) -Fix: [Network] Send to team now results in correct company on the sender
truelight
parents: 722
diff changeset
  1117
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1118
		// Display the message locally (so you know you have sent it)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1119
		if (ci != NULL && show_local) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1120
			if (from_index == NETWORK_SERVER_INDEX) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1121
				char name[NETWORK_NAME_LENGTH];
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
  1122
				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: 6952
diff changeset
  1123
				SetDParam(0, ci_to->client_playas);
4945
d20e68851e70 (svn r6933) -Feature: Allow spectators to speak to eachother (team)
Darkvater
parents: 4944
diff changeset
  1124
				GetString(name, str, lastof(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
  1125
				NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci_own->client_playas), true, name, "%s", msg);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1126
			} else {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1127
				FOR_ALL_CLIENTS(cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1128
					if (cs->index == from_index) {
779
9f1c1091b9c8 (svn r1246) -Fix: [ 1090099 ] Company-messages did crash the game, because
truelight
parents: 748
diff changeset
  1129
						SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, ci_to->client_index, true, msg);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1130
					}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1131
				}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1132
			}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1133
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1134
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1135
		break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1136
	default:
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
  1137
		DEBUG(net, 0, "[server] received unknown chat destination type %d. Doing broadcast instead", desttype);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1138
		/* fall-through to next case */
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1139
	case DESTTYPE_BROADCAST:
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1140
		FOR_ALL_CLIENTS(cs) {
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1141
			SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_index, false, msg);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1142
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1143
		ci = NetworkFindClientInfoFromIndex(from_index);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1144
		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
  1145
			NetworkTextMessage(action, (ConsoleColour)GetDrawStringCompanyColor(ci->client_playas), false, ci->client_name, "%s", msg);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1146
		break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1147
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1148
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1149
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1150
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_CHAT)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1151
{
9474
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1152
	if (cs->status < STATUS_AUTH) {
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1153
		/* Illegal call, return error and ignore the packet */
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1154
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED);
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1155
		return;
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1156
	}
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1157
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
  1158
	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
  1159
	DestType desttype = (DestType)p->Recv_uint8();
6531
4445b25832c4 (svn r9719) -Fix: in-game private messages did not work for clients with a Client ID > 255.
rubidium
parents: 6259
diff changeset
  1160
	int dest = p->Recv_uint16();
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: 9676
diff changeset
  1161
	char msg[NETWORK_CHAT_LENGTH];
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1162
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: 9676
diff changeset
  1163
	p->Recv_string(msg, NETWORK_CHAT_LENGTH);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1164
9675
f76b3b37bf5a (svn r13775) -Codechange: enforce the validity of a NetworkAction (chat packet) issued by a client
glx
parents: 9673
diff changeset
  1165
	const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
f76b3b37bf5a (svn r13775) -Codechange: enforce the validity of a NetworkAction (chat packet) issued by a client
glx
parents: 9673
diff changeset
  1166
	switch (action) {
f76b3b37bf5a (svn r13775) -Codechange: enforce the validity of a NetworkAction (chat packet) issued by a client
glx
parents: 9673
diff changeset
  1167
		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
  1168
			if (!IsValidCompanyID(ci->client_playas)) break;
9675
f76b3b37bf5a (svn r13775) -Codechange: enforce the validity of a NetworkAction (chat packet) issued by a client
glx
parents: 9673
diff changeset
  1169
			/* Fall-through */
f76b3b37bf5a (svn r13775) -Codechange: enforce the validity of a NetworkAction (chat packet) issued by a client
glx
parents: 9673
diff changeset
  1170
		case NETWORK_ACTION_CHAT:
f76b3b37bf5a (svn r13775) -Codechange: enforce the validity of a NetworkAction (chat packet) issued by a client
glx
parents: 9673
diff changeset
  1171
		case NETWORK_ACTION_CHAT_CLIENT:
f76b3b37bf5a (svn r13775) -Codechange: enforce the validity of a NetworkAction (chat packet) issued by a client
glx
parents: 9673
diff changeset
  1172
		case NETWORK_ACTION_CHAT_COMPANY:
f76b3b37bf5a (svn r13775) -Codechange: enforce the validity of a NetworkAction (chat packet) issued by a client
glx
parents: 9673
diff changeset
  1173
			NetworkServerSendChat(action, desttype, dest, msg, cs->index);
f76b3b37bf5a (svn r13775) -Codechange: enforce the validity of a NetworkAction (chat packet) issued by a client
glx
parents: 9673
diff changeset
  1174
			break;
f76b3b37bf5a (svn r13775) -Codechange: enforce the validity of a NetworkAction (chat packet) issued by a client
glx
parents: 9673
diff changeset
  1175
		default:
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
  1176
			IConsolePrintF(CC_ERROR, "WARNING: invalid chat action from client %d (IP: %s).", ci->client_index, GetClientIP(ci));
9675
f76b3b37bf5a (svn r13775) -Codechange: enforce the validity of a NetworkAction (chat packet) issued by a client
glx
parents: 9673
diff changeset
  1177
			SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
f76b3b37bf5a (svn r13775) -Codechange: enforce the validity of a NetworkAction (chat packet) issued by a client
glx
parents: 9673
diff changeset
  1178
			break;
f76b3b37bf5a (svn r13775) -Codechange: enforce the validity of a NetworkAction (chat packet) issued by a client
glx
parents: 9673
diff changeset
  1179
	}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1180
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1181
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1182
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1183
{
9474
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1184
	if (cs->status != STATUS_ACTIVE) {
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1185
		/* Illegal call, return error and ignore the packet */
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1186
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1187
		return;
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1188
	}
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1189
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1190
	char password[NETWORK_PASSWORD_LENGTH];
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1191
	const NetworkClientInfo *ci;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1192
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
  1193
	p->Recv_string(password, sizeof(password));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1194
	ci = DEREF_CLIENT_INFO(cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1195
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
  1196
	if (IsValidCompanyID(ci->client_playas)) {
10319
987cb9dc47b4 (svn r14564) -Fix (r14555): one more incorrect use of lastof()
smatz
parents: 10315
diff changeset
  1197
		strecpy(_network_company_info[ci->client_playas].password, password, lastof(_network_company_info[ci->client_playas].password));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1198
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1199
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1200
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1201
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1202
{
9474
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1203
	if (cs->status != STATUS_ACTIVE) {
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1204
		/* Illegal call, return error and ignore the packet */
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1205
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1206
		return;
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1207
	}
9add31759797 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 9470
diff changeset
  1208
793
7bee9006a465 (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1209
	char client_name[NETWORK_CLIENT_NAME_LENGTH];
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1210
	NetworkClientInfo *ci;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1211
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
  1212
	p->Recv_string(client_name, sizeof(client_name));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1213
	ci = DEREF_CLIENT_INFO(cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1214
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1215
	if (cs->has_quit) return;
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
  1216
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1217
	if (ci != NULL) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1218
		// Display change
793
7bee9006a465 (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1219
		if (NetworkFindName(client_name)) {
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
  1220
			NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, "%s", client_name);
10310
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10208
diff changeset
  1221
			strecpy(ci->client_name, client_name, lastof(ci->client_name));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1222
			NetworkUpdateClientInfo(ci->client_index);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1223
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1224
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1225
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1226
1026
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1227
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_RCON)
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1228
{
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1229
	char pass[NETWORK_PASSWORD_LENGTH];
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1230
	char command[NETWORK_RCONCOMMAND_LENGTH];
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1231
9451
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
  1232
	if (StrEmpty(_settings_client.network.rcon_password)) return;
1026
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1233
5900
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
  1234
	p->Recv_string(pass, sizeof(pass));
135d10dd7219 (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 5898
diff changeset
  1235
	p->Recv_string(command, sizeof(command));
1026
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1236
9451
0d6806ba5504 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 9428
diff changeset
  1237
	if (strcmp(pass, _settings_client.network.rcon_password) != 0) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
  1238
		DEBUG(net, 0, "[rcon] wrong password from client-id %d", cs->index);
1026
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1239
		return;
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1240
	}
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1241
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
  1242
	DEBUG(net, 0, "[rcon] client-id %d executed: '%s'", cs->index, command);
1026
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1243
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1244
	_redirect_console_to_client = cs->index;
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1245
	IConsoleCmdExec(command);
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1246
	_redirect_console_to_client = 0;
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1247
	return;
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1248
}
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1249
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1250
// The layout for the receive-functions by the server
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
  1251
typedef void NetworkServerPacket(NetworkTCPSocketHandler *cs, Packet *p);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1252
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1253
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1254
// This array matches PacketType. At an incoming
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1255
//  packet it is matches against this array
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1256
//  and that way the right function to handle that
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1257
//  packet is found.
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1258
static NetworkServerPacket* const _network_server_packet[] = {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1259
	NULL, /*PACKET_SERVER_FULL,*/
841
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 839
diff changeset
  1260
	NULL, /*PACKET_SERVER_BANNED,*/
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1261
	RECEIVE_COMMAND(PACKET_CLIENT_JOIN),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1262
	NULL, /*PACKET_SERVER_ERROR,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1263
	RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1264
	NULL, /*PACKET_SERVER_COMPANY_INFO,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1265
	NULL, /*PACKET_SERVER_CLIENT_INFO,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1266
	NULL, /*PACKET_SERVER_NEED_PASSWORD,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1267
	RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1268
	NULL, /*PACKET_SERVER_WELCOME,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1269
	RECEIVE_COMMAND(PACKET_CLIENT_GETMAP),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1270
	NULL, /*PACKET_SERVER_WAIT,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1271
	NULL, /*PACKET_SERVER_MAP,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1272
	RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1273
	NULL, /*PACKET_SERVER_JOIN,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1274
	NULL, /*PACKET_SERVER_FRAME,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1275
	NULL, /*PACKET_SERVER_SYNC,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1276
	RECEIVE_COMMAND(PACKET_CLIENT_ACK),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1277
	RECEIVE_COMMAND(PACKET_CLIENT_COMMAND),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1278
	NULL, /*PACKET_SERVER_COMMAND,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1279
	RECEIVE_COMMAND(PACKET_CLIENT_CHAT),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1280
	NULL, /*PACKET_SERVER_CHAT,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1281
	RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1282
	RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1283
	RECEIVE_COMMAND(PACKET_CLIENT_QUIT),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1284
	RECEIVE_COMMAND(PACKET_CLIENT_ERROR),
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1285
	NULL, /*PACKET_SERVER_QUIT,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1286
	NULL, /*PACKET_SERVER_ERROR_QUIT,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1287
	NULL, /*PACKET_SERVER_SHUTDOWN,*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1288
	NULL, /*PACKET_SERVER_NEWGAME,*/
1026
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1289
	NULL, /*PACKET_SERVER_RCON,*/
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1290
	RECEIVE_COMMAND(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: 5854
diff changeset
  1291
	NULL, /*PACKET_CLIENT_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: 5854
diff changeset
  1292
	RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED),
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1293
};
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1294
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1295
// 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
  1296
assert_compile(lengthof(_network_server_packet) == PACKET_END);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1297
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1298
// This update the company_info-stuff
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6235
diff changeset
  1299
void NetworkPopulateCompanyInfo()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1300
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1301
	char password[NETWORK_PASSWORD_LENGTH];
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
  1302
	const Company *c;
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1303
	const Vehicle *v;
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1304
	const Station *s;
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
  1305
	NetworkTCPSocketHandler *cs;
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1306
	const NetworkClientInfo *ci;
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1307
	uint i;
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1308
	uint16 months_empty;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1309
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
  1310
	for (CompanyID cid = COMPANY_FIRST; cid < MAX_COMPANIES; cid++) {
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
  1311
		if (!IsValidCompanyID(cid)) memset(&_network_company_info[cid], 0, sizeof(NetworkCompanyInfo));
9663
bcc853b3d1ac (svn r13739) -Fix (r13731): some variables would be erroneously zeroed, causing autoclean and company passwords not to work.
rubidium
parents: 9659
diff changeset
  1312
	}
9659
187142ff9b6c (svn r13731) -Codechange: make a pool of the array of players.
rubidium
parents: 9652
diff changeset
  1313
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
  1314
	FOR_ALL_COMPANIES(c) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1315
		// Clean the info but not the password
10310
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10208
diff changeset
  1316
		strecpy(password, _network_company_info[c->index].password, lastof(password));
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
  1317
		months_empty = _network_company_info[c->index].months_empty;
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
  1318
		memset(&_network_company_info[c->index], 0, sizeof(NetworkCompanyInfo));
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
  1319
		_network_company_info[c->index].months_empty = months_empty;
10310
ca2eb5811a07 (svn r14555) -Codechange: replace ttd_strlcat and ttd_strlcpy with strecat and strecpy where direct conversion is possible
skidd13
parents: 10208
diff changeset
  1320
		strecpy(_network_company_info[c->index].password, password, lastof(_network_company_info[c->index].password));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1321
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1322
		// Grap the company 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
  1323
		SetDParam(0, c->index);
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
  1324
		GetString(_network_company_info[c->index].company_name, STR_COMPANY_NAME, lastof(_network_company_info[c->index].company_name));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1325
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1326
		// Check the income
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
  1327
		if (_cur_year - 1 == c->inaugurated_year) {
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10039
diff changeset
  1328
			// The company is here just 1 year, so display [2], else display[1]
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
  1329
			for (i = 0; i < lengthof(c->yearly_expenses[2]); i++) {
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
  1330
				_network_company_info[c->index].income -= c->yearly_expenses[2][i];
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1331
			}
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1332
		} 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
  1333
			for (i = 0; i < lengthof(c->yearly_expenses[1]); i++) {
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
  1334
				_network_company_info[c->index].income -= c->yearly_expenses[1][i];
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1335
			}
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1336
		}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1337
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1338
		// Set some general stuff
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
  1339
		_network_company_info[c->index].inaugurated_year = c->inaugurated_year;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10039
diff changeset
  1340
		_network_company_info[c->index].company_value = c->old_economy[0].company_value;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10039
diff changeset
  1341
		_network_company_info[c->index].money = c->money;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10039
diff changeset
  1342
		_network_company_info[c->index].performance = c->old_economy[0].performance_history;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1343
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1344
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1345
	// Go through all vehicles and count the type of vehicles
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1346
	FOR_ALL_VEHICLES(v) {
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
  1347
		if (!IsValidCompanyID(v->owner) || !v->IsPrimaryVehicle()) continue;
8280
bb1cd1c23f4b (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8270
diff changeset
  1348
		byte type = 0;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1349
		switch (v->type) {
8280
bb1cd1c23f4b (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8270
diff changeset
  1350
			case VEH_TRAIN: type = 0; break;
bb1cd1c23f4b (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8270
diff changeset
  1351
			case VEH_ROAD: type = (v->cargo_type != CT_PASSENGERS) ? 1 : 2; break;
bb1cd1c23f4b (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8270
diff changeset
  1352
			case VEH_AIRCRAFT: type = 3; break;
bb1cd1c23f4b (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8270
diff changeset
  1353
			case VEH_SHIP: type = 4; break;
bb1cd1c23f4b (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8270
diff changeset
  1354
			default: continue;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1355
		}
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
  1356
		_network_company_info[v->owner].num_vehicle[type]++;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1357
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1358
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1359
	// Go through all stations and count the types of stations
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1360
	FOR_ALL_STATIONS(s) {
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
  1361
		if (IsValidCompanyID(s->owner)) {
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
  1362
			NetworkCompanyInfo *npi = &_network_company_info[s->owner];
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1363
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1364
			if (s->facilities & FACIL_TRAIN)      npi->num_station[0]++;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1365
			if (s->facilities & FACIL_TRUCK_STOP) npi->num_station[1]++;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1366
			if (s->facilities & FACIL_BUS_STOP)   npi->num_station[2]++;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1367
			if (s->facilities & FACIL_AIRPORT)    npi->num_station[3]++;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1368
			if (s->facilities & FACIL_DOCK)       npi->num_station[4]++;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1369
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1370
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1371
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1372
	ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
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
  1373
	// Register local company (if not dedicated)
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
  1374
	if (ci != NULL && IsValidCompanyID(ci->client_playas))
10315
b3c652b3a316 (svn r14560) -Fix [FS#2396](r14555): lengthof() can't be simply replaced by lastof() in some cases (part by Aali)
smatz
parents: 10310
diff changeset
  1375
		strecpy(_network_company_info[ci->client_playas].clients, ci->client_name, lastof(_network_company_info[ci->client_playas].clients));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1376
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1377
	FOR_ALL_CLIENTS(cs) {
793
7bee9006a465 (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1378
		char client_name[NETWORK_CLIENT_NAME_LENGTH];
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1379
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1380
		NetworkGetClientName(client_name, sizeof(client_name), cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1381
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1382
		ci = DEREF_CLIENT_INFO(cs);
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
  1383
		if (ci != NULL && IsValidCompanyID(ci->client_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
  1384
			if (!StrEmpty(_network_company_info[ci->client_playas].clients)) {
10315
b3c652b3a316 (svn r14560) -Fix [FS#2396](r14555): lengthof() can't be simply replaced by lastof() in some cases (part by Aali)
smatz
parents: 10310
diff changeset
  1385
				strecat(_network_company_info[ci->client_playas].clients, ", ", lastof(_network_company_info[ci->client_playas].clients));
5638
ee1871005c80 (svn r8097) Replace strlen() {==,!=,>} 0 by the more concise {,!}StrEmpty(). Additionally the test takes O(1) instead of O(n) now
tron
parents: 5624
diff changeset
  1386
			}
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 667
diff changeset
  1387
10315
b3c652b3a316 (svn r14560) -Fix [FS#2396](r14555): lengthof() can't be simply replaced by lastof() in some cases (part by Aali)
smatz
parents: 10310
diff changeset
  1388
			strecat(_network_company_info[ci->client_playas].clients, client_name, lastof(_network_company_info[ci->client_playas].clients));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1389
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1390
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1391
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1392
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1393
// Send a packet to all clients with updated info about this client_index
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1394
void NetworkUpdateClientInfo(uint16 client_index)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1395
{
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
  1396
	NetworkTCPSocketHandler *cs;
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1397
	NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(client_index);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1398
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1399
	if (ci == NULL) return;
656
e3e435079efa (svn r1090) -Fix: Made the _openttd_revision variable global, and with that
truelight
parents: 624
diff changeset
  1400
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1401
	FOR_ALL_CLIENTS(cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1402
		SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, ci);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1403
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1404
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1405
785
bba7b3b35dec (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1406
/* Check if we want to restart the map */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6235
diff changeset
  1407
static void NetworkCheckRestartMap()
785
bba7b3b35dec (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1408
{
9420
8a38703928e8 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 9413
diff changeset
  1409
	if (_settings_client.network.restart_game_year != 0 && _cur_year >= _settings_client.network.restart_game_year) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
  1410
		DEBUG(net, 0, "Auto-restarting map. Year %d reached", _cur_year);
785
bba7b3b35dec (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1411
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4293
diff changeset
  1412
		StartNewGameWithoutGUI(GENERATE_NEW_SEED);
785
bba7b3b35dec (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1413
	}
bba7b3b35dec (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1414
}
bba7b3b35dec (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1415
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1416
/* Check if the server has autoclean_companies activated
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1417
    Two things happen:
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1418
      1) If a company is not protected, it is closed after 1 year (for example)
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1419
      2) If a company is protected, protection is disabled after 3 years (for example)
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1420
           (and item 1. happens a year later) */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6235
diff changeset
  1421
static void NetworkAutoCleanCompanies()
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1422
{
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
  1423
	NetworkTCPSocketHandler *cs;
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1424
	const NetworkClientInfo *ci;
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
  1425
	const Company *c;
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
  1426
	bool clients_in_company[MAX_COMPANIES];
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1427
9420
8a38703928e8 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 9413
diff changeset
  1428
	if (!_settings_client.network.autoclean_companies) return;
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1429
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1430
	memset(clients_in_company, 0, sizeof(clients_in_company));
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1431
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1432
	/* Detect the active companies */
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1433
	FOR_ALL_CLIENTS(cs) {
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1434
		ci = DEREF_CLIENT_INFO(cs);
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
  1435
		if (IsValidCompanyID(ci->client_playas)) clients_in_company[ci->client_playas] = true;
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1436
	}
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1437
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1438
	if (!_network_dedicated) {
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1439
		ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
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
  1440
		if (IsValidCompanyID(ci->client_playas)) clients_in_company[ci->client_playas] = true;
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1441
	}
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1442
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1443
	/* Go through all the comapnies */
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
  1444
	FOR_ALL_COMPANIES(c) {
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1445
		/* Skip the non-active once */
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
  1446
		if (c->is_ai) continue;
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1447
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
  1448
		if (!clients_in_company[c->index]) {
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1449
			/* The company is empty for one month more */
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
  1450
			_network_company_info[c->index].months_empty++;
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1451
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1452
			/* Is the company empty for autoclean_unprotected-months, and is there no protection? */
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
  1453
			if (_settings_client.network.autoclean_unprotected != 0 && _network_company_info[c->index].months_empty > _settings_client.network.autoclean_unprotected && StrEmpty(_network_company_info[c->index].password)) {
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1454
				/* Shut the company down */
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
  1455
				DoCommandP(0, 2, c->index, NULL, CMD_COMPANY_CTRL);
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
  1456
				IConsolePrintF(CC_DEFAULT, "Auto-cleaned company #%d", c->index + 1);
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1457
			}
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
  1458
			/* Is the company empty for autoclean_protected-months, and there is a protection? */
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
  1459
			if (_settings_client.network.autoclean_protected != 0 && _network_company_info[c->index].months_empty > _settings_client.network.autoclean_protected && !StrEmpty(_network_company_info[c->index].password)) {
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1460
				/* Unprotect the 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
  1461
				_network_company_info[c->index].password[0] = '\0';
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
  1462
				IConsolePrintF(CC_DEFAULT, "Auto-removed protection from company #%d", c->index + 1);
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
  1463
				_network_company_info[c->index].months_empty = 0;
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1464
			}
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1465
		} else {
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1466
			/* It is not empty, reset the date */
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
  1467
			_network_company_info[c->index].months_empty = 0;
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1468
		}
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1469
	}
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1470
}
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1471
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1472
// This function changes new_name to a name that is unique (by adding #1 ...)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1473
//  and it returns true if that succeeded.
793
7bee9006a465 (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1474
bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH])
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1475
{
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
  1476
	NetworkTCPSocketHandler *new_cs;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1477
	bool found_name = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1478
	byte number = 0;
793
7bee9006a465 (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1479
	char original_name[NETWORK_CLIENT_NAME_LENGTH];
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1480
3623
9b612a4bbd39 (svn r4521) - Fix: be consistent about the size of the player-name in MP. This hopefully fixes a crash on lesser OS's (eg Win98). Use ttd_strlcpy() to just copy over strings and properly terminate them because different implementations of snprintf() behave differently. Courtesy of TrueLight
Darkvater
parents: 3437
diff changeset
  1481
	// We use NETWORK_CLIENT_NAME_LENGTH in here, because new_name is really a pointer
793
7bee9006a465 (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1482
	ttd_strlcpy(original_name, new_name, NETWORK_CLIENT_NAME_LENGTH);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1483
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1484
	while (!found_name) {
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1485
		const NetworkClientInfo *ci;
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1486
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1487
		found_name = true;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1488
		FOR_ALL_CLIENTS(new_cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1489
			ci = DEREF_CLIENT_INFO(new_cs);
4026
035972c11282 (svn r5265) Replace a dozen bogus uses of strncmp() by strcmp()
tron
parents: 3623
diff changeset
  1490
			if (strcmp(ci->client_name, new_name) == 0) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1491
				// Name already in use
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1492
				found_name = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1493
				break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1494
			}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1495
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1496
		// Check if it is the same as the server-name
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1497
		ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1498
		if (ci != NULL) {
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1499
			if (strcmp(ci->client_name, new_name) == 0) found_name = false; // name already in use
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1500
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1501
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1502
		if (!found_name) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1503
			// Try a new name (<name> #1, <name> #2, and so on)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1504
826
fff56bbc3606 (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 793
diff changeset
  1505
			// Stop if we tried for more than 50 times..
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1506
			if (number++ > 50) break;
793
7bee9006a465 (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1507
			snprintf(new_name, NETWORK_CLIENT_NAME_LENGTH, "%s #%d", original_name, number);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1508
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1509
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1510
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1511
	return found_name;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1512
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1513
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1514
// Reads a packet from the stream
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
  1515
bool NetworkServer_ReadPackets(NetworkTCPSocketHandler *cs)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1516
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1517
	Packet *p;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1518
	NetworkRecvStatus res;
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5900
diff changeset
  1519
	while ((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
  1520
		byte type = p->Recv_uint8();
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1521
		if (type < PACKET_END && _network_server_packet[type] != NULL && !cs->has_quit) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1522
			_network_server_packet[type](cs, p);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1523
		} else {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
  1524
			DEBUG(net, 0, "[server] received invalid packet type %d", type);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1525
		}
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
  1526
		delete p;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1527
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1528
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1529
	return true;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1530
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1531
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1532
// Handle the local command-queue
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
  1533
static void NetworkHandleCommandQueue(NetworkTCPSocketHandler* cs)
2817
cdf488223c23 (svn r3365) Staticise 36 functions
tron
parents: 2676
diff changeset
  1534
{
2081
d17c3514b6da (svn r2591) Codechange: [network] Cleanup NetworkHandleCommandQueue
ludde
parents: 2079
diff changeset
  1535
	CommandPacket *cp;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1536
2081
d17c3514b6da (svn r2591) Codechange: [network] Cleanup NetworkHandleCommandQueue
ludde
parents: 2079
diff changeset
  1537
	while ( (cp = cs->command_queue) != NULL) {
d17c3514b6da (svn r2591) Codechange: [network] Cleanup NetworkHandleCommandQueue
ludde
parents: 2079
diff changeset
  1538
		SEND_COMMAND(PACKET_SERVER_COMMAND)(cs, cp);
d17c3514b6da (svn r2591) Codechange: [network] Cleanup NetworkHandleCommandQueue
ludde
parents: 2079
diff changeset
  1539
d17c3514b6da (svn r2591) Codechange: [network] Cleanup NetworkHandleCommandQueue
ludde
parents: 2079
diff changeset
  1540
		cs->command_queue = cp->next;
d17c3514b6da (svn r2591) Codechange: [network] Cleanup NetworkHandleCommandQueue
ludde
parents: 2079
diff changeset
  1541
		free(cp);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1542
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1543
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1544
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1545
// This is called every tick if this is a _network_server
2235
7630c38913fd (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2204
diff changeset
  1546
void NetworkServer_Tick(bool send_frame)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1547
{
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
  1548
	NetworkTCPSocketHandler *cs;
2079
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1549
#ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1550
	bool send_sync = false;
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1551
#endif
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1552
2079
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1553
#ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
9420
8a38703928e8 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 9413
diff changeset
  1554
	if (_frame_counter >= _last_sync_frame + _settings_client.network.sync_freq) {
2079
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1555
		_last_sync_frame = _frame_counter;
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1556
		send_sync = true;
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1557
	}
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1558
#endif
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1559
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1560
	// Now we are done with the frame, inform the clients that they can
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1561
	//  do their frame!
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1562
	FOR_ALL_CLIENTS(cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1563
		// Check if the speed of the client is what we can expect from a client
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1564
		if (cs->status == STATUS_ACTIVE) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1565
			// 1 lag-point per day
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1566
			int lag = NetworkCalculateLag(cs) / DAY_TICKS;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1567
			if (lag > 0) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1568
				if (lag > 3) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1569
					// Client did still not report in after 4 game-day, drop him
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1570
					//  (that is, the 3 of above, + 1 before any lag is counted)
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
  1571
					IConsolePrintF(CC_ERROR,"Client #%d is dropped because the client did not respond for more than 4 game-days", cs->index);
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 692
diff changeset
  1572
					NetworkCloseClient(cs);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1573
					continue;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1574
				}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1575
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1576
				// Report once per time we detect the lag
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1577
				if (cs->lag_test == 0) {
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
  1578
					IConsolePrintF(CC_WARNING,"[%d] Client #%d is slow, try increasing *net_frame_freq to a higher value!", _frame_counter, cs->index);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1579
					cs->lag_test = 1;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1580
				}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1581
			} else {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1582
				cs->lag_test = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1583
			}
1602
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1584
		} else if (cs->status == STATUS_PRE_ACTIVE) {
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1585
			int lag = NetworkCalculateLag(cs);
9420
8a38703928e8 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 9413
diff changeset
  1586
			if (lag > _settings_client.network.max_join_time) {
8a38703928e8 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 9413
diff changeset
  1587
				IConsolePrintF(CC_ERROR,"Client #%d is dropped because it took longer than %d ticks for him to join", cs->index, _settings_client.network.max_join_time);
1602
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1588
				NetworkCloseClient(cs);
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1589
			}
6235
a4ea32acd914 (svn r9038) -Fix [FS#115]: inactive connections are not automatically kicked, i.e. people who only open a telnet (or similar) connection to a server.
rubidium
parents: 6178
diff changeset
  1590
		} else if (cs->status == STATUS_INACTIVE) {
a4ea32acd914 (svn r9038) -Fix [FS#115]: inactive connections are not automatically kicked, i.e. people who only open a telnet (or similar) connection to a server.
rubidium
parents: 6178
diff changeset
  1591
			int lag = NetworkCalculateLag(cs);
a4ea32acd914 (svn r9038) -Fix [FS#115]: inactive connections are not automatically kicked, i.e. people who only open a telnet (or similar) connection to a server.
rubidium
parents: 6178
diff changeset
  1592
			if (lag > 4 * DAY_TICKS) {
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
  1593
				IConsolePrintF(CC_ERROR,"Client #%d is dropped because it took longer than %d ticks to start the joining process", cs->index, 4 * DAY_TICKS);
6235
a4ea32acd914 (svn r9038) -Fix [FS#115]: inactive connections are not automatically kicked, i.e. people who only open a telnet (or similar) connection to a server.
rubidium
parents: 6178
diff changeset
  1594
				NetworkCloseClient(cs);
a4ea32acd914 (svn r9038) -Fix [FS#115]: inactive connections are not automatically kicked, i.e. people who only open a telnet (or similar) connection to a server.
rubidium
parents: 6178
diff changeset
  1595
			}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1596
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1597
2079
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1598
		if (cs->status >= STATUS_PRE_ACTIVE) {
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1599
			// Check if we can send command, and if we have anything in the queue
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1600
			NetworkHandleCommandQueue(cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1601
2079
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1602
			// Send an updated _frame_counter_max to the client
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1603
			if (send_frame) SEND_COMMAND(PACKET_SERVER_FRAME)(cs);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1604
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1605
#ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
2079
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1606
			// Send a sync-check packet
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1607
			if (send_sync) SEND_COMMAND(PACKET_SERVER_SYNC)(cs);
2079
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1608
#endif
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1609
		}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1610
	}
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 667
diff changeset
  1611
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 667
diff changeset
  1612
	/* See if we need to advertise */
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 667
diff changeset
  1613
	NetworkUDPAdvertise();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1614
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1615
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6235
diff changeset
  1616
void NetworkServerYearlyLoop()
785
bba7b3b35dec (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1617
{
bba7b3b35dec (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1618
	NetworkCheckRestartMap();
bba7b3b35dec (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1619
}
bba7b3b35dec (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1620
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6235
diff changeset
  1621
void NetworkServerMonthlyLoop()
690
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1622
{
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1623
	NetworkAutoCleanCompanies();
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1624
}
3afcad69d4f7 (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1625
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
  1626
void NetworkServerChangeOwner(Owner current_owner, Owner new_owner)
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
  1627
{
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
  1628
	/* The server has to handle all administrative issues, for example
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
  1629
	 * updating and notifying all clients of what has happened */
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
  1630
	NetworkTCPSocketHandler *cs;
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
  1631
	NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(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
  1632
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
  1633
	/* The server has just changed from owner */
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
  1634
	if (current_owner == ci->client_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
  1635
		ci->client_playas = new_owner;
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
  1636
		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
  1637
	}
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
  1638
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
  1639
	/* Find all clients that were in control of this company, and mark them as new_owner */
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
  1640
	FOR_ALL_CLIENTS(cs) {
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
  1641
		ci = DEREF_CLIENT_INFO(cs);
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
  1642
		if (current_owner == ci->client_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
  1643
			ci->client_playas = new_owner;
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
  1644
			NetworkUpdateClientInfo(ci->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
  1645
		}
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
  1646
	}
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
  1647
}
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
  1648
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
  1649
const char* GetClientIP(const NetworkClientInfo* ci)
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
  1650
{
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
  1651
	struct in_addr addr;
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
  1652
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
  1653
	addr.s_addr = ci->client_ip;
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
  1654
	return inet_ntoa(addr);
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
  1655
}
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
  1656
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
  1657
void NetworkServerShowStatusToConsole()
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
  1658
{
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
  1659
	static const char* const stat_str[] = {
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
  1660
		"inactive",
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
  1661
		"authorizing",
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
  1662
		"authorized",
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
  1663
		"waiting",
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
  1664
		"loading map",
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
  1665
		"map done",
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
  1666
		"ready",
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
  1667
		"active"
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
  1668
	};
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
  1669
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
  1670
	NetworkTCPSocketHandler *cs;
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
  1671
	FOR_ALL_CLIENTS(cs) {
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
  1672
		int lag = NetworkCalculateLag(cs);
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
  1673
		const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
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
  1674
		const char* status;
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
  1675
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
  1676
		status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown");
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
  1677
		IConsolePrintF(CC_INFO, "Client #%1d  name: '%s'  status: '%s'  frame-lag: %3d  company: %1d  IP: %s  unique-id: '%s'",
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
  1678
			cs->index, ci->client_name, status, lag,
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
  1679
			ci->client_playas + (IsValidCompanyID(ci->client_playas) ? 1 : 0),
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
  1680
			GetClientIP(ci), ci->unique_id);
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
  1681
	}
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
  1682
}
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
  1683
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
  1684
void NetworkServerSendRcon(uint16 client_index, ConsoleColour colour_code, const char *string)
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
  1685
{
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
  1686
	SEND_COMMAND(PACKET_SERVER_RCON)(NetworkFindClientStateFromIndex(client_index), colour_code, string);
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
  1687
}
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
  1688
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
  1689
void NetworkServerSendError(uint16 client_index, NetworkErrorCode error)
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
  1690
{
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
  1691
	SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(client_index), error);
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
  1692
}
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
  1693
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
  1694
bool NetworkCompanyHasClients(CompanyID company)
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
  1695
{
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
  1696
	const NetworkTCPSocketHandler *cs;
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
  1697
	const NetworkClientInfo *ci;
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
  1698
	FOR_ALL_CLIENTS(cs) {
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
  1699
		ci = DEREF_CLIENT_INFO(cs);
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
  1700
		if (ci->client_playas == company) return true;
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
  1701
	}
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
  1702
	return false;
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
  1703
}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1704
#endif /* ENABLE_NETWORK */