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