src/network/network.cpp
author rubidium
Wed, 26 Dec 2007 13:50:40 +0000
changeset 8140 0d0d8c94f84b
parent 8139 4e91c448c409
child 8199 3c13a496ca80
permissions -rw-r--r--
(svn r11702) -Codechange: move all date related stuff to date*.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5432
diff changeset
     3
#include "../stdafx.h"
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
     4
#include "network_data.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
7564
fa54ab680589 (svn r11089) -Codechange: add revision detection to MSVC.
rubidium
parents: 7454
diff changeset
     6
extern const char _openttd_revision[];
663
80c66fd07863 (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
     7
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
     8
#ifdef ENABLE_NETWORK
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     9
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5432
diff changeset
    10
#include "../openttd.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5432
diff changeset
    11
#include "../debug.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5432
diff changeset
    12
#include "../string.h"
8114
dd6d21dc99c1 (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8024
diff changeset
    13
#include "../strings_func.h"
8139
4e91c448c409 (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8133
diff changeset
    14
#include "../map_func.h"
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8114
diff changeset
    15
#include "../command_func.h"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5432
diff changeset
    16
#include "../variables.h"
8140
0d0d8c94f84b (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8139
diff changeset
    17
#include "../date_func.h"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5432
diff changeset
    18
#include "../newgrf_config.h"
5475
2e6990a8c7c4 (svn r7759) -Merge: makefile rewrite. This merge features:
rubidium
parents: 5469
diff changeset
    19
#include "table/strings.h"
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    20
#include "network_client.h"
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    21
#include "network_server.h"
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    22
#include "network_udp.h"
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    23
#include "network_gamelist.h"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5432
diff changeset
    24
#include "core/udp.h"
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5432
diff changeset
    25
#include "core/tcp.h"
5523
623a8856e35b (svn r7829) -Codechange: move the network (core) initialization/shutdown functions into network/core, so the can be reused in the masterserver_updater.
rubidium
parents: 5521
diff changeset
    26
#include "core/core.h"
5432
5940d6a253c5 (svn r7638) -Codechange: Remove special window ShowJoinStatusWindowAfterJoin and shuffle around
Darkvater
parents: 5380
diff changeset
    27
#include "network_gui.h"
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5432
diff changeset
    28
#include "../console.h" /* IConsoleCmdExec */
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    29
#include <stdarg.h> /* va_list */
5469
7edfc643abbc (svn r7751) -Codechange: move network_* to a new network map. Furthermore move the low level network functions to network/core, so they can be reused by the masterserver and website-serverlist-updater.
rubidium
parents: 5432
diff changeset
    30
#include "../md5.h"
7187
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
    31
#include "../fileio.h"
8119
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8116
diff changeset
    32
#include "../texteff.hpp"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8119
diff changeset
    33
#include "../core/random_func.hpp"
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8119
diff changeset
    34
#include "../window_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
5810
d24e5ce302b9 (svn r8372) -Fix (8361): NUM_LANDSCAPE comes (via some detour) from openttd.h, which does not exist in the masterserver/updater.
rubidium
parents: 5704
diff changeset
    36
/* Check whether NETWORK_NUM_LANDSCAPES is still in sync with NUM_LANDSCAPE */
d24e5ce302b9 (svn r8372) -Fix (8361): NUM_LANDSCAPE comes (via some detour) from openttd.h, which does not exist in the masterserver/updater.
rubidium
parents: 5704
diff changeset
    37
assert_compile((int)NETWORK_NUM_LANDSCAPES == (int)NUM_LANDSCAPE);
d24e5ce302b9 (svn r8372) -Fix (8361): NUM_LANDSCAPE comes (via some detour) from openttd.h, which does not exist in the masterserver/updater.
rubidium
parents: 5704
diff changeset
    38
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    39
// global variables (declared in network_data.h)
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    40
CommandPacket *_local_command_queue;
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    41
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    42
extern NetworkUDPSocketHandler *_udp_client_socket; ///< udp client socket
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    43
extern NetworkUDPSocketHandler *_udp_server_socket; ///< udp server socket
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
    44
extern NetworkUDPSocketHandler *_udp_master_socket; ///< udp master socket
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    45
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    46
// Here we keep track of the clients
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    47
//  (and the client uses [0] for his own communication)
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
    48
NetworkTCPSocketHandler _clients[MAX_CLIENTS];
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    49
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    50
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    51
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    52
// The listen socket for the server
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    53
static SOCKET _listensocket;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    55
// The amount of clients connected
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    56
static byte _network_clients_connected = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    57
// The index counter for new clients (is never decreased)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    58
static uint16 _network_client_index = NETWORK_SERVER_INDEX + 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
    60
/* Some externs / forwards */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
    61
extern void StateGameLoop();
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
    62
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    63
// Function that looks up the CI for a given client-index
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    64
NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    65
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    66
	NetworkClientInfo *ci;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
4883
c9c7d15c0b0b (svn r6820) -Codechange: Some more const correctness, coding style.
Darkvater
parents: 4880
diff changeset
    68
	for (ci = _network_client_info; ci != endof(_network_client_info); ci++) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
    69
		if (ci->client_index == client_index) return ci;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
    70
	}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    71
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    72
	return NULL;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    73
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    74
2859
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    75
/** Return the CI for a given IP
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    76
 * @param ip IP of the client we are looking for. This must be in string-format
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    77
 * @return return a pointer to the corresponding NetworkClientInfo struct or NULL on failure */
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    78
NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip)
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    79
{
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    80
	NetworkClientInfo *ci;
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    81
	uint32 ip_number = inet_addr(ip);
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    82
4883
c9c7d15c0b0b (svn r6820) -Codechange: Some more const correctness, coding style.
Darkvater
parents: 4880
diff changeset
    83
	for (ci = _network_client_info; ci != endof(_network_client_info); ci++) {
2859
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    84
		if (ci->client_ip == ip_number) return ci;
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    85
	}
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    86
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    87
	return NULL;
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    88
}
ed471404f531 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    89
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    90
// Function that looks up the CS for a given client-index
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
    91
NetworkTCPSocketHandler *NetworkFindClientStateFromIndex(uint16 client_index)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    92
{
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
    93
	NetworkTCPSocketHandler *cs;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    94
5569
d89d1235add5 (svn r7984) -Fix: out-of-bounds read access on _clients array (Quark)
Darkvater
parents: 5524
diff changeset
    95
	for (cs = _clients; cs != endof(_clients); cs++) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
    96
		if (cs->index == client_index) return cs;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
    97
	}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    98
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    99
	return NULL;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   100
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   101
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   102
// NetworkGetClientName is a server-safe function to get the name of the client
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   103
//  if the user did not send it yet, Client #<no> is used.
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   104
void NetworkGetClientName(char *client_name, size_t size, const NetworkTCPSocketHandler *cs)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   105
{
3623
9b612a4bbd39 (svn r4521) - Fix: be consistent about the size of the player-name in MP. This hopefully fixes a crash on lesser OS's (eg Win98). Use ttd_strlcpy() to just copy over strings and properly terminate them because different implementations of snprintf() behave differently. Courtesy of TrueLight
Darkvater
parents: 3438
diff changeset
   106
	const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
   107
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
   108
	if (ci->client_name[0] == '\0') {
3623
9b612a4bbd39 (svn r4521) - Fix: be consistent about the size of the player-name in MP. This hopefully fixes a crash on lesser OS's (eg Win98). Use ttd_strlcpy() to just copy over strings and properly terminate them because different implementations of snprintf() behave differently. Courtesy of TrueLight
Darkvater
parents: 3438
diff changeset
   109
		snprintf(client_name, size, "Client #%4d", cs->index);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
   110
	} else {
3623
9b612a4bbd39 (svn r4521) - Fix: be consistent about the size of the player-name in MP. This hopefully fixes a crash on lesser OS's (eg Win98). Use ttd_strlcpy() to just copy over strings and properly terminate them because different implementations of snprintf() behave differently. Courtesy of TrueLight
Darkvater
parents: 3438
diff changeset
   111
		ttd_strlcpy(client_name, ci->client_name, size);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
   112
	}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   113
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   114
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
   115
byte NetworkSpectatorCount()
2944
2360b2da2bb0 (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: 2915
diff changeset
   116
{
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   117
	NetworkTCPSocketHandler *cs;
2944
2360b2da2bb0 (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: 2915
diff changeset
   118
	byte count = 0;
2360b2da2bb0 (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: 2915
diff changeset
   119
2360b2da2bb0 (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: 2915
diff changeset
   120
	FOR_ALL_CLIENTS(cs) {
4848
45f848b46222 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4829
diff changeset
   121
		if (DEREF_CLIENT_INFO(cs)->client_playas == PLAYER_SPECTATOR) count++;
2944
2360b2da2bb0 (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: 2915
diff changeset
   122
	}
2360b2da2bb0 (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: 2915
diff changeset
   123
2360b2da2bb0 (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: 2915
diff changeset
   124
	return count;
2360b2da2bb0 (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: 2915
diff changeset
   125
}
2360b2da2bb0 (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: 2915
diff changeset
   126
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   127
// This puts a text-message to the console, or in the future, the chat-box,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   128
//  (to keep it all a bit more general)
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   129
// If 'self_send' is true, this is the client who is sending the message
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   130
void CDECL NetworkTextMessage(NetworkAction action, uint16 color, bool self_send, const char *name, const char *str, ...)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   131
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   132
	char buf[1024];
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   133
	va_list va;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   134
	const int duration = 10; // Game days the messages stay visible
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   135
	char message[1024];
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   136
	char temp[1024];
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   137
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   138
	va_start(va, str);
2373
e073ec69bf56 (svn r2899) -Fix: Several format string vulnerabilities and buffer overflows in the network code
tron
parents: 2235
diff changeset
   139
	vsnprintf(buf, lengthof(buf), str, va);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   140
	va_end(va);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   141
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   142
	switch (action) {
4944
f597859bba38 (svn r6932) -Codechange: Send server messages with format NETWORK_ACTION_SERVER_MESSAGE so it is
Darkvater
parents: 4943
diff changeset
   143
		case NETWORK_ACTION_SERVER_MESSAGE:
f597859bba38 (svn r6932) -Codechange: Send server messages with format NETWORK_ACTION_SERVER_MESSAGE so it is
Darkvater
parents: 4943
diff changeset
   144
			color = 1;
f597859bba38 (svn r6932) -Codechange: Send server messages with format NETWORK_ACTION_SERVER_MESSAGE so it is
Darkvater
parents: 4943
diff changeset
   145
			snprintf(message, sizeof(message), "*** %s", buf);
f597859bba38 (svn r6932) -Codechange: Send server messages with format NETWORK_ACTION_SERVER_MESSAGE so it is
Darkvater
parents: 4943
diff changeset
   146
			break;
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   147
		case NETWORK_ACTION_JOIN:
4944
f597859bba38 (svn r6932) -Codechange: Send server messages with format NETWORK_ACTION_SERVER_MESSAGE so it is
Darkvater
parents: 4943
diff changeset
   148
			color = 1;
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4908
diff changeset
   149
			GetString(temp, STR_NETWORK_CLIENT_JOINED, lastof(temp));
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   150
			snprintf(message, sizeof(message), "*** %s %s", name, temp);
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   151
			break;
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   152
		case NETWORK_ACTION_LEAVE:
4944
f597859bba38 (svn r6932) -Codechange: Send server messages with format NETWORK_ACTION_SERVER_MESSAGE so it is
Darkvater
parents: 4943
diff changeset
   153
			color = 1;
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4908
diff changeset
   154
			GetString(temp, STR_NETWORK_ERR_LEFT, lastof(temp));
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   155
			snprintf(message, sizeof(message), "*** %s %s (%s)", name, temp, buf);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   156
			break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   157
		case NETWORK_ACTION_GIVE_MONEY:
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   158
			if (self_send) {
2055
4926ed1a7562 (svn r2564) Fix: Fixed conceptual issue in network_gui.c. AllocateName is not meant to be used by GUI-code, because it modifies the "game-state".
ludde
parents: 1899
diff changeset
   159
				SetDParamStr(0, name);
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   160
				SetDParam(1, atoi(buf));
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4908
diff changeset
   161
				GetString(temp, STR_NETWORK_GAVE_MONEY_AWAY, lastof(temp));
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   162
				snprintf(message, sizeof(message), "*** %s", temp);
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   163
			} else {
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   164
				SetDParam(0, atoi(buf));
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4908
diff changeset
   165
				GetString(temp, STR_NETWORK_GIVE_MONEY, lastof(temp));
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   166
				snprintf(message, sizeof(message), "*** %s %s", name, temp);
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   167
			}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   168
			break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   169
		case NETWORK_ACTION_NAME_CHANGE:
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4908
diff changeset
   170
			GetString(temp, STR_NETWORK_NAME_CHANGE, lastof(temp));
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   171
			snprintf(message, sizeof(message), "*** %s %s %s", name, temp, buf);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   172
			break;
4943
d3b8e26a7071 (svn r6931) -Codechange: Change textmessage format a bit. Only the sender's name and target are
Darkvater
parents: 4912
diff changeset
   173
		case NETWORK_ACTION_CHAT_COMPANY:
d3b8e26a7071 (svn r6931) -Codechange: Change textmessage format a bit. Only the sender's name and target are
Darkvater
parents: 4912
diff changeset
   174
			SetDParamStr(0, name);
d3b8e26a7071 (svn r6931) -Codechange: Change textmessage format a bit. Only the sender's name and target are
Darkvater
parents: 4912
diff changeset
   175
			SetDParamStr(1, buf);
d3b8e26a7071 (svn r6931) -Codechange: Change textmessage format a bit. Only the sender's name and target are
Darkvater
parents: 4912
diff changeset
   176
			GetString(temp, self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY, lastof(temp));
d3b8e26a7071 (svn r6931) -Codechange: Change textmessage format a bit. Only the sender's name and target are
Darkvater
parents: 4912
diff changeset
   177
			ttd_strlcpy(message, temp, sizeof(message));
d3b8e26a7071 (svn r6931) -Codechange: Change textmessage format a bit. Only the sender's name and target are
Darkvater
parents: 4912
diff changeset
   178
			break;
d3b8e26a7071 (svn r6931) -Codechange: Change textmessage format a bit. Only the sender's name and target are
Darkvater
parents: 4912
diff changeset
   179
		case NETWORK_ACTION_CHAT_CLIENT:
d3b8e26a7071 (svn r6931) -Codechange: Change textmessage format a bit. Only the sender's name and target are
Darkvater
parents: 4912
diff changeset
   180
			SetDParamStr(0, name);
d3b8e26a7071 (svn r6931) -Codechange: Change textmessage format a bit. Only the sender's name and target are
Darkvater
parents: 4912
diff changeset
   181
			SetDParamStr(1, buf);
d3b8e26a7071 (svn r6931) -Codechange: Change textmessage format a bit. Only the sender's name and target are
Darkvater
parents: 4912
diff changeset
   182
			GetString(temp, self_send ? STR_NETWORK_CHAT_TO_CLIENT : STR_NETWORK_CHAT_CLIENT, lastof(temp));
d3b8e26a7071 (svn r6931) -Codechange: Change textmessage format a bit. Only the sender's name and target are
Darkvater
parents: 4912
diff changeset
   183
			ttd_strlcpy(message, temp, sizeof(message));
d3b8e26a7071 (svn r6931) -Codechange: Change textmessage format a bit. Only the sender's name and target are
Darkvater
parents: 4912
diff changeset
   184
			break;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   185
		default:
2055
4926ed1a7562 (svn r2564) Fix: Fixed conceptual issue in network_gui.c. AllocateName is not meant to be used by GUI-code, because it modifies the "game-state".
ludde
parents: 1899
diff changeset
   186
			SetDParamStr(0, name);
4943
d3b8e26a7071 (svn r6931) -Codechange: Change textmessage format a bit. Only the sender's name and target are
Darkvater
parents: 4912
diff changeset
   187
			SetDParamStr(1, buf);
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4908
diff changeset
   188
			GetString(temp, STR_NETWORK_CHAT_ALL, lastof(temp));
4943
d3b8e26a7071 (svn r6931) -Codechange: Change textmessage format a bit. Only the sender's name and target are
Darkvater
parents: 4912
diff changeset
   189
			ttd_strlcpy(message, temp, sizeof(message));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   190
			break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   191
	}
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   192
7187
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
   193
#ifdef DEBUG_DUMP_COMMANDS
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
   194
	debug_dump_commands("ddc:cmsg:%d;%d;%s\n", _date, _date_fract, message);
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
   195
#endif /* DUMP_COMMANDS */
732
0b3002da2589 (svn r1184) -Fix: [Network] %p as chat message no longer gives funny result (tnx to
truelight
parents: 722
diff changeset
   196
	IConsolePrintF(color, "%s", message);
7454
e55eea8c05c7 (svn r10932) -Codechange: replace "text" with "chat" for the chat related function and variables.
rubidium
parents: 7220
diff changeset
   197
	AddChatMessage(color, duration, "%s", message);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   198
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   199
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   200
// Calculate the frame-lag of a client
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   201
uint NetworkCalculateLag(const NetworkTCPSocketHandler *cs)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   202
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   203
	int lag = cs->last_frame_server - cs->last_frame;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   204
	// This client has missed his ACK packet after 1 DAY_TICKS..
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   205
	//  so we increase his lag for every frame that passes!
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   206
	// The packet can be out by a max of _net_frame_freq
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   207
	if (cs->last_frame_server + DAY_TICKS + _network_frame_freq < _frame_counter)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   208
		lag += _frame_counter - (cs->last_frame_server + DAY_TICKS + _network_frame_freq);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   209
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   210
	return lag;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   211
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   212
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   213
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   214
// There was a non-recoverable error, drop back to the main menu with a nice
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   215
//  error
1095
b59632d9df1b (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   216
static void NetworkError(StringID error_string)
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 188
diff changeset
   217
{
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   218
	_switch_mode = SM_MENU;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   219
	_switch_mode_errorstr = error_string;
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   220
}
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   221
1010
977aafc6b8e3 (svn r1509) Const correctness and add static where appropriate while touching the lines anyway
tron
parents: 959
diff changeset
   222
static void ClientStartError(const char *error)
977aafc6b8e3 (svn r1509) Const correctness and add static where appropriate while touching the lines anyway
tron
parents: 959
diff changeset
   223
{
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   224
	DEBUG(net, 0, "[client] could not start network: %s",error);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   225
	NetworkError(STR_NETWORK_ERR_CLIENT_START);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   227
1010
977aafc6b8e3 (svn r1509) Const correctness and add static where appropriate while touching the lines anyway
tron
parents: 959
diff changeset
   228
static void ServerStartError(const char *error)
977aafc6b8e3 (svn r1509) Const correctness and add static where appropriate while touching the lines anyway
tron
parents: 959
diff changeset
   229
{
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   230
	DEBUG(net, 0, "[server] could not start network: %s",error);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   231
	NetworkError(STR_NETWORK_ERR_SERVER_START);
208
f3998d4089f0 (svn r209) -Fix: network code based desync
signde
parents: 206
diff changeset
   232
}
f3998d4089f0 (svn r209) -Fix: network code based desync
signde
parents: 206
diff changeset
   233
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   234
static void NetworkClientError(NetworkRecvStatus res, NetworkTCPSocketHandler* cs)
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
   235
{
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   236
	// First, send a CLIENT_ERROR to the server, so he knows we are
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   237
	//  disconnection (and why!)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   238
	NetworkErrorCode errorno;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   239
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   240
	// We just want to close the connection..
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   241
	if (res == NETWORK_RECV_STATUS_CLOSE_QUERY) {
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   242
		cs->has_quit = true;
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   243
		NetworkCloseClient(cs);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   244
		_networking = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   245
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   246
		DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   247
		return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   248
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   249
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2944
diff changeset
   250
	switch (res) {
5872
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5810
diff changeset
   251
		case NETWORK_RECV_STATUS_DESYNC:          errorno = NETWORK_ERROR_DESYNC; break;
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5810
diff changeset
   252
		case NETWORK_RECV_STATUS_SAVEGAME:        errorno = NETWORK_ERROR_SAVEGAME_FAILED; break;
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5810
diff changeset
   253
		case NETWORK_RECV_STATUS_NEWGRF_MISMATCH: errorno = NETWORK_ERROR_NEWGRF_MISMATCH; break;
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5810
diff changeset
   254
		default:                                  errorno = NETWORK_ERROR_GENERAL; break;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   255
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   256
	// This means we fucked up and the server closed the connection
841
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   257
	if (res != NETWORK_RECV_STATUS_SERVER_ERROR && res != NETWORK_RECV_STATUS_SERVER_FULL &&
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   258
			res != NETWORK_RECV_STATUS_SERVER_BANNED) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   259
		SEND_COMMAND(PACKET_CLIENT_ERROR)(errorno);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   260
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   261
		// Dequeue all commands before closing the socket
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5898
diff changeset
   262
		DEREF_CLIENT(0)->Send_Packets();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   263
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   264
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   265
	_switch_mode = SM_MENU;
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   266
	NetworkCloseClient(cs);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   267
	_networking = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   268
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   269
3437
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   270
/** Retrieve a string representation of an internal error number
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   271
 * @param buf buffer where the error message will be stored
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
   272
 * @param err NetworkErrorCode
3437
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   273
 * @return returns a pointer to the error message (buf) */
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4908
diff changeset
   274
char* GetNetworkErrorMsg(char* buf, NetworkErrorCode err, const char* last)
3437
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   275
{
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   276
	/* List of possible network errors, used by
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   277
	 * PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   278
	static const StringID network_error_strings[] = {
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   279
		STR_NETWORK_ERR_CLIENT_GENERAL,
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   280
		STR_NETWORK_ERR_CLIENT_DESYNC,
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   281
		STR_NETWORK_ERR_CLIENT_SAVEGAME,
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   282
		STR_NETWORK_ERR_CLIENT_CONNECTION_LOST,
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   283
		STR_NETWORK_ERR_CLIENT_PROTOCOL_ERROR,
5872
63eafeb3f393 (svn r8461) -Feature: check for NewGRF compatability before actually downloading the map from a game server when connecting from the command prompt and internal console.
rubidium
parents: 5810
diff changeset
   284
		STR_NETWORK_ERR_CLIENT_NEWGRF_MISMATCH,
3437
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   285
		STR_NETWORK_ERR_CLIENT_NOT_AUTHORIZED,
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   286
		STR_NETWORK_ERR_CLIENT_NOT_EXPECTED,
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   287
		STR_NETWORK_ERR_CLIENT_WRONG_REVISION,
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   288
		STR_NETWORK_ERR_CLIENT_NAME_IN_USE,
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   289
		STR_NETWORK_ERR_CLIENT_WRONG_PASSWORD,
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   290
		STR_NETWORK_ERR_CLIENT_PLAYER_MISMATCH,
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   291
		STR_NETWORK_ERR_CLIENT_KICKED,
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   292
		STR_NETWORK_ERR_CLIENT_CHEATER,
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
   293
		STR_NETWORK_ERR_CLIENT_SERVER_FULL
3437
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   294
	};
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   295
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   296
	if (err >= (ptrdiff_t)lengthof(network_error_strings)) err = NETWORK_ERROR_GENERAL;
3437
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   297
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4908
diff changeset
   298
	return GetString(buf, network_error_strings[err], last);
3437
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   299
}
044111ac24cf (svn r4267) - Fix (r4241): also validate the error number that a client receives from a server, and encapsulate this functionality into GetNetworkErrorMsg().
Darkvater
parents: 2952
diff changeset
   300
4716
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   301
/* Count the number of active clients connected */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
   302
static uint NetworkCountPlayers()
4716
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   303
{
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   304
	NetworkTCPSocketHandler *cs;
4716
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   305
	uint count = 0;
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   306
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   307
	FOR_ALL_CLIENTS(cs) {
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   308
		const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
4878
4d4f76a898fd (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4859
diff changeset
   309
		if (IsValidPlayer(ci->client_playas)) count++;
4716
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   310
	}
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   311
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   312
	return count;
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   313
}
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   314
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   315
static bool _min_players_paused = false;
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   316
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   317
/* Check if the minimum number of players has been reached and pause or unpause the game as appropriate */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
   318
void CheckMinPlayers()
4716
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   319
{
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   320
	if (!_network_dedicated) return;
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   321
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   322
	if (NetworkCountPlayers() < _network_min_players) {
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   323
		if (_min_players_paused) return;
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   324
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   325
		_min_players_paused = true;
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   326
		DoCommandP(0, 1, 0, NULL, CMD_PAUSE);
4944
f597859bba38 (svn r6932) -Codechange: Send server messages with format NETWORK_ACTION_SERVER_MESSAGE so it is
Darkvater
parents: 4943
diff changeset
   327
		NetworkServer_HandleChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game paused (not enough players)", NETWORK_SERVER_INDEX);
4716
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   328
	} else {
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   329
		if (!_min_players_paused) return;
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   330
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   331
		_min_players_paused = false;
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   332
		DoCommandP(0, 0, 0, NULL, CMD_PAUSE);
4944
f597859bba38 (svn r6932) -Codechange: Send server messages with format NETWORK_ACTION_SERVER_MESSAGE so it is
Darkvater
parents: 4943
diff changeset
   333
		NetworkServer_HandleChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused (enough players)", NETWORK_SERVER_INDEX);
4716
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   334
	}
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   335
}
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   336
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   337
// Find all IP-aliases for this host
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
   338
static void NetworkFindIPs()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   339
{
5976
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   340
#if !defined(PSP)
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   341
	int i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   342
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   343
#if defined(BEOS_NET_SERVER) /* doesn't have neither getifaddrs or net/if.h */
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   344
	/* Based on Andrew Bachmann's netstat+.c. Big thanks to him! */
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   345
	int _netstat(int fd, char **output, int verbose);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   346
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   347
	int seek_past_header(char **pos, const char *header) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   348
		char *new_pos = strstr(*pos, header);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   349
		if (new_pos == 0) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   350
			return B_ERROR;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   351
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   352
		*pos += strlen(header) + new_pos - *pos + 1;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   353
		return B_OK;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   354
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   355
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   356
	int output_length;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   357
	char *output_pointer = NULL;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   358
	char **output;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   359
	int sock = socket(AF_INET, SOCK_DGRAM, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   360
	i = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   361
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   362
	// If something fails, make sure the list is empty
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   363
	_broadcast_list[0] = 0;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   364
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   365
	if (sock < 0) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   366
		DEBUG(net, 0, "[core] error creating socket");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   367
		return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   368
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   369
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   370
	output_length = _netstat(sock, &output_pointer, 1);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   371
	if (output_length < 0) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   372
		DEBUG(net, 0, "[core] error running _netstat");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   373
		return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   374
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   375
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   376
	output = &output_pointer;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   377
	if (seek_past_header(output, "IP Interfaces:") == B_OK) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   378
		for (;;) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   379
			uint32 n, fields, read;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   380
			uint8 i1, i2, i3, i4, j1, j2, j3, j4;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   381
			struct in_addr inaddr;
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   382
			uint32 ip;
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   383
			uint32 netmask;
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   384
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   385
			fields = sscanf(*output, "%u: %hhu.%hhu.%hhu.%hhu, netmask %hhu.%hhu.%hhu.%hhu%n",
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   386
												&n, &i1,&i2,&i3,&i4, &j1,&j2,&j3,&j4, &read);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   387
			read += 1;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   388
			if (fields != 9) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   389
				break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   390
			}
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   391
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   392
			ip      = (uint32)i1 << 24 | (uint32)i2 << 16 | (uint32)i3 << 8 | (uint32)i4;
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   393
			netmask = (uint32)j1 << 24 | (uint32)j2 << 16 | (uint32)j3 << 8 | (uint32)j4;
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   394
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   395
			if (ip != INADDR_LOOPBACK && ip != INADDR_ANY) {
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   396
				inaddr.s_addr = htonl(ip | ~netmask);
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   397
				_broadcast_list[i] = inaddr.s_addr;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   398
				i++;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   399
			}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   400
			if (read < 0) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   401
				break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   402
			}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   403
			*output += read;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   404
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   405
		/* XXX - Using either one of these crashes openttd heavily? - wber */
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   406
		/*free(output_pointer);*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   407
		/*free(output);*/
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   408
		closesocket(sock);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   409
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   410
#elif defined(HAVE_GETIFADDRS)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   411
	struct ifaddrs *ifap, *ifa;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   412
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   413
	// If something fails, make sure the list is empty
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   414
	_broadcast_list[0] = 0;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   415
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   416
	if (getifaddrs(&ifap) != 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   418
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   419
	i = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   420
	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   421
		if (!(ifa->ifa_flags & IFF_BROADCAST)) continue;
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   422
		if (ifa->ifa_broadaddr == NULL) continue;
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   423
		if (ifa->ifa_broadaddr->sa_family != AF_INET) continue;
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   424
		_broadcast_list[i] = ((struct sockaddr_in*)ifa->ifa_broadaddr)->sin_addr.s_addr;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   425
		i++;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   426
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   427
	freeifaddrs(ifap);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   429
#else /* not HAVE_GETIFADDRS */
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   430
	SOCKET sock;
4033
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   431
#ifdef WIN32
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   432
	DWORD len = 0;
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   433
	INTERFACE_INFO ifo[MAX_INTERFACES];
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   434
	uint j;
4033
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   435
#else
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   436
	char buf[4 * 1024]; // Arbitrary buffer size
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   437
	struct ifconf ifconf;
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   438
	const char* buf_end;
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   439
	const char* p;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   440
#endif
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   441
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   442
	// If something fails, make sure the list is empty
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   443
	_broadcast_list[0] = 0;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   444
1466
93e67e9654c5 (svn r1970) Fix some warnings which Cygwin showed
tron
parents: 1332
diff changeset
   445
	sock = socket(AF_INET, SOCK_DGRAM, 0);
93e67e9654c5 (svn r1970) Fix some warnings which Cygwin showed
tron
parents: 1332
diff changeset
   446
	if (sock == INVALID_SOCKET) return;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   447
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   448
#ifdef WIN32
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   449
	memset(&ifo[0], 0, sizeof(ifo));
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   450
	if ((WSAIoctl(sock, SIO_GET_INTERFACE_LIST, NULL, 0, &ifo[0], sizeof(ifo), &len, NULL, NULL)) != 0) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   451
		closesocket(sock);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   452
		return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   453
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   454
4036
8e08ed32acfd (svn r5295) Handle a quirk in the Windows interface detection code and fix a typo in r5293 (thanks to Darkvater for testing)
tron
parents: 4034
diff changeset
   455
	i = 0;
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   456
	for (j = 0; j < len / sizeof(*ifo); j++) {
4036
8e08ed32acfd (svn r5295) Handle a quirk in the Windows interface detection code and fix a typo in r5293 (thanks to Darkvater for testing)
tron
parents: 4034
diff changeset
   457
		if (ifo[j].iiFlags & IFF_LOOPBACK) continue;
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   458
		if (!(ifo[j].iiFlags & IFF_BROADCAST)) continue;
4036
8e08ed32acfd (svn r5295) Handle a quirk in the Windows interface detection code and fix a typo in r5293 (thanks to Darkvater for testing)
tron
parents: 4034
diff changeset
   459
		/* iiBroadcast is unusable, because it always seems to be set to
8e08ed32acfd (svn r5295) Handle a quirk in the Windows interface detection code and fix a typo in r5293 (thanks to Darkvater for testing)
tron
parents: 4034
diff changeset
   460
		 * 255.255.255.255.
8e08ed32acfd (svn r5295) Handle a quirk in the Windows interface detection code and fix a typo in r5293 (thanks to Darkvater for testing)
tron
parents: 4034
diff changeset
   461
		 */
8e08ed32acfd (svn r5295) Handle a quirk in the Windows interface detection code and fix a typo in r5293 (thanks to Darkvater for testing)
tron
parents: 4034
diff changeset
   462
		_broadcast_list[i++] =
8e08ed32acfd (svn r5295) Handle a quirk in the Windows interface detection code and fix a typo in r5293 (thanks to Darkvater for testing)
tron
parents: 4034
diff changeset
   463
			 ifo[j].iiAddress.AddressIn.sin_addr.s_addr |
8e08ed32acfd (svn r5295) Handle a quirk in the Windows interface detection code and fix a typo in r5293 (thanks to Darkvater for testing)
tron
parents: 4034
diff changeset
   464
			~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr;
4033
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   465
	}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   466
#else
4033
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   467
	ifconf.ifc_len = sizeof(buf);
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   468
	ifconf.ifc_buf = buf;
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   469
	if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) {
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   470
		closesocket(sock);
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   471
		return;
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   472
	}
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   473
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   474
	i = 0;
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   475
	buf_end = buf + ifconf.ifc_len;
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   476
	for (p = buf; p < buf_end;) {
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   477
		const struct ifreq* req = (const struct ifreq*)p;
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   478
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   479
		if (req->ifr_addr.sa_family == AF_INET) {
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   480
			struct ifreq r;
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   481
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   482
			strncpy(r.ifr_name, req->ifr_name, lengthof(r.ifr_name));
4037
10d5b916cc92 (svn r5297) Check the interface flags if IFF_BROADCAST is set before asking for the broadcast address
tron
parents: 4036
diff changeset
   483
			if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 &&
10d5b916cc92 (svn r5297) Check the interface flags if IFF_BROADCAST is set before asking for the broadcast address
tron
parents: 4036
diff changeset
   484
					r.ifr_flags & IFF_BROADCAST &&
10d5b916cc92 (svn r5297) Check the interface flags if IFF_BROADCAST is set before asking for the broadcast address
tron
parents: 4036
diff changeset
   485
					ioctl(sock, SIOCGIFBRDADDR, &r) != -1) {
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   486
				_broadcast_list[i++] =
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   487
					((struct sockaddr_in*)&r.ifr_broadaddr)->sin_addr.s_addr;
4033
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   488
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   489
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
4033
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   491
		p += sizeof(struct ifreq);
6219
343fba5c9a44 (svn r9016) [SunOS] -Fix: Don't use a struct-member that doesn't exist.
celestar
parents: 6178
diff changeset
   492
#if defined(AF_LINK) && !defined(SUNOS)
4033
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   493
		p += req->ifr_addr.sa_len - sizeof(struct sockaddr);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   494
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   495
	}
4033
20c942fbe0c0 (svn r5292) -Fix: When using SIOCGIFCONF to detect network interfaces accomodate for the fact that struct sockaddr doesn't have fixed size in all implementations
tron
parents: 4000
diff changeset
   496
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   497
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   498
	closesocket(sock);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   499
#endif /* not HAVE_GETIFADDRS */
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   500
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   501
	_broadcast_list[i] = 0;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   502
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   503
	DEBUG(net, 3, "Detected broadcast addresses:");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   504
	// Now display to the debug all the detected ips
4034
581c6cb62e72 (svn r5293) -Fix: Not all network interfaces are capable of broadcasting. Don't record those which aren't
tron
parents: 4033
diff changeset
   505
	for (i = 0; _broadcast_list[i] != 0; i++) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   506
		DEBUG(net, 3, "%d) %s", i, inet_ntoa(*(struct in_addr *)&_broadcast_list[i]));//inet_ntoa(inaddr));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   507
	}
5976
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   508
#endif /* PSP */
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   509
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   510
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   511
// Resolve a hostname to a inet_addr
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   512
unsigned long NetworkResolveHost(const char *hostname)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   513
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   514
	in_addr_t ip;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   515
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   516
	// First try: is it an ip address?
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   517
	ip = inet_addr(hostname);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   518
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   519
	// If not try to resolve the name
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   520
	if (ip == INADDR_NONE) {
5976
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   521
		struct in_addr addr;
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   522
#if !defined(PSP)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   523
		struct hostent *he = gethostbyname(hostname);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   524
		if (he == NULL) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   525
			DEBUG(net, 0, "Cannot resolve '%s'", hostname);
5976
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   526
			return ip;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   527
		}
5976
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   528
		addr = *(struct in_addr *)he->h_addr_list[0];
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   529
#else
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   530
		int rid = -1;
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   531
		char buf[1024];
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   532
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   533
		/* Create a resolver */
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   534
		if (sceNetResolverCreate(&rid, buf, sizeof(buf)) < 0) {
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   535
			DEBUG(net, 0, "[NET] Error connecting resolver");
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   536
			return ip;
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   537
		}
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   538
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   539
		/* Try to resolve the name */
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   540
		if (sceNetResolverStartNtoA(rid, hostname, &addr, 2, 3) < 0) {
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   541
			DEBUG(net, 0, "[NET] Cannot resolve %s", hostname);
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   542
			sceNetResolverDelete(rid);
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   543
			return ip;
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   544
		}
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   545
		sceNetResolverDelete(rid);
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   546
#endif /* PSP */
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   547
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   548
		DEBUG(net, 1, "[NET] Resolved %s to %s", hostname, inet_ntoa(addr));
aa1cae2b975a (svn r8674) [PSP] -Add: added network code for PSP, based on the work of Turulo
truelight
parents: 5959
diff changeset
   549
		ip = addr.s_addr;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   550
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   551
	return ip;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   552
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   553
5704
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   554
/** Converts a string to ip/port/player
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   555
 *  Format: IP#player:port
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   556
 *
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   557
 * connection_string will be re-terminated to seperate out the hostname, and player and port will
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   558
 * be set to the player and port strings given by the user, inside the memory area originally
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   559
 * occupied by connection_string. */
1329
6988419aa6f0 (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   560
void ParseConnectionString(const char **player, const char **port, char *connection_string)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   561
{
1329
6988419aa6f0 (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   562
	char *p;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   563
	for (p = connection_string; *p != '\0'; p++) {
5704
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   564
		switch (*p) {
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   565
			case '#':
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   566
				*player = p + 1;
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   567
				*p = '\0';
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   568
				break;
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   569
			case ':':
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   570
				*port = p + 1;
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   571
				*p = '\0';
0e8e27d44b3c (svn r8168) -Regression (r6783): ParseConnectionstring didn't use the port parameter if a player was also specified. (both IP#Player:Port and IP:Port#Player btw)
Darkvater
parents: 5624
diff changeset
   572
				break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   573
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   575
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   577
// Creates a new client from a socket
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   578
//   Used both by the server and the client
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   579
static NetworkTCPSocketHandler *NetworkAllocClient(SOCKET s)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
{
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   581
	NetworkTCPSocketHandler *cs;
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   582
	byte client_no = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   584
	if (_network_server) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   585
		// Can we handle a new client?
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   586
		if (_network_clients_connected >= MAX_CLIENTS) return NULL;
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   587
		if (_network_game_info.clients_on >= _network_game_info.clients_max) return NULL;
206
7f8c26d8526b (svn r207) -Codechange: randomizer handling
signde
parents: 193
diff changeset
   588
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   589
		// Register the login
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   590
		client_no = _network_clients_connected++;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   591
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   592
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   593
	cs = DEREF_CLIENT(client_no);
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   594
	cs->Initialize();
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   595
	cs->sock = s;
1602
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1466
diff changeset
   596
	cs->last_frame = _frame_counter;
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1466
diff changeset
   597
	cs->last_frame_server = _frame_counter;
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1466
diff changeset
   598
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   599
	if (_network_server) {
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   600
		NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   601
		memset(ci, 0, sizeof(*ci));
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   602
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   603
		cs->index = _network_client_index++;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   604
		ci->client_index = cs->index;
4878
4d4f76a898fd (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4859
diff changeset
   605
		ci->client_playas = PLAYER_INACTIVE_CLIENT;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   606
		ci->join_date = _date;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   607
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   608
		InvalidateWindow(WC_CLIENT_LIST, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   609
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   610
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   611
	return cs;
206
7f8c26d8526b (svn r207) -Codechange: randomizer handling
signde
parents: 193
diff changeset
   612
}
7f8c26d8526b (svn r207) -Codechange: randomizer handling
signde
parents: 193
diff changeset
   613
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   614
// Close a connection
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   615
void NetworkCloseClient(NetworkTCPSocketHandler *cs)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
{
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   617
	NetworkClientInfo *ci;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   618
	// Socket is already dead
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   619
	if (cs->sock == INVALID_SOCKET) {
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   620
		cs->has_quit = true;
903
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 895
diff changeset
   621
		return;
a301a264e0fc (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 895
diff changeset
   622
	}
260
4819bcce8389 (svn r266) -Fix: hopefully fixed the desync problem nicely (and reverted the
truelight
parents: 256
diff changeset
   623
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   624
	DEBUG(net, 1, "Closed client connection %d", cs->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   626
	if (!cs->has_quit && _network_server && cs->status > STATUS_INACTIVE) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   627
		// We did not receive a leave message from this client...
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   628
		NetworkErrorCode errorno = NETWORK_ERROR_CONNECTION_LOST;
722
f420fa9bd521 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   629
		char str[100];
3623
9b612a4bbd39 (svn r4521) - Fix: be consistent about the size of the player-name in MP. This hopefully fixes a crash on lesser OS's (eg Win98). Use ttd_strlcpy() to just copy over strings and properly terminate them because different implementations of snprintf() behave differently. Courtesy of TrueLight
Darkvater
parents: 3438
diff changeset
   630
		char client_name[NETWORK_CLIENT_NAME_LENGTH];
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   631
		NetworkTCPSocketHandler *new_cs;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   633
		NetworkGetClientName(client_name, sizeof(client_name), cs);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   634
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4908
diff changeset
   635
		GetNetworkErrorMsg(str, errorno, lastof(str));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
2373
e073ec69bf56 (svn r2899) -Fix: Several format string vulnerabilities and buffer overflows in the network code
tron
parents: 2235
diff changeset
   637
		NetworkTextMessage(NETWORK_ACTION_LEAVE, 1, false, client_name, "%s", str);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   638
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   639
		// Inform other clients of this... strange leaving ;)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   640
		FOR_ALL_CLIENTS(new_cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   641
			if (new_cs->status > STATUS_AUTH && cs != new_cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   642
				SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, errorno);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   644
		}
1604
e5a1b1ff044d (svn r2108) -Fix: the server now also unpause when a client desyncs or something
truelight
parents: 1602
diff changeset
   645
	}
1602
79f98b4b83fc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1466
diff changeset
   646
1604
e5a1b1ff044d (svn r2108) -Fix: the server now also unpause when a client desyncs or something
truelight
parents: 1602
diff changeset
   647
	/* When the client was PRE_ACTIVE, the server was in pause mode, so unpause */
e5a1b1ff044d (svn r2108) -Fix: the server now also unpause when a client desyncs or something
truelight
parents: 1602
diff changeset
   648
	if (cs->status == STATUS_PRE_ACTIVE && _network_pause_on_join) {
e5a1b1ff044d (svn r2108) -Fix: the server now also unpause when a client desyncs or something
truelight
parents: 1602
diff changeset
   649
		DoCommandP(0, 0, 0, NULL, CMD_PAUSE);
4944
f597859bba38 (svn r6932) -Codechange: Send server messages with format NETWORK_ACTION_SERVER_MESSAGE so it is
Darkvater
parents: 4943
diff changeset
   650
		NetworkServer_HandleChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused", NETWORK_SERVER_INDEX);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   651
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   652
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5898
diff changeset
   653
	cs->Destroy();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   655
	// Close the gap in the client-list
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   656
	ci = DEREF_CLIENT_INFO(cs);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   657
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   658
	if (_network_server) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   659
		// We just lost one client :(
6347
b6f059f16318 (svn r9378) -Fix [FS#688] (r9038): cancel in password queries reduces amount of players in the network game when they haven't joined the game yet.
rubidium
parents: 6247
diff changeset
   660
		if (cs->status >= STATUS_AUTH) _network_game_info.clients_on--;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   661
		_network_clients_connected--;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   662
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   663
		while ((cs + 1) != DEREF_CLIENT(MAX_CLIENTS) && (cs + 1)->sock != INVALID_SOCKET) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   664
			*cs = *(cs + 1);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   665
			*ci = *(ci + 1);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   666
			cs++;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   667
			ci++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   668
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   669
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   670
		InvalidateWindow(WC_CLIENT_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   671
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   672
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   673
	// Reset the status of the last socket
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   674
	cs->sock = INVALID_SOCKET;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   675
	cs->status = STATUS_INACTIVE;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   676
	cs->index = NETWORK_EMPTY_INDEX;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   677
	ci->client_index = NETWORK_EMPTY_INDEX;
4716
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   678
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
   679
	CheckMinPlayers();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   680
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   681
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   682
// A client wants to connect to a server
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   683
static bool NetworkConnect(const char *hostname, int port)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   684
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   685
	SOCKET s;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   686
	struct sockaddr_in sin;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   687
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   688
	DEBUG(net, 1, "Connecting to %s %d", hostname, port);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   689
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   690
	s = socket(AF_INET, SOCK_STREAM, 0);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   691
	if (s == INVALID_SOCKET) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   692
		ClientStartError("socket() failed");
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   693
		return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   694
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   695
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   696
	if (!SetNoDelay(s)) DEBUG(net, 1, "Setting TCP_NODELAY failed");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   697
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   698
	sin.sin_family = AF_INET;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   699
	sin.sin_addr.s_addr = NetworkResolveHost(hostname);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   700
	sin.sin_port = htons(port);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   701
	_network_last_host_ip = sin.sin_addr.s_addr;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   702
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   703
	/* We failed to connect for which reason what so ever */
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   704
	if (connect(s, (struct sockaddr*) &sin, sizeof(sin)) != 0) return false;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   705
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   706
	if (!SetNonBlocking(s)) DEBUG(net, 0, "Setting non-blocking mode failed"); // XXX should this be an error?
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   707
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   708
	// in client mode, only the first client field is used. it's pointing to the server.
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   709
	NetworkAllocClient(s);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   710
5432
5940d6a253c5 (svn r7638) -Codechange: Remove special window ShowJoinStatusWindowAfterJoin and shuffle around
Darkvater
parents: 5380
diff changeset
   711
	_network_join_status = NETWORK_JOIN_STATUS_CONNECTING;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   712
	ShowJoinStatusWindow();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   713
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   714
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   715
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   717
// For the server, to accept new clients
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
   718
static void NetworkAcceptClients()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   719
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   720
	struct sockaddr_in sin;
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   721
	NetworkTCPSocketHandler *cs;
959
e6a3bbda610f (svn r1451) Fix some of the signed/unsigned comparison warnings
tron
parents: 903
diff changeset
   722
	uint i;
841
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   723
	bool banned;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   725
	// Should never ever happen.. is it possible??
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   726
	assert(_listensocket != INVALID_SOCKET);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   727
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   728
	for (;;) {
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   729
		socklen_t sin_len = sizeof(sin);
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   730
		SOCKET s = accept(_listensocket, (struct sockaddr*)&sin, &sin_len);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
		if (s == INVALID_SOCKET) return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   732
1332
0cae74c93b5d (svn r1836) Clean up some strange constructs concerning socket options
tron
parents: 1329
diff changeset
   733
		SetNonBlocking(s); // XXX error handling?
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   734
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   735
		DEBUG(net, 1, "Client connected from %s on frame %d", inet_ntoa(sin.sin_addr), _frame_counter);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   736
1332
0cae74c93b5d (svn r1836) Clean up some strange constructs concerning socket options
tron
parents: 1329
diff changeset
   737
		SetNoDelay(s); // XXX error handling?
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   738
841
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   739
		/* Check if the client is banned */
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   740
		banned = false;
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   741
		for (i = 0; i < lengthof(_network_ban_list); i++) {
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   742
			if (_network_ban_list[i] == NULL) continue;
841
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   743
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   744
			if (sin.sin_addr.s_addr == inet_addr(_network_ban_list[i])) {
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5897
diff changeset
   745
				Packet p(PACKET_SERVER_BANNED);
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5897
diff changeset
   746
				p.PrepareToSend();
841
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   747
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   748
				DEBUG(net, 1, "Banned ip tried to join (%s), refused", _network_ban_list[i]);
841
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   749
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5897
diff changeset
   750
				send(s, (const char*)p.buffer, p.size, 0);
841
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   751
				closesocket(s);
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   752
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   753
				banned = true;
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   754
				break;
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   755
			}
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   756
		}
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   757
		/* If this client is banned, continue with next client */
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   758
		if (banned) continue;
841
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   759
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   760
		cs = NetworkAllocClient(s);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   761
		if (cs == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   762
			// no more clients allowed?
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   763
			// Send to the client that we are full!
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5897
diff changeset
   764
			Packet p(PACKET_SERVER_FULL);
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5897
diff changeset
   765
			p.PrepareToSend();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   766
5898
4c682e9a58cc (svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
rubidium
parents: 5897
diff changeset
   767
			send(s, (const char*)p.buffer, p.size, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   768
			closesocket(s);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   769
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   770
			continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   771
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   772
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   773
		// a new client has connected. We set him at inactive for now
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   774
		//  maybe he is only requesting server-info. Till he has sent a PACKET_CLIENT_MAP_OK
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   775
		//  the client stays inactive
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   776
		cs->status = STATUS_INACTIVE;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   777
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   778
		DEREF_CLIENT_INFO(cs)->client_ip = sin.sin_addr.s_addr; // Save the IP of the client
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   779
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   780
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   781
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   782
// Set up the listen socket for the server
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
   783
static bool NetworkListen()
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
   784
{
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   785
	SOCKET ls;
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   786
	struct sockaddr_in sin;
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   787
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   788
	DEBUG(net, 1, "Listening on %s:%d", _network_server_bind_ip_host, _network_server_port);
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   789
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   790
	ls = socket(AF_INET, SOCK_STREAM, 0);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   791
	if (ls == INVALID_SOCKET) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   792
		ServerStartError("socket() on listen socket failed");
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   793
		return false;
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   794
	}
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   795
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   796
	{ // reuse the socket
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   797
		int reuse = 1;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   798
		// The (const char*) cast is needed for windows!!
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   799
		if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) == -1) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   800
			ServerStartError("setsockopt() on listen socket failed");
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   801
			return false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   802
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   803
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   804
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   805
	if (!SetNonBlocking(ls)) DEBUG(net, 0, "Setting non-blocking mode failed"); // XXX should this be an error?
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   806
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   807
	sin.sin_family = AF_INET;
629
4bb1f0fb0109 (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
   808
	sin.sin_addr.s_addr = _network_server_bind_ip;
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   809
	sin.sin_port = htons(_network_server_port);
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   810
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   811
	if (bind(ls, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   812
		ServerStartError("bind() failed");
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   813
		return false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   814
	}
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   815
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   816
	if (listen(ls, 1) != 0) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   817
		ServerStartError("listen() failed");
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   818
		return false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   819
	}
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   820
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   821
	_listensocket = ls;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   822
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   823
	return true;
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   824
}
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   825
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   826
// Close all current connections
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
   827
static void NetworkClose()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   828
{
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   829
	NetworkTCPSocketHandler *cs;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   830
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   831
	FOR_ALL_CLIENTS(cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   832
		if (!_network_server) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   833
			SEND_COMMAND(PACKET_CLIENT_QUIT)("leaving");
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5898
diff changeset
   834
			cs->Send_Packets();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   835
		}
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   836
		NetworkCloseClient(cs);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   837
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   838
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   839
	if (_network_server) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   840
		// We are a server, also close the listensocket
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   841
		closesocket(_listensocket);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   842
		_listensocket = INVALID_SOCKET;
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
   843
		DEBUG(net, 1, "Closed listener");
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   844
		NetworkUDPCloseAll();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   845
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   846
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   847
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   848
// Inits the network (cleans sockets and stuff)
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
   849
static void NetworkInitialize()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   850
{
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   851
	NetworkTCPSocketHandler *cs;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   852
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   853
	_local_command_queue = NULL;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   854
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   855
	// Clean all client-sockets
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   856
	for (cs = _clients; cs != &_clients[MAX_CLIENTS]; cs++) {
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
   857
		cs->Initialize();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   858
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   859
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   860
	// Clean the client_info memory
2879
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   861
	memset(&_network_client_info, 0, sizeof(_network_client_info));
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   862
	memset(&_network_player_info, 0, sizeof(_network_player_info));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   863
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   864
	_sync_frame = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   865
	_network_first_time = true;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   866
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   867
	_network_reconnect = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   868
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   869
	NetworkUDPInitialize();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   870
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   871
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   872
// Query a server to fetch his game-info
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   873
//  If game_info is true, only the gameinfo is fetched,
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   874
//   else only the client_info is fetched
5916
8931f4450f51 (svn r8543) -Codechange: make a real difference between querying the server via UDP and TCP.
rubidium
parents: 5902
diff changeset
   875
void NetworkTCPQueryServer(const char* host, unsigned short port)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   876
{
5916
8931f4450f51 (svn r8543) -Codechange: make a real difference between querying the server via UDP and TCP.
rubidium
parents: 5902
diff changeset
   877
	if (!_network_available) return;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   878
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   879
	NetworkDisconnect();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   880
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   881
	NetworkInitialize();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   882
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   883
	_network_server = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   884
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   885
	// Try to connect
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   886
	_networking = NetworkConnect(host, port);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   887
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   888
	// We are connected
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   889
	if (_networking) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   890
		SEND_COMMAND(PACKET_CLIENT_COMPANY_INFO)();
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   891
	} else { // No networking, close everything down again
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   892
		NetworkDisconnect();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   893
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   894
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   895
738
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 734
diff changeset
   896
/* Validates an address entered as a string and adds the server to
2881
ade9a553be53 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2879
diff changeset
   897
 * the list. If you use this function, the games will be marked
738
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 734
diff changeset
   898
 * as manually added. */
1329
6988419aa6f0 (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   899
void NetworkAddServer(const char *b)
710
2a13862fe86b (svn r1162) The server list can now be automatically filled from the config file. Add a section [servers] with the addresses each in a new line. Those will be checked upon OpenTTD startup.
dominik
parents: 693
diff changeset
   900
{
2a13862fe86b (svn r1162) The server list can now be automatically filled from the config file. Add a section [servers] with the addresses each in a new line. Those will be checked upon OpenTTD startup.
dominik
parents: 693
diff changeset
   901
	if (*b != '\0') {
1329
6988419aa6f0 (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   902
		const char *port = NULL;
6988419aa6f0 (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   903
		const char *player = NULL;
6988419aa6f0 (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   904
		char host[NETWORK_HOSTNAME_LENGTH];
710
2a13862fe86b (svn r1162) The server list can now be automatically filled from the config file. Add a section [servers] with the addresses each in a new line. Those will be checked upon OpenTTD startup.
dominik
parents: 693
diff changeset
   905
		uint16 rport;
2a13862fe86b (svn r1162) The server list can now be automatically filled from the config file. Add a section [servers] with the addresses each in a new line. Those will be checked upon OpenTTD startup.
dominik
parents: 693
diff changeset
   906
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   907
		ttd_strlcpy(host, b, lengthof(host));
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   908
710
2a13862fe86b (svn r1162) The server list can now be automatically filled from the config file. Add a section [servers] with the addresses each in a new line. Those will be checked upon OpenTTD startup.
dominik
parents: 693
diff changeset
   909
		ttd_strlcpy(_network_default_ip, b, lengthof(_network_default_ip));
2a13862fe86b (svn r1162) The server list can now be automatically filled from the config file. Add a section [servers] with the addresses each in a new line. Those will be checked upon OpenTTD startup.
dominik
parents: 693
diff changeset
   910
		rport = NETWORK_DEFAULT_PORT;
2a13862fe86b (svn r1162) The server list can now be automatically filled from the config file. Add a section [servers] with the addresses each in a new line. Those will be checked upon OpenTTD startup.
dominik
parents: 693
diff changeset
   911
716
8af847728d5b (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   912
		ParseConnectionString(&player, &port, host);
710
2a13862fe86b (svn r1162) The server list can now be automatically filled from the config file. Add a section [servers] with the addresses each in a new line. Those will be checked upon OpenTTD startup.
dominik
parents: 693
diff changeset
   913
		if (port != NULL) rport = atoi(port);
2a13862fe86b (svn r1162) The server list can now be automatically filled from the config file. Add a section [servers] with the addresses each in a new line. Those will be checked upon OpenTTD startup.
dominik
parents: 693
diff changeset
   914
5916
8931f4450f51 (svn r8543) -Codechange: make a real difference between querying the server via UDP and TCP.
rubidium
parents: 5902
diff changeset
   915
		NetworkUDPQueryServer(host, rport, true);
738
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 734
diff changeset
   916
	}
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 734
diff changeset
   917
}
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 734
diff changeset
   918
746
e282b0bd36b5 (svn r1202) -Fix: removed unused variable
truelight
parents: 738
diff changeset
   919
/* Generates the list of manually added hosts from NetworkGameList and
738
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 734
diff changeset
   920
 * dumps them into the array _network_host_list. This array is needed
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 734
diff changeset
   921
 * by the function that generates the config file. */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
   922
void NetworkRebuildHostList()
738
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 734
diff changeset
   923
{
959
e6a3bbda610f (svn r1451) Fix some of the signed/unsigned comparison warnings
tron
parents: 903
diff changeset
   924
	uint i = 0;
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   925
	const NetworkGameList *item = _network_game_list;
841
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   926
	while (item != NULL && i != lengthof(_network_host_list)) {
2915
e08f661f1b8e (svn r3470) - Fix: plug a memleak in _network_host_list.
Darkvater
parents: 2881
diff changeset
   927
		if (item->manually) {
e08f661f1b8e (svn r3470) - Fix: plug a memleak in _network_host_list.
Darkvater
parents: 2881
diff changeset
   928
			free(_network_host_list[i]);
895
4d8e39aba340 (svn r1381) Fix: [ 1095143 ] Servers list now also saves the port
dominik
parents: 863
diff changeset
   929
			_network_host_list[i++] = str_fmt("%s:%i", item->info.hostname, item->port);
2915
e08f661f1b8e (svn r3470) - Fix: plug a memleak in _network_host_list.
Darkvater
parents: 2881
diff changeset
   930
		}
738
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 734
diff changeset
   931
		item = item->next;
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 734
diff changeset
   932
	}
b96ab9e63d22 (svn r1194) Feature: You can now add and remove servers from the server list. Those will be remembered until you delete them by pressing the Delete key.
dominik
parents: 734
diff changeset
   933
841
4874b9ce2765 (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   934
	for (; i < lengthof(_network_host_list); i++) {
2915
e08f661f1b8e (svn r3470) - Fix: plug a memleak in _network_host_list.
Darkvater
parents: 2881
diff changeset
   935
		free(_network_host_list[i]);
e08f661f1b8e (svn r3470) - Fix: plug a memleak in _network_host_list.
Darkvater
parents: 2881
diff changeset
   936
		_network_host_list[i] = NULL;
710
2a13862fe86b (svn r1162) The server list can now be automatically filled from the config file. Add a section [servers] with the addresses each in a new line. Those will be checked upon OpenTTD startup.
dominik
parents: 693
diff changeset
   937
	}
2a13862fe86b (svn r1162) The server list can now be automatically filled from the config file. Add a section [servers] with the addresses each in a new line. Those will be checked upon OpenTTD startup.
dominik
parents: 693
diff changeset
   938
}
2a13862fe86b (svn r1162) The server list can now be automatically filled from the config file. Add a section [servers] with the addresses each in a new line. Those will be checked upon OpenTTD startup.
dominik
parents: 693
diff changeset
   939
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   940
// Used by clients, to connect to a server
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
   941
bool NetworkClientConnectGame(const char *host, uint16 port)
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   942
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   943
	if (!_network_available) return false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   944
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   945
	if (port == 0) return false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   946
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   947
	ttd_strlcpy(_network_last_host, host, sizeof(_network_last_host));
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   948
	_network_last_port = port;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   949
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   950
	NetworkDisconnect();
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
   951
	NetworkUDPCloseAll();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   952
	NetworkInitialize();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   953
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   954
	// Try to connect
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   955
	_networking = NetworkConnect(host, port);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   956
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   957
	// We are connected
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   958
	if (_networking) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   959
		IConsoleCmdExec("exec scripts/on_client.scr 0");
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   960
		NetworkClient_Connected();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   961
	} else {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   962
		// Connecting failed
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   963
		NetworkError(STR_NETWORK_ERR_NOCONNECTION);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   964
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   965
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   966
	return _networking;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   967
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   968
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
   969
static void NetworkInitGameInfo()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   970
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   971
	NetworkClientInfo *ci;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   972
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   973
	ttd_strlcpy(_network_game_info.server_name, _network_server_name, sizeof(_network_game_info.server_name));
1026
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1010
diff changeset
   974
	ttd_strlcpy(_network_game_info.server_password, _network_server_password, sizeof(_network_server_password));
02cc18821508 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1010
diff changeset
   975
	ttd_strlcpy(_network_game_info.rcon_password, _network_rcon_password, sizeof(_network_rcon_password));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   976
	if (_network_game_info.server_name[0] == '\0')
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   977
		snprintf(_network_game_info.server_name, sizeof(_network_game_info.server_name), "Unnamed Server");
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   978
2879
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   979
	ttd_strlcpy(_network_game_info.server_revision, _openttd_revision, sizeof(_network_game_info.server_revision));
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   980
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   981
	// The server is a client too ;)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   982
	if (_network_dedicated) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   983
		_network_game_info.clients_on = 0;
2879
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   984
		_network_game_info.companies_on = 0;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   985
		_network_game_info.dedicated = true;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   986
	} else {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   987
		_network_game_info.clients_on = 1;
2879
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   988
		_network_game_info.companies_on = 1;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   989
		_network_game_info.dedicated = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   990
	}
2879
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   991
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   992
	_network_game_info.spectators_on = 0;
2879
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
   993
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   994
	_network_game_info.game_date = _date;
4295
51b5d00b5f09 (svn r5928) -Cleanup: remove ConvertIntDate and use ConvertYMDToDate as ConvertIntDate was only called with Years.
rubidium
parents: 4285
diff changeset
   995
	_network_game_info.start_date = ConvertYMDToDate(_patches.starting_year, 0, 1);
863
6a1444534f62 (svn r1344) Use MapSize[XY]() (or MapSize()/MapMax[XY]() where appropriate) instead of TILES_[XY]
tron
parents: 841
diff changeset
   996
	_network_game_info.map_width = MapSizeX();
6a1444534f62 (svn r1344) Use MapSize[XY]() (or MapSize()/MapMax[XY]() where appropriate) instead of TILES_[XY]
tron
parents: 841
diff changeset
   997
	_network_game_info.map_height = MapSizeY();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   998
	_network_game_info.map_set = _opt.landscape;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   999
1899
81214598ab45 (svn r2405) Simplify a few '? true : false' and '? false : true', especially the latter is confusing
tron
parents: 1832
diff changeset
  1000
	_network_game_info.use_password = (_network_server_password[0] != '\0');
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1001
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1002
	// We use _network_client_info[MAX_CLIENT_INFO - 1] to store the server-data in it
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1003
	//  The index is NETWORK_SERVER_INDEX ( = 1)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1004
	ci = &_network_client_info[MAX_CLIENT_INFO - 1];
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1005
	memset(ci, 0, sizeof(*ci));
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1006
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1007
	ci->client_index = NETWORK_SERVER_INDEX;
4878
4d4f76a898fd (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4859
diff changeset
  1008
	ci->client_playas = _network_dedicated ? PLAYER_SPECTATOR : _local_player;
4d4f76a898fd (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4859
diff changeset
  1009
693
5b1c1929bb91 (svn r1137) -Fix: [Network] Replaces all strncpy with ttd_strlcpy.. windows really
truelight
parents: 683
diff changeset
  1010
	ttd_strlcpy(ci->client_name, _network_player_name, sizeof(ci->client_name));
5b1c1929bb91 (svn r1137) -Fix: [Network] Replaces all strncpy with ttd_strlcpy.. windows really
truelight
parents: 683
diff changeset
  1011
	ttd_strlcpy(ci->unique_id, _network_unique_id, sizeof(ci->unique_id));
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1012
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1013
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
  1014
bool NetworkServerStart()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1015
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1016
	if (!_network_available) return false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1017
629
4bb1f0fb0109 (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
  1018
	/* Call the pre-scripts */
4bb1f0fb0109 (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
  1019
	IConsoleCmdExec("exec scripts/pre_server.scr 0");
4bb1f0fb0109 (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
  1020
	if (_network_dedicated) IConsoleCmdExec("exec scripts/pre_dedicated.scr 0");
4bb1f0fb0109 (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
  1021
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1022
	NetworkInitialize();
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1023
	if (!NetworkListen()) return false;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1024
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1025
	// Try to start UDP-server
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1026
	_network_udp_server = true;
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
  1027
	_network_udp_server = _udp_server_socket->Listen(_network_server_bind_ip, _network_server_port, false);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1028
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1029
	_network_server = true;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1030
	_networking = true;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1031
	_frame_counter = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1032
	_frame_counter_server = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1033
	_frame_counter_max = 0;
2079
c9c32cf56cb9 (svn r2589) Fix: [network] Fixed static variable that wasn't initialized. Would stop the sync checking from working in some cases.
ludde
parents: 2078
diff changeset
  1034
	_last_sync_frame = 0;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1035
	_network_own_client_index = NETWORK_SERVER_INDEX;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1036
4878
4d4f76a898fd (svn r6814) -Codechange: For network games ci->client_playas was always p->index + 1. To
Darkvater
parents: 4859
diff changeset
  1037
	/* Non-dedicated server will always be player #1 */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1038
	if (!_network_dedicated) _network_playas = PLAYER_FIRST;
683
3bd410628868 (svn r1124) -Fix: [Network] Also the server now has the option Give Money in
truelight
parents: 679
diff changeset
  1039
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1040
	_network_clients_connected = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1041
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1042
	NetworkInitGameInfo();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1043
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1044
	// execute server initialization script
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1045
	IConsoleCmdExec("exec scripts/on_server.scr 0");
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1046
	// if the server is dedicated ... add some other script
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1047
	if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0");
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 663
diff changeset
  1048
4716
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
  1049
	_min_players_paused = false;
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
  1050
	CheckMinPlayers();
8a05bf6d951d (svn r6628) - Feature: Add the ability to pause a server if not enough players are connected. The setting for this is 'min_players' and can be set in the config and via the console. If the number of players drops below this number, the server will pause the game.
peter1138
parents: 4417
diff changeset
  1051
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 663
diff changeset
  1052
	/* Try to register us to the master server */
2861
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2859
diff changeset
  1053
	_network_last_advertise_frame = 0;
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2859
diff changeset
  1054
	_network_need_advertise = true;
668
1fe298df8526 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 663
diff changeset
  1055
	NetworkUDPAdvertise();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1056
	return true;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1057
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1058
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1059
// The server is rebooting...
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1060
// The only difference with NetworkDisconnect, is the packets that is sent
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
  1061
void NetworkReboot()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1062
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1063
	if (_network_server) {
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
  1064
		NetworkTCPSocketHandler *cs;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1065
		FOR_ALL_CLIENTS(cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1066
			SEND_COMMAND(PACKET_SERVER_NEWGAME)(cs);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5898
diff changeset
  1067
			cs->Send_Packets();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1068
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1069
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1070
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1071
	NetworkClose();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1072
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1073
	// Free all queued commands
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1074
	while (_local_command_queue != NULL) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1075
		CommandPacket *p = _local_command_queue;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1076
		_local_command_queue = _local_command_queue->next;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1077
		free(p);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1078
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1079
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1080
	_networking = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1081
	_network_server = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1082
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1083
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1084
// We want to disconnect from the host/clients
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
  1085
void NetworkDisconnect()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1086
{
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1087
	if (_network_server) {
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
  1088
		NetworkTCPSocketHandler *cs;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1089
		FOR_ALL_CLIENTS(cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1090
			SEND_COMMAND(PACKET_SERVER_SHUTDOWN)(cs);
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5898
diff changeset
  1091
			cs->Send_Packets();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1092
		}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1093
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1094
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1095
	if (_network_advertise) NetworkUDPRemoveAdvertise();
765
18104986675d (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
  1096
598
b8c5ae66f9ff (svn r1022) -Fix: [Network] [ 1083690 ] The password dialog made the network
truelight
parents: 543
diff changeset
  1097
	DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
b8c5ae66f9ff (svn r1022) -Fix: [Network] [ 1083690 ] The password dialog made the network
truelight
parents: 543
diff changeset
  1098
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1099
	NetworkClose();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1100
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1101
	// Free all queued commands
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1102
	while (_local_command_queue != NULL) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1103
		CommandPacket *p = _local_command_queue;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1104
		_local_command_queue = _local_command_queue->next;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1105
		free(p);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1106
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1107
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1108
	_networking = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1109
	_network_server = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1110
}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1111
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1112
// Receives something from the network
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
  1113
static bool NetworkReceive()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1114
{
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
  1115
	NetworkTCPSocketHandler *cs;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1116
	int n;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1117
	fd_set read_fd, write_fd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1118
	struct timeval tv;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 188
diff changeset
  1119
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1120
	FD_ZERO(&read_fd);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1121
	FD_ZERO(&write_fd);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1122
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1123
	FOR_ALL_CLIENTS(cs) {
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
  1124
		FD_SET(cs->sock, &read_fd);
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
  1125
		FD_SET(cs->sock, &write_fd);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1126
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1127
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1128
	// take care of listener port
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1129
	if (_network_server) FD_SET(_listensocket, &read_fd);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1130
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1131
	tv.tv_sec = tv.tv_usec = 0; // don't block at all.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1132
#if !defined(__MORPHOS__) && !defined(__AMIGA__)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1133
	n = select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1134
#else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1135
	n = WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1136
#endif
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1137
	if (n == -1 && !_network_server) NetworkError(STR_NETWORK_ERR_LOSTCONNECTION);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1138
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1139
	// accept clients..
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1140
	if (_network_server && FD_ISSET(_listensocket, &read_fd)) NetworkAcceptClients();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1141
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1142
	// read stuff from clients
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1143
	FOR_ALL_CLIENTS(cs) {
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
  1144
		cs->writable = !!FD_ISSET(cs->sock, &write_fd);
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
  1145
		if (FD_ISSET(cs->sock, &read_fd)) {
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
  1146
			if (_network_server) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1147
				NetworkServer_ReadPackets(cs);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
  1148
			} else {
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1149
				NetworkRecvStatus res;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
  1150
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1151
				// The client already was quiting!
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1152
				if (cs->has_quit) return false;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
  1153
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
  1154
				res = NetworkClient_ReadPackets(cs);
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4038
diff changeset
  1155
				if (res != NETWORK_RECV_STATUS_OKAY) {
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1156
					// The client made an error of which we can not recover
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1157
					//   close the client and drop back to main menu
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1158
					NetworkClientError(res, cs);
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1159
					return false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1160
				}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1161
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1162
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1163
	}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1164
	return true;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1165
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1166
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1167
// This sends all buffered commands (if possible)
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
  1168
static void NetworkSend()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1169
{
5624
6afe9d27430a (svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
rubidium
parents: 5619
diff changeset
  1170
	NetworkTCPSocketHandler *cs;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1171
	FOR_ALL_CLIENTS(cs) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1172
		if (cs->writable) {
5902
a9508c166390 (svn r8525) -Codechange: make NetworkSend_Packet(s) and NetworkRecv_Packet functions of NetworkTCPSocketHandler.
rubidium
parents: 5898
diff changeset
  1173
			cs->Send_Packets();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1174
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1175
			if (cs->status == STATUS_MAP) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1176
				// This client is in the middle of a map-send, call the function for that
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1177
				SEND_COMMAND(PACKET_SERVER_MAP)(cs);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1178
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1179
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1180
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1181
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1182
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1183
// Handle the local-command-queue
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
  1184
static void NetworkHandleLocalQueue()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1185
{
2080
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1186
	CommandPacket *cp, **cp_prev;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1187
2080
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1188
	cp_prev = &_local_command_queue;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1189
2080
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1190
	while ( (cp = *cp_prev) != NULL) {
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1191
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1192
		// The queue is always in order, which means
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1193
		// that the first element will be executed first.
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1194
		if (_frame_counter < cp->frame) break;
2080
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1195
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1196
		if (_frame_counter > cp->frame) {
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1197
			// If we reach here, it means for whatever reason, we've already executed
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1198
			// past the command we need to execute.
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
  1199
			DEBUG(net, 0, "Trying to execute a packet in the past!");
2080
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1200
			assert(0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1201
		}
2080
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1202
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1203
		// We can execute this command
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1204
		NetworkExecuteCommand(cp);
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1205
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1206
		*cp_prev = cp->next;
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1207
		free(cp);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1208
	}
2080
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1209
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1210
	// Just a safety check, to be removed in the future.
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1211
	// Make sure that no older command appears towards the end of the queue
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1212
	// In that case we missed executing it. This will never happen.
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2944
diff changeset
  1213
	for (cp = _local_command_queue; cp; cp = cp->next) {
2080
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1214
		assert(_frame_counter < cp->frame);
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1215
	}
cee8c96659b1 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1216
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1217
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1218
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
  1219
static bool NetworkDoClientLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1220
{
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1221
	_frame_counter++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1222
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1223
	NetworkHandleLocalQueue();
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1224
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1225
	StateGameLoop();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1226
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1227
	// Check if we are in sync!
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1228
	if (_sync_frame != 0) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1229
		if (_sync_frame == _frame_counter) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1230
#ifdef NETWORK_SEND_DOUBLE_SEED
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1231
			if (_sync_seed_1 != _random_seeds[0][0] || _sync_seed_2 != _random_seeds[0][1]) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1232
#else
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1233
			if (_sync_seed_1 != _random_seeds[0][0]) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1234
#endif
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1235
				NetworkError(STR_NETWORK_ERR_DESYNC);
7187
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1236
#ifdef DEBUG_DUMP_COMMANDS
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1237
				debug_dump_commands("ddc:serr:%d;%d\n", _date, _date_fract);
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1238
#endif /* DUMP_COMMANDS */
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
  1239
				DEBUG(net, 0, "Sync error detected!");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1240
				NetworkClientError(NETWORK_RECV_STATUS_DESYNC, DEREF_CLIENT(0));
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1241
				return false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1242
			}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1243
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1244
			// If this is the first time we have a sync-frame, we
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1245
			//   need to let the server know that we are ready and at the same
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1246
			//   frame as he is.. so we can start playing!
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1247
			if (_network_first_time) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1248
				_network_first_time = false;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1249
				SEND_COMMAND(PACKET_CLIENT_ACK)();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1250
			}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1251
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1252
			_sync_frame = 0;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1253
		} else if (_sync_frame < _frame_counter) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
  1254
			DEBUG(net, 1, "Missed frame for sync-test (%d / %d)", _sync_frame, _frame_counter);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1255
			_sync_frame = 0;
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
  1256
		}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1257
	}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1258
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1259
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1260
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1261
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1262
// We have to do some UDP checking
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
  1263
void NetworkUDPGameLoop()
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1264
{
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 746
diff changeset
  1265
	if (_network_udp_server) {
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
  1266
		_udp_server_socket->ReceivePackets();
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
  1267
		_udp_master_socket->ReceivePackets();
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
  1268
	} else {
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
  1269
		_udp_client_socket->ReceivePackets();
4000
4009d092b306 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3623
diff changeset
  1270
		if (_network_udp_broadcast > 0) _network_udp_broadcast--;
5897
893fe6a810dc (svn r8520) -Fix/Feature: requery gameservers that did not respond to their first query.
rubidium
parents: 5872
diff changeset
  1271
		NetworkGameListRequery();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1272
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1273
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1274
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1275
// The main loop called from ttd.c
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1276
//  Here we also have to do StateGameLoop if needed!
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
  1277
void NetworkGameLoop()
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1278
{
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1279
	if (!_networking) return;
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
  1280
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1281
	if (!NetworkReceive()) return;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1282
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1283
	if (_network_server) {
7187
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1284
#ifdef DEBUG_DUMP_COMMANDS
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1285
		static FILE *f = FioFOpenFile("commands.log", "rb", SAVE_DIR);
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1286
		static Date next_date = 0;
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1287
		static uint32 next_date_fract;
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1288
		static CommandPacket *cp = NULL;
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1289
		if (f == NULL && next_date == 0) {
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1290
			printf("Cannot open commands.log\n");
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1291
			next_date = 1;
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1292
		}
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1293
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1294
		while (f != NULL && !feof(f)) {
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1295
			if (cp != NULL && _date == next_date && _date_fract == next_date_fract) {
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1296
				_current_player = cp->player;
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1297
				_cmd_text = cp->text;
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1298
				DoCommandP(cp->tile, cp->p1, cp->p2, NULL, cp->cmd);
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1299
				free(cp);
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1300
				cp = NULL;
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1301
			}
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1302
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1303
			if (cp != NULL) break;
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1304
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1305
			char buff[4096];
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1306
			if (fgets(buff, lengthof(buff), f) == NULL) break;
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1307
			if (strncmp(buff, "ddc:cmd:", 8) != 0) continue;
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1308
			cp = MallocT<CommandPacket>(1);
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1309
			int player;
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1310
			sscanf(&buff[8], "%d;%d;%d;%d;%d;%d;%d;%s", &next_date, &next_date_fract, &player, &cp->tile, &cp->p1, &cp->p2, &cp->cmd, cp->text);
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1311
			cp->player = (Owner)player;
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1312
		}
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1313
#endif /* DUMP_COMMANDS */
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1314
2235
7630c38913fd (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1315
		bool send_frame = false;
7630c38913fd (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1316
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1317
		// We first increase the _frame_counter
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1318
		_frame_counter++;
2235
7630c38913fd (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1319
		// Update max-frame-counter
7630c38913fd (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1320
		if (_frame_counter > _frame_counter_max) {
7630c38913fd (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1321
			_frame_counter_max = _frame_counter + _network_frame_freq;
7630c38913fd (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1322
			send_frame = true;
7630c38913fd (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1323
		}
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1324
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1325
		NetworkHandleLocalQueue();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1326
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1327
		// Then we make the frame
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1328
		StateGameLoop();
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1329
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1330
		_sync_seed_1 = _random_seeds[0][0];
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1331
#ifdef NETWORK_SEND_DOUBLE_SEED
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1332
		_sync_seed_2 = _random_seeds[0][1];
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1333
#endif
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1334
2235
7630c38913fd (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1335
		NetworkServer_Tick(send_frame);
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1336
	} else {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1337
		// Client
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1338
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1339
		// Make sure we are at the frame were the server is (quick-frames)
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1340
		if (_frame_counter_server > _frame_counter) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1341
			while (_frame_counter_server > _frame_counter) {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1342
				if (!NetworkDoClientLoop()) break;
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1343
			}
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1344
		} else {
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1345
			// Else, keep on going till _frame_counter_max
4880
aac84a9dcd03 (svn r6816) -Codechange: Some coding style, variable localization, const correctness.
Darkvater
parents: 4878
diff changeset
  1346
			if (_frame_counter_max > _frame_counter) NetworkDoClientLoop();
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1347
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1348
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 188
diff changeset
  1349
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1350
	NetworkSend();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1351
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1352
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
  1353
static void NetworkGenerateUniqueId()
602
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1354
{
8133
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8131
diff changeset
  1355
	Md5 checksum;
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8131
diff changeset
  1356
	uint8 digest[16];
602
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1357
	char hex_output[16*2 + 1];
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1358
	char coding_string[NETWORK_NAME_LENGTH];
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1359
	int di;
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1360
607
1ae7deaa77d9 (svn r1031) -Fix: [Network] The unique-id was not as unique as I though it was..
truelight
parents: 602
diff changeset
  1361
	snprintf(coding_string, sizeof(coding_string), "%d%s", (uint)Random(), "OpenTTD Unique ID");
602
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1362
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1363
	/* Generate the MD5 hash */
8133
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8131
diff changeset
  1364
	checksum.Append((const uint8*)coding_string, strlen(coding_string));
3ab8eaf85ca0 (svn r11695) -Codechange: Converted the md5 algorithm to OOP
skidd13
parents: 8131
diff changeset
  1365
	checksum.Finish(digest);
602
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1366
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1367
	for (di = 0; di < 16; ++di)
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1368
		sprintf(hex_output + di * 2, "%02x", digest[di]);
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1369
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1370
	/* _network_unique_id is our id */
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1371
	snprintf(_network_unique_id, sizeof(_network_unique_id), "%s", hex_output);
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1372
}
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1373
5959
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1374
void NetworkStartDebugLog(const char *hostname, uint16 port)
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1375
{
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1376
	extern SOCKET _debug_socket;  // Comes from debug.c
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1377
	SOCKET s;
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1378
	struct sockaddr_in sin;
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1379
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1380
	DEBUG(net, 0, "Redirecting DEBUG() to %s:%d", hostname, port);
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1381
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1382
	s = socket(AF_INET, SOCK_STREAM, 0);
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1383
	if (s == INVALID_SOCKET) {
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1384
		DEBUG(net, 0, "Failed to open socket for redirection DEBUG()");
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1385
		return;
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1386
	}
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1387
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1388
	if (!SetNoDelay(s)) DEBUG(net, 1, "Setting TCP_NODELAY failed");
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1389
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1390
	sin.sin_family = AF_INET;
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1391
	sin.sin_addr.s_addr = NetworkResolveHost(hostname);
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1392
	sin.sin_port = htons(port);
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1393
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1394
	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) != 0) {
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1395
		DEBUG(net, 0, "Failed to redirection DEBUG() to %s:%d", hostname, port);
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1396
		return;
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1397
	}
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1398
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1399
	if (!SetNonBlocking(s)) DEBUG(net, 0, "Setting non-blocking mode failed");
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1400
	_debug_socket = s;
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1401
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1402
	DEBUG(net, 0, "DEBUG() is now redirected");
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1403
}
98a64c6e7f1f (svn r8631) -Add: added parameter -l ip[:port] to ./openttd, which redirects DEBUG() to a remote connection over TCP
truelight
parents: 5916
diff changeset
  1404
5524
93460e0fed07 (svn r7830) -Codechange: let NetworkCoreInitialize return a bool, so we have to set _network_available only once.
rubidium
parents: 5523
diff changeset
  1405
/** This tries to launch the network for a given OS */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
  1406
void NetworkStartUp()
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1407
{
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
  1408
	DEBUG(net, 3, "[core] starting network...");
770
c2eacca29601 (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1409
5524
93460e0fed07 (svn r7830) -Codechange: let NetworkCoreInitialize return a bool, so we have to set _network_available only once.
rubidium
parents: 5523
diff changeset
  1410
	/* Network is available */
93460e0fed07 (svn r7830) -Codechange: let NetworkCoreInitialize return a bool, so we have to set _network_available only once.
rubidium
parents: 5523
diff changeset
  1411
	_network_available = NetworkCoreInitialize();;
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1412
	_network_dedicated = false;
2861
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2859
diff changeset
  1413
	_network_last_advertise_frame = 0;
e42d249af3cb (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2859
diff changeset
  1414
	_network_need_advertise = true;
764
7e1e17b7c7d4 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 746
diff changeset
  1415
	_network_advertise_retries = 0;
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
  1416
629
4bb1f0fb0109 (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
  1417
	/* Load the ip from the openttd.cfg */
4bb1f0fb0109 (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
  1418
	_network_server_bind_ip = inet_addr(_network_server_bind_ip_host);
4bb1f0fb0109 (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
  1419
	/* And put the data back in it in case it was an invalid ip */
4bb1f0fb0109 (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
  1420
	snprintf(_network_server_bind_ip_host, sizeof(_network_server_bind_ip_host), "%s", inet_ntoa(*(struct in_addr *)&_network_server_bind_ip));
4bb1f0fb0109 (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
  1421
602
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1422
	/* Generate an unique id when there is none yet */
2879
365ecd52f2db (svn r3427) - Feature: Allow seeing and setting the maximum amount of companies and spectators for a server. This can be changed/viewed during runtime as well in the console.
Darkvater
parents: 2861
diff changeset
  1423
	if (_network_unique_id[0] == '\0') NetworkGenerateUniqueId();
602
f0afb4018788 (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1424
4417
df176ef5fa73 (svn r6170) -Feature: save max_companies/clients/spectators in the config file.
Darkvater
parents: 4295
diff changeset
  1425
	{
df176ef5fa73 (svn r6170) -Feature: save max_companies/clients/spectators in the config file.
Darkvater
parents: 4295
diff changeset
  1426
		byte cl_max = _network_game_info.clients_max;
df176ef5fa73 (svn r6170) -Feature: save max_companies/clients/spectators in the config file.
Darkvater
parents: 4295
diff changeset
  1427
		byte cp_max = _network_game_info.companies_max;
df176ef5fa73 (svn r6170) -Feature: save max_companies/clients/spectators in the config file.
Darkvater
parents: 4295
diff changeset
  1428
		byte sp_max = _network_game_info.spectators_max;
6528
3afd9c7a0a23 (svn r9716) -Add: server_lang in [network] section of openttd.cfg, so dedicated servers can have the little lang flag.
glx
parents: 6347
diff changeset
  1429
		byte s_lang = _network_game_info.server_lang;
4417
df176ef5fa73 (svn r6170) -Feature: save max_companies/clients/spectators in the config file.
Darkvater
parents: 4295
diff changeset
  1430
df176ef5fa73 (svn r6170) -Feature: save max_companies/clients/spectators in the config file.
Darkvater
parents: 4295
diff changeset
  1431
		memset(&_network_game_info, 0, sizeof(_network_game_info));
df176ef5fa73 (svn r6170) -Feature: save max_companies/clients/spectators in the config file.
Darkvater
parents: 4295
diff changeset
  1432
		_network_game_info.clients_max = cl_max;
df176ef5fa73 (svn r6170) -Feature: save max_companies/clients/spectators in the config file.
Darkvater
parents: 4295
diff changeset
  1433
		_network_game_info.companies_max = cp_max;
df176ef5fa73 (svn r6170) -Feature: save max_companies/clients/spectators in the config file.
Darkvater
parents: 4295
diff changeset
  1434
		_network_game_info.spectators_max = sp_max;
6528
3afd9c7a0a23 (svn r9716) -Add: server_lang in [network] section of openttd.cfg, so dedicated servers can have the little lang flag.
glx
parents: 6347
diff changeset
  1435
		_network_game_info.server_lang = s_lang;
4417
df176ef5fa73 (svn r6170) -Feature: save max_companies/clients/spectators in the config file.
Darkvater
parents: 4295
diff changeset
  1436
	}
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
  1437
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1438
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1439
	NetworkInitialize();
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
  1440
	DEBUG(net, 3, "[core] network online, multiplayer available");
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1441
	NetworkFindIPs();
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1442
}
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1443
5524
93460e0fed07 (svn r7830) -Codechange: let NetworkCoreInitialize return a bool, so we have to set _network_available only once.
rubidium
parents: 5523
diff changeset
  1444
/** This shuts the network down */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6219
diff changeset
  1445
void NetworkShutDown()
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1446
{
4829
9a0a034d0f55 (svn r6753) -Codechange: Move some function declerations from functions.h into network.h
Darkvater
parents: 4826
diff changeset
  1447
	NetworkDisconnect();
5619
9f5a7152403a (svn r8078) -Codechange: rewrite UDP part of the network code to make use classes. This is only one of the many steps to really cleanup the network code.
rubidium
parents: 5587
diff changeset
  1448
	NetworkUDPShutdown();
4829
9a0a034d0f55 (svn r6753) -Codechange: Move some function declerations from functions.h into network.h
Darkvater
parents: 4826
diff changeset
  1449
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
  1450
	DEBUG(net, 3, "[core] shutting down network");
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1451
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1452
	_network_available = false;
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1453
5523
623a8856e35b (svn r7829) -Codechange: move the network (core) initialization/shutdown functions into network/core, so the can be reused in the masterserver_updater.
rubidium
parents: 5521
diff changeset
  1454
	NetworkCoreShutdown();
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
  1455
}
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
  1456
6178
c29a7d37c3ce (svn r8949) -Codechange: only test the first NETWORK_REVISION_LENGTH - 1 characters when determining network compatability. This makes it possible to have 'long' branch names while still being able to play network games.
rubidium
parents: 5976
diff changeset
  1457
/**
c29a7d37c3ce (svn r8949) -Codechange: only test the first NETWORK_REVISION_LENGTH - 1 characters when determining network compatability. This makes it possible to have 'long' branch names while still being able to play network games.
rubidium
parents: 5976
diff changeset
  1458
 * Checks whether the given version string is compatible with our version.
c29a7d37c3ce (svn r8949) -Codechange: only test the first NETWORK_REVISION_LENGTH - 1 characters when determining network compatability. This makes it possible to have 'long' branch names while still being able to play network games.
rubidium
parents: 5976
diff changeset
  1459
 * @param other the version string to compare to
c29a7d37c3ce (svn r8949) -Codechange: only test the first NETWORK_REVISION_LENGTH - 1 characters when determining network compatability. This makes it possible to have 'long' branch names while still being able to play network games.
rubidium
parents: 5976
diff changeset
  1460
 */
c29a7d37c3ce (svn r8949) -Codechange: only test the first NETWORK_REVISION_LENGTH - 1 characters when determining network compatability. This makes it possible to have 'long' branch names while still being able to play network games.
rubidium
parents: 5976
diff changeset
  1461
bool IsNetworkCompatibleVersion(const char *other)
c29a7d37c3ce (svn r8949) -Codechange: only test the first NETWORK_REVISION_LENGTH - 1 characters when determining network compatability. This makes it possible to have 'long' branch names while still being able to play network games.
rubidium
parents: 5976
diff changeset
  1462
{
c29a7d37c3ce (svn r8949) -Codechange: only test the first NETWORK_REVISION_LENGTH - 1 characters when determining network compatability. This makes it possible to have 'long' branch names while still being able to play network games.
rubidium
parents: 5976
diff changeset
  1463
	extern const char _openttd_revision[];
7220
f44697e71145 (svn r10498) -Fix [FS#1008]: remove inconsistency between a warning and the actual behaviour.
rubidium
parents: 7187
diff changeset
  1464
	return strncmp(_openttd_revision, other, NETWORK_REVISION_LENGTH - 1) == 0;
6178
c29a7d37c3ce (svn r8949) -Codechange: only test the first NETWORK_REVISION_LENGTH - 1 characters when determining network compatability. This makes it possible to have 'long' branch names while still being able to play network games.
rubidium
parents: 5976
diff changeset
  1465
}
c29a7d37c3ce (svn r8949) -Codechange: only test the first NETWORK_REVISION_LENGTH - 1 characters when determining network compatability. This makes it possible to have 'long' branch names while still being able to play network games.
rubidium
parents: 5976
diff changeset
  1466
7187
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1467
#ifdef DEBUG_DUMP_COMMANDS
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1468
void CDECL debug_dump_commands(const char *s, ...)
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1469
{
8024
5feb155012fe (svn r11584) -Change: add some extra checking in the hope to find the cause of FS#1482.
rubidium
parents: 7564
diff changeset
  1470
	static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
7187
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1471
	if (f == NULL) return;
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1472
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1473
	va_list va;
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1474
	va_start(va, s);
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1475
	vfprintf(f, s, va);
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1476
	va_end(va);
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1477
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1478
	fflush(f);
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1479
}
7c9bf9e10d8b (svn r10462) -Add: a command dumper/loader that could be enabled compile-time and server side only to aid debugging some desyncs, i.e. dump the stream of commands so it could be replayed in exactly the same way later. This should primarily be used to make desyncs more easily reproducable, so it can be properly debugged.
rubidium
parents: 6528
diff changeset
  1480
#endif /* DEBUG_DUMP_COMMANDS */
543
946badd71033 (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1481
#endif /* ENABLE_NETWORK */