network.c
author matthijs
Wed, 22 Mar 2006 22:26:16 +0000
branch0.4.5
changeset 9958 bed516c67d61
parent 2944 7c392e7b51c6
child 2952 6a26eeda9679
permissions -rw-r--r--
(svn r4041) [Debian] Change next version number to 0.4.6 instead of 0.4.5.1.
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
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2153
diff changeset
     4
#include "openttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1095
diff changeset
     5
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2153
diff changeset
     6
#include "functions.h"
1317
f382f1b439c7 (svn r1821) Move generic string handling functions to string.[ch] and introduce stre{cpy,cat}, see string.h for their semantics
tron
parents: 1309
diff changeset
     7
#include "string.h"
1309
dab90d4cbf2d (svn r1813) Declare functions implemented in strings.c in their own shiny new header (though i think some of these function don't belong into strings.c)
tron
parents: 1299
diff changeset
     8
#include "strings.h"
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents: 675
diff changeset
     9
#include "map.h"
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    10
#include "network_data.h"
1602
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1466
diff changeset
    11
#include "command.h"
2153
91e89aa8c299 (svn r2663) Include variables.h only in these files which need it, not globally via openttd.h
tron
parents: 2080
diff changeset
    12
#include "variables.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
663
3236ec743f75 (svn r1098) -Fix: server without revision build doesn't care about the client's version. Also moved all revision things where it belongs (network.c)
darkvater
parents: 662
diff changeset
    14
#if defined(WITH_REV)
3236ec743f75 (svn r1098) -Fix: server without revision build doesn't care about the client's version. Also moved all revision things where it belongs (network.c)
darkvater
parents: 662
diff changeset
    15
	extern const char _openttd_revision[];
3236ec743f75 (svn r1098) -Fix: server without revision build doesn't care about the client's version. Also moved all revision things where it belongs (network.c)
darkvater
parents: 662
diff changeset
    16
#elif defined(WITH_REV_HACK)
3236ec743f75 (svn r1098) -Fix: server without revision build doesn't care about the client's version. Also moved all revision things where it belongs (network.c)
darkvater
parents: 662
diff changeset
    17
	#define WITH_REV
3236ec743f75 (svn r1098) -Fix: server without revision build doesn't care about the client's version. Also moved all revision things where it belongs (network.c)
darkvater
parents: 662
diff changeset
    18
	const char _openttd_revision[] = WITH_REV_HACK;
3236ec743f75 (svn r1098) -Fix: server without revision build doesn't care about the client's version. Also moved all revision things where it belongs (network.c)
darkvater
parents: 662
diff changeset
    19
#else
3236ec743f75 (svn r1098) -Fix: server without revision build doesn't care about the client's version. Also moved all revision things where it belongs (network.c)
darkvater
parents: 662
diff changeset
    20
	const char _openttd_revision[] = NOREV_STRING;
3236ec743f75 (svn r1098) -Fix: server without revision build doesn't care about the client's version. Also moved all revision things where it belongs (network.c)
darkvater
parents: 662
diff changeset
    21
#endif
3236ec743f75 (svn r1098) -Fix: server without revision build doesn't care about the client's version. Also moved all revision things where it belongs (network.c)
darkvater
parents: 662
diff changeset
    22
3236ec743f75 (svn r1098) -Fix: server without revision build doesn't care about the client's version. Also moved all revision things where it belongs (network.c)
darkvater
parents: 662
diff changeset
    23
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    24
#ifdef ENABLE_NETWORK
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    26
#include "table/strings.h"
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    27
#include "network_client.h"
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    28
#include "network_server.h"
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    29
#include "network_udp.h"
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    30
#include "network_gamelist.h"
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    31
#include "console.h" /* IConsoleCmdExec */
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    32
#include <stdarg.h> /* va_list */
602
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
    33
#include "md5.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
770
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
    35
#ifdef __MORPHOS__
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
    36
// the library base is required here
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
    37
struct Library *SocketBase = NULL;
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
    38
#endif
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
    39
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    40
// The listen socket for the server
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    41
static SOCKET _listensocket;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    43
// Network copy of patches, so the patches of a client are not fucked up
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    44
//  after he joined a server
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    45
static Patches network_tmp_patches;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    47
// The amount of clients connected
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    48
static byte _network_clients_connected = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    49
// The index counter for new clients (is never decreased)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    50
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
    51
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
    52
/* Some externs / forwards */
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1026
diff changeset
    53
extern void ShowJoinStatusWindow(void);
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1026
diff changeset
    54
extern void StateGameLoop(void);
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
    55
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    56
// 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
    57
NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    58
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    59
	NetworkClientInfo *ci;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    61
	for (ci = _network_client_info; ci != &_network_client_info[MAX_CLIENT_INFO]; ci++)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    62
		if (ci->client_index == client_index)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    63
			return ci;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    64
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    65
	return NULL;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    66
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    67
2859
d45695cd3e98 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    68
/** Return the CI for a given IP
d45695cd3e98 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    69
 * @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
    70
 * @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
    71
NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip)
d45695cd3e98 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    72
{
d45695cd3e98 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    73
	NetworkClientInfo *ci;
d45695cd3e98 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    74
	uint32 ip_number = inet_addr(ip);
d45695cd3e98 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    75
d45695cd3e98 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    76
	for (ci = _network_client_info; ci != &_network_client_info[MAX_CLIENT_INFO]; ci++) {
d45695cd3e98 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    77
		if (ci->client_ip == ip_number) return ci;
d45695cd3e98 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    78
	}
d45695cd3e98 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    79
d45695cd3e98 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    80
	return NULL;
d45695cd3e98 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    81
}
d45695cd3e98 (svn r3407) - Feature: Kick and ban now with IP numbers.
Darkvater
parents: 2639
diff changeset
    82
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    83
// Function that looks up the CS for a given client-index
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
    84
NetworkClientState *NetworkFindClientStateFromIndex(uint16 client_index)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    85
{
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
    86
	NetworkClientState *cs;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    87
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    88
	for (cs = _clients; cs != &_clients[MAX_CLIENT_INFO]; cs++)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    89
		if (cs->index == client_index)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    90
			return cs;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    91
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    92
	return NULL;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    93
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    94
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    95
// 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
    96
//  if the user did not send it yet, Client #<no> is used.
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
    97
void NetworkGetClientName(char *client_name, size_t size, const NetworkClientState *cs)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    98
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
    99
	NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   100
	if (ci->client_name[0] == '\0')
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   101
		snprintf(client_name, size, "Client #%d", cs->index);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   102
	else
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   103
		snprintf(client_name, size, "%s", ci->client_name);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   104
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   105
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
   106
byte NetworkSpectatorCount(void)
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
   107
{
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
   108
	const NetworkClientState *cs;
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
   109
	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
   110
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
   111
	FOR_ALL_CLIENTS(cs) {
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
   112
		if (DEREF_CLIENT_INFO(cs)->client_playas == OWNER_SPECTATOR) 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
   113
	}
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
   114
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
   115
	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
   116
}
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
   117
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   118
// 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
   119
//  (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
   120
// If 'self_send' is true, this is the client who is sending the message
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   121
void CDECL NetworkTextMessage(NetworkAction action, uint16 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
   122
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   123
	char buf[1024];
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   124
	va_list va;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   125
	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
   126
	char message[1024];
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   127
	char temp[1024];
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   128
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   129
	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
   130
	vsnprintf(buf, lengthof(buf), str, va);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   131
	va_end(va);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   132
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   133
	switch (action) {
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   134
		case NETWORK_ACTION_JOIN:
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   135
			GetString(temp, STR_NETWORK_CLIENT_JOINED);
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   136
			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
   137
			break;
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   138
		case NETWORK_ACTION_LEAVE:
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   139
			GetString(temp, STR_NETWORK_ERR_LEFT);
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   140
			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
   141
			break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   142
		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
   143
			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
   144
				SetDParamStr(0, name);
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   145
				SetDParam(1, atoi(buf));
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   146
				GetString(temp, STR_NETWORK_GAVE_MONEY_AWAY);
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   147
				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
   148
			} else {
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   149
				SetDParam(0, atoi(buf));
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   150
				GetString(temp, STR_NETWORK_GIVE_MONEY);
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   151
				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
   152
			}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   153
			break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   154
		case NETWORK_ACTION_CHAT_PLAYER:
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   155
			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
   156
				SetDParamStr(0, name);
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   157
				GetString(temp, STR_NETWORK_CHAT_TO_COMPANY);
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   158
				snprintf(message, sizeof(message), "%s %s", temp, buf);
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   159
			} else {
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
   160
				SetDParamStr(0, name);
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   161
				GetString(temp, STR_NETWORK_CHAT_COMPANY);
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   162
				snprintf(message, sizeof(message), "%s %s", temp, buf);
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   163
			}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   164
			break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   165
		case NETWORK_ACTION_CHAT_CLIENT:
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   166
			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
   167
				SetDParamStr(0, name);
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   168
				GetString(temp, STR_NETWORK_CHAT_TO_CLIENT);
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   169
				snprintf(message, sizeof(message), "%s %s", temp, buf);
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   170
			} else {
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
   171
				SetDParamStr(0, name);
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   172
				GetString(temp, STR_NETWORK_CHAT_CLIENT);
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   173
				snprintf(message, sizeof(message), "%s %s", temp, buf);
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   174
			}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   175
			break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   176
		case NETWORK_ACTION_NAME_CHANGE:
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   177
			GetString(temp, STR_NETWORK_NAME_CHANGE);
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   178
			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
   179
			break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   180
		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
   181
			SetDParamStr(0, name);
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   182
			GetString(temp, STR_NETWORK_CHAT_ALL);
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   183
			snprintf(message, sizeof(message), "%s %s", temp, buf);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   184
			break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   185
	}
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   186
732
2d9c718eedcf (svn r1184) -Fix: [Network] %p as chat message no longer gives funny result (tnx to
truelight
parents: 722
diff changeset
   187
	IConsolePrintF(color, "%s", message);
2d9c718eedcf (svn r1184) -Fix: [Network] %p as chat message no longer gives funny result (tnx to
truelight
parents: 722
diff changeset
   188
	AddTextMessage(color, duration, "%s", message);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   189
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   190
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   191
// Calculate the frame-lag of a client
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   192
uint NetworkCalculateLag(const NetworkClientState *cs)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   193
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   194
	int lag = cs->last_frame_server - cs->last_frame;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   195
	// 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
   196
	//  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
   197
	// The packet can be out by a max of _net_frame_freq
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   198
	if (cs->last_frame_server + DAY_TICKS + _network_frame_freq < _frame_counter)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   199
		lag += _frame_counter - (cs->last_frame_server + DAY_TICKS + _network_frame_freq);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   200
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   201
	return lag;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   202
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   205
// 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
   206
//  error
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
   207
static void NetworkError(StringID error_string)
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 188
diff changeset
   208
{
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   209
	_switch_mode = SM_MENU;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   210
	_switch_mode_errorstr = error_string;
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   211
}
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   212
1010
17470a8e5eb7 (svn r1509) Const correctness and add static where appropriate while touching the lines anyway
tron
parents: 959
diff changeset
   213
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
   214
{
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   215
	DEBUG(net, 0)("[NET] Client could not start network: %s",error);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   216
	NetworkError(STR_NETWORK_ERR_CLIENT_START);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   217
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
1010
17470a8e5eb7 (svn r1509) Const correctness and add static where appropriate while touching the lines anyway
tron
parents: 959
diff changeset
   219
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
   220
{
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   221
	DEBUG(net, 0)("[NET] Server could not start network: %s",error);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   222
	NetworkError(STR_NETWORK_ERR_SERVER_START);
208
f3998d4089f0 (svn r209) -Fix: network code based desync
signde
parents: 206
diff changeset
   223
}
f3998d4089f0 (svn r209) -Fix: network code based desync
signde
parents: 206
diff changeset
   224
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   225
static void NetworkClientError(byte res, NetworkClientState *cs) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   226
	// 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
   227
	//  disconnection (and why!)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   228
	NetworkErrorCode errorno;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   229
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   230
	// We just want to close the connection..
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   231
	if (res == NETWORK_RECV_STATUS_CLOSE_QUERY) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   232
		cs->quited = true;
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   233
		NetworkCloseClient(cs);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   234
		_networking = false;
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
		DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   237
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   238
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   239
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   240
	switch(res) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   241
		case NETWORK_RECV_STATUS_DESYNC: errorno = NETWORK_ERROR_DESYNC; break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   242
		case NETWORK_RECV_STATUS_SAVEGAME: errorno = NETWORK_ERROR_SAVEGAME_FAILED; break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   243
		default: errorno = NETWORK_ERROR_GENERAL;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   244
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   245
	// 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
   246
	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
   247
			res != NETWORK_RECV_STATUS_SERVER_BANNED) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   248
		SEND_COMMAND(PACKET_CLIENT_ERROR)(errorno);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   249
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   250
		// Dequeue all commands before closing the socket
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   251
		NetworkSend_Packets(DEREF_CLIENT(0));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   252
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   253
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   254
	_switch_mode = SM_MENU;
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   255
	NetworkCloseClient(cs);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   256
	_networking = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   257
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   258
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   259
// Find all IP-aliases for this host
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   260
static void NetworkFindIPs(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   261
{
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   262
	int i, last;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   263
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   264
#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
   265
	/* 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
   266
	int _netstat(int fd, char **output, int verbose);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   267
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   268
	int seek_past_header(char **pos, const char *header) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   269
		char *new_pos = strstr(*pos, header);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   270
		if (new_pos == 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   271
			return B_ERROR;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   272
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   273
		*pos += strlen(header) + new_pos - *pos + 1;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   274
		return B_OK;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   275
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   276
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   277
	int output_length;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   278
	char *output_pointer = NULL;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   279
	char **output;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   280
	int sock = socket(AF_INET, SOCK_DGRAM, 0);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   281
	i = 0;
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
	// If something fails, make sure the list is empty
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   284
	_network_ip_list[0] = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   285
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   286
	if (sock < 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   287
		DEBUG(net, 0)("Error creating socket!");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   288
		return;
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
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   291
	output_length = _netstat(sock, &output_pointer, 1);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   292
	if (output_length < 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   293
		DEBUG(net, 0)("Error running _netstat!");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   294
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   295
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   296
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   297
	output = &output_pointer;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   298
	if (seek_past_header(output, "IP Interfaces:") == B_OK) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   299
		for (;;) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   300
			uint32 n, fields, read;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   301
			uint8 i1, i2, i3, i4, j1, j2, j3, j4;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   302
			struct in_addr inaddr;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   303
			fields = sscanf(*output, "%u: %hhu.%hhu.%hhu.%hhu, netmask %hhu.%hhu.%hhu.%hhu%n",
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   304
												&n, &i1,&i2,&i3,&i4, &j1,&j2,&j3,&j4, &read);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   305
			read += 1;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   306
			if (fields != 9) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   307
				break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   308
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   309
			inaddr.s_addr = htonl((uint32)i1 << 24 | (uint32)i2 << 16 | (uint32)i3 << 8 | (uint32)i4);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   310
			if (inaddr.s_addr != 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   311
				_network_ip_list[i] = inaddr.s_addr;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   312
				i++;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   313
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   314
			if (read < 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   315
				break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   316
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   317
			*output += read;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   318
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   319
		/* 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
   320
		/*free(output_pointer);*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   321
		/*free(output);*/
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   322
		closesocket(sock);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   323
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   324
#elif defined(HAVE_GETIFADDRS)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   325
	struct ifaddrs *ifap, *ifa;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   326
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   327
	// If something fails, make sure the list is empty
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   328
	_network_ip_list[0] = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   329
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   330
	if (getifaddrs(&ifap) != 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   333
	i = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   334
	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   335
		if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   336
			continue;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   337
		_network_ip_list[i] = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   338
		i++;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   339
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   340
	freeifaddrs(ifap);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   341
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   342
#else /* not HAVE_GETIFADDRS */
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   343
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   344
	unsigned long len = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   345
	SOCKET sock;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   346
	IFREQ ifo[MAX_INTERFACES];
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   347
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   348
#ifndef WIN32
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   349
	struct ifconf if_conf;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   350
#endif
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   351
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   352
	// If something fails, make sure the list is empty
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   353
	_network_ip_list[0] = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   354
1466
fd5c1d8c992a (svn r1970) Fix some warnings which Cygwin showed
tron
parents: 1332
diff changeset
   355
	sock = socket(AF_INET, SOCK_DGRAM, 0);
fd5c1d8c992a (svn r1970) Fix some warnings which Cygwin showed
tron
parents: 1332
diff changeset
   356
	if (sock == INVALID_SOCKET) return;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   357
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   358
#ifdef WIN32
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   359
	// On windows it is easy
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   360
	memset(&ifo[0], 0, sizeof(ifo));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   361
	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
   362
		closesocket(sock);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   363
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   364
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   365
#else
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   366
	// On linux a bit harder
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   367
	if_conf.ifc_len = (sizeof (struct ifreq)) * MAX_INTERFACES;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   368
	if_conf.ifc_buf = (char *)&ifo[0];
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   369
	if ((ioctl(sock, SIOCGIFCONF, &if_conf)) == -1) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   370
		closesocket(sock);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   371
		return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   372
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   373
	len = if_conf.ifc_len;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   374
#endif /* WIN32 */
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   375
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   376
	// Now walk through all IPs and list them
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   377
	for (i = 0; i < (int)(len / sizeof(IFREQ)); i++) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   378
		// Request IP for this interface
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   379
#ifdef WIN32
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   380
		_network_ip_list[i] = *(&ifo[i].iiAddress.AddressIn.sin_addr.s_addr);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   381
#else
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   382
		if ((ioctl(sock, SIOCGIFADDR, &ifo[i])) != 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   383
			closesocket(sock);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   384
			return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   387
		_network_ip_list[i] = ((struct sockaddr_in *)&ifo[i].ifr_addr)->sin_addr.s_addr;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   388
#endif
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   389
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   390
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   391
	closesocket(sock);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   393
#endif /* not HAVE_GETIFADDRS */
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   394
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   395
	_network_ip_list[i] = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   396
	last = i - 1;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   397
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   398
	DEBUG(net, 3)("Detected IPs:");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   399
	// Now display to the debug all the detected ips
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   400
	i = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   401
	while (_network_ip_list[i] != 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   402
		// Also check for non-used ips (127.0.0.1)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   403
		if (_network_ip_list[i] == inet_addr("127.0.0.1")) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   404
			// If there is an ip after thisone, put him in here
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   405
			if (last > i)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   406
				_network_ip_list[i] = _network_ip_list[last];
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   407
			// Clear the last ip
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   408
			_network_ip_list[last] = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   409
			// And we have 1 ip less
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   410
			last--;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   411
			continue;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   412
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   413
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   414
		DEBUG(net, 3)(" %d) %s", i, inet_ntoa(*(struct in_addr *)&_network_ip_list[i]));//inet_ntoa(inaddr));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   415
		i++;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   416
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   417
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   418
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   419
// Resolve a hostname to a inet_addr
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   420
unsigned long NetworkResolveHost(const char *hostname)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   421
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   422
	in_addr_t ip;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   423
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   424
	// First try: is it an ip address?
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   425
	ip = inet_addr(hostname);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   426
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   427
	// If not try to resolve the name
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   428
	if (ip == INADDR_NONE) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   429
		struct hostent *he = gethostbyname(hostname);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   430
		if (he == NULL) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   431
			DEBUG(net, 0) ("[NET] Cannot resolve %s", hostname);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   432
		} else {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   433
			struct in_addr addr = *(struct in_addr *)he->h_addr_list[0];
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   434
			DEBUG(net, 1) ("[NET] Resolved %s to %s", hostname, inet_ntoa(addr));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   435
			ip = addr.s_addr;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   436
		}
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
	return ip;
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
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   441
// Converts a string to ip/port/player
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   442
//  Format: IP#player:port
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   443
//
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   444
// connection_string will be re-terminated to seperate out the hostname, and player and port will
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   445
// be set to the player and port strings given by the user, inside the memory area originally
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   446
// occupied by connection_string.
1329
a8a0d60b0a8e (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   447
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
   448
{
1329
a8a0d60b0a8e (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   449
	char *p;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   450
	for (p = connection_string; *p != '\0'; p++) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   451
		if (*p == '#') {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   452
			*player = p + 1;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   453
			*p = '\0';
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   454
		} else if (*p == ':') {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   455
			*port = p + 1;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   456
			*p = '\0';
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   457
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   460
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   461
// Creates a new client from a socket
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   462
//   Used both by the server and the client
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   463
static NetworkClientState *NetworkAllocClient(SOCKET s)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
{
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   465
	NetworkClientState *cs;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   466
	NetworkClientInfo *ci;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   467
	byte client_no;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   468
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   469
	client_no = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   470
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   471
	if (_network_server) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   472
		// Can we handle a new client?
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   473
		if (_network_clients_connected >= MAX_CLIENTS)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   474
			return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   475
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   476
		if (_network_game_info.clients_on >= _network_game_info.clients_max)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   477
			return NULL;
206
7f8c26d8526b (svn r207) -Codechange: randomizer handling
signde
parents: 193
diff changeset
   478
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   479
		// Register the login
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   480
		client_no = _network_clients_connected++;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   481
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   482
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   483
	cs = &_clients[client_no];
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   484
	memset(cs, 0, sizeof(*cs));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   485
	cs->socket = s;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   486
	cs->last_frame = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   487
	cs->quited = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   488
1602
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1466
diff changeset
   489
	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
   490
	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
   491
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   492
	if (_network_server) {
662
029d67bd54e6 (svn r1096) -Fix: [Network] PlayAs is now registered correctly
truelight
parents: 659
diff changeset
   493
		ci = DEREF_CLIENT_INFO(cs);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   494
		memset(ci, 0, sizeof(*ci));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   495
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   496
		cs->index = _network_client_index++;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   497
		ci->client_index = cs->index;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   498
		ci->join_date = _date;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   499
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   500
		InvalidateWindow(WC_CLIENT_LIST, 0);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   501
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   502
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   503
	return cs;
206
7f8c26d8526b (svn r207) -Codechange: randomizer handling
signde
parents: 193
diff changeset
   504
}
7f8c26d8526b (svn r207) -Codechange: randomizer handling
signde
parents: 193
diff changeset
   505
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   506
// Close a connection
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   507
void NetworkCloseClient(NetworkClientState *cs)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
{
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   509
	NetworkClientInfo *ci;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   510
	// Socket is already dead
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 895
diff changeset
   511
	if (cs->socket == INVALID_SOCKET) {
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 895
diff changeset
   512
		cs->quited = true;
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 895
diff changeset
   513
		return;
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 895
diff changeset
   514
	}
260
32fcaaf9f4ef (svn r266) -Fix: hopefully fixed the desync problem nicely (and reverted the
truelight
parents: 256
diff changeset
   515
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   516
	DEBUG(net, 1) ("[NET] Closed client connection");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   517
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   518
	if (!cs->quited && _network_server && cs->status > STATUS_INACTIVE) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   519
		// 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
   520
		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
   521
		char str[100];
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   522
		char client_name[NETWORK_NAME_LENGTH];
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   523
		NetworkClientState *new_cs;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   525
		NetworkGetClientName(client_name, sizeof(client_name), cs);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   526
722
d0b031be5232 (svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents: 716
diff changeset
   527
		GetString(str, STR_NETWORK_ERR_CLIENT_GENERAL + errorno);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   528
2373
f06403621870 (svn r2899) -Fix: Several format string vulnerabilities and buffer overflows in the network code
tron
parents: 2235
diff changeset
   529
		NetworkTextMessage(NETWORK_ACTION_LEAVE, 1, false, client_name, "%s", str);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   530
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   531
		// Inform other clients of this... strange leaving ;)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   532
		FOR_ALL_CLIENTS(new_cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   533
			if (new_cs->status > STATUS_AUTH && cs != new_cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   534
				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
   535
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
		}
1604
ee7859b804f6 (svn r2108) -Fix: the server now also unpause when a client desyncs or something
truelight
parents: 1602
diff changeset
   537
	}
1602
08783e4287dc (svn r2106) -Fix: improved the network-join algoritm, it is now a bit more stable
truelight
parents: 1466
diff changeset
   538
1604
ee7859b804f6 (svn r2108) -Fix: the server now also unpause when a client desyncs or something
truelight
parents: 1602
diff changeset
   539
	/* When the client was PRE_ACTIVE, the server was in pause mode, so unpause */
ee7859b804f6 (svn r2108) -Fix: the server now also unpause when a client desyncs or something
truelight
parents: 1602
diff changeset
   540
	if (cs->status == STATUS_PRE_ACTIVE && _network_pause_on_join) {
ee7859b804f6 (svn r2108) -Fix: the server now also unpause when a client desyncs or something
truelight
parents: 1602
diff changeset
   541
		DoCommandP(0, 0, 0, NULL, CMD_PAUSE);
ee7859b804f6 (svn r2108) -Fix: the server now also unpause when a client desyncs or something
truelight
parents: 1602
diff changeset
   542
		NetworkServer_HandleChat(NETWORK_ACTION_CHAT, DESTTYPE_BROADCAST, 0, "Game unpaused", NETWORK_SERVER_INDEX);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   544
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   545
	closesocket(cs->socket);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   546
	cs->writable = false;
903
f860c7234167 (svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents: 895
diff changeset
   547
	cs->quited = true;
185
646403e35006 (svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents: 177
diff changeset
   548
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   549
	// Free all pending and partially received packets
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   550
	while (cs->packet_queue != NULL) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   551
		Packet *p = cs->packet_queue->next;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   552
		free(cs->packet_queue);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   553
		cs->packet_queue = p;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
	}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   555
	free(cs->packet_recv);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   556
	cs->packet_recv = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   558
	while (cs->command_queue != NULL) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   559
		CommandPacket *p = cs->command_queue->next;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   560
		free(cs->command_queue);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   561
		cs->command_queue = p;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   562
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   563
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   564
	// Close the gap in the client-list
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   565
	ci = DEREF_CLIENT_INFO(cs);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   567
	if (_network_server) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   568
		// We just lost one client :(
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
   569
		if (cs->status > STATUS_INACTIVE) _network_game_info.clients_on--;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   570
		_network_clients_connected--;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   571
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   572
		while ((cs + 1) != DEREF_CLIENT(MAX_CLIENTS) && (cs + 1)->socket != INVALID_SOCKET) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   573
			*cs = *(cs + 1);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   574
			*ci = *(ci + 1);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   575
			cs++;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   576
			ci++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   579
		InvalidateWindow(WC_CLIENT_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   582
	// Reset the status of the last socket
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
	cs->socket = INVALID_SOCKET;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   584
	cs->status = STATUS_INACTIVE;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   585
	cs->index = NETWORK_EMPTY_INDEX;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   586
	ci->client_index = NETWORK_EMPTY_INDEX;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   587
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   588
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   589
// 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
   590
static bool NetworkConnect(const char *hostname, int port)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   591
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   592
	SOCKET s;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   593
	struct sockaddr_in sin;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   594
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   595
	DEBUG(net, 1) ("[NET] Connecting to %s %d", hostname, port);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   597
	s = socket(AF_INET, SOCK_STREAM, 0);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   598
	if (s == INVALID_SOCKET) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   599
		ClientStartError("socket() failed");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   600
		return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
1332
86c9ebbcda31 (svn r1836) Clean up some strange constructs concerning socket options
tron
parents: 1329
diff changeset
   603
	if (!SetNoDelay(s))
86c9ebbcda31 (svn r1836) Clean up some strange constructs concerning socket options
tron
parents: 1329
diff changeset
   604
		DEBUG(net, 1)("[NET] Setting TCP_NODELAY failed");
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   605
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   606
	sin.sin_family = AF_INET;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   607
	sin.sin_addr.s_addr = NetworkResolveHost(hostname);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   608
	sin.sin_port = htons(port);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   609
	_network_last_host_ip = sin.sin_addr.s_addr;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   610
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   611
	if (connect(s, (struct sockaddr*) &sin, sizeof(sin)) != 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   612
		// We failed to connect for which reason what so ever
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   613
		return false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   614
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   615
1332
86c9ebbcda31 (svn r1836) Clean up some strange constructs concerning socket options
tron
parents: 1329
diff changeset
   616
	if (!SetNonBlocking(s))
86c9ebbcda31 (svn r1836) Clean up some strange constructs concerning socket options
tron
parents: 1329
diff changeset
   617
		DEBUG(net, 0)("[NET] Setting non-blocking failed"); // XXX should this be an error?
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   618
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   619
	// 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
   620
	NetworkAllocClient(s);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   621
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   622
	ShowJoinStatusWindow();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   623
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   624
	memcpy(&network_tmp_patches, &_patches, sizeof(_patches));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   626
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   629
// For the server, to accept new clients
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   630
static void NetworkAcceptClients(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
	struct sockaddr_in sin;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
	SOCKET s;
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   634
	NetworkClientState *cs;
959
b031d88c76f3 (svn r1451) Fix some of the signed/unsigned comparison warnings
tron
parents: 903
diff changeset
   635
	uint i;
841
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   636
	bool banned;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   638
	// Should never ever happen.. is it possible??
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
	assert(_listensocket != INVALID_SOCKET);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   640
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   641
	for (;;) {
1332
86c9ebbcda31 (svn r1836) Clean up some strange constructs concerning socket options
tron
parents: 1329
diff changeset
   642
		socklen_t sin_len;
86c9ebbcda31 (svn r1836) Clean up some strange constructs concerning socket options
tron
parents: 1329
diff changeset
   643
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   644
		sin_len = sizeof(sin);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
		s = accept(_listensocket, (struct sockaddr*)&sin, &sin_len);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   646
		if (s == INVALID_SOCKET) return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   647
1332
86c9ebbcda31 (svn r1836) Clean up some strange constructs concerning socket options
tron
parents: 1329
diff changeset
   648
		SetNonBlocking(s); // XXX error handling?
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   649
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   650
		DEBUG(net, 1) ("[NET] 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
   651
1332
86c9ebbcda31 (svn r1836) Clean up some strange constructs concerning socket options
tron
parents: 1329
diff changeset
   652
		SetNoDelay(s); // XXX error handling?
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   653
841
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   654
		/* Check if the client is banned */
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   655
		banned = false;
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   656
		for (i = 0; i < lengthof(_network_ban_list); i++) {
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   657
			if (_network_ban_list[i] == NULL)
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   658
				continue;
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   659
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   660
			if (sin.sin_addr.s_addr == inet_addr(_network_ban_list[i])) {
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   661
				Packet *p = NetworkSend_Init(PACKET_SERVER_BANNED);
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   662
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   663
				DEBUG(net, 1)("[NET] Banned ip tried to join (%s), refused", _network_ban_list[i]);
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   664
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   665
				p->buffer[0] = p->size & 0xFF;
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   666
				p->buffer[1] = p->size >> 8;
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   667
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   668
				send(s, p->buffer, p->size, 0);
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   669
				closesocket(s);
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   670
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   671
				free(p);
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   672
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   673
				banned = true;
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   674
				break;
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   675
			}
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   676
		}
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   677
		/* If this client is banned, continue with next client */
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   678
		if (banned)
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   679
			continue;
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   680
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   681
		cs = NetworkAllocClient(s);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   682
		if (cs == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   683
			// no more clients allowed?
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   684
			// Send to the client that we are full!
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   685
			Packet *p = NetworkSend_Init(PACKET_SERVER_FULL);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   686
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   687
			p->buffer[0] = p->size & 0xFF;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   688
			p->buffer[1] = p->size >> 8;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   689
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   690
			send(s, p->buffer, p->size, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   691
			closesocket(s);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   692
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   693
			free(p);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   694
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   695
			continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   696
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   697
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   698
		// 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
   699
		//  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
   700
		//  the client stays inactive
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   701
		cs->status = STATUS_INACTIVE;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   702
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   703
		{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   704
			// Save the IP of the client
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   705
			NetworkClientInfo *ci;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   706
			ci = DEREF_CLIENT_INFO(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   707
			ci->client_ip = sin.sin_addr.s_addr;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   708
		}
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
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   711
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   712
// Set up the listen socket for the server
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   713
static bool NetworkListen(void)
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
   714
{
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   715
	SOCKET ls;
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   716
	struct sockaddr_in sin;
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   717
	int port;
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   718
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   719
	port = _network_server_port;
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   720
629
ce914eb4023d (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
   721
	DEBUG(net, 1) ("[NET] Listening on %s:%d", _network_server_bind_ip_host, port);
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   722
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   723
	ls = socket(AF_INET, SOCK_STREAM, 0);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   724
	if (ls == INVALID_SOCKET) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   725
		ServerStartError("socket() on listen socket failed");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   726
		return false;
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   727
	}
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   728
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   729
	{ // reuse the socket
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   730
		int reuse = 1;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   731
		// The (const char*) cast is needed for windows!!
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   732
		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
   733
			ServerStartError("setsockopt() on listen socket failed");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   734
			return false;
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
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   737
1332
86c9ebbcda31 (svn r1836) Clean up some strange constructs concerning socket options
tron
parents: 1329
diff changeset
   738
	if (!SetNonBlocking(ls))
86c9ebbcda31 (svn r1836) Clean up some strange constructs concerning socket options
tron
parents: 1329
diff changeset
   739
		DEBUG(net, 0)("[NET] Setting non-blocking failed"); // XXX should this be an error?
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   740
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   741
	sin.sin_family = AF_INET;
629
ce914eb4023d (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
   742
	sin.sin_addr.s_addr = _network_server_bind_ip;
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   743
	sin.sin_port = htons(port);
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   744
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   745
	if (bind(ls, (struct sockaddr*)&sin, sizeof(sin)) != 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   746
		ServerStartError("bind() failed");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   747
		return false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   748
	}
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   749
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   750
	if (listen(ls, 1) != 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   751
		ServerStartError("listen() failed");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   752
		return false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   753
	}
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   754
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   755
	_listensocket = ls;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   756
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   757
	return true;
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   758
}
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
   759
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   760
// Close all current connections
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   761
static void NetworkClose(void)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   762
{
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   763
	NetworkClientState *cs;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   764
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   765
	FOR_ALL_CLIENTS(cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   766
		if (!_network_server) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   767
			SEND_COMMAND(PACKET_CLIENT_QUIT)("leaving");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   768
			NetworkSend_Packets(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   769
		}
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   770
		NetworkCloseClient(cs);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   771
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   772
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   773
	if (_network_server) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   774
		// We are a server, also close the listensocket
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   775
		closesocket(_listensocket);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   776
		_listensocket = INVALID_SOCKET;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   777
		DEBUG(net, 1) ("[NET] Closed listener");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   778
		NetworkUDPClose();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   779
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   780
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   781
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   782
// Inits the network (cleans sockets and stuff)
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   783
static void NetworkInitialize(void)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   784
{
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   785
	NetworkClientState *cs;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   786
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   787
	_local_command_queue = NULL;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   788
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   789
	// Clean all client-sockets
662
029d67bd54e6 (svn r1096) -Fix: [Network] PlayAs is now registered correctly
truelight
parents: 659
diff changeset
   790
	memset(_clients, 0, sizeof(_clients));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   791
	for (cs = _clients; cs != &_clients[MAX_CLIENTS]; cs++) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   792
		cs->socket = INVALID_SOCKET;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   793
		cs->status = STATUS_INACTIVE;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   794
		cs->command_queue = NULL;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   795
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   796
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   797
	// 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
   798
	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
   799
	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
   800
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   801
	_sync_frame = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   802
	_network_first_time = true;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   803
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   804
	_network_reconnect = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   805
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   806
	NetworkUDPInitialize();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   807
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   808
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   809
// Query a server to fetch his game-info
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   810
//  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
   811
//   else only the client_info is fetched
1329
a8a0d60b0a8e (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   812
NetworkGameList *NetworkQueryServer(const char* host, unsigned short port, bool game_info)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   813
{
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
   814
	if (!_network_available) return NULL;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   815
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   816
	NetworkDisconnect();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   817
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   818
	if (game_info) {
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
   819
		return NetworkUDPQueryServer(host, port);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   820
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   821
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   822
	NetworkInitialize();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   823
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   824
	_network_server = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   825
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   826
	// Try to connect
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   827
	_networking = NetworkConnect(host, port);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   828
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   829
//	ttd_strlcpy(_network_last_host, host, sizeof(_network_last_host));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   830
//	_network_last_port = port;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   831
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   832
	// We are connected
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   833
	if (_networking) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   834
		SEND_COMMAND(PACKET_CLIENT_COMPANY_INFO)();
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
   835
		return NULL;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   836
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   837
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   838
	// No networking, close everything down again
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   839
	NetworkDisconnect();
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
   840
	return NULL;
543
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
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
   843
/* 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
   844
 * 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
   845
 * as manually added. */
1329
a8a0d60b0a8e (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   846
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
   847
{
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
   848
	if (*b != '\0') {
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
   849
		NetworkGameList *item;
1329
a8a0d60b0a8e (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   850
		const char *port = NULL;
a8a0d60b0a8e (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   851
		const char *player = NULL;
a8a0d60b0a8e (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   852
		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
   853
		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
   854
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   855
		ttd_strlcpy(host, b, lengthof(host));
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   856
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
   857
		ttd_strlcpy(_network_default_ip, b, lengthof(_network_default_ip));
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
   858
		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
   859
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   860
		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
   861
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
   862
		if (player != NULL) _network_playas = atoi(player);
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
   863
		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
   864
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
   865
		item = NetworkQueryServer(host, rport, true);
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
   866
		item->manually = true;
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
   867
	}
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
   868
}
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
   869
746
976fc7c0edf3 (svn r1202) -Fix: removed unused variable
truelight
parents: 738
diff changeset
   870
/* 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
   871
 * 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
   872
 * by the function that generates the config file. */
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1026
diff changeset
   873
void NetworkRebuildHostList(void)
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
   874
{
959
b031d88c76f3 (svn r1451) Fix some of the signed/unsigned comparison warnings
tron
parents: 903
diff changeset
   875
	uint i = 0;
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
   876
	NetworkGameList *item = _network_game_list;
841
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   877
	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
   878
		if (item->manually) {
c59cbb76b5b2 (svn r3470) - Fix: plug a memleak in _network_host_list.
Darkvater
parents: 2881
diff changeset
   879
			free(_network_host_list[i]);
895
d73b3d8a5280 (svn r1381) Fix: [ 1095143 ] Servers list now also saves the port
dominik
parents: 863
diff changeset
   880
			_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
   881
		}
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
   882
		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
   883
	}
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
   884
841
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
   885
	for (; i < lengthof(_network_host_list); i++) {
2915
c59cbb76b5b2 (svn r3470) - Fix: plug a memleak in _network_host_list.
Darkvater
parents: 2881
diff changeset
   886
		free(_network_host_list[i]);
c59cbb76b5b2 (svn r3470) - Fix: plug a memleak in _network_host_list.
Darkvater
parents: 2881
diff changeset
   887
		_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
   888
	}
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
   889
}
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
   890
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   891
// Used by clients, to connect to a server
1329
a8a0d60b0a8e (svn r1833) byte -> char transition: the rest
tron
parents: 1317
diff changeset
   892
bool NetworkClientConnectGame(const char* host, unsigned short port)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   893
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   894
	if (!_network_available) return false;
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
	if (port == 0) return false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   897
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   898
	ttd_strlcpy(_network_last_host, host, sizeof(_network_last_host));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   899
	_network_last_port = port;
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
	NetworkDisconnect();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   902
	NetworkUDPClose();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   903
	NetworkInitialize();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   904
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   905
	// Try to connect
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   906
	_networking = NetworkConnect(host, port);
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
	// We are connected
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   909
	if (_networking) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   910
		IConsoleCmdExec("exec scripts/on_client.scr 0");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   911
		NetworkClient_Connected();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   912
	} else {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   913
		// Connecting failed
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   914
		NetworkError(STR_NETWORK_ERR_NOCONNECTION);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   915
	}
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
	return _networking;
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
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
   920
static void NetworkInitGameInfo(void)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   921
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   922
	NetworkClientInfo *ci;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   923
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   924
	ttd_strlcpy(_network_game_info.server_name, _network_server_name, sizeof(_network_game_info.server_name));
1026
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1010
diff changeset
   925
	ttd_strlcpy(_network_game_info.server_password, _network_server_password, sizeof(_network_server_password));
5b2681cad645 (svn r1527) -Add: RCon (Remote Connection). A server can set:
truelight
parents: 1010
diff changeset
   926
	ttd_strlcpy(_network_game_info.rcon_password, _network_rcon_password, sizeof(_network_rcon_password));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   927
	if (_network_game_info.server_name[0] == '\0')
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   928
		snprintf(_network_game_info.server_name, sizeof(_network_game_info.server_name), "Unnamed Server");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   929
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
   930
	ttd_strlcpy(_network_game_info.server_revision, _openttd_revision, sizeof(_network_game_info.server_revision));
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
   931
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   932
	// The server is a client too ;)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   933
	if (_network_dedicated) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   934
		_network_game_info.clients_on = 0;
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
   935
		_network_game_info.companies_on = 0;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   936
		_network_game_info.dedicated = true;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   937
	} else {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   938
		_network_game_info.clients_on = 1;
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
   939
		_network_game_info.companies_on = 1;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   940
		_network_game_info.dedicated = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   941
	}
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
   942
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   943
	_network_game_info.spectators_on = 0;
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
   944
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   945
	_network_game_info.game_date = _date;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   946
	_network_game_info.start_date = ConvertIntDate(_patches.starting_date);
863
8d09f9331a80 (svn r1344) Use MapSize[XY]() (or MapSize()/MapMax[XY]() where appropriate) instead of TILES_[XY]
tron
parents: 841
diff changeset
   947
	_network_game_info.map_width = MapSizeX();
8d09f9331a80 (svn r1344) Use MapSize[XY]() (or MapSize()/MapMax[XY]() where appropriate) instead of TILES_[XY]
tron
parents: 841
diff changeset
   948
	_network_game_info.map_height = MapSizeY();
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   949
	_network_game_info.map_set = _opt.landscape;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   950
1899
98317b840d7b (svn r2405) Simplify a few '? true : false' and '? false : true', especially the latter is confusing
tron
parents: 1832
diff changeset
   951
	_network_game_info.use_password = (_network_server_password[0] != '\0');
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   952
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   953
	// 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
   954
	//  The index is NETWORK_SERVER_INDEX ( = 1)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   955
	ci = &_network_client_info[MAX_CLIENT_INFO - 1];
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   956
	memset(ci, 0, sizeof(*ci));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   957
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   958
	ci->client_index = NETWORK_SERVER_INDEX;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   959
	if (_network_dedicated)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   960
		ci->client_playas = OWNER_SPECTATOR;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   961
	else
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   962
		ci->client_playas = _local_player + 1;
693
d52f31c390c1 (svn r1137) -Fix: [Network] Replaces all strncpy with ttd_strlcpy.. windows really
truelight
parents: 683
diff changeset
   963
	ttd_strlcpy(ci->client_name, _network_player_name, sizeof(ci->client_name));
d52f31c390c1 (svn r1137) -Fix: [Network] Replaces all strncpy with ttd_strlcpy.. windows really
truelight
parents: 683
diff changeset
   964
	ttd_strlcpy(ci->unique_id, _network_unique_id, sizeof(ci->unique_id));
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   965
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   966
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   967
bool NetworkServerStart(void)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   968
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   969
	if (!_network_available) return false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   970
629
ce914eb4023d (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
   971
	/* Call the pre-scripts */
ce914eb4023d (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
   972
	IConsoleCmdExec("exec scripts/pre_server.scr 0");
ce914eb4023d (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
   973
	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
   974
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   975
	NetworkInitialize();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   976
	if (!NetworkListen())
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   977
		return false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   978
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   979
	// Try to start UDP-server
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   980
	_network_udp_server = true;
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 746
diff changeset
   981
	_network_udp_server = NetworkUDPListen(&_udp_server_socket, _network_server_bind_ip, _network_server_port, false);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   982
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   983
	_network_server = true;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   984
	_networking = true;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   985
	_frame_counter = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   986
	_frame_counter_server = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   987
	_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
   988
	_last_sync_frame = 0;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   989
	_network_own_client_index = NETWORK_SERVER_INDEX;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   990
683
a46ac7956e12 (svn r1124) -Fix: [Network] Also the server now has the option Give Money in
truelight
parents: 679
diff changeset
   991
	if (!_network_dedicated)
a46ac7956e12 (svn r1124) -Fix: [Network] Also the server now has the option Give Money in
truelight
parents: 679
diff changeset
   992
		_network_playas = 1;
a46ac7956e12 (svn r1124) -Fix: [Network] Also the server now has the option Give Money in
truelight
parents: 679
diff changeset
   993
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   994
	_network_clients_connected = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   995
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   996
	NetworkInitGameInfo();
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
	// execute server initialization script
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
   999
	IConsoleCmdExec("exec scripts/on_server.scr 0");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1000
	// if the server is dedicated ... add some other script
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1001
	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
  1002
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 663
diff changeset
  1003
	/* 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
  1004
	_network_last_advertise_frame = 0;
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2859
diff changeset
  1005
	_network_need_advertise = true;
668
346853c8b513 (svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents: 663
diff changeset
  1006
	NetworkUDPAdvertise();
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1007
	return true;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1008
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1009
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1010
// The server is rebooting...
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1011
// The only difference with NetworkDisconnect, is the packets that is sent
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1012
void NetworkReboot(void)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1013
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1014
	if (_network_server) {
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
  1015
		NetworkClientState *cs;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1016
		FOR_ALL_CLIENTS(cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1017
			SEND_COMMAND(PACKET_SERVER_NEWGAME)(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1018
			NetworkSend_Packets(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1019
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1020
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1021
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1022
	NetworkClose();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1023
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1024
	// Free all queued commands
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1025
	while (_local_command_queue != NULL) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1026
		CommandPacket *p = _local_command_queue;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1027
		_local_command_queue = _local_command_queue->next;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1028
		free(p);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1029
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1030
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1031
	_networking = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1032
	_network_server = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1033
}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1034
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1035
// We want to disconnect from the host/clients
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1036
void NetworkDisconnect(void)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1037
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1038
	if (_network_server) {
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
  1039
		NetworkClientState *cs;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1040
		FOR_ALL_CLIENTS(cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1041
			SEND_COMMAND(PACKET_SERVER_SHUTDOWN)(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1042
			NetworkSend_Packets(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1043
		}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1044
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1045
1832
84c9fb08f7eb (svn r2337) -Fix: NetworkUDPRemoveAdvertise wasn't fully correct (which made Darkvater go crazy ;))
truelight
parents: 1739
diff changeset
  1046
	if (_network_advertise)
84c9fb08f7eb (svn r2337) -Fix: NetworkUDPRemoveAdvertise wasn't fully correct (which made Darkvater go crazy ;))
truelight
parents: 1739
diff changeset
  1047
		NetworkUDPRemoveAdvertise();
765
7e9c5cdfdf1c (svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents: 764
diff changeset
  1048
598
200de7111fa9 (svn r1022) -Fix: [Network] [ 1083690 ] The password dialog made the network
truelight
parents: 543
diff changeset
  1049
	DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0);
200de7111fa9 (svn r1022) -Fix: [Network] [ 1083690 ] The password dialog made the network
truelight
parents: 543
diff changeset
  1050
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1051
	NetworkClose();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1052
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1053
	// Free all queued commands
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1054
	while (_local_command_queue != NULL) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1055
		CommandPacket *p = _local_command_queue;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1056
		_local_command_queue = _local_command_queue->next;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1057
		free(p);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1058
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1059
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1060
	if (_networking && !_network_server) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1061
		memcpy(&_patches, &network_tmp_patches, sizeof(_patches));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1062
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1063
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1064
	_networking = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1065
	_network_server = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1066
}
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
// Receives something from the network
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
  1069
static bool NetworkReceive(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1070
{
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
  1071
	NetworkClientState *cs;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1072
	int n;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1073
	fd_set read_fd, write_fd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1074
	struct timeval tv;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 188
diff changeset
  1075
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1076
	FD_ZERO(&read_fd);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1077
	FD_ZERO(&write_fd);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1078
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1079
	FOR_ALL_CLIENTS(cs) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1080
		FD_SET(cs->socket, &read_fd);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1081
		FD_SET(cs->socket, &write_fd);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1082
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1083
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1084
	// take care of listener port
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1085
	if (_network_server) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1086
		FD_SET(_listensocket, &read_fd);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1087
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1088
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1089
	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
  1090
#if !defined(__MORPHOS__) && !defined(__AMIGA__)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1091
	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
  1092
#else
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1093
	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
  1094
#endif
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1095
	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
  1096
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1097
	// accept clients..
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1098
	if (_network_server && FD_ISSET(_listensocket, &read_fd))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1099
		NetworkAcceptClients();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1100
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1101
	// read stuff from clients
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1102
	FOR_ALL_CLIENTS(cs) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1103
		cs->writable = !!FD_ISSET(cs->socket, &write_fd);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1104
		if (FD_ISSET(cs->socket, &read_fd)) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1105
			if (_network_server)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1106
				NetworkServer_ReadPackets(cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1107
			else {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1108
				byte res;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1109
				// The client already was quiting!
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1110
				if (cs->quited) return false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1111
				if ((res = NetworkClient_ReadPackets(cs)) != NETWORK_RECV_STATUS_OKAY) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1112
					// 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
  1113
					//   close the client and drop back to main menu
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1114
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1115
					NetworkClientError(res, cs);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1116
					return false;
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
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1120
	}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1121
	return true;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1122
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1123
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1124
// This sends all buffered commands (if possible)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1125
static void NetworkSend(void)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1126
{
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
  1127
	NetworkClientState *cs;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1128
	FOR_ALL_CLIENTS(cs) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1129
		if (cs->writable) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1130
			NetworkSend_Packets(cs);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1131
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1132
			if (cs->status == STATUS_MAP) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1133
				// 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
  1134
				SEND_COMMAND(PACKET_SERVER_MAP)(cs);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1135
			}
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
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1138
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1139
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1140
// Handle the local-command-queue
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
  1141
static void NetworkHandleLocalQueue(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1142
{
2080
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1143
	CommandPacket *cp, **cp_prev;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1144
2080
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1145
	cp_prev = &_local_command_queue;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1146
2080
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1147
	while ( (cp = *cp_prev) != NULL) {
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1148
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1149
		// The queue is always in order, which means
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1150
		// that the first element will be executed first.
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1151
		if (_frame_counter < cp->frame)
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1152
			break;
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1153
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1154
		if (_frame_counter > cp->frame) {
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1155
			// 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
  1156
			// past the command we need to execute.
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1157
			DEBUG(net, 0)("[NET] Trying to execute a packet in the past!");
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1158
			assert(0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1159
		}
2080
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1160
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1161
		// We can execute this command
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1162
		NetworkExecuteCommand(cp);
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1163
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1164
		*cp_prev = cp->next;
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1165
		free(cp);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1166
	}
2080
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1167
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1168
	// Just a safety check, to be removed in the future.
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1169
	// 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
  1170
	// In that case we missed executing it. This will never happen.
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1171
	for(cp = _local_command_queue; cp; cp = cp->next) {
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1172
		assert(_frame_counter < cp->frame);
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1173
	}
48d009b783f3 (svn r2590) Fix: [network] Fixed NetworkHandleLocalQueue
ludde
parents: 2079
diff changeset
  1174
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1175
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1176
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
  1177
static bool NetworkDoClientLoop(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1178
{
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1179
	_frame_counter++;
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
	NetworkHandleLocalQueue();
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1182
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1183
	StateGameLoop();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1184
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1185
	// Check if we are in sync!
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1186
	if (_sync_frame != 0) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1187
		if (_sync_frame == _frame_counter) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1188
#ifdef NETWORK_SEND_DOUBLE_SEED
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1189
			if (_sync_seed_1 != _random_seeds[0][0] || _sync_seed_2 != _random_seeds[0][1]) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1190
#else
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1191
			if (_sync_seed_1 != _random_seeds[0][0]) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1192
#endif
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1193
				NetworkError(STR_NETWORK_ERR_DESYNC);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1194
				DEBUG(net, 0)("[NET] Sync error detected!");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1195
				NetworkClientError(NETWORK_RECV_STATUS_DESYNC, DEREF_CLIENT(0));
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1196
				return false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1197
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1198
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1199
			// 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
  1200
			//   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
  1201
			//   frame as he is.. so we can start playing!
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1202
			if (_network_first_time) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1203
				_network_first_time = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1204
				SEND_COMMAND(PACKET_CLIENT_ACK)();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1205
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1206
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1207
			_sync_frame = 0;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1208
		} else if (_sync_frame < _frame_counter) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1209
			DEBUG(net, 1)("[NET] Missed frame for sync-test (%d / %d)", _sync_frame, _frame_counter);
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1210
			_sync_frame = 0;
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
  1211
		}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1212
	}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1213
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1214
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1215
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1216
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1217
// We have to do some UDP checking
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1218
void NetworkUDPGameLoop(void)
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1219
{
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 746
diff changeset
  1220
	if (_network_udp_server) {
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 746
diff changeset
  1221
		NetworkUDPReceive(_udp_server_socket);
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 746
diff changeset
  1222
		if (_udp_master_socket != INVALID_SOCKET) {
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 746
diff changeset
  1223
			NetworkUDPReceive(_udp_master_socket);
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 746
diff changeset
  1224
		}
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 746
diff changeset
  1225
	}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1226
	else if (_udp_client_socket != INVALID_SOCKET) {
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 746
diff changeset
  1227
		NetworkUDPReceive(_udp_client_socket);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1228
		if (_network_udp_broadcast > 0)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1229
			_network_udp_broadcast--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1230
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1231
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1232
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1233
// The main loop called from ttd.c
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1234
//  Here we also have to do StateGameLoop if needed!
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1235
void NetworkGameLoop(void)
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1236
{
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1237
	if (!_networking) return;
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
  1238
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1239
	if (!NetworkReceive()) return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1240
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1241
	if (_network_server) {
2235
09e4565e5138 (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1242
		bool send_frame = false;
09e4565e5138 (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1243
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1244
		// We first increase the _frame_counter
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1245
		_frame_counter++;
2235
09e4565e5138 (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1246
		// Update max-frame-counter
09e4565e5138 (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1247
		if (_frame_counter > _frame_counter_max) {
09e4565e5138 (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1248
			_frame_counter_max = _frame_counter + _network_frame_freq;
09e4565e5138 (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1249
			send_frame = true;
09e4565e5138 (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1250
		}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1251
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1252
		NetworkHandleLocalQueue();
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
		// Then we make the frame
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1255
		StateGameLoop();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1256
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1257
		_sync_seed_1 = _random_seeds[0][0];
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1258
#ifdef NETWORK_SEND_DOUBLE_SEED
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1259
		_sync_seed_2 = _random_seeds[0][1];
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1260
#endif
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1261
2235
09e4565e5138 (svn r2755) Fix: Fix a desync issue with autoreplace
ludde
parents: 2186
diff changeset
  1262
		NetworkServer_Tick(send_frame);
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1263
	} else {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1264
		// Client
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1265
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1266
		// 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
  1267
		if (_frame_counter_server > _frame_counter) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1268
			while (_frame_counter_server > _frame_counter) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1269
				if (!NetworkDoClientLoop()) break;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1270
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1271
		} else {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1272
			// Else, keep on going till _frame_counter_max
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1273
			if (_frame_counter_max > _frame_counter) {
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1274
				NetworkDoClientLoop();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1275
			}
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1276
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1277
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 188
diff changeset
  1278
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1279
	NetworkSend();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1280
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1281
716
40a349345f82 (svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents: 710
diff changeset
  1282
static void NetworkGenerateUniqueId(void)
602
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1283
{
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1284
	md5_state_t state;
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1285
	md5_byte_t digest[16];
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1286
	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
  1287
	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
  1288
	int di;
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1289
607
d47e1f2d7bdc (svn r1031) -Fix: [Network] The unique-id was not as unique as I though it was..
truelight
parents: 602
diff changeset
  1290
	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
  1291
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1292
	/* Generate the MD5 hash */
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1293
	md5_init(&state);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2373
diff changeset
  1294
	md5_append(&state, (const md5_byte_t*)coding_string, strlen(coding_string));
602
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1295
	md5_finish(&state, digest);
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1296
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1297
	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
  1298
		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
  1299
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1300
	/* _network_unique_id is our id */
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1301
	snprintf(_network_unique_id, sizeof(_network_unique_id), "%s", hex_output);
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1302
}
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1303
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1304
// This tries to launch the network for a given OS
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1305
void NetworkStartUp(void)
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1306
{
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1307
	DEBUG(net, 3) ("[NET][Core] Starting network...");
770
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1308
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1309
	#if defined(__MORPHOS__) || defined(__AMIGA__)
841
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
  1310
	/*
770
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1311
	 *  IMPORTANT NOTE: SocketBase needs to be initialized before we use _any_
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1312
	 *  network related function, else: crash.
841
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
  1313
	 */
770
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1314
	{
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1315
		DEBUG(misc,3) ("[NET][Core] Loading bsd socket library");
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1316
		if (!(SocketBase = OpenLibrary("bsdsocket.library", 4))) {
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1317
			DEBUG(net, 0) ("[NET][Core] Error: couldn't open bsdsocket.library version 4. Network not available.");
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1318
			_network_available = false;
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1319
			return;
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1320
		}
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1321
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1322
		#if defined(__AMIGA__)
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1323
		// for usleep() implementation (only required for legacy AmigaOS builds)
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1324
		if ( (TimerPort = CreateMsgPort()) ) {
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1325
			if ( (TimerRequest = (struct timerequest *) CreateIORequest(TimerPort, sizeof(struct timerequest))) ) {
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1326
				if ( OpenDevice("timer.device", UNIT_MICROHZ, (struct IORequest *) TimerRequest, 0) == 0 ) {
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1327
					if ( !(TimerBase = TimerRequest->tr_node.io_Device) ) {
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1328
						// free ressources...
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1329
						DEBUG(net, 0) ("[NET][Core] Error: couldn't initialize timer. Network not available.");
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1330
						_network_available = false;
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1331
						return;
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1332
					}
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1333
				}
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1334
			}
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1335
		}
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1336
		#endif // __AMIGA__
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1337
	}
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1338
	#endif // __MORPHOS__ / __AMIGA__
841
8f0a68c6f43b (svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents: 826
diff changeset
  1339
770
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1340
    // Network is available
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1341
	_network_available = true;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1342
	_network_dedicated = false;
2861
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2859
diff changeset
  1343
	_network_last_advertise_frame = 0;
c7e2c8217fed (svn r3409) - Change the server advertisement interval to use the frame counter instead
peter1138
parents: 2859
diff changeset
  1344
	_network_need_advertise = true;
764
68b398e2b780 (svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents: 746
diff changeset
  1345
	_network_advertise_retries = 0;
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
  1346
629
ce914eb4023d (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
  1347
	/* Load the ip from the openttd.cfg */
ce914eb4023d (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
  1348
	_network_server_bind_ip = inet_addr(_network_server_bind_ip_host);
ce914eb4023d (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
  1349
	/* And put the data back in it in case it was an invalid ip */
ce914eb4023d (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
  1350
	snprintf(_network_server_bind_ip_host, sizeof(_network_server_bind_ip_host), "%s", inet_ntoa(*(struct in_addr *)&_network_server_bind_ip));
ce914eb4023d (svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents: 619
diff changeset
  1351
602
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1352
	/* Generate an unique id when there is none yet */
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
  1353
	if (_network_unique_id[0] == '\0') NetworkGenerateUniqueId();
602
9bbe42a9e3ed (svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents: 598
diff changeset
  1354
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1355
	memset(&_network_game_info, 0, sizeof(_network_game_info));
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
  1356
	_network_game_info.clients_max = 10; // XXX - hardcoded, string limiation -- TrueLight
2881
1ffbbdbf1685 (svn r3429) - Feature (Followup): Change the gamelist window to accomodate for the new information.
Darkvater
parents: 2879
diff changeset
  1357
	_network_game_info.companies_max = MAX_PLAYERS; // 8
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
  1358
	_network_game_info.spectators_max = _network_game_info.clients_max;
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
  1359
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1360
	// Let's load the network in windows
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1361
	#if defined(WIN32)
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1362
	{
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1363
		WSADATA wsa;
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1364
		DEBUG(net, 3) ("[NET][Core] Loading windows socket library");
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1365
		if (WSAStartup(MAKEWORD(2,0), &wsa) != 0) {
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1366
			DEBUG(net, 0) ("[NET][Core] Error: WSAStartup failed. Network not available.");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1367
			_network_available = false;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1368
			return;
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1369
		}
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1370
	}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1371
	#endif // WIN32
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1372
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1373
	NetworkInitialize();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1374
	DEBUG(net, 3) ("[NET][Core] Network online. Multiplayer available.");
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1375
	NetworkFindIPs();
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1376
}
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1377
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1378
// This shuts the network down
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1379
void NetworkShutDown(void)
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1380
{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1381
	DEBUG(net, 3) ("[NET][Core] Shutting down the network.");
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1382
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1383
	_network_available = false;
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1384
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1385
	#if defined(__MORPHOS__) || defined(__AMIGA__)
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1386
	{
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1387
		// free allocated ressources
770
89dab23f04ca (svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents: 765
diff changeset
  1388
		#if defined(__AMIGA__)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1389
			if (TimerBase)    { CloseDevice((struct IORequest *) TimerRequest); }
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1390
			if (TimerRequest) { DeleteIORequest(TimerRequest); }
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1391
			if (TimerPort)    { DeleteMsgPort(TimerPort); }
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1392
		#endif
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1393
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1394
		if (SocketBase) {
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1395
			CloseLibrary(SocketBase);
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1396
		}
144
6b42494d405e (svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents: 110
diff changeset
  1397
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 188
diff changeset
  1398
	#endif
173
df7c566d219f (svn r174) -Network: [ 023268] small network.c changes/cleanups (MorphOS/AmigaOS) and one compiler warning fix (MorphOS) (tokai)
darkvater
parents: 172
diff changeset
  1399
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1400
	#if defined(WIN32)
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1401
	{
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1402
		WSACleanup();
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1403
	}
173
df7c566d219f (svn r174) -Network: [ 023268] small network.c changes/cleanups (MorphOS/AmigaOS) and one compiler warning fix (MorphOS) (tokai)
darkvater
parents: 172
diff changeset
  1404
	#endif
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
  1405
}
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1406
#else
211
2954c4b30525 (svn r212) -Fix: Network-gui fixes (sign_de)
darkvater
parents: 208
diff changeset
  1407
1622
2cde8f050c51 (svn r2126) - Fix: #2 Fix compilation with network disabled, and comment out some of the warnings (unused function) (I missed this out the last commit, sorry)
Darkvater
parents: 1604
diff changeset
  1408
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
  1409
void NetworkUpdateClientInfo(uint16 client_index) {}
105
44e894da0fef (svn r106) New network core (by sign_de)
dominik
parents: 81
diff changeset
  1410
543
e3b43338096b (svn r942) -Merged branch/network back into the trunk
truelight
parents: 534
diff changeset
  1411
#endif /* ENABLE_NETWORK */