src/network/network_server.cpp
author rubidium
Thu, 17 Jul 2008 15:14:42 +0000
changeset 11158 2004a5570047
parent 11156 cf146f346724
child 11161 7d0fac8f14cd
permissions -rw-r--r--
(svn r13716) -Fix [FS#2144]: any player could construct new companies.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
10429
1b99254f9607 (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: 9281
diff changeset
     3
/** @file network_server.cpp Server part of the network protocol. */
1b99254f9607 (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: 9281
diff changeset
     4
4826
63b1eb7c966b (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4717
diff changeset
     5
#ifdef ENABLE_NETWORK
63b1eb7c966b (svn r6750) -Codechange: Juggle around some header includes as they're only used when
Darkvater
parents: 4717
diff changeset
     6
5720
cc0ceeafaa55 (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: 5569
diff changeset
     7
#include "../stdafx.h"
cc0ceeafaa55 (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: 5569
diff changeset
     8
#include "../openttd.h" // XXX StringID
cc0ceeafaa55 (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: 5569
diff changeset
     9
#include "../debug.h"
8610
17cc343a23dd (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8495
diff changeset
    10
#include "../strings_func.h"
10792
95b6eb9fcc9e (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: 10784
diff changeset
    11
#include "network_internal.h"
5720
cc0ceeafaa55 (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: 5569
diff changeset
    12
#include "core/tcp.h"
8776
4a65bbcd6935 (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8766
diff changeset
    13
#include "../vehicle_base.h"
4a65bbcd6935 (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8766
diff changeset
    14
#include "../vehicle_func.h"
8636
2b158acb649c (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8627
diff changeset
    15
#include "../date_func.h"
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    16
#include "network_server.h"
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 667
diff changeset
    17
#include "network_udp.h"
10684
7cc2278c2ac0 (svn r13228) -Codechange: split console.h.
rubidium
parents: 10429
diff changeset
    18
#include "../console_func.h"
8612
6414fc21c2f3 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8610
diff changeset
    19
#include "../command_func.h"
5720
cc0ceeafaa55 (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: 5569
diff changeset
    20
#include "../saveload.h"
9281
d8cd9ac52a68 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 8850
diff changeset
    21
#include "../station_base.h"
5720
cc0ceeafaa55 (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: 5569
diff changeset
    22
#include "../variables.h"
cc0ceeafaa55 (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: 5569
diff changeset
    23
#include "../genworld.h"
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8612
diff changeset
    24
#include "../core/alloc_func.hpp"
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7332
diff changeset
    25
#include "../fileio.h"
8710
52015340050c (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents: 8640
diff changeset
    26
#include "../string_func.h"
8750
fdd6054e7bae (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8710
diff changeset
    27
#include "../player_base.h"
fdd6054e7bae (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8710
diff changeset
    28
#include "../player_func.h"
fdd6054e7bae (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8710
diff changeset
    29
#include "../player_gui.h"
8766
c86cfa3a7580 (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8760
diff changeset
    30
#include "../settings_type.h"
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    31
8760
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8750
diff changeset
    32
#include "table/strings.h"
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8750
diff changeset
    33
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    34
// This file handles all the server-commands
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    35
5875
4a1391019791 (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: 5860
diff changeset
    36
static void NetworkHandleCommandQueue(NetworkTCPSocketHandler* cs);
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 692
diff changeset
    37
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    38
// **********
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    39
// Sending functions
5875
4a1391019791 (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: 5860
diff changeset
    40
//   DEF_SERVER_SEND_COMMAND has parameter: NetworkTCPSocketHandler *cs
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    41
// **********
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    42
5875
4a1391019791 (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: 5860
diff changeset
    43
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CLIENT_INFO)(NetworkTCPSocketHandler *cs, NetworkClientInfo *ci)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    44
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    45
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    46
	// Packet: SERVER_CLIENT_INFO
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    47
	// Function: Sends info about a client
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    48
	// Data:
e3b43338096b (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)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    50
	//    uint8:  As which player the client is playing
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    51
	//    String: The name of the client
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    52
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    53
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    54
	if (ci->client_index != NETWORK_EMPTY_INDEX) {
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
    55
		Packet *p = NetworkSend_Init(PACKET_SERVER_CLIENT_INFO);
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
    56
		p->Send_uint16(ci->client_index);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
    57
		p->Send_uint8 (ci->client_playas);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
    58
		p->Send_string(ci->client_name);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    59
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
    60
		cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    61
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    62
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    63
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    64
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_COMPANY_INFO)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    65
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    66
//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    67
	// Packet: SERVER_COMPANY_INFO
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    68
	// Function: Sends info about the companies
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    69
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    70
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    71
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    72
	int i;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    73
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    74
	Player *player;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    75
	Packet *p;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    76
2944
7c392e7b51c6 (svn r3500) - Workaround the inaccurate count of spectators/companies that can happen in certain border-cases. For now just dynamically get this value when requested so it is always right. To do properly all player/client creation/destruction needs a hook for networking.
Darkvater
parents: 2882
diff changeset
    77
	byte active = ActivePlayerCount();
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    78
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    79
	if (active == 0) {
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
    80
		p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    81
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
    82
		p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
    83
		p->Send_uint8 (active);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    84
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
    85
		cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    86
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    87
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    88
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    89
	NetworkPopulateCompanyInfo();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    90
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    91
	FOR_ALL_PLAYERS(player) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
    92
		if (!player->is_active) continue;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    93
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    94
		p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    95
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
    96
		p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
    97
		p->Send_uint8 (active);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
    98
		p->Send_uint8 (player->index);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
    99
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   100
		p->Send_string(_network_player_info[player->index].company_name);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   101
		p->Send_uint32(_network_player_info[player->index].inaugurated_year);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   102
		p->Send_uint64(_network_player_info[player->index].company_value);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   103
		p->Send_uint64(_network_player_info[player->index].money);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   104
		p->Send_uint64(_network_player_info[player->index].income);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   105
		p->Send_uint16(_network_player_info[player->index].performance);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   106
1011
2587838541bf (svn r1510) -Add: Improved Network Lobby GUI: (bociusz)
truelight
parents: 970
diff changeset
   107
		/* Send 1 if there is a passord for the company else send 0 */
7034
7ef1af3e6071 (svn r9727) -Fix (r8546): Company password field in network player info was inverted.
peter1138
parents: 7027
diff changeset
   108
		p->Send_bool(!StrEmpty(_network_player_info[player->index].password));
1011
2587838541bf (svn r1510) -Add: Improved Network Lobby GUI: (bociusz)
truelight
parents: 970
diff changeset
   109
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   110
		for (i = 0; i < NETWORK_VEHICLE_TYPES; i++) {
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   111
			p->Send_uint16(_network_player_info[player->index].num_vehicle[i]);
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   112
		}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   113
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   114
		for (i = 0; i < NETWORK_STATION_TYPES; i++) {
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   115
			p->Send_uint16(_network_player_info[player->index].num_station[i]);
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   116
		}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   117
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   118
		if (_network_player_info[player->index].players[0] == '\0') {
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   119
			p->Send_string("<none>");
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   120
		} else {
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   121
			p->Send_string(_network_player_info[player->index].players);
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   122
		}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   123
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   124
		cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   125
	}
734
018e549265ed (svn r1186) -Fix: [Network] You can now join a company on a server where a company
truelight
parents: 733
diff changeset
   126
018e549265ed (svn r1186) -Fix: [Network] You can now join a company on a server where a company
truelight
parents: 733
diff changeset
   127
	p = NetworkSend_Init(PACKET_SERVER_COMPANY_INFO);
018e549265ed (svn r1186) -Fix: [Network] You can now join a company on a server where a company
truelight
parents: 733
diff changeset
   128
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   129
	p->Send_uint8 (NETWORK_COMPANY_INFO_VERSION);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   130
	p->Send_uint8 (0);
734
018e549265ed (svn r1186) -Fix: [Network] You can now join a company on a server where a company
truelight
parents: 733
diff changeset
   131
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   132
	cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   133
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   134
5875
4a1391019791 (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: 5860
diff changeset
   135
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR)(NetworkTCPSocketHandler *cs, NetworkErrorCode error)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   136
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   137
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   138
	// Packet: SERVER_ERROR
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   139
	// Function: The client made an error
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   140
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   141
	//    uint8:  ErrorID (see network_data.h, NetworkErrorCode)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   142
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   143
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   144
	char str[100];
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   145
	Packet *p = NetworkSend_Init(PACKET_SERVER_ERROR);
3417
c867f87873ae (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
   146
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   147
	p->Send_uint8(error);
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   148
	cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   149
4912
d04b3f2bca70 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4906
diff changeset
   150
	GetNetworkErrorMsg(str, error, lastof(str));
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   151
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   152
	// Only send when the current client was in game
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   153
	if (cs->status > STATUS_AUTH) {
5875
4a1391019791 (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: 5860
diff changeset
   154
		NetworkTCPSocketHandler *new_cs;
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   155
		char client_name[NETWORK_CLIENT_NAME_LENGTH];
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   156
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   157
		NetworkGetClientName(client_name, sizeof(client_name), cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   158
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
   159
		DEBUG(net, 1, "'%s' made an error and has been disconnected. Reason: '%s'", client_name, str);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   160
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
   161
		NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "%s", str);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   162
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   163
		FOR_ALL_CLIENTS(new_cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   164
			if (new_cs->status > STATUS_AUTH && new_cs != cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   165
				// Some errors we filter to a more general error. Clients don't have to know the real
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   166
				//  reason a joining failed.
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   167
				if (error == NETWORK_ERROR_NOT_AUTHORIZED || error == NETWORK_ERROR_NOT_EXPECTED || error == NETWORK_ERROR_WRONG_REVISION)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   168
					error = NETWORK_ERROR_ILLEGAL_PACKET;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   169
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   170
				SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, error);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   171
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   172
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   173
	} else {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
   174
		DEBUG(net, 1, "Client %d made an error and has been disconnected. Reason: '%s'", cs->index, str);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   175
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   176
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   177
	cs->has_quit = true;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   178
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   179
	// Make sure the data get's there before we close the connection
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   180
	cs->Send_Packets();
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   181
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   182
	// The client made a mistake, so drop his connection now!
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 692
diff changeset
   183
	NetworkCloseClient(cs);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   184
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   185
6123
87aa45c64f8f (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: 6105
diff changeset
   186
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CHECK_NEWGRFS)(NetworkTCPSocketHandler *cs)
87aa45c64f8f (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: 6105
diff changeset
   187
{
87aa45c64f8f (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: 6105
diff changeset
   188
	//
87aa45c64f8f (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: 6105
diff changeset
   189
	// Packet: PACKET_SERVER_CHECK_NEWGRFS
87aa45c64f8f (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: 6105
diff changeset
   190
	// Function: Sends info about the used GRFs to the client
87aa45c64f8f (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: 6105
diff changeset
   191
	// Data:
87aa45c64f8f (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: 6105
diff changeset
   192
	//      uint8:  Amount of GRFs
87aa45c64f8f (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: 6105
diff changeset
   193
	//    And then for each GRF:
87aa45c64f8f (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: 6105
diff changeset
   194
	//      uint32: GRF ID
87aa45c64f8f (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: 6105
diff changeset
   195
	// 16 * uint8:  MD5 checksum of the GRF
87aa45c64f8f (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: 6105
diff changeset
   196
	//
87aa45c64f8f (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: 6105
diff changeset
   197
87aa45c64f8f (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: 6105
diff changeset
   198
	Packet *p = NetworkSend_Init(PACKET_SERVER_CHECK_NEWGRFS);
87aa45c64f8f (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: 6105
diff changeset
   199
	const GRFConfig *c;
87aa45c64f8f (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: 6105
diff changeset
   200
	uint grf_count = 0;
87aa45c64f8f (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: 6105
diff changeset
   201
7636
d04531c9f0a4 (svn r10414) -Fix: the network protocol check for required newgrfs sent static newgrfs too.
rubidium
parents: 7556
diff changeset
   202
	for (c = _grfconfig; c != NULL; c = c->next) {
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8303
diff changeset
   203
		if (!HasBit(c->flags, GCF_STATIC)) grf_count++;
7636
d04531c9f0a4 (svn r10414) -Fix: the network protocol check for required newgrfs sent static newgrfs too.
rubidium
parents: 7556
diff changeset
   204
	}
6123
87aa45c64f8f (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: 6105
diff changeset
   205
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   206
	p->Send_uint8 (grf_count);
6123
87aa45c64f8f (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: 6105
diff changeset
   207
	for (c = _grfconfig; c != NULL; c = c->next) {
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8303
diff changeset
   208
		if (!HasBit(c->flags, GCF_STATIC)) cs->Send_GRFIdentifier(p, c);
6123
87aa45c64f8f (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: 6105
diff changeset
   209
	}
87aa45c64f8f (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: 6105
diff changeset
   210
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   211
	cs->Send_Packet(p);
6123
87aa45c64f8f (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: 6105
diff changeset
   212
}
87aa45c64f8f (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: 6105
diff changeset
   213
5875
4a1391019791 (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: 5860
diff changeset
   214
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_NEED_PASSWORD)(NetworkTCPSocketHandler *cs, NetworkPasswordType type)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   215
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   216
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   217
	// Packet: SERVER_NEED_PASSWORD
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   218
	// Function: Indication to the client that the server needs a password
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   219
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   220
	//    uint8:  Type of password
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   221
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   222
6561
75a3a9146abb (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: 6504
diff changeset
   223
	/* Invalid packet when status is AUTH or higher */
75a3a9146abb (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: 6504
diff changeset
   224
	if (cs->status >= STATUS_AUTH) return;
75a3a9146abb (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: 6504
diff changeset
   225
75a3a9146abb (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: 6504
diff changeset
   226
	cs->status = STATUS_AUTHORIZING;
75a3a9146abb (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: 6504
diff changeset
   227
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   228
	Packet *p = NetworkSend_Init(PACKET_SERVER_NEED_PASSWORD);
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   229
	p->Send_uint8(type);
10775
7061477bfbcf (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: 10703
diff changeset
   230
	p->Send_uint32(_settings_game.game_creation.generation_seed);
10784
c3c7b558b4f1 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 10775
diff changeset
   231
	p->Send_string(_settings_client.network.network_id);
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   232
	cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   233
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   234
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   235
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WELCOME)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   236
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   237
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   238
	// Packet: SERVER_WELCOME
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   239
	// Function: The client is joined and ready to receive his map
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   240
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   241
	//    uint16:  Own ClientID
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   242
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   243
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   244
	Packet *p;
5875
4a1391019791 (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: 5860
diff changeset
   245
	NetworkTCPSocketHandler *new_cs;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   246
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   247
	// Invalid packet when status is AUTH or higher
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   248
	if (cs->status >= STATUS_AUTH) return;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   249
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   250
	cs->status = STATUS_AUTH;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   251
	_network_game_info.clients_on++;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   252
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   253
	p = NetworkSend_Init(PACKET_SERVER_WELCOME);
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   254
	p->Send_uint16(cs->index);
10775
7061477bfbcf (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: 10703
diff changeset
   255
	p->Send_uint32(_settings_game.game_creation.generation_seed);
10784
c3c7b558b4f1 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 10775
diff changeset
   256
	p->Send_string(_settings_client.network.network_id);
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   257
	cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   258
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   259
		// Transmit info about all the active clients
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   260
	FOR_ALL_CLIENTS(new_cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   261
		if (new_cs != cs && new_cs->status > STATUS_AUTH)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   262
			SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, DEREF_CLIENT_INFO(new_cs));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   263
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   264
	// Also send the info of the server
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   265
	SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   266
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   267
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   268
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WAIT)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   269
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   270
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   271
	// Packet: PACKET_SERVER_WAIT
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   272
	// Function: The client can not receive the map at the moment because
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   273
	//             someone else is already receiving the map
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   274
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   275
	//    uint8:  Clients awaiting map
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   276
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   277
	int waiting = 0;
5875
4a1391019791 (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: 5860
diff changeset
   278
	NetworkTCPSocketHandler *new_cs;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   279
	Packet *p;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   280
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   281
	// Count how many players are waiting in the queue
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   282
	FOR_ALL_CLIENTS(new_cs) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   283
		if (new_cs->status == STATUS_MAP_WAIT) waiting++;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   284
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   285
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   286
	p = NetworkSend_Init(PACKET_SERVER_WAIT);
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   287
	p->Send_uint8(waiting);
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   288
	cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   289
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   290
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   291
// This sends the map to the client
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   292
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_MAP)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   293
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   294
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   295
	// Packet: SERVER_MAP
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   296
	// Function: Sends the map to the client, or a part of it (it is splitted in
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   297
	//   a lot of multiple packets)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   298
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   299
	//    uint8:  packet-type (MAP_PACKET_START, MAP_PACKET_NORMAL and MAP_PACKET_END)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   300
	//  if MAP_PACKET_START:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   301
	//    uint32: The current FrameCounter
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   302
	//  if MAP_PACKET_NORMAL:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   303
	//    piece of the map (till max-size of packet)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   304
	//  if MAP_PACKET_END:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   305
	//    uint32: seed0 of player
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   306
	//    uint32: seed1 of player
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   307
	//      last 2 are repeated MAX_PLAYERS time
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   308
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   309
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   310
	static FILE *file_pointer;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   311
	static uint sent_packets; // How many packets we did send succecfully last time
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   312
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   313
	if (cs->status < STATUS_AUTH) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   314
		// Illegal call, return error and ignore the packet
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   315
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   316
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   317
	}
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   318
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   319
	if (cs->status == STATUS_AUTH) {
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7332
diff changeset
   320
		const char *filename = "network_server.tmp";
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   321
		Packet *p;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   322
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   323
		// Make a dump of the current game
10839
a62547c31fdb (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: 10818
diff changeset
   324
		if (SaveOrLoad(filename, SL_SAVE, AUTOSAVE_DIR) != SL_OK) usererror("network savedump failed");
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   325
7425
350b9265b7a2 (svn r10182) -Codechange: rewrite most part of the file loading/searching to be more flexible.
rubidium
parents: 7332
diff changeset
   326
		file_pointer = FioFOpenFile(filename, "rb", AUTOSAVE_DIR);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   327
		fseek(file_pointer, 0, SEEK_END);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   328
10839
a62547c31fdb (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: 10818
diff changeset
   329
		if (ftell(file_pointer) == 0) usererror("network savedump failed - zero sized savegame?");
5956
04e89c320747 (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: 5889
diff changeset
   330
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   331
		// Now send the _frame_counter and how many packets are coming
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   332
		p = NetworkSend_Init(PACKET_SERVER_MAP);
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   333
		p->Send_uint8 (MAP_PACKET_START);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   334
		p->Send_uint32(_frame_counter);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   335
		p->Send_uint32(ftell(file_pointer));
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   336
		cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   337
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   338
		fseek(file_pointer, 0, SEEK_SET);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   339
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   340
		sent_packets = 4; // We start with trying 4 packets
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   341
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   342
		cs->status = STATUS_MAP;
1602
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   343
		/* Mark the start of download */
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   344
		cs->last_frame = _frame_counter;
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   345
		cs->last_frame_server = _frame_counter;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   346
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   347
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   348
	if (cs->status == STATUS_MAP) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   349
		uint i;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   350
		int res;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   351
		for (i = 0; i < sent_packets; i++) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   352
			Packet *p = NetworkSend_Init(PACKET_SERVER_MAP);
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   353
			p->Send_uint8(MAP_PACKET_NORMAL);
4321
b763b7007162 (svn r5974) -Codechange: added casts all around the place to make Windows 64bit happy (michi_cc)
truelight
parents: 4300
diff changeset
   354
			res = (int)fread(p->buffer + p->size, 1, SEND_MTU - p->size, file_pointer);
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   355
10839
a62547c31fdb (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: 10818
diff changeset
   356
			if (ferror(file_pointer)) usererror("Error reading temporary network savegame!");
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   357
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   358
			p->size += res;
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   359
			cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   360
			if (feof(file_pointer)) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   361
				// Done reading!
3121
2e50f731567a (svn r3726) - [6/6] Finalize conversion, finally save the patches struct.
Darkvater
parents: 3117
diff changeset
   362
				Packet *p = NetworkSend_Init(PACKET_SERVER_MAP);
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   363
				p->Send_uint8(MAP_PACKET_END);
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   364
				cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   365
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   366
				// Set the status to DONE_MAP, no we will wait for the client
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   367
				//  to send it is ready (maybe that happens like never ;))
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   368
				cs->status = STATUS_DONE_MAP;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   369
				fclose(file_pointer);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   370
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   371
				{
5875
4a1391019791 (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: 5860
diff changeset
   372
					NetworkTCPSocketHandler *new_cs;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   373
					bool new_map_client = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   374
					// Check if there is a client waiting for receiving the map
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   375
					//  and start sending him the map
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   376
					FOR_ALL_CLIENTS(new_cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   377
						if (new_cs->status == STATUS_MAP_WAIT) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   378
							// Check if we already have a new client to send the map to
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   379
							if (!new_map_client) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   380
								// If not, this client will get the map
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   381
								new_cs->status = STATUS_AUTH;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   382
								new_map_client = true;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   383
								SEND_COMMAND(PACKET_SERVER_MAP)(new_cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   384
							} else {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   385
								// Else, send the other clients how many clients are in front of them
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   386
								SEND_COMMAND(PACKET_SERVER_WAIT)(new_cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   387
							}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   388
						}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   389
					}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   390
				}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   391
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   392
				// There is no more data, so break the for
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   393
				break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   394
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   395
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   396
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   397
		// Send all packets (forced) and check if we have send it all
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   398
		cs->Send_Packets();
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   399
		if (cs->IsPacketQueueEmpty()) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   400
			// All are sent, increase the sent_packets
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   401
			sent_packets *= 2;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   402
		} else {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   403
			// Not everything is sent, decrease the sent_packets
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   404
			if (sent_packets > 1) sent_packets /= 2;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   405
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   406
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   407
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   408
5875
4a1391019791 (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: 5860
diff changeset
   409
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_JOIN)(NetworkTCPSocketHandler *cs, uint16 client_index)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   410
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   411
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   412
	// Packet: SERVER_JOIN
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   413
	// Function: A client is joined (all active clients receive this after a
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   414
	//     PACKET_CLIENT_MAP_OK) Mostly what directly follows is a
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   415
	//     PACKET_SERVER_CLIENT_INFO
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   416
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   417
	//    uint16:  Client-Index
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   418
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   419
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   420
	Packet *p = NetworkSend_Init(PACKET_SERVER_JOIN);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   421
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   422
	p->Send_uint16(client_index);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   423
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   424
	cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   425
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   426
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   427
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   428
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_FRAME)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   429
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   430
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   431
	// Packet: SERVER_FRAME
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   432
	// Function: Sends the current frame-counter to the client
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   433
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   434
	//    uint32: Frame Counter
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   435
	//    uint32: Frame Counter Max (how far may the client walk before the server?)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   436
	//    [uint32: general-seed-1]
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   437
	//    [uint32: general-seed-2]
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   438
	//      (last two depends on compile-settings, and are not default settings)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   439
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   440
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   441
	Packet *p = NetworkSend_Init(PACKET_SERVER_FRAME);
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   442
	p->Send_uint32(_frame_counter);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   443
	p->Send_uint32(_frame_counter_max);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   444
#ifdef ENABLE_NETWORK_SYNC_EVERY_FRAME
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   445
	p->Send_uint32(_sync_seed_1);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   446
#ifdef NETWORK_SEND_DOUBLE_SEED
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   447
	p->Send_uint32(_sync_seed_2);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   448
#endif
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   449
#endif
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   450
	cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   451
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   452
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   453
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_SYNC)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   454
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   455
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   456
	// Packet: SERVER_SYNC
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   457
	// Function: Sends a sync-check to the client
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   458
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   459
	//    uint32: Frame Counter
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   460
	//    uint32: General-seed-1
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   461
	//    [uint32: general-seed-2]
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   462
	//      (last one depends on compile-settings, and are not default settings)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   463
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   464
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   465
	Packet *p = NetworkSend_Init(PACKET_SERVER_SYNC);
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   466
	p->Send_uint32(_frame_counter);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   467
	p->Send_uint32(_sync_seed_1);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   468
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   469
#ifdef NETWORK_SEND_DOUBLE_SEED
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   470
	p->Send_uint32(_sync_seed_2);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   471
#endif
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   472
	cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   473
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   474
5875
4a1391019791 (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: 5860
diff changeset
   475
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_COMMAND)(NetworkTCPSocketHandler *cs, CommandPacket *cp)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   476
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   477
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   478
	// Packet: SERVER_COMMAND
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   479
	// Function: Sends a DoCommand to the client
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   480
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   481
	//    uint8:  PlayerID (0..MAX_PLAYERS-1)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   482
	//    uint32: CommandID (see command.h)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   483
	//    uint32: P1 (free variables used in DoCommand)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   484
	//    uint32: P2
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   485
	//    uint32: Tile
1820
9b6458526480 (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
   486
	//    string: text
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   487
	//    uint8:  CallBackID (see callback_table.c)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   488
	//    uint32: Frame of execution
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   489
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   490
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   491
	Packet *p = NetworkSend_Init(PACKET_SERVER_COMMAND);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   492
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   493
	p->Send_uint8 (cp->player);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   494
	p->Send_uint32(cp->cmd);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   495
	p->Send_uint32(cp->p1);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   496
	p->Send_uint32(cp->p2);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   497
	p->Send_uint32(cp->tile);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   498
	p->Send_string(cp->text);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   499
	p->Send_uint8 (cp->callback);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   500
	p->Send_uint32(cp->frame);
7718
220603f0122e (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: 7636
diff changeset
   501
	p->Send_bool  (cp->my_cmd);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   502
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   503
	cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   504
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   505
5875
4a1391019791 (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: 5860
diff changeset
   506
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_CHAT)(NetworkTCPSocketHandler *cs, NetworkAction action, uint16 client_index, bool self_send, const char *msg)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   507
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   508
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   509
	// Packet: SERVER_CHAT
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   510
	// Function: Sends a chat-packet to the client
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   511
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   512
	//    uint8:  ActionID (see network_data.h, NetworkAction)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   513
	//    uint16:  Client-index
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   514
	//    String: Message (max MAX_TEXT_MSG_LEN)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   515
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   516
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   517
	Packet *p = NetworkSend_Init(PACKET_SERVER_CHAT);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   518
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   519
	p->Send_uint8 (action);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   520
	p->Send_uint16(client_index);
6169
31cdbb4845f1 (svn r8546) -Codechange: add a seperate (wrapper) functions to send/receive booleans.
rubidium
parents: 6153
diff changeset
   521
	p->Send_bool  (self_send);
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   522
	p->Send_string(msg);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   523
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   524
	cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   525
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   526
5875
4a1391019791 (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: 5860
diff changeset
   527
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_ERROR_QUIT)(NetworkTCPSocketHandler *cs, uint16 client_index, NetworkErrorCode errorno)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   528
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   529
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   530
	// Packet: SERVER_ERROR_QUIT
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   531
	// Function: One of the clients made an error and is quiting the game
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   532
	//      This packet informs the other clients of that.
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   533
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   534
	//    uint16:  Client-index
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   535
	//    uint8:  ErrorID (see network_data.h, NetworkErrorCode)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   536
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   537
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   538
	Packet *p = NetworkSend_Init(PACKET_SERVER_ERROR_QUIT);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   539
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   540
	p->Send_uint16(client_index);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   541
	p->Send_uint8 (errorno);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   542
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   543
	cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   544
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   545
5875
4a1391019791 (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: 5860
diff changeset
   546
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_QUIT)(NetworkTCPSocketHandler *cs, uint16 client_index, const char *leavemsg)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   547
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   548
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   549
	// Packet: SERVER_ERROR_QUIT
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   550
	// Function: A client left the game, and this packets informs the other clients
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   551
	//      of that.
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   552
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   553
	//    uint16:  Client-index
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   554
	//    String: leave-message
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   555
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   556
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   557
	Packet *p = NetworkSend_Init(PACKET_SERVER_QUIT);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   558
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   559
	p->Send_uint16(client_index);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   560
	p->Send_string(leavemsg);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   561
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   562
	cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   563
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   564
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   565
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_SHUTDOWN)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   566
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   567
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   568
	// Packet: SERVER_SHUTDOWN
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   569
	// Function: Let the clients know that the server is closing
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   570
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   571
	//     <none>
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   572
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   573
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   574
	Packet *p = NetworkSend_Init(PACKET_SERVER_SHUTDOWN);
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   575
	cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   576
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   577
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   578
DEF_SERVER_SEND_COMMAND(PACKET_SERVER_NEWGAME)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   579
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   580
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   581
	// Packet: PACKET_SERVER_NEWGAME
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   582
	// Function: Let the clients know that the server is loading a new map
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   583
	// Data:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   584
	//     <none>
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   585
	//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   586
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   587
	Packet *p = NetworkSend_Init(PACKET_SERVER_NEWGAME);
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   588
	cs->Send_Packet(p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   589
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   590
5875
4a1391019791 (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: 5860
diff changeset
   591
DEF_SERVER_SEND_COMMAND_PARAM(PACKET_SERVER_RCON)(NetworkTCPSocketHandler *cs, uint16 color, const char *command)
1026
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
   592
{
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
   593
	Packet *p = NetworkSend_Init(PACKET_SERVER_RCON);
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
   594
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   595
	p->Send_uint16(color);
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   596
	p->Send_string(command);
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
   597
	cs->Send_Packet(p);
1026
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
   598
}
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
   599
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   600
// **********
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   601
// Receiving functions
5875
4a1391019791 (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: 5860
diff changeset
   602
//   DEF_SERVER_RECEIVE_COMMAND has parameter: NetworkTCPSocketHandler *cs, Packet *p
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   603
// **********
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   604
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   605
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   606
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   607
	SEND_COMMAND(PACKET_SERVER_COMPANY_INFO)(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   608
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   609
6123
87aa45c64f8f (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: 6105
diff changeset
   610
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)
87aa45c64f8f (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: 6105
diff changeset
   611
{
10857
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
   612
	if (cs->status != STATUS_INACTIVE) {
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
   613
		/* Illegal call, return error and ignore the packet */
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
   614
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
   615
		return;
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
   616
	}
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
   617
6123
87aa45c64f8f (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: 6105
diff changeset
   618
	NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
87aa45c64f8f (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: 6105
diff changeset
   619
87aa45c64f8f (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: 6105
diff changeset
   620
	/* We now want a password from the client else we do not allow him in! */
10818
95a5bba4e812 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 10792
diff changeset
   621
	if (!StrEmpty(_settings_client.network.server_password)) {
6123
87aa45c64f8f (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: 6105
diff changeset
   622
		SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_GAME_PASSWORD);
87aa45c64f8f (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: 6105
diff changeset
   623
	} else {
87aa45c64f8f (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: 6105
diff changeset
   624
		if (IsValidPlayer(ci->client_playas) && _network_player_info[ci->client_playas].password[0] != '\0') {
87aa45c64f8f (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: 6105
diff changeset
   625
			SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD);
87aa45c64f8f (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: 6105
diff changeset
   626
		} else {
87aa45c64f8f (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: 6105
diff changeset
   627
			SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
87aa45c64f8f (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: 6105
diff changeset
   628
		}
87aa45c64f8f (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: 6105
diff changeset
   629
	}
87aa45c64f8f (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: 6105
diff changeset
   630
}
87aa45c64f8f (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: 6105
diff changeset
   631
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   632
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   633
{
10857
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
   634
	if (cs->status != STATUS_INACTIVE) {
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
   635
		/* Illegal call, return error and ignore the packet */
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
   636
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
   637
		return;
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
   638
	}
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
   639
3623
2dd90d28fe20 (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
   640
	char name[NETWORK_CLIENT_NAME_LENGTH];
8303
67babdcebe96 (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: 7991
diff changeset
   641
	char unique_id[NETWORK_UNIQUE_ID_LENGTH];
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   642
	NetworkClientInfo *ci;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   643
	PlayerID playas;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   644
	NetworkLanguage client_lang;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   645
	char client_revision[NETWORK_REVISION_LENGTH];
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   646
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   647
	p->Recv_string(client_revision, sizeof(client_revision));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   648
692
b69a17776812 (svn r1136) -Fix: [Network] Revision check on windows-servers is working now
truelight
parents: 690
diff changeset
   649
#if defined(WITH_REV) || defined(WITH_REV_HACK)
659
a98c731921b4 (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
   650
	// Check if the client has revision control enabled
6504
5578f2fa1a73 (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: 6169
diff changeset
   651
	if (!IsNetworkCompatibleVersion(client_revision)) {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   652
		// Different revisions!!
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   653
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_REVISION);
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
   654
		return;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   655
	}
663
3236ec743f75 (svn r1098) -Fix: server without revision build doesn't care about the client's version. Also moved all revision things where it belongs (network.c)
darkvater
parents: 662
diff changeset
   656
#endif
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   657
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   658
	p->Recv_string(name, sizeof(name));
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   659
	playas = (Owner)p->Recv_uint8();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   660
	client_lang = (NetworkLanguage)p->Recv_uint8();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   661
	p->Recv_string(unique_id, sizeof(unique_id));
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   662
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   663
	if (cs->has_quit) return;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   664
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   665
	// join another company does not affect these values
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   666
	switch (playas) {
4861
f28bfa84f9ad (svn r6787) -Codechange: Use PLAYER_NEW_COMPANY as a player identifier wishing to become a
Darkvater
parents: 4860
diff changeset
   667
		case PLAYER_NEW_COMPANY: /* New company */
10818
95a5bba4e812 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 10792
diff changeset
   668
			if (ActivePlayerCount() >= _settings_client.network.max_companies) {
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   669
				SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_FULL);
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   670
				return;
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   671
			}
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   672
			break;
4848
56549aa3e234 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4826
diff changeset
   673
		case PLAYER_SPECTATOR: /* Spectator */
10818
95a5bba4e812 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 10792
diff changeset
   674
			if (NetworkSpectatorCount() >= _settings_client.network.max_spectators) {
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   675
				SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_FULL);
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   676
				return;
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   677
			}
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   678
			break;
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
   679
		default: /* Join another company (companies 1-8 (index 0-7)) */
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
   680
			if (!IsValidPlayer(playas)) {
4861
f28bfa84f9ad (svn r6787) -Codechange: Use PLAYER_NEW_COMPANY as a player identifier wishing to become a
Darkvater
parents: 4860
diff changeset
   681
				SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_PLAYER_MISMATCH);
f28bfa84f9ad (svn r6787) -Codechange: Use PLAYER_NEW_COMPANY as a player identifier wishing to become a
Darkvater
parents: 4860
diff changeset
   682
				return;
f28bfa84f9ad (svn r6787) -Codechange: Use PLAYER_NEW_COMPANY as a player identifier wishing to become a
Darkvater
parents: 4860
diff changeset
   683
			}
f28bfa84f9ad (svn r6787) -Codechange: Use PLAYER_NEW_COMPANY as a player identifier wishing to become a
Darkvater
parents: 4860
diff changeset
   684
			break;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   685
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   686
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   687
	// We need a valid name.. make it Player
3623
2dd90d28fe20 (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
   688
	if (*name == '\0') ttd_strlcpy(name, "Player", sizeof(name));
2879
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   689
d270cb37c0ec (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2817
diff changeset
   690
	if (!NetworkFindName(name)) { // Change name if duplicate
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   691
		// We could not create a name for this player
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   692
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NAME_IN_USE);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   693
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   694
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   695
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   696
	ci = DEREF_CLIENT_INFO(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   697
3623
2dd90d28fe20 (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
   698
	ttd_strlcpy(ci->client_name, name, sizeof(ci->client_name));
2dd90d28fe20 (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
   699
	ttd_strlcpy(ci->unique_id, unique_id, sizeof(ci->unique_id));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   700
	ci->client_playas = playas;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   701
	ci->client_lang = client_lang;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   702
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
   703
	/* Make sure companies to which people try to join are not autocleaned */
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
   704
	if (IsValidPlayer(playas)) _network_player_info[playas].months_empty = 0;
6123
87aa45c64f8f (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: 6105
diff changeset
   705
87aa45c64f8f (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: 6105
diff changeset
   706
	if (_grfconfig == NULL) {
87aa45c64f8f (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: 6105
diff changeset
   707
		RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)(cs, NULL);
87aa45c64f8f (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: 6105
diff changeset
   708
	} else {
87aa45c64f8f (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: 6105
diff changeset
   709
		SEND_COMMAND(PACKET_SERVER_CHECK_NEWGRFS)(cs);
87aa45c64f8f (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: 6105
diff changeset
   710
	}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   711
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   712
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   713
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   714
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   715
	NetworkPasswordType type;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   716
	char password[NETWORK_PASSWORD_LENGTH];
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   717
	const NetworkClientInfo *ci;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   718
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   719
	type = (NetworkPasswordType)p->Recv_uint8();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   720
	p->Recv_string(password, sizeof(password));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   721
6578
b6d4f973f81a (svn r9061) -Fix [r9038, FS#668]: passwords are send when the server is in the 'authorizing' state, not 'inactive'.
rubidium
parents: 6573
diff changeset
   722
	if (cs->status == STATUS_AUTHORIZING && type == NETWORK_GAME_PASSWORD) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   723
		// Check game-password
10818
95a5bba4e812 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 10792
diff changeset
   724
		if (strcmp(password, _settings_client.network.server_password) != 0) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   725
			// Password is invalid
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   726
			SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   727
			return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   728
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   729
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   730
		ci = DEREF_CLIENT_INFO(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   731
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
   732
		if (IsValidPlayer(ci->client_playas) && _network_player_info[ci->client_playas].password[0] != '\0') {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   733
			SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   734
			return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   735
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   736
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   737
		// Valid password, allow user
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   738
		SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   739
		return;
6578
b6d4f973f81a (svn r9061) -Fix [r9038, FS#668]: passwords are send when the server is in the 'authorizing' state, not 'inactive'.
rubidium
parents: 6573
diff changeset
   740
	} else if (cs->status == STATUS_AUTHORIZING && type == NETWORK_COMPANY_PASSWORD) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   741
		ci = DEREF_CLIENT_INFO(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   742
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
   743
		if (strcmp(password, _network_player_info[ci->client_playas].password) != 0) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   744
			// Password is invalid
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   745
			SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_WRONG_PASSWORD);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   746
			return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   747
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   748
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   749
		SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   750
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   751
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   752
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   753
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   754
	SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   755
	return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   756
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   757
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   758
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_GETMAP)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   759
{
5875
4a1391019791 (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: 5860
diff changeset
   760
	NetworkTCPSocketHandler *new_cs;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   761
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   762
	// The client was never joined.. so this is impossible, right?
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   763
	//  Ignore the packet, give the client a warning, and close his connection
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   764
	if (cs->status < STATUS_AUTH || cs->has_quit) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   765
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   766
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   767
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   768
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   769
	// Check if someone else is receiving the map
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   770
	FOR_ALL_CLIENTS(new_cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   771
		if (new_cs->status == STATUS_MAP) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   772
			// Tell the new client to wait
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   773
			cs->status = STATUS_MAP_WAIT;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   774
			SEND_COMMAND(PACKET_SERVER_WAIT)(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   775
			return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   776
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   777
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   778
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   779
	// We receive a request to upload the map.. give it to the client!
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   780
	SEND_COMMAND(PACKET_SERVER_MAP)(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   781
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   782
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   783
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   784
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   785
	// Client has the map, now start syncing
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   786
	if (cs->status == STATUS_DONE_MAP && !cs->has_quit) {
793
0fbdcb357a2c (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
   787
		char client_name[NETWORK_CLIENT_NAME_LENGTH];
5875
4a1391019791 (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: 5860
diff changeset
   788
		NetworkTCPSocketHandler *new_cs;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   789
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   790
		NetworkGetClientName(client_name, sizeof(client_name), cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   791
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
   792
		NetworkTextMessage(NETWORK_ACTION_JOIN, CC_DEFAULT, false, client_name, "");
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   793
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   794
		// Mark the client as pre-active, and wait for an ACK
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   795
		//  so we know he is done loading and in sync with us
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   796
		cs->status = STATUS_PRE_ACTIVE;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   797
		NetworkHandleCommandQueue(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   798
		SEND_COMMAND(PACKET_SERVER_FRAME)(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   799
		SEND_COMMAND(PACKET_SERVER_SYNC)(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   800
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   801
		// This is the frame the client receives
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   802
		//  we need it later on to make sure the client is not too slow
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   803
		cs->last_frame = _frame_counter;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   804
		cs->last_frame_server = _frame_counter;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   805
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   806
		FOR_ALL_CLIENTS(new_cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   807
			if (new_cs->status > STATUS_AUTH) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   808
				SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(new_cs, DEREF_CLIENT_INFO(cs));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   809
				SEND_COMMAND(PACKET_SERVER_JOIN)(new_cs, cs->index);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   810
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   811
		}
1602
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   812
10784
c3c7b558b4f1 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 10775
diff changeset
   813
		if (_settings_client.network.pause_on_join) {
1602
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   814
			/* Now pause the game till the client is in sync */
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   815
			DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   816
10792
95b6eb9fcc9e (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: 10784
diff changeset
   817
			NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game paused (incoming client)", NETWORK_SERVER_INDEX);
1602
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
   818
		}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   819
	} else {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   820
		// Wrong status for this packet, give a warning to client, and close connection
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   821
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   822
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   823
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   824
1804
7810fc0aa941 (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
/** Enforce the command flags.
7810fc0aa941 (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
 * Eg a server-only command can only be executed by a server, etc.
7810fc0aa941 (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
 * @param *cp the commandpacket that is going to be checked
7810fc0aa941 (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
 * @param *ci client information for debugging output to console
7810fc0aa941 (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
   829
 */
7810fc0aa941 (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
static bool CheckCommandFlags(const CommandPacket *cp, const NetworkClientInfo *ci)
7810fc0aa941 (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
{
7810fc0aa941 (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
	byte flags = GetCommandFlags(cp->cmd);
7810fc0aa941 (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
7810fc0aa941 (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
   834
	if (flags & CMD_SERVER && ci->client_index != NETWORK_SERVER_INDEX) {
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
   835
		IConsolePrintF(CC_ERROR, "WARNING: server only command from client %d (IP: %s), kicking...", ci->client_index, GetPlayerIP(ci));
1804
7810fc0aa941 (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
		return false;
7810fc0aa941 (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
   837
	}
7810fc0aa941 (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
   838
7810fc0aa941 (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
   839
	if (flags & CMD_OFFLINE) {
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
   840
		IConsolePrintF(CC_ERROR, "WARNING: offline only command from client %d (IP: %s), kicking...", ci->client_index, GetPlayerIP(ci));
1804
7810fc0aa941 (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
   841
		return false;
7810fc0aa941 (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
   842
	}
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   843
7991
05dd904bb306 (svn r11006) -Fix: spectators are not allowed to issue commands. Issue spotted by SmatZ.
rubidium
parents: 7718
diff changeset
   844
	if (cp->cmd != CMD_PLAYER_CTRL && !IsValidPlayer(cp->player) && ci->client_index != NETWORK_SERVER_INDEX) {
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
   845
		IConsolePrintF(CC_ERROR, "WARNING: spectator issueing command from client %d (IP: %s), kicking...", ci->client_index, GetPlayerIP(ci));
7991
05dd904bb306 (svn r11006) -Fix: spectators are not allowed to issue commands. Issue spotted by SmatZ.
rubidium
parents: 7718
diff changeset
   846
		return false;
05dd904bb306 (svn r11006) -Fix: spectators are not allowed to issue commands. Issue spotted by SmatZ.
rubidium
parents: 7718
diff changeset
   847
	}
05dd904bb306 (svn r11006) -Fix: spectators are not allowed to issue commands. Issue spotted by SmatZ.
rubidium
parents: 7718
diff changeset
   848
1804
7810fc0aa941 (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
	return true;
7810fc0aa941 (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
   850
}
7810fc0aa941 (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
   851
7810fc0aa941 (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
   852
/** The client has done a command and wants us to handle it
7810fc0aa941 (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
 * @param *cs the connected client that has sent the command
7810fc0aa941 (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
   854
 * @param *p the packet in which the command was sent
7810fc0aa941 (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
   855
 */
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   856
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   857
{
5875
4a1391019791 (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: 5860
diff changeset
   858
	NetworkTCPSocketHandler *new_cs;
1804
7810fc0aa941 (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
   859
	const NetworkClientInfo *ci;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   860
	byte callback;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   861
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   862
	// The client was never joined.. so this is impossible, right?
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   863
	//  Ignore the packet, give the client a warning, and close his connection
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   864
	if (cs->status < STATUS_DONE_MAP || cs->has_quit) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   865
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   866
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   867
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   868
7332
7192d58a77c9 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 7117
diff changeset
   869
	CommandPacket *cp = MallocT<CommandPacket>(1);
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   870
	cp->player = (Owner)p->Recv_uint8();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   871
	cp->cmd    = p->Recv_uint32();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   872
	cp->p1     = p->Recv_uint32();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   873
	cp->p2     = p->Recv_uint32();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   874
	cp->tile   = p->Recv_uint32();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   875
	p->Recv_string(cp->text, lengthof(cp->text));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   876
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   877
	callback = p->Recv_uint8();
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   878
7332
7192d58a77c9 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 7117
diff changeset
   879
	if (cs->has_quit) {
7192d58a77c9 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 7117
diff changeset
   880
		free(cp);
7192d58a77c9 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 7117
diff changeset
   881
		return;
7192d58a77c9 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 7117
diff changeset
   882
	}
1804
7810fc0aa941 (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
   883
7810fc0aa941 (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
   884
	ci = DEREF_CLIENT_INFO(cs);
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   885
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   886
	/* Check if cp->cmd is valid */
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   887
	if (!IsValidCommand(cp->cmd)) {
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
   888
		IConsolePrintF(CC_ERROR, "WARNING: invalid command from client %d (IP: %s).", ci->client_index, GetPlayerIP(ci));
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   889
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
7332
7192d58a77c9 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 7117
diff changeset
   890
		free(cp);
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   891
		return;
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   892
	}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   893
1804
7810fc0aa941 (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
   894
	if (!CheckCommandFlags(cp, ci)) {
7810fc0aa941 (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
   895
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_KICKED);
7332
7192d58a77c9 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 7117
diff changeset
   896
		free(cp);
1804
7810fc0aa941 (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
		return;
7810fc0aa941 (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
   898
	}
7810fc0aa941 (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
   899
7810fc0aa941 (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
   900
	/** Only CMD_PLAYER_CTRL is always allowed, for the rest, playas needs
7810fc0aa941 (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
   901
	 * to match the player in the packet. If it doesn't, the client has done
7810fc0aa941 (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
   902
	 * something pretty naughty (or a bug), and will be kicked
7810fc0aa941 (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
   903
	 */
11158
2004a5570047 (svn r13716) -Fix [FS#2144]: any player could construct new companies.
rubidium
parents: 11156
diff changeset
   904
	if (!(cp->cmd == CMD_PLAYER_CTRL && cp->p1 == 0 && ci->client_playas == PLAYER_NEW_COMPANY) && ci->client_playas != cp->player) {
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
   905
		IConsolePrintF(CC_ERROR, "WARNING: player %d (IP: %s) tried to execute a command as player %d, kicking...",
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
   906
		               ci->client_playas + 1, GetPlayerIP(ci), cp->player + 1);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   907
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_PLAYER_MISMATCH);
7332
7192d58a77c9 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 7117
diff changeset
   908
		free(cp);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   909
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   910
	}
748
b48388344610 (svn r1204) -Add: [Network] Added some cheaters-protection (money-cheat mostly)
truelight
parents: 734
diff changeset
   911
1804
7810fc0aa941 (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
	/** @todo CMD_PLAYER_CTRL with p1 = 0 announces a new player to the server. To give the
7810fc0aa941 (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
	 * player the correct ID, the server injects p2 and executes the command. Any other p1
7810fc0aa941 (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
   914
	 * is prohibited. Pretty ugly and should be redone together with its function.
7810fc0aa941 (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
	 * @see CmdPlayerCtrl() players.c:655
7810fc0aa941 (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
	 */
7810fc0aa941 (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
	if (cp->cmd == CMD_PLAYER_CTRL) {
7810fc0aa941 (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
   918
		if (cp->p1 != 0) {
7810fc0aa941 (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
   919
			SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_CHEATER);
7332
7192d58a77c9 (svn r10075) -Fix [FS#844]: small memory leak on some early returns.
rubidium
parents: 7117
diff changeset
   920
			free(cp);
1804
7810fc0aa941 (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
   921
			return;
7810fc0aa941 (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
   922
		}
7810fc0aa941 (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
   923
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   924
		/* XXX - Execute the command as a valid player. Normally this would be done by a
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   925
		 * spectator, but that is not allowed any commands. So do an impersonation. The drawback
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   926
		 * of this is that the first company's last_built_tile is also updated... */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   927
		cp->player = OWNER_BEGIN;
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   928
		cp->p2 = cs - _clients; // XXX - UGLY! p2 is mis-used to get the client-id in CmdPlayerCtrl
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   929
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   930
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   931
	// The frame can be executed in the same frame as the next frame-packet
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   932
	//  That frame just before that frame is saved in _frame_counter_max
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   933
	cp->frame = _frame_counter_max + 1;
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
   934
	cp->next  = NULL;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   935
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   936
	// Queue the command for the clients (are send at the end of the frame
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   937
	//   if they can handle it ;))
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   938
	FOR_ALL_CLIENTS(new_cs) {
5569
e84e1f365344 (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: 5568
diff changeset
   939
		if (new_cs->status >= STATUS_MAP) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   940
			// Callbacks are only send back to the client who sent them in the
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   941
			//  first place. This filters that out.
1804
7810fc0aa941 (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
   942
			cp->callback = (new_cs != cs) ? 0 : callback;
7718
220603f0122e (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: 7636
diff changeset
   943
			cp->my_cmd = (new_cs == cs);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   944
			NetworkAddCommandQueue(new_cs, cp);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   945
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   946
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   947
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   948
	cp->callback = 0;
7718
220603f0122e (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: 7636
diff changeset
   949
	cp->my_cmd = false;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   950
	// Queue the command on the server
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   951
	if (_local_command_queue == NULL) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   952
		_local_command_queue = cp;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   953
	} else {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   954
		// Find last packet
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   955
		CommandPacket *c = _local_command_queue;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   956
		while (c->next != NULL) c = c->next;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   957
		c->next = cp;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   958
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   959
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   960
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   961
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ERROR)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   962
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   963
	// This packets means a client noticed an error and is reporting this
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   964
	//  to us. Display the error and report it to the other clients
5875
4a1391019791 (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: 5860
diff changeset
   965
	NetworkTCPSocketHandler *new_cs;
3437
2768c018e5f7 (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
   966
	char str[100];
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   967
	char client_name[NETWORK_CLIENT_NAME_LENGTH];
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
   968
	NetworkErrorCode errorno = (NetworkErrorCode)p->Recv_uint8();
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   969
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   970
	// The client was never joined.. thank the client for the packet, but ignore it
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   971
	if (cs->status < STATUS_DONE_MAP || cs->has_quit) {
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   972
		cs->has_quit = true;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   973
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   974
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   975
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   976
	NetworkGetClientName(client_name, sizeof(client_name), cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   977
4912
d04b3f2bca70 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4906
diff changeset
   978
	GetNetworkErrorMsg(str, errorno, lastof(str));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   979
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
   980
	DEBUG(net, 2, "'%s' reported an error and is closing its connection (%s)", client_name, str);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   981
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
   982
	NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "%s", str);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   983
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   984
	FOR_ALL_CLIENTS(new_cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   985
		if (new_cs->status > STATUS_AUTH) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   986
			SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, errorno);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   987
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   988
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   989
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   990
	cs->has_quit = true;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   991
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   992
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   993
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_QUIT)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   994
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   995
	// The client wants to leave. Display this and report it to the other
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
   996
	//  clients.
5875
4a1391019791 (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: 5860
diff changeset
   997
	NetworkTCPSocketHandler *new_cs;
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   998
	char str[100];
793
0fbdcb357a2c (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
   999
	char client_name[NETWORK_CLIENT_NAME_LENGTH];
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1000
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1001
	// The client was never joined.. thank the client for the packet, but ignore it
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1002
	if (cs->status < STATUS_DONE_MAP || cs->has_quit) {
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1003
		cs->has_quit = true;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1004
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1005
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1006
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
  1007
	p->Recv_string(str, lengthof(str));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1008
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1009
	NetworkGetClientName(client_name, sizeof(client_name), cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1010
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
  1011
	NetworkTextMessage(NETWORK_ACTION_LEAVE, CC_DEFAULT, false, client_name, "%s", str);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1012
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1013
	FOR_ALL_CLIENTS(new_cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1014
		if (new_cs->status > STATUS_AUTH) {
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1015
			SEND_COMMAND(PACKET_SERVER_QUIT)(new_cs, cs->index, str);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1016
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1017
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1018
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1019
	cs->has_quit = true;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1020
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1021
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1022
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_ACK)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1023
{
10857
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1024
	if (cs->status < STATUS_AUTH) {
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1025
		/* Illegal call, return error and ignore the packet */
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1026
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED);
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1027
		return;
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1028
	}
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1029
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
  1030
	uint32 frame = p->Recv_uint32();
1602
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1031
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1032
	/* The client is trying to catch up with the server */
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1033
	if (cs->status == STATUS_PRE_ACTIVE) {
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1034
		/* The client is not yet catched up? */
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1035
		if (frame + DAY_TICKS < _frame_counter) return;
1602
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1036
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1037
		/* Now he is! Unpause the game */
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1038
		cs->status = STATUS_ACTIVE;
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1039
10784
c3c7b558b4f1 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 10775
diff changeset
  1040
		if (_settings_client.network.pause_on_join) {
1602
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1041
			DoCommandP(0, 0, 0, NULL, CMD_PAUSE);
10792
95b6eb9fcc9e (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: 10784
diff changeset
  1042
			NetworkServerSendChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused (client connected)", NETWORK_SERVER_INDEX);
1602
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1043
		}
4713
e8628d1db064 (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
  1044
4717
873a0ee211af (svn r6629) - Fix (r6628): One check for the number of players got misplaced by patch...
peter1138
parents: 4716
diff changeset
  1045
		CheckMinPlayers();
873a0ee211af (svn r6629) - Fix (r6628): One check for the number of players got misplaced by patch...
peter1138
parents: 4716
diff changeset
  1046
4713
e8628d1db064 (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
  1047
		/* Execute script for, e.g. MOTD */
e8628d1db064 (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
  1048
		IConsoleCmdExec("exec scripts/on_server_connect.scr 0");
1602
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1049
	}
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1050
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1051
	// The client received the frame, make note of it
1602
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1052
	cs->last_frame = frame;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1053
	// With those 2 values we can calculate the lag realtime
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1054
	cs->last_frame_server = _frame_counter;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1055
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1056
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1057
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1058
10792
95b6eb9fcc9e (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: 10784
diff changeset
  1059
void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, const char *msg, uint16 from_index)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1060
{
5875
4a1391019791 (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: 5860
diff changeset
  1061
	NetworkTCPSocketHandler *cs;
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1062
	const NetworkClientInfo *ci, *ci_own, *ci_to;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1063
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1064
	switch (desttype) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1065
	case DESTTYPE_CLIENT:
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1066
		/* Are we sending to the server? */
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1067
		if (dest == NETWORK_SERVER_INDEX) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1068
			ci = NetworkFindClientInfoFromIndex(from_index);
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1069
			/* Display the text locally, and that is it */
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1070
			if (ci != NULL)
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
  1071
				NetworkTextMessage(action, (ConsoleColour)GetDrawStringPlayerColor(ci->client_playas), false, ci->client_name, "%s", msg);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1072
		} else {
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1073
			/* Else find the client to send the message to */
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1074
			FOR_ALL_CLIENTS(cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1075
				if (cs->index == dest) {
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1076
					SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_index, false, msg);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1077
					break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1078
				}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1079
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1080
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1081
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1082
		// Display the message locally (so you know you have sent it)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1083
		if (from_index != dest) {
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1084
			if (from_index == NETWORK_SERVER_INDEX) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1085
				ci = NetworkFindClientInfoFromIndex(from_index);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1086
				ci_to = NetworkFindClientInfoFromIndex(dest);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1087
				if (ci != NULL && ci_to != NULL)
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
  1088
					NetworkTextMessage(action, (ConsoleColour)GetDrawStringPlayerColor(ci->client_playas), true, ci_to->client_name, "%s", msg);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1089
			} else {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1090
				FOR_ALL_CLIENTS(cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1091
					if (cs->index == from_index) {
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1092
						SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, dest, true, msg);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1093
						break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1094
					}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1095
				}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1096
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1097
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1098
		break;
4906
8a10678a62a3 (svn r6876) -Codechange: Change the naming of _PLAYER in DESTTYPE_PLAYER/NETWORK_ACTION_CHAT_PLAYER
Darkvater
parents: 4880
diff changeset
  1099
	case DESTTYPE_TEAM: {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1100
		bool show_local = true; // If this is false, the message is already displayed
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1101
														// on the client who did sent it.
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1102
		/* Find all clients that belong to this player */
733
2ac0ff0e5797 (svn r1185) -Fix: [Network] Send to team now results in correct company on the sender
truelight
parents: 722
diff changeset
  1103
		ci_to = NULL;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1104
		FOR_ALL_CLIENTS(cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1105
			ci = DEREF_CLIENT_INFO(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1106
			if (ci->client_playas == dest) {
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1107
				SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_index, false, msg);
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1108
				if (cs->index == from_index) show_local = false;
733
2ac0ff0e5797 (svn r1185) -Fix: [Network] Send to team now results in correct company on the sender
truelight
parents: 722
diff changeset
  1109
				ci_to = ci; // Remember a client that is in the company for company-name
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1110
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1111
		}
733
2ac0ff0e5797 (svn r1185) -Fix: [Network] Send to team now results in correct company on the sender
truelight
parents: 722
diff changeset
  1112
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1113
		ci = NetworkFindClientInfoFromIndex(from_index);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1114
		ci_own = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1115
		if (ci != NULL && ci_own != NULL && ci_own->client_playas == dest) {
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
  1116
			NetworkTextMessage(action, (ConsoleColour)GetDrawStringPlayerColor(ci->client_playas), false, ci->client_name, "%s", msg);
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1117
			if (from_index == NETWORK_SERVER_INDEX) show_local = false;
1834
33612fb9c7d7 (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
  1118
			ci_to = ci_own;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1119
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1120
733
2ac0ff0e5797 (svn r1185) -Fix: [Network] Send to team now results in correct company on the sender
truelight
parents: 722
diff changeset
  1121
		/* There is no such player */
1834
33612fb9c7d7 (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
  1122
		if (ci_to == NULL) break;
733
2ac0ff0e5797 (svn r1185) -Fix: [Network] Send to team now results in correct company on the sender
truelight
parents: 722
diff changeset
  1123
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1124
		// Display the message locally (so you know you have sent it)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1125
		if (ci != NULL && show_local) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1126
			if (from_index == NETWORK_SERVER_INDEX) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1127
				char name[NETWORK_NAME_LENGTH];
7556
de5c6eb5465d (svn r10325) -Fix (r10323): Missed 3 company name references
peter1138
parents: 7448
diff changeset
  1128
				StringID str = IsValidPlayer(ci_to->client_playas) ? STR_COMPANY_NAME : STR_NETWORK_SPECTATORS;
de5c6eb5465d (svn r10325) -Fix (r10323): Missed 3 company name references
peter1138
parents: 7448
diff changeset
  1129
				SetDParam(0, ci_to->client_playas);
4945
c5c9bfb156fb (svn r6933) -Feature: Allow spectators to speak to eachother (team)
Darkvater
parents: 4944
diff changeset
  1130
				GetString(name, str, lastof(name));
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
  1131
				NetworkTextMessage(action, (ConsoleColour)GetDrawStringPlayerColor(ci_own->client_playas), true, name, "%s", msg);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1132
			} else {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1133
				FOR_ALL_CLIENTS(cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1134
					if (cs->index == from_index) {
779
1abefd084ffb (svn r1246) -Fix: [ 1090099 ] Company-messages did crash the game, because
truelight
parents: 748
diff changeset
  1135
						SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, ci_to->client_index, true, msg);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1136
					}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1137
				}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1138
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1139
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1140
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1141
		break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1142
	default:
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
  1143
		DEBUG(net, 0, "[server] received unknown chat destination type %d. Doing broadcast instead", desttype);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1144
		/* fall-through to next case */
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1145
	case DESTTYPE_BROADCAST:
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1146
		FOR_ALL_CLIENTS(cs) {
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
  1147
			SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_index, false, msg);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1148
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1149
		ci = NetworkFindClientInfoFromIndex(from_index);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1150
		if (ci != NULL)
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
  1151
			NetworkTextMessage(action, (ConsoleColour)GetDrawStringPlayerColor(ci->client_playas), false, ci->client_name, "%s", msg);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1152
		break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1153
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1154
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1155
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1156
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_CHAT)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1157
{
10857
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1158
	if (cs->status < STATUS_AUTH) {
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1159
		/* Illegal call, return error and ignore the packet */
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1160
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_AUTHORIZED);
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1161
		return;
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1162
	}
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1163
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
  1164
	NetworkAction action = (NetworkAction)p->Recv_uint8();
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
  1165
	DestType desttype = (DestType)p->Recv_uint8();
7027
f3d60745346d (svn r9719) -Fix: in-game private messages did not work for clients with a Client ID > 255.
rubidium
parents: 6585
diff changeset
  1166
	int dest = p->Recv_uint16();
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1167
	char msg[MAX_TEXT_MSG_LEN];
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1168
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
  1169
	p->Recv_string(msg, MAX_TEXT_MSG_LEN);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1170
10792
95b6eb9fcc9e (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: 10784
diff changeset
  1171
	NetworkServerSendChat(action, desttype, dest, msg, cs->index);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1172
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1173
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1174
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1175
{
10857
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1176
	if (cs->status != STATUS_ACTIVE) {
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1177
		/* Illegal call, return error and ignore the packet */
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1178
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1179
		return;
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1180
	}
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1181
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1182
	char password[NETWORK_PASSWORD_LENGTH];
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1183
	const NetworkClientInfo *ci;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1184
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
  1185
	p->Recv_string(password, sizeof(password));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1186
	ci = DEREF_CLIENT_INFO(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1187
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
  1188
	if (IsValidPlayer(ci->client_playas)) {
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
  1189
		ttd_strlcpy(_network_player_info[ci->client_playas].password, password, sizeof(_network_player_info[0].password));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1190
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1191
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1192
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1193
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1194
{
10857
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1195
	if (cs->status != STATUS_ACTIVE) {
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1196
		/* Illegal call, return error and ignore the packet */
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1197
		SEND_COMMAND(PACKET_SERVER_ERROR)(cs, NETWORK_ERROR_NOT_EXPECTED);
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1198
		return;
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1199
	}
469b8f8a7872 (svn r13408) -Codechange: some stricter checking.
rubidium
parents: 10839
diff changeset
  1200
793
0fbdcb357a2c (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1201
	char client_name[NETWORK_CLIENT_NAME_LENGTH];
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1202
	NetworkClientInfo *ci;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1203
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
  1204
	p->Recv_string(client_name, sizeof(client_name));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1205
	ci = DEREF_CLIENT_INFO(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1206
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1207
	if (cs->has_quit) return;
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 841
diff changeset
  1208
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1209
	if (ci != NULL) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1210
		// Display change
793
0fbdcb357a2c (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1211
		if (NetworkFindName(client_name)) {
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
  1212
			NetworkTextMessage(NETWORK_ACTION_NAME_CHANGE, CC_DEFAULT, false, ci->client_name, "%s", client_name);
793
0fbdcb357a2c (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1213
			ttd_strlcpy(ci->client_name, client_name, sizeof(ci->client_name));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1214
			NetworkUpdateClientInfo(ci->client_index);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1215
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1216
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1217
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1218
1026
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1219
DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_RCON)
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1220
{
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1221
	char pass[NETWORK_PASSWORD_LENGTH];
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1222
	char command[NETWORK_RCONCOMMAND_LENGTH];
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1223
10818
95a5bba4e812 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 10792
diff changeset
  1224
	if (StrEmpty(_settings_client.network.rcon_password)) return;
1026
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1225
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
  1226
	p->Recv_string(pass, sizeof(pass));
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
  1227
	p->Recv_string(command, sizeof(command));
1026
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1228
10818
95a5bba4e812 (svn r13369) -Codechange: remove duplication of network configuration settings between NetworkSettings and NetworkGameInfo.
rubidium
parents: 10792
diff changeset
  1229
	if (strcmp(pass, _settings_client.network.rcon_password) != 0) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
  1230
		DEBUG(net, 0, "[rcon] wrong password from client-id %d", cs->index);
1026
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1231
		return;
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1232
	}
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1233
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
  1234
	DEBUG(net, 0, "[rcon] client-id %d executed: '%s'", cs->index, command);
1026
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1235
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1236
	_redirect_console_to_client = cs->index;
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1237
	IConsoleCmdExec(command);
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1238
	_redirect_console_to_client = 0;
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1239
	return;
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1240
}
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1241
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1242
// The layout for the receive-functions by the server
5875
4a1391019791 (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: 5860
diff changeset
  1243
typedef void NetworkServerPacket(NetworkTCPSocketHandler *cs, Packet *p);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1244
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1245
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1246
// This array matches PacketType. At an incoming
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1247
//  packet it is matches against this array
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1248
//  and that way the right function to handle that
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1249
//  packet is found.
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1250
static NetworkServerPacket* const _network_server_packet[] = {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1251
	NULL, /*PACKET_SERVER_FULL,*/
841
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 839
diff changeset
  1252
	NULL, /*PACKET_SERVER_BANNED,*/
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1253
	RECEIVE_COMMAND(PACKET_CLIENT_JOIN),
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1254
	NULL, /*PACKET_SERVER_ERROR,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1255
	RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO),
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1256
	NULL, /*PACKET_SERVER_COMPANY_INFO,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1257
	NULL, /*PACKET_SERVER_CLIENT_INFO,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1258
	NULL, /*PACKET_SERVER_NEED_PASSWORD,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1259
	RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD),
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1260
	NULL, /*PACKET_SERVER_WELCOME,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1261
	RECEIVE_COMMAND(PACKET_CLIENT_GETMAP),
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1262
	NULL, /*PACKET_SERVER_WAIT,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1263
	NULL, /*PACKET_SERVER_MAP,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1264
	RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK),
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1265
	NULL, /*PACKET_SERVER_JOIN,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1266
	NULL, /*PACKET_SERVER_FRAME,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1267
	NULL, /*PACKET_SERVER_SYNC,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1268
	RECEIVE_COMMAND(PACKET_CLIENT_ACK),
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1269
	RECEIVE_COMMAND(PACKET_CLIENT_COMMAND),
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1270
	NULL, /*PACKET_SERVER_COMMAND,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1271
	RECEIVE_COMMAND(PACKET_CLIENT_CHAT),
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1272
	NULL, /*PACKET_SERVER_CHAT,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1273
	RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD),
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1274
	RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME),
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1275
	RECEIVE_COMMAND(PACKET_CLIENT_QUIT),
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1276
	RECEIVE_COMMAND(PACKET_CLIENT_ERROR),
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1277
	NULL, /*PACKET_SERVER_QUIT,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1278
	NULL, /*PACKET_SERVER_ERROR_QUIT,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1279
	NULL, /*PACKET_SERVER_SHUTDOWN,*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1280
	NULL, /*PACKET_SERVER_NEWGAME,*/
1026
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1281
	NULL, /*PACKET_SERVER_RCON,*/
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1017
diff changeset
  1282
	RECEIVE_COMMAND(PACKET_CLIENT_RCON),
6123
87aa45c64f8f (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: 6105
diff changeset
  1283
	NULL, /*PACKET_CLIENT_CHECK_NEWGRFS,*/
87aa45c64f8f (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: 6105
diff changeset
  1284
	RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED),
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1285
};
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1286
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1287
// If this fails, check the array above with network_data.h
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1288
assert_compile(lengthof(_network_server_packet) == PACKET_END);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1289
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1290
// This update the company_info-stuff
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6561
diff changeset
  1291
void NetworkPopulateCompanyInfo()
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1292
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1293
	char password[NETWORK_PASSWORD_LENGTH];
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1294
	const Player *p;
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1295
	const Vehicle *v;
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1296
	const Station *s;
5875
4a1391019791 (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: 5860
diff changeset
  1297
	NetworkTCPSocketHandler *cs;
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1298
	const NetworkClientInfo *ci;
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1299
	uint i;
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1300
	uint16 months_empty;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1301
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1302
	FOR_ALL_PLAYERS(p) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1303
		if (!p->is_active) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1304
			memset(&_network_player_info[p->index], 0, sizeof(NetworkPlayerInfo));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1305
			continue;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1306
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1307
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1308
		// Clean the info but not the password
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1309
		ttd_strlcpy(password, _network_player_info[p->index].password, sizeof(password));
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1310
		months_empty = _network_player_info[p->index].months_empty;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1311
		memset(&_network_player_info[p->index], 0, sizeof(NetworkPlayerInfo));
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1312
		_network_player_info[p->index].months_empty = months_empty;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1313
		ttd_strlcpy(_network_player_info[p->index].password, password, sizeof(_network_player_info[p->index].password));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1314
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1315
		// Grap the company name
7556
de5c6eb5465d (svn r10325) -Fix (r10323): Missed 3 company name references
peter1138
parents: 7448
diff changeset
  1316
		SetDParam(0, p->index);
de5c6eb5465d (svn r10325) -Fix (r10323): Missed 3 company name references
peter1138
parents: 7448
diff changeset
  1317
		GetString(_network_player_info[p->index].company_name, STR_COMPANY_NAME, lastof(_network_player_info[p->index].company_name));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1318
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1319
		// Check the income
4329
9759d5c52010 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4326
diff changeset
  1320
		if (_cur_year - 1 == p->inaugurated_year) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1321
			// The player is here just 1 year, so display [2], else display[1]
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1322
			for (i = 0; i < lengthof(p->yearly_expenses[2]); i++) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1323
				_network_player_info[p->index].income -= p->yearly_expenses[2][i];
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1324
			}
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1325
		} else {
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1326
			for (i = 0; i < lengthof(p->yearly_expenses[1]); i++) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1327
				_network_player_info[p->index].income -= p->yearly_expenses[1][i];
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1328
			}
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1329
		}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1330
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1331
		// Set some general stuff
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1332
		_network_player_info[p->index].inaugurated_year = p->inaugurated_year;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1333
		_network_player_info[p->index].company_value = p->old_economy[0].company_value;
7448
424ab8df8adb (svn r10207) -Codechange: remove the redundant player_money in favour of the money64, which is now renamed to player_money.
rubidium
parents: 7425
diff changeset
  1334
		_network_player_info[p->index].money = p->player_money;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1335
		_network_player_info[p->index].performance = p->old_economy[0].performance_history;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1336
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1337
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1338
	// Go through all vehicles and count the type of vehicles
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1339
	FOR_ALL_VEHICLES(v) {
8850
f52bfe770937 (svn r11920) -Fix (r11844): train count was incorrect in network lobby window (and 'players' command)
glx
parents: 8776
diff changeset
  1340
		if (!IsValidPlayer(v->owner) || !v->IsPrimaryVehicle()) continue;
8776
4a65bbcd6935 (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8766
diff changeset
  1341
		byte type = 0;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1342
		switch (v->type) {
8776
4a65bbcd6935 (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8766
diff changeset
  1343
			case VEH_TRAIN: type = 0; break;
4a65bbcd6935 (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8766
diff changeset
  1344
			case VEH_ROAD: type = (v->cargo_type != CT_PASSENGERS) ? 1 : 2; break;
4a65bbcd6935 (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8766
diff changeset
  1345
			case VEH_AIRCRAFT: type = 3; break;
4a65bbcd6935 (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8766
diff changeset
  1346
			case VEH_SHIP: type = 4; break;
4a65bbcd6935 (svn r11844) -Fix: road vehicle count was incorrect in network lobby window
glx
parents: 8766
diff changeset
  1347
			default: continue;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1348
		}
8850
f52bfe770937 (svn r11920) -Fix (r11844): train count was incorrect in network lobby window (and 'players' command)
glx
parents: 8776
diff changeset
  1349
		_network_player_info[v->owner].num_vehicle[type]++;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1350
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1351
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1352
	// Go through all stations and count the types of stations
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1353
	FOR_ALL_STATIONS(s) {
4850
b4e9be22945f (svn r6776) -Codechange: Use IsValidPlayer() function to determine of a PlayerID is an
Darkvater
parents: 4848
diff changeset
  1354
		if (IsValidPlayer(s->owner)) {
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1355
			NetworkPlayerInfo *npi = &_network_player_info[s->owner];
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1356
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1357
			if (s->facilities & FACIL_TRAIN)      npi->num_station[0]++;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1358
			if (s->facilities & FACIL_TRUCK_STOP) npi->num_station[1]++;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1359
			if (s->facilities & FACIL_BUS_STOP)   npi->num_station[2]++;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1360
			if (s->facilities & FACIL_AIRPORT)    npi->num_station[3]++;
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1361
			if (s->facilities & FACIL_DOCK)       npi->num_station[4]++;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1362
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1363
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1364
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1365
	ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1366
	// Register local player (if not dedicated)
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
  1367
	if (ci != NULL && IsValidPlayer(ci->client_playas))
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
  1368
		ttd_strlcpy(_network_player_info[ci->client_playas].players, ci->client_name, sizeof(_network_player_info[0].players));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1369
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1370
	FOR_ALL_CLIENTS(cs) {
793
0fbdcb357a2c (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1371
		char client_name[NETWORK_CLIENT_NAME_LENGTH];
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1372
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1373
		NetworkGetClientName(client_name, sizeof(client_name), cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1374
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1375
		ci = DEREF_CLIENT_INFO(cs);
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
  1376
		if (ci != NULL && IsValidPlayer(ci->client_playas)) {
5889
272800d151ac (svn r8097) Replace strlen() {==,!=,>} 0 by the more concise {,!}StrEmpty(). Additionally the test takes O(1) instead of O(n) now
tron
parents: 5875
diff changeset
  1377
			if (!StrEmpty(_network_player_info[ci->client_playas].players)) {
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
  1378
				ttd_strlcat(_network_player_info[ci->client_playas].players, ", ", lengthof(_network_player_info[0].players));
5889
272800d151ac (svn r8097) Replace strlen() {==,!=,>} 0 by the more concise {,!}StrEmpty(). Additionally the test takes O(1) instead of O(n) now
tron
parents: 5875
diff changeset
  1379
			}
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 667
diff changeset
  1380
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
  1381
			ttd_strlcat(_network_player_info[ci->client_playas].players, client_name, lengthof(_network_player_info[0].players));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1382
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1383
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1384
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1385
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1386
// Send a packet to all clients with updated info about this client_index
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1387
void NetworkUpdateClientInfo(uint16 client_index)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1388
{
5875
4a1391019791 (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: 5860
diff changeset
  1389
	NetworkTCPSocketHandler *cs;
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1390
	NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(client_index);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1391
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1392
	if (ci == NULL) return;
656
01540e6c0fed (svn r1090) -Fix: Made the _openttd_revision variable global, and with that
truelight
parents: 624
diff changeset
  1393
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1394
	FOR_ALL_CLIENTS(cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1395
		SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, ci);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1396
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1397
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1398
785
e9ca2bcc9c8f (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1399
/* Check if we want to restart the map */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6561
diff changeset
  1400
static void NetworkCheckRestartMap()
785
e9ca2bcc9c8f (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1401
{
10784
c3c7b558b4f1 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 10775
diff changeset
  1402
	if (_settings_client.network.restart_game_year != 0 && _cur_year >= _settings_client.network.restart_game_year) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
  1403
		DEBUG(net, 0, "Auto-restarting map. Year %d reached", _cur_year);
785
e9ca2bcc9c8f (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1404
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4293
diff changeset
  1405
		StartNewGameWithoutGUI(GENERATE_NEW_SEED);
785
e9ca2bcc9c8f (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1406
	}
e9ca2bcc9c8f (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1407
}
e9ca2bcc9c8f (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1408
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1409
/* Check if the server has autoclean_companies activated
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1410
    Two things happen:
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1411
      1) If a company is not protected, it is closed after 1 year (for example)
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1412
      2) If a company is protected, protection is disabled after 3 years (for example)
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1413
           (and item 1. happens a year later) */
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6561
diff changeset
  1414
static void NetworkAutoCleanCompanies()
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1415
{
5875
4a1391019791 (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: 5860
diff changeset
  1416
	NetworkTCPSocketHandler *cs;
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1417
	const NetworkClientInfo *ci;
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1418
	const Player *p;
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1419
	bool clients_in_company[MAX_PLAYERS];
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1420
10784
c3c7b558b4f1 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 10775
diff changeset
  1421
	if (!_settings_client.network.autoclean_companies) return;
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1422
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1423
	memset(clients_in_company, 0, sizeof(clients_in_company));
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1424
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1425
	/* Detect the active companies */
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1426
	FOR_ALL_CLIENTS(cs) {
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1427
		ci = DEREF_CLIENT_INFO(cs);
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
  1428
		if (IsValidPlayer(ci->client_playas)) clients_in_company[ci->client_playas] = true;
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1429
	}
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1430
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1431
	if (!_network_dedicated) {
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1432
		ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
4878
744717de172e (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4861
diff changeset
  1433
		if (IsValidPlayer(ci->client_playas)) clients_in_company[ci->client_playas] = true;
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1434
	}
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1435
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1436
	/* Go through all the comapnies */
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1437
	FOR_ALL_PLAYERS(p) {
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1438
		/* Skip the non-active once */
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1439
		if (!p->is_active || p->is_ai) continue;
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1440
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1441
		if (!clients_in_company[p->index]) {
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1442
			/* The company is empty for one month more */
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1443
			_network_player_info[p->index].months_empty++;
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1444
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1445
			/* Is the company empty for autoclean_unprotected-months, and is there no protection? */
10784
c3c7b558b4f1 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 10775
diff changeset
  1446
			if (_network_player_info[p->index].months_empty > _settings_client.network.autoclean_unprotected && _network_player_info[p->index].password[0] == '\0') {
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1447
				/* Shut the company down */
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1448
				DoCommandP(0, 2, p->index, NULL, CMD_PLAYER_CTRL);
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
  1449
				IConsolePrintF(CC_DEFAULT, "Auto-cleaned company #%d", p->index + 1);
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1450
			}
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1451
			/* Is the compnay empty for autoclean_protected-months, and there is a protection? */
10784
c3c7b558b4f1 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 10775
diff changeset
  1452
			if (_network_player_info[p->index].months_empty > _settings_client.network.autoclean_protected && _network_player_info[p->index].password[0] != '\0') {
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1453
				/* Unprotect the company */
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1454
				_network_player_info[p->index].password[0] = '\0';
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
  1455
				IConsolePrintF(CC_DEFAULT, "Auto-removed protection from company #%d", p->index+1);
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1456
				_network_player_info[p->index].months_empty = 0;
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1457
			}
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1458
		} else {
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1459
			/* It is not empty, reset the date */
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1460
			_network_player_info[p->index].months_empty = 0;
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1461
		}
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1462
	}
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1463
}
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1464
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1465
// This function changes new_name to a name that is unique (by adding #1 ...)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1466
//  and it returns true if that succeeded.
793
0fbdcb357a2c (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1467
bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH])
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1468
{
5875
4a1391019791 (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: 5860
diff changeset
  1469
	NetworkTCPSocketHandler *new_cs;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1470
	bool found_name = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1471
	byte number = 0;
793
0fbdcb357a2c (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1472
	char original_name[NETWORK_CLIENT_NAME_LENGTH];
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1473
3623
2dd90d28fe20 (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
  1474
	// We use NETWORK_CLIENT_NAME_LENGTH in here, because new_name is really a pointer
793
0fbdcb357a2c (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1475
	ttd_strlcpy(original_name, new_name, NETWORK_CLIENT_NAME_LENGTH);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1476
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1477
	while (!found_name) {
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1478
		const NetworkClientInfo *ci;
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1479
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1480
		found_name = true;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1481
		FOR_ALL_CLIENTS(new_cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1482
			ci = DEREF_CLIENT_INFO(new_cs);
4026
f6faccd92b1c (svn r5265) Replace a dozen bogus uses of strncmp() by strcmp()
tron
parents: 3623
diff changeset
  1483
			if (strcmp(ci->client_name, new_name) == 0) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1484
				// Name already in use
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1485
				found_name = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1486
				break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1487
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1488
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1489
		// Check if it is the same as the server-name
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1490
		ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1491
		if (ci != NULL) {
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1492
			if (strcmp(ci->client_name, new_name) == 0) found_name = false; // name already in use
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1493
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1494
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1495
		if (!found_name) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1496
			// Try a new name (<name> #1, <name> #2, and so on)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1497
826
0e2b569b737b (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 793
diff changeset
  1498
			// Stop if we tried for more than 50 times..
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1499
			if (number++ > 50) break;
793
0fbdcb357a2c (svn r1263) -Fix: [Network] Client-names are limited (serverside) to 25 chars
truelight
parents: 785
diff changeset
  1500
			snprintf(new_name, NETWORK_CLIENT_NAME_LENGTH, "%s #%d", original_name, number);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1501
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1502
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1503
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1504
	return found_name;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1505
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1506
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1507
// Reads a packet from the stream
5875
4a1391019791 (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: 5860
diff changeset
  1508
bool NetworkServer_ReadPackets(NetworkTCPSocketHandler *cs)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1509
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1510
	Packet *p;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1511
	NetworkRecvStatus res;
6153
2ecdbf81caa7 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 6151
diff changeset
  1512
	while ((p = cs->Recv_Packet(&res)) != NULL) {
6151
4b5e33f35bbe (svn r8523) -Codechange: move all the Network(Recv|Send)_(uintXX|string) functions to Packet.
rubidium
parents: 6149
diff changeset
  1513
		byte type = p->Recv_uint8();
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1514
		if (type < PACKET_END && _network_server_packet[type] != NULL && !cs->has_quit) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1515
			_network_server_packet[type](cs, p);
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1516
		} else {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5296
diff changeset
  1517
			DEBUG(net, 0, "[server] received invalid packet type %d", type);
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4026
diff changeset
  1518
		}
6149
44aedfd50791 (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: 6123
diff changeset
  1519
		delete p;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1520
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1521
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1522
	return true;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1523
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1524
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1525
// Handle the local command-queue
5875
4a1391019791 (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: 5860
diff changeset
  1526
static void NetworkHandleCommandQueue(NetworkTCPSocketHandler* cs)
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2676
diff changeset
  1527
{
2081
3339c5808316 (svn r2591) Codechange: [network] Cleanup NetworkHandleCommandQueue
ludde
parents: 2079
diff changeset
  1528
	CommandPacket *cp;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1529
2081
3339c5808316 (svn r2591) Codechange: [network] Cleanup NetworkHandleCommandQueue
ludde
parents: 2079
diff changeset
  1530
	while ( (cp = cs->command_queue) != NULL) {
3339c5808316 (svn r2591) Codechange: [network] Cleanup NetworkHandleCommandQueue
ludde
parents: 2079
diff changeset
  1531
		SEND_COMMAND(PACKET_SERVER_COMMAND)(cs, cp);
3339c5808316 (svn r2591) Codechange: [network] Cleanup NetworkHandleCommandQueue
ludde
parents: 2079
diff changeset
  1532
3339c5808316 (svn r2591) Codechange: [network] Cleanup NetworkHandleCommandQueue
ludde
parents: 2079
diff changeset
  1533
		cs->command_queue = cp->next;
3339c5808316 (svn r2591) Codechange: [network] Cleanup NetworkHandleCommandQueue
ludde
parents: 2079
diff changeset
  1534
		free(cp);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1535
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1536
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1537
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1538
// This is called every tick if this is a _network_server
2235
09e4565e5138 (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2204
diff changeset
  1539
void NetworkServer_Tick(bool send_frame)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1540
{
5875
4a1391019791 (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: 5860
diff changeset
  1541
	NetworkTCPSocketHandler *cs;
2079
5afd2e7fc3bd (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
  1542
#ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
5afd2e7fc3bd (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
  1543
	bool send_sync = false;
5afd2e7fc3bd (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
  1544
#endif
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1545
2079
5afd2e7fc3bd (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
  1546
#ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
10784
c3c7b558b4f1 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 10775
diff changeset
  1547
	if (_frame_counter >= _last_sync_frame + _settings_client.network.sync_freq) {
2079
5afd2e7fc3bd (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
  1548
		_last_sync_frame = _frame_counter;
5afd2e7fc3bd (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
		send_sync = true;
5afd2e7fc3bd (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
	}
5afd2e7fc3bd (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
5afd2e7fc3bd (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
  1552
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1553
	// Now we are done with the frame, inform the clients that they can
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1554
	//  do their frame!
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1555
	FOR_ALL_CLIENTS(cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1556
		// Check if the speed of the client is what we can expect from a client
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1557
		if (cs->status == STATUS_ACTIVE) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1558
			// 1 lag-point per day
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1559
			int lag = NetworkCalculateLag(cs) / DAY_TICKS;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1560
			if (lag > 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1561
				if (lag > 3) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1562
					// Client did still not report in after 4 game-day, drop him
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1563
					//  (that is, the 3 of above, + 1 before any lag is counted)
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
  1564
					IConsolePrintF(CC_ERROR,"Client #%d is dropped because the client did not respond for more than 4 game-days", cs->index);
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 692
diff changeset
  1565
					NetworkCloseClient(cs);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1566
					continue;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1567
				}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1568
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1569
				// Report once per time we detect the lag
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1570
				if (cs->lag_test == 0) {
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
  1571
					IConsolePrintF(CC_WARNING,"[%d] Client #%d is slow, try increasing *net_frame_freq to a higher value!", _frame_counter, cs->index);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1572
					cs->lag_test = 1;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1573
				}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1574
			} else {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1575
				cs->lag_test = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1576
			}
1602
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1577
		} else if (cs->status == STATUS_PRE_ACTIVE) {
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1578
			int lag = NetworkCalculateLag(cs);
10784
c3c7b558b4f1 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 10775
diff changeset
  1579
			if (lag > _settings_client.network.max_join_time) {
c3c7b558b4f1 (svn r13334) -Codechange: move the network settings variables from network/network_internal to settings.
rubidium
parents: 10775
diff changeset
  1580
				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
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1581
				NetworkCloseClient(cs);
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1317
diff changeset
  1582
			}
6561
75a3a9146abb (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: 6504
diff changeset
  1583
		} else if (cs->status == STATUS_INACTIVE) {
75a3a9146abb (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: 6504
diff changeset
  1584
			int lag = NetworkCalculateLag(cs);
75a3a9146abb (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: 6504
diff changeset
  1585
			if (lag > 4 * DAY_TICKS) {
10685
955ddec7961e (svn r13229) -Codechange: replace some global variables that are only initialised once and always with the same value with enums.
rubidium
parents: 10684
diff changeset
  1586
				IConsolePrintF(CC_ERROR,"Client #%d is dropped because it took longer than %d ticks to start the joining process", cs->index, 4 * DAY_TICKS);
6561
75a3a9146abb (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: 6504
diff changeset
  1587
				NetworkCloseClient(cs);
75a3a9146abb (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: 6504
diff changeset
  1588
			}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1589
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1590
2079
5afd2e7fc3bd (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
  1591
		if (cs->status >= STATUS_PRE_ACTIVE) {
5afd2e7fc3bd (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
  1592
			// Check if we can send command, and if we have anything in the queue
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1593
			NetworkHandleCommandQueue(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1594
2079
5afd2e7fc3bd (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
  1595
			// Send an updated _frame_counter_max to the client
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1596
			if (send_frame) SEND_COMMAND(PACKET_SERVER_FRAME)(cs);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1597
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1598
#ifndef ENABLE_NETWORK_SYNC_EVERY_FRAME
2079
5afd2e7fc3bd (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
			// Send a sync-check packet
4880
0708f34e3586 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1600
			if (send_sync) SEND_COMMAND(PACKET_SERVER_SYNC)(cs);
2079
5afd2e7fc3bd (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
  1601
#endif
5afd2e7fc3bd (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
		}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1603
	}
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 667
diff changeset
  1604
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 667
diff changeset
  1605
	/* See if we need to advertise */
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 667
diff changeset
  1606
	NetworkUDPAdvertise();
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1607
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1608
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6561
diff changeset
  1609
void NetworkServerYearlyLoop()
785
e9ca2bcc9c8f (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1610
{
e9ca2bcc9c8f (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1611
	NetworkCheckRestartMap();
e9ca2bcc9c8f (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1612
}
e9ca2bcc9c8f (svn r1252) -Add: [Network] With 'set restart_game_date' you can set the date for in
truelight
parents: 779
diff changeset
  1613
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6561
diff changeset
  1614
void NetworkServerMonthlyLoop()
690
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1615
{
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1616
	NetworkAutoCleanCompanies();
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1617
}
9f449a6b133e (svn r1131) -Add: [Network] Autoclean_companies (set it with 'set autoclean_companies on/off').
truelight
parents: 686
diff changeset
  1618
10792
95b6eb9fcc9e (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: 10784
diff changeset
  1619
void NetworkServerChangeOwner(PlayerID current_player, PlayerID new_player)
95b6eb9fcc9e (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: 10784
diff changeset
  1620
{
95b6eb9fcc9e (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: 10784
diff changeset
  1621
	/* The server has to handle all administrative issues, for example
95b6eb9fcc9e (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: 10784
diff changeset
  1622
	 * updating and notifying all clients of what has happened */
95b6eb9fcc9e (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: 10784
diff changeset
  1623
	NetworkTCPSocketHandler *cs;
95b6eb9fcc9e (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: 10784
diff changeset
  1624
	NetworkClientInfo *ci = NetworkFindClientInfoFromIndex(NETWORK_SERVER_INDEX);
95b6eb9fcc9e (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: 10784
diff changeset
  1625
95b6eb9fcc9e (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: 10784
diff changeset
  1626
	/* The server has just changed from player */
95b6eb9fcc9e (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: 10784
diff changeset
  1627
	if (current_player == ci->client_playas) {
95b6eb9fcc9e (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: 10784
diff changeset
  1628
		ci->client_playas = new_player;
95b6eb9fcc9e (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: 10784
diff changeset
  1629
		NetworkUpdateClientInfo(NETWORK_SERVER_INDEX);
95b6eb9fcc9e (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: 10784
diff changeset
  1630
	}
95b6eb9fcc9e (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: 10784
diff changeset
  1631
95b6eb9fcc9e (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: 10784
diff changeset
  1632
	/* Find all clients that were in control of this company, and mark them as new_player */
95b6eb9fcc9e (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: 10784
diff changeset
  1633
	FOR_ALL_CLIENTS(cs) {
95b6eb9fcc9e (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: 10784
diff changeset
  1634
		ci = DEREF_CLIENT_INFO(cs);
95b6eb9fcc9e (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: 10784
diff changeset
  1635
		if (current_player == ci->client_playas) {
95b6eb9fcc9e (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: 10784
diff changeset
  1636
			ci->client_playas = new_player;
95b6eb9fcc9e (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: 10784
diff changeset
  1637
			NetworkUpdateClientInfo(ci->client_index);
95b6eb9fcc9e (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: 10784
diff changeset
  1638
		}
95b6eb9fcc9e (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: 10784
diff changeset
  1639
	}
95b6eb9fcc9e (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: 10784
diff changeset
  1640
}
95b6eb9fcc9e (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: 10784
diff changeset
  1641
95b6eb9fcc9e (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: 10784
diff changeset
  1642
const char* GetPlayerIP(const NetworkClientInfo* ci)
95b6eb9fcc9e (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: 10784
diff changeset
  1643
{
95b6eb9fcc9e (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: 10784
diff changeset
  1644
	struct in_addr addr;
95b6eb9fcc9e (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: 10784
diff changeset
  1645
95b6eb9fcc9e (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: 10784
diff changeset
  1646
	addr.s_addr = ci->client_ip;
95b6eb9fcc9e (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: 10784
diff changeset
  1647
	return inet_ntoa(addr);
95b6eb9fcc9e (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: 10784
diff changeset
  1648
}
95b6eb9fcc9e (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: 10784
diff changeset
  1649
95b6eb9fcc9e (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: 10784
diff changeset
  1650
void NetworkServerShowStatusToConsole()
95b6eb9fcc9e (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: 10784
diff changeset
  1651
{
95b6eb9fcc9e (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: 10784
diff changeset
  1652
	static const char* const stat_str[] = {
95b6eb9fcc9e (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: 10784
diff changeset
  1653
		"inactive",
95b6eb9fcc9e (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: 10784
diff changeset
  1654
		"authorizing",
95b6eb9fcc9e (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: 10784
diff changeset
  1655
		"authorized",
95b6eb9fcc9e (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: 10784
diff changeset
  1656
		"waiting",
95b6eb9fcc9e (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: 10784
diff changeset
  1657
		"loading map",
95b6eb9fcc9e (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: 10784
diff changeset
  1658
		"map done",
95b6eb9fcc9e (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: 10784
diff changeset
  1659
		"ready",
95b6eb9fcc9e (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: 10784
diff changeset
  1660
		"active"
95b6eb9fcc9e (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: 10784
diff changeset
  1661
	};
95b6eb9fcc9e (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: 10784
diff changeset
  1662
95b6eb9fcc9e (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: 10784
diff changeset
  1663
	NetworkTCPSocketHandler *cs;
95b6eb9fcc9e (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: 10784
diff changeset
  1664
	FOR_ALL_CLIENTS(cs) {
95b6eb9fcc9e (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: 10784
diff changeset
  1665
		int lag = NetworkCalculateLag(cs);
95b6eb9fcc9e (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: 10784
diff changeset
  1666
		const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
95b6eb9fcc9e (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: 10784
diff changeset
  1667
		const char* status;
95b6eb9fcc9e (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: 10784
diff changeset
  1668
95b6eb9fcc9e (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: 10784
diff changeset
  1669
		status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown");
95b6eb9fcc9e (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: 10784
diff changeset
  1670
		IConsolePrintF(CC_INFO, "Client #%1d  name: '%s'  status: '%s'  frame-lag: %3d  company: %1d  IP: %s  unique-id: '%s'",
95b6eb9fcc9e (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: 10784
diff changeset
  1671
			cs->index, ci->client_name, status, lag,
95b6eb9fcc9e (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: 10784
diff changeset
  1672
			ci->client_playas + (IsValidPlayer(ci->client_playas) ? 1 : 0),
95b6eb9fcc9e (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: 10784
diff changeset
  1673
			GetPlayerIP(ci), ci->unique_id);
95b6eb9fcc9e (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: 10784
diff changeset
  1674
	}
95b6eb9fcc9e (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: 10784
diff changeset
  1675
}
95b6eb9fcc9e (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: 10784
diff changeset
  1676
95b6eb9fcc9e (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: 10784
diff changeset
  1677
void NetworkServerSendRcon(uint16 client_index, ConsoleColour colour_code, const char *string)
95b6eb9fcc9e (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: 10784
diff changeset
  1678
{
95b6eb9fcc9e (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: 10784
diff changeset
  1679
	SEND_COMMAND(PACKET_SERVER_RCON)(NetworkFindClientStateFromIndex(client_index), colour_code, string);
95b6eb9fcc9e (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: 10784
diff changeset
  1680
}
95b6eb9fcc9e (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: 10784
diff changeset
  1681
95b6eb9fcc9e (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: 10784
diff changeset
  1682
void NetworkServerSendError(uint16 client_index, NetworkErrorCode error)
95b6eb9fcc9e (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: 10784
diff changeset
  1683
{
95b6eb9fcc9e (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: 10784
diff changeset
  1684
	SEND_COMMAND(PACKET_SERVER_ERROR)(NetworkFindClientStateFromIndex(client_index), error);
95b6eb9fcc9e (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: 10784
diff changeset
  1685
}
95b6eb9fcc9e (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: 10784
diff changeset
  1686
95b6eb9fcc9e (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: 10784
diff changeset
  1687
bool NetworkCompanyHasPlayers(PlayerID company)
95b6eb9fcc9e (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: 10784
diff changeset
  1688
{
95b6eb9fcc9e (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: 10784
diff changeset
  1689
	const NetworkTCPSocketHandler *cs;
95b6eb9fcc9e (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: 10784
diff changeset
  1690
	const NetworkClientInfo *ci;
95b6eb9fcc9e (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: 10784
diff changeset
  1691
	FOR_ALL_CLIENTS(cs) {
95b6eb9fcc9e (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: 10784
diff changeset
  1692
		ci = DEREF_CLIENT_INFO(cs);
95b6eb9fcc9e (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: 10784
diff changeset
  1693
		if (ci->client_playas == company) return true;
95b6eb9fcc9e (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: 10784
diff changeset
  1694
	}
95b6eb9fcc9e (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: 10784
diff changeset
  1695
	return false;
95b6eb9fcc9e (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: 10784
diff changeset
  1696
}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents:
diff changeset
  1697
#endif /* ENABLE_NETWORK */