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