author | truelight |
Thu, 24 Mar 2005 14:43:40 +0000 | |
changeset 1540 | 6eef12dab5bf |
parent 1466 | 93e67e9654c5 |
child 1602 | 79f98b4b83fc |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
1299
39c06aba09aa
(svn r1803) Move debugging stuff into files of it's own
tron
parents:
1095
diff
changeset
|
2 |
#include "debug.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
|
3 |
#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
|
4 |
#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
|
5 |
#include "map.h" |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
6 |
#include "network_data.h" |
0 | 7 |
|
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
|
8 |
#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
|
9 |
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
|
10 |
#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
|
11 |
#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
|
12 |
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
|
13 |
#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
|
14 |
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
|
15 |
#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
|
16 |
|
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 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
18 |
#ifdef ENABLE_NETWORK |
0 | 19 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
20 |
#include "table/strings.h" |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
21 |
#include "network_client.h" |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
22 |
#include "network_server.h" |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
23 |
#include "network_udp.h" |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
24 |
#include "network_gamelist.h" |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
25 |
#include "console.h" /* IConsoleCmdExec */ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
26 |
#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
|
27 |
#include "md5.h" |
0 | 28 |
|
770
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
29 |
#ifdef __MORPHOS__ |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
30 |
// the library base is required here |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
31 |
struct Library *SocketBase = NULL; |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
32 |
#endif |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
33 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
34 |
// The listen socket for the server |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
35 |
static SOCKET _listensocket; |
0 | 36 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
37 |
// Network copy of patches, so the patches of a client are not fucked up |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
38 |
// after he joined a server |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
39 |
static Patches network_tmp_patches; |
0 | 40 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
41 |
// The amount of clients connected |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
42 |
static byte _network_clients_connected = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
43 |
// The index counter for new clients (is never decreased) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
44 |
static uint16 _network_client_index = NETWORK_SERVER_INDEX + 1; |
0 | 45 |
|
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
46 |
/* 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
|
47 |
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
|
48 |
extern void StateGameLoop(void); |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
49 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
50 |
// 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
|
51 |
NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
52 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
53 |
NetworkClientInfo *ci; |
0 | 54 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
55 |
for (ci = _network_client_info; ci != &_network_client_info[MAX_CLIENT_INFO]; ci++) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
56 |
if (ci->client_index == client_index) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
57 |
return ci; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
58 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
59 |
return NULL; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
60 |
} |
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 |
// 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
|
63 |
NetworkClientState *NetworkFindClientStateFromIndex(uint16 client_index) |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
64 |
{ |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
65 |
NetworkClientState *cs; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
66 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
67 |
for (cs = _clients; cs != &_clients[MAX_CLIENT_INFO]; cs++) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
68 |
if (cs->index == client_index) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
69 |
return cs; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
70 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
71 |
return NULL; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
72 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
73 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
74 |
// 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
|
75 |
// 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
|
76 |
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
|
77 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
78 |
NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
79 |
if (ci->client_name[0] == '\0') |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
80 |
snprintf(client_name, size, "Client #%d", cs->index); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
81 |
else |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
82 |
snprintf(client_name, size, "%s", ci->client_name); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
83 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
84 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
85 |
// 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
|
86 |
// (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
|
87 |
// 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
|
88 |
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
|
89 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
90 |
char buf[1024]; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
91 |
va_list va; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
92 |
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
|
93 |
char message[1024]; |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
94 |
char temp[1024]; |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
95 |
StringID TempStr = STR_NULL; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
96 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
97 |
va_start(va, str); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
98 |
vsprintf(buf, str, va); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
99 |
va_end(va); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
100 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
101 |
switch (action) { |
722
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
102 |
case NETWORK_ACTION_JOIN: |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
103 |
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
|
104 |
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
|
105 |
break; |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
106 |
case NETWORK_ACTION_LEAVE: |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
107 |
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
|
108 |
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
|
109 |
break; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
110 |
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
|
111 |
if (self_send) { |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
112 |
TempStr = AllocateName(name, 0); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
113 |
SetDParam(0, TempStr); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
114 |
SetDParam(1, atoi(buf)); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
115 |
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
|
116 |
DeleteName(TempStr); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
117 |
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
|
118 |
} else { |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
119 |
SetDParam(0, atoi(buf)); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
120 |
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
|
121 |
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
|
122 |
} |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
123 |
break; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
124 |
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
|
125 |
if (self_send) { |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
126 |
TempStr = AllocateName(name, 0); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
127 |
SetDParam(0, TempStr); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
128 |
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
|
129 |
DeleteName(TempStr); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
130 |
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
|
131 |
} else { |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
132 |
TempStr = AllocateName(name, 0); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
133 |
SetDParam(0, TempStr); |
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_CHAT_COMPANY); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
135 |
DeleteName(TempStr); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
136 |
snprintf(message, sizeof(message), "%s %s", temp, buf); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
137 |
} |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
138 |
break; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
139 |
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
|
140 |
if (self_send) { |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
141 |
TempStr = AllocateName(name, 0); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
142 |
SetDParam(0, TempStr); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
143 |
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
|
144 |
DeleteName(TempStr); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
145 |
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
|
146 |
} else { |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
147 |
TempStr = AllocateName(name, 0); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
148 |
SetDParam(0, TempStr); |
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_CHAT_CLIENT); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
150 |
DeleteName(TempStr); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
151 |
snprintf(message, sizeof(message), "%s %s", temp, buf); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
152 |
} |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
153 |
break; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
154 |
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
|
155 |
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
|
156 |
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
|
157 |
break; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
158 |
default: |
722
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
159 |
TempStr = AllocateName(name, 0); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
160 |
SetDParam(0, TempStr); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
161 |
GetString(temp, STR_NETWORK_CHAT_ALL); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
162 |
DeleteName(TempStr); |
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
163 |
snprintf(message, sizeof(message), "%s %s", temp, buf); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
164 |
break; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
165 |
} |
722
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
166 |
|
732
0b3002da2589
(svn r1184) -Fix: [Network] %p as chat message no longer gives funny result (tnx to
truelight
parents:
722
diff
changeset
|
167 |
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
|
168 |
AddTextMessage(color, duration, "%s", message); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
169 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
170 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
171 |
// 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
|
172 |
uint NetworkCalculateLag(const NetworkClientState *cs) |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
173 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
174 |
int lag = cs->last_frame_server - cs->last_frame; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
175 |
// 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
|
176 |
// 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
|
177 |
// 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
|
178 |
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
|
179 |
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
|
180 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
181 |
return lag; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
182 |
} |
0 | 183 |
|
184 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
185 |
// 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
|
186 |
// error |
1095 | 187 |
static void NetworkError(StringID error_string) |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
188 |
{ |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
189 |
_switch_mode = SM_MENU; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
190 |
_switch_mode_errorstr = error_string; |
105 | 191 |
} |
192 |
||
1010
977aafc6b8e3
(svn r1509) Const correctness and add static where appropriate while touching the lines anyway
tron
parents:
959
diff
changeset
|
193 |
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
|
194 |
{ |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
195 |
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
|
196 |
NetworkError(STR_NETWORK_ERR_CLIENT_START); |
0 | 197 |
} |
198 |
||
1010
977aafc6b8e3
(svn r1509) Const correctness and add static where appropriate while touching the lines anyway
tron
parents:
959
diff
changeset
|
199 |
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
|
200 |
{ |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
201 |
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
|
202 |
NetworkError(STR_NETWORK_ERR_SERVER_START); |
208 | 203 |
} |
204 |
||
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
205 |
static void NetworkClientError(byte res, NetworkClientState *cs) { |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
206 |
// 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
|
207 |
// disconnection (and why!) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
208 |
NetworkErrorCode errorno; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
209 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
210 |
// We just want to close the connection.. |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
211 |
if (res == NETWORK_RECV_STATUS_CLOSE_QUERY) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
212 |
cs->quited = true; |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
213 |
NetworkCloseClient(cs); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
214 |
_networking = false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
215 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
216 |
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
217 |
return; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
218 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
219 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
220 |
switch(res) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
221 |
case NETWORK_RECV_STATUS_DESYNC: errorno = NETWORK_ERROR_DESYNC; break; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
222 |
case NETWORK_RECV_STATUS_SAVEGAME: errorno = NETWORK_ERROR_SAVEGAME_FAILED; break; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
223 |
default: errorno = NETWORK_ERROR_GENERAL; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
224 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
225 |
// 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
|
226 |
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
|
227 |
res != NETWORK_RECV_STATUS_SERVER_BANNED) { |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
228 |
SEND_COMMAND(PACKET_CLIENT_ERROR)(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 |
// Dequeue all commands before closing the socket |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
231 |
NetworkSend_Packets(DEREF_CLIENT(0)); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
232 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
233 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
234 |
_switch_mode = SM_MENU; |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
235 |
NetworkCloseClient(cs); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
236 |
_networking = false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
237 |
} |
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 |
// 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
|
240 |
static void NetworkFindIPs(void) |
0 | 241 |
{ |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
242 |
int i, last; |
0 | 243 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
244 |
#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
|
245 |
/* 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
|
246 |
int _netstat(int fd, char **output, int verbose); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
247 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
248 |
int seek_past_header(char **pos, const char *header) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
249 |
char *new_pos = strstr(*pos, header); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
250 |
if (new_pos == 0) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
251 |
return B_ERROR; |
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 |
*pos += strlen(header) + new_pos - *pos + 1; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
254 |
return B_OK; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
255 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
256 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
257 |
int output_length; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
258 |
char *output_pointer = NULL; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
259 |
char **output; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
260 |
int sock = socket(AF_INET, SOCK_DGRAM, 0); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
261 |
i = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
262 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
263 |
// If something fails, make sure the list is empty |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
264 |
_network_ip_list[0] = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
265 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
266 |
if (sock < 0) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
267 |
DEBUG(net, 0)("Error creating socket!"); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
268 |
return; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
269 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
270 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
271 |
output_length = _netstat(sock, &output_pointer, 1); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
272 |
if (output_length < 0) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
273 |
DEBUG(net, 0)("Error running _netstat!"); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
274 |
return; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
275 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
276 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
277 |
output = &output_pointer; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
278 |
if (seek_past_header(output, "IP Interfaces:") == B_OK) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
279 |
for (;;) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
280 |
uint32 n, fields, read; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
281 |
uint8 i1, i2, i3, i4, j1, j2, j3, j4; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
282 |
struct in_addr inaddr; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
283 |
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
|
284 |
&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
|
285 |
read += 1; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
286 |
if (fields != 9) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
287 |
break; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
288 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
289 |
inaddr.s_addr = htonl((uint32)i1 << 24 | (uint32)i2 << 16 | (uint32)i3 << 8 | (uint32)i4); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
290 |
if (inaddr.s_addr != 0) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
291 |
_network_ip_list[i] = inaddr.s_addr; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
292 |
i++; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
293 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
294 |
if (read < 0) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
295 |
break; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
296 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
297 |
*output += read; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
298 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
299 |
/* 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
|
300 |
/*free(output_pointer);*/ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
301 |
/*free(output);*/ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
302 |
closesocket(sock); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
303 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
304 |
#elif defined(HAVE_GETIFADDRS) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
305 |
struct ifaddrs *ifap, *ifa; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
306 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
307 |
// If something fails, make sure the list is empty |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
308 |
_network_ip_list[0] = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
309 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
310 |
if (getifaddrs(&ifap) != 0) |
0 | 311 |
return; |
312 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
313 |
i = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
314 |
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
315 |
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
316 |
continue; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
317 |
_network_ip_list[i] = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
318 |
i++; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
319 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
320 |
freeifaddrs(ifap); |
0 | 321 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
322 |
#else /* not HAVE_GETIFADDRS */ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
323 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
324 |
unsigned long len = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
325 |
SOCKET sock; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
326 |
IFREQ ifo[MAX_INTERFACES]; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
327 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
328 |
#ifndef WIN32 |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
329 |
struct ifconf if_conf; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
330 |
#endif |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
331 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
332 |
// If something fails, make sure the list is empty |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
333 |
_network_ip_list[0] = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
334 |
|
1466 | 335 |
sock = socket(AF_INET, SOCK_DGRAM, 0); |
336 |
if (sock == INVALID_SOCKET) return; |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
337 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
338 |
#ifdef WIN32 |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
339 |
// On windows it is easy |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
340 |
memset(&ifo[0], 0, sizeof(ifo)); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
341 |
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
|
342 |
closesocket(sock); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
343 |
return; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
344 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
345 |
#else |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
346 |
// On linux a bit harder |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
347 |
if_conf.ifc_len = (sizeof (struct ifreq)) * MAX_INTERFACES; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
348 |
if_conf.ifc_buf = (char *)&ifo[0]; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
349 |
if ((ioctl(sock, SIOCGIFCONF, &if_conf)) == -1) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
350 |
closesocket(sock); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
351 |
return; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
352 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
353 |
len = if_conf.ifc_len; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
354 |
#endif /* WIN32 */ |
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 |
// Now walk through all IPs and list them |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
357 |
for (i = 0; i < (int)(len / sizeof(IFREQ)); i++) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
358 |
// Request IP for this interface |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
359 |
#ifdef WIN32 |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
360 |
_network_ip_list[i] = *(&ifo[i].iiAddress.AddressIn.sin_addr.s_addr); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
361 |
#else |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
362 |
if ((ioctl(sock, SIOCGIFADDR, &ifo[i])) != 0) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
363 |
closesocket(sock); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
364 |
return; |
0 | 365 |
} |
366 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
367 |
_network_ip_list[i] = ((struct sockaddr_in *)&ifo[i].ifr_addr)->sin_addr.s_addr; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
368 |
#endif |
0 | 369 |
} |
370 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
371 |
closesocket(sock); |
0 | 372 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
373 |
#endif /* not HAVE_GETIFADDRS */ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
374 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
375 |
_network_ip_list[i] = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
376 |
last = i - 1; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
377 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
378 |
DEBUG(net, 3)("Detected IPs:"); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
379 |
// Now display to the debug all the detected ips |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
380 |
i = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
381 |
while (_network_ip_list[i] != 0) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
382 |
// Also check for non-used ips (127.0.0.1) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
383 |
if (_network_ip_list[i] == inet_addr("127.0.0.1")) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
384 |
// If there is an ip after thisone, put him in here |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
385 |
if (last > i) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
386 |
_network_ip_list[i] = _network_ip_list[last]; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
387 |
// Clear the last ip |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
388 |
_network_ip_list[last] = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
389 |
// And we have 1 ip less |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
390 |
last--; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
391 |
continue; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
392 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
393 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
394 |
DEBUG(net, 3)(" %d) %s", i, inet_ntoa(*(struct in_addr *)&_network_ip_list[i]));//inet_ntoa(inaddr)); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
395 |
i++; |
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 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
398 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
399 |
// Resolve a hostname to a inet_addr |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
400 |
unsigned long NetworkResolveHost(const char *hostname) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
401 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
402 |
in_addr_t ip; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
403 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
404 |
// First try: is it an ip address? |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
405 |
ip = inet_addr(hostname); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
406 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
407 |
// If not try to resolve the name |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
408 |
if (ip == INADDR_NONE) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
409 |
struct hostent *he = gethostbyname(hostname); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
410 |
if (he == NULL) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
411 |
DEBUG(net, 0) ("[NET] Cannot resolve %s", hostname); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
412 |
} else { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
413 |
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
|
414 |
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
|
415 |
ip = addr.s_addr; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
416 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
417 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
418 |
return ip; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
419 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
420 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
421 |
// Converts a string to ip/port/player |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
422 |
// Format: IP#player:port |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
423 |
// |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
424 |
// 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
|
425 |
// 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
|
426 |
// occupied by connection_string. |
1329 | 427 |
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
|
428 |
{ |
1329 | 429 |
char *p; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
430 |
for (p = connection_string; *p != '\0'; p++) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
431 |
if (*p == '#') { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
432 |
*player = p + 1; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
433 |
*p = '\0'; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
434 |
} else if (*p == ':') { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
435 |
*port = p + 1; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
436 |
*p = '\0'; |
0 | 437 |
} |
438 |
} |
|
439 |
} |
|
440 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
441 |
// Creates a new client from a socket |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
442 |
// 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
|
443 |
static NetworkClientState *NetworkAllocClient(SOCKET s) |
0 | 444 |
{ |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
445 |
NetworkClientState *cs; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
446 |
NetworkClientInfo *ci; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
447 |
byte client_no; |
0 | 448 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
449 |
client_no = 0; |
0 | 450 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
451 |
if (_network_server) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
452 |
// Can we handle a new client? |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
453 |
if (_network_clients_connected >= MAX_CLIENTS) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
454 |
return NULL; |
0 | 455 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
456 |
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
|
457 |
return NULL; |
206 | 458 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
459 |
// Register the login |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
460 |
client_no = _network_clients_connected++; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
461 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
462 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
463 |
cs = &_clients[client_no]; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
464 |
memset(cs, 0, sizeof(*cs)); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
465 |
cs->socket = s; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
466 |
cs->last_frame = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
467 |
cs->quited = false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
468 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
469 |
if (_network_server) { |
662
22708a1cbba9
(svn r1096) -Fix: [Network] PlayAs is now registered correctly
truelight
parents:
659
diff
changeset
|
470 |
ci = DEREF_CLIENT_INFO(cs); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
471 |
memset(ci, 0, sizeof(*ci)); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
472 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
473 |
cs->index = _network_client_index++; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
474 |
ci->client_index = cs->index; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
475 |
ci->join_date = _date; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
476 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
477 |
InvalidateWindow(WC_CLIENT_LIST, 0); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
478 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
479 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
480 |
return cs; |
206 | 481 |
} |
482 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
483 |
// Close a connection |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
484 |
void NetworkCloseClient(NetworkClientState *cs) |
0 | 485 |
{ |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
486 |
NetworkClientInfo *ci; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
487 |
// Socket is already dead |
903
a301a264e0fc
(svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents:
895
diff
changeset
|
488 |
if (cs->socket == INVALID_SOCKET) { |
a301a264e0fc
(svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents:
895
diff
changeset
|
489 |
cs->quited = true; |
a301a264e0fc
(svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents:
895
diff
changeset
|
490 |
return; |
a301a264e0fc
(svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents:
895
diff
changeset
|
491 |
} |
260
4819bcce8389
(svn r266) -Fix: hopefully fixed the desync problem nicely (and reverted the
truelight
parents:
256
diff
changeset
|
492 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
493 |
DEBUG(net, 1) ("[NET] Closed client connection"); |
0 | 494 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
495 |
if (!cs->quited && _network_server && cs->status > STATUS_INACTIVE) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
496 |
// 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
|
497 |
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
|
498 |
char str[100]; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
499 |
char client_name[NETWORK_NAME_LENGTH]; |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
500 |
NetworkClientState *new_cs; |
0 | 501 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
502 |
NetworkGetClientName(client_name, sizeof(client_name), cs); |
0 | 503 |
|
722
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
504 |
GetString(str, STR_NETWORK_ERR_CLIENT_GENERAL + errorno); |
0 | 505 |
|
722
f420fa9bd521
(svn r1174) -Fix: [Network] All strings can now be translated (no more hardcoded strings)
truelight
parents:
716
diff
changeset
|
506 |
NetworkTextMessage(NETWORK_ACTION_LEAVE, 1, false, client_name, str); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
507 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
508 |
// Inform other clients of this... strange leaving ;) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
509 |
FOR_ALL_CLIENTS(new_cs) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
510 |
if (new_cs->status > STATUS_AUTH && cs != new_cs) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
511 |
SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, errorno); |
0 | 512 |
} |
513 |
} |
|
514 |
} |
|
515 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
516 |
closesocket(cs->socket); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
517 |
cs->writable = false; |
903
a301a264e0fc
(svn r1389) -Add: [Network] Added packet protection. No longer a client or server
truelight
parents:
895
diff
changeset
|
518 |
cs->quited = true; |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
519 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
520 |
// Free all pending and partially received packets |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
521 |
while (cs->packet_queue != NULL) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
522 |
Packet *p = cs->packet_queue->next; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
523 |
free(cs->packet_queue); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
524 |
cs->packet_queue = p; |
0 | 525 |
} |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
526 |
free(cs->packet_recv); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
527 |
cs->packet_recv = NULL; |
0 | 528 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
529 |
while (cs->command_queue != NULL) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
530 |
CommandPacket *p = cs->command_queue->next; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
531 |
free(cs->command_queue); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
532 |
cs->command_queue = p; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
533 |
} |
0 | 534 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
535 |
// Close the gap in the client-list |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
536 |
ci = DEREF_CLIENT_INFO(cs); |
0 | 537 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
538 |
if (_network_server) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
539 |
// We just lost one client :( |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
540 |
if (cs->status > STATUS_INACTIVE) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
541 |
_network_game_info.clients_on--; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
542 |
_network_clients_connected--; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
543 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
544 |
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
|
545 |
*cs = *(cs + 1); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
546 |
*ci = *(ci + 1); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
547 |
cs++; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
548 |
ci++; |
0 | 549 |
} |
550 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
551 |
InvalidateWindow(WC_CLIENT_LIST, 0); |
0 | 552 |
} |
553 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
554 |
// Reset the status of the last socket |
0 | 555 |
cs->socket = INVALID_SOCKET; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
556 |
cs->status = STATUS_INACTIVE; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
557 |
cs->index = NETWORK_EMPTY_INDEX; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
558 |
ci->client_index = NETWORK_EMPTY_INDEX; |
0 | 559 |
} |
560 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
561 |
// 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
|
562 |
static bool NetworkConnect(const char *hostname, int port) |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
563 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
564 |
SOCKET s; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
565 |
struct sockaddr_in sin; |
0 | 566 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
567 |
DEBUG(net, 1) ("[NET] Connecting to %s %d", hostname, port); |
0 | 568 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
569 |
s = socket(AF_INET, SOCK_STREAM, 0); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
570 |
if (s == INVALID_SOCKET) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
571 |
ClientStartError("socket() failed"); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
572 |
return false; |
0 | 573 |
} |
574 |
||
1332
0cae74c93b5d
(svn r1836) Clean up some strange constructs concerning socket options
tron
parents:
1329
diff
changeset
|
575 |
if (!SetNoDelay(s)) |
0cae74c93b5d
(svn r1836) Clean up some strange constructs concerning socket options
tron
parents:
1329
diff
changeset
|
576 |
DEBUG(net, 1)("[NET] Setting TCP_NODELAY failed"); |
543
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 |
sin.sin_family = AF_INET; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
579 |
sin.sin_addr.s_addr = NetworkResolveHost(hostname); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
580 |
sin.sin_port = htons(port); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
581 |
_network_last_host_ip = sin.sin_addr.s_addr; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
582 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
583 |
if (connect(s, (struct sockaddr*) &sin, sizeof(sin)) != 0) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
584 |
// 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
|
585 |
return false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
586 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
587 |
|
1332
0cae74c93b5d
(svn r1836) Clean up some strange constructs concerning socket options
tron
parents:
1329
diff
changeset
|
588 |
if (!SetNonBlocking(s)) |
0cae74c93b5d
(svn r1836) Clean up some strange constructs concerning socket options
tron
parents:
1329
diff
changeset
|
589 |
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
|
590 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
591 |
// 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
|
592 |
NetworkAllocClient(s); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
593 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
594 |
ShowJoinStatusWindow(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
595 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
596 |
memcpy(&network_tmp_patches, &_patches, sizeof(_patches)); |
0 | 597 |
|
598 |
return true; |
|
599 |
} |
|
600 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
601 |
// For the server, to accept new clients |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
602 |
static void NetworkAcceptClients(void) |
0 | 603 |
{ |
604 |
struct sockaddr_in sin; |
|
605 |
SOCKET s; |
|
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
606 |
NetworkClientState *cs; |
959
e6a3bbda610f
(svn r1451) Fix some of the signed/unsigned comparison warnings
tron
parents:
903
diff
changeset
|
607 |
uint i; |
841
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
608 |
bool banned; |
0 | 609 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
610 |
// Should never ever happen.. is it possible?? |
0 | 611 |
assert(_listensocket != INVALID_SOCKET); |
612 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
613 |
for (;;) { |
1332
0cae74c93b5d
(svn r1836) Clean up some strange constructs concerning socket options
tron
parents:
1329
diff
changeset
|
614 |
socklen_t sin_len; |
0cae74c93b5d
(svn r1836) Clean up some strange constructs concerning socket options
tron
parents:
1329
diff
changeset
|
615 |
|
0 | 616 |
sin_len = sizeof(sin); |
617 |
s = accept(_listensocket, (struct sockaddr*)&sin, &sin_len); |
|
618 |
if (s == INVALID_SOCKET) return; |
|
619 |
||
1332
0cae74c93b5d
(svn r1836) Clean up some strange constructs concerning socket options
tron
parents:
1329
diff
changeset
|
620 |
SetNonBlocking(s); // XXX error handling? |
0 | 621 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
622 |
DEBUG(net, 1) ("[NET] Client connected from %s on frame %d", inet_ntoa(sin.sin_addr), _frame_counter); |
0 | 623 |
|
1332
0cae74c93b5d
(svn r1836) Clean up some strange constructs concerning socket options
tron
parents:
1329
diff
changeset
|
624 |
SetNoDelay(s); // XXX error handling? |
0 | 625 |
|
841
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
626 |
/* Check if the client is banned */ |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
627 |
banned = false; |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
628 |
for (i = 0; i < lengthof(_network_ban_list); i++) { |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
629 |
if (_network_ban_list[i] == NULL) |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
630 |
continue; |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
631 |
|
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
632 |
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
|
633 |
Packet *p = NetworkSend_Init(PACKET_SERVER_BANNED); |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
634 |
|
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
635 |
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
|
636 |
|
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
637 |
p->buffer[0] = p->size & 0xFF; |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
638 |
p->buffer[1] = p->size >> 8; |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
639 |
|
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
640 |
send(s, p->buffer, p->size, 0); |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
641 |
closesocket(s); |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
642 |
|
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
643 |
free(p); |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
644 |
|
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
645 |
banned = true; |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
646 |
break; |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
647 |
} |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
648 |
} |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
649 |
/* If this client is banned, continue with next client */ |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
650 |
if (banned) |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
651 |
continue; |
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
652 |
|
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
653 |
cs = NetworkAllocClient(s); |
0 | 654 |
if (cs == NULL) { |
655 |
// no more clients allowed? |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
656 |
// Send to the client that we are full! |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
657 |
Packet *p = NetworkSend_Init(PACKET_SERVER_FULL); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
658 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
659 |
p->buffer[0] = p->size & 0xFF; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
660 |
p->buffer[1] = p->size >> 8; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
661 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
662 |
send(s, p->buffer, p->size, 0); |
0 | 663 |
closesocket(s); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
664 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
665 |
free(p); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
666 |
|
0 | 667 |
continue; |
668 |
} |
|
669 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
670 |
// 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
|
671 |
// 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
|
672 |
// the client stays inactive |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
673 |
cs->status = STATUS_INACTIVE; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
674 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
675 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
676 |
// Save the IP of the client |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
677 |
NetworkClientInfo *ci; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
678 |
ci = DEREF_CLIENT_INFO(cs); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
679 |
ci->client_ip = sin.sin_addr.s_addr; |
0 | 680 |
} |
681 |
} |
|
682 |
} |
|
683 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
684 |
// 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
|
685 |
static bool NetworkListen(void) |
211 | 686 |
{ |
105 | 687 |
SOCKET ls; |
688 |
struct sockaddr_in sin; |
|
689 |
int port; |
|
690 |
||
691 |
port = _network_server_port; |
|
692 |
||
629
4bb1f0fb0109
(svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents:
619
diff
changeset
|
693 |
DEBUG(net, 1) ("[NET] Listening on %s:%d", _network_server_bind_ip_host, port); |
105 | 694 |
|
695 |
ls = socket(AF_INET, SOCK_STREAM, 0); |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
696 |
if (ls == INVALID_SOCKET) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
697 |
ServerStartError("socket() on listen socket failed"); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
698 |
return false; |
105 | 699 |
} |
700 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
701 |
{ // reuse the socket |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
702 |
int reuse = 1; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
703 |
// The (const char*) cast is needed for windows!! |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
704 |
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
|
705 |
ServerStartError("setsockopt() on listen socket failed"); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
706 |
return false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
707 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
708 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
709 |
|
1332
0cae74c93b5d
(svn r1836) Clean up some strange constructs concerning socket options
tron
parents:
1329
diff
changeset
|
710 |
if (!SetNonBlocking(ls)) |
0cae74c93b5d
(svn r1836) Clean up some strange constructs concerning socket options
tron
parents:
1329
diff
changeset
|
711 |
DEBUG(net, 0)("[NET] Setting non-blocking failed"); // XXX should this be an error? |
105 | 712 |
|
713 |
sin.sin_family = AF_INET; |
|
629
4bb1f0fb0109
(svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents:
619
diff
changeset
|
714 |
sin.sin_addr.s_addr = _network_server_bind_ip; |
105 | 715 |
sin.sin_port = htons(port); |
716 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
717 |
if (bind(ls, (struct sockaddr*)&sin, sizeof(sin)) != 0) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
718 |
ServerStartError("bind() failed"); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
719 |
return false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
720 |
} |
105 | 721 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
722 |
if (listen(ls, 1) != 0) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
723 |
ServerStartError("listen() failed"); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
724 |
return false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
725 |
} |
105 | 726 |
|
727 |
_listensocket = ls; |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
728 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
729 |
return true; |
105 | 730 |
} |
731 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
732 |
// Close all current connections |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
733 |
static void NetworkClose(void) |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
734 |
{ |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
735 |
NetworkClientState *cs; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
736 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
737 |
FOR_ALL_CLIENTS(cs) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
738 |
if (!_network_server) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
739 |
SEND_COMMAND(PACKET_CLIENT_QUIT)("leaving"); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
740 |
NetworkSend_Packets(cs); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
741 |
} |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
742 |
NetworkCloseClient(cs); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
743 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
744 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
745 |
if (_network_server) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
746 |
// We are a server, also close the listensocket |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
747 |
closesocket(_listensocket); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
748 |
_listensocket = INVALID_SOCKET; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
749 |
DEBUG(net, 1) ("[NET] Closed listener"); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
750 |
NetworkUDPClose(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
751 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
752 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
753 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
754 |
// 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
|
755 |
static void NetworkInitialize(void) |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
756 |
{ |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
757 |
NetworkClientState *cs; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
758 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
759 |
_local_command_queue = NULL; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
760 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
761 |
// Clean all client-sockets |
662
22708a1cbba9
(svn r1096) -Fix: [Network] PlayAs is now registered correctly
truelight
parents:
659
diff
changeset
|
762 |
memset(_clients, 0, sizeof(_clients)); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
763 |
for (cs = _clients; cs != &_clients[MAX_CLIENTS]; cs++) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
764 |
cs->socket = INVALID_SOCKET; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
765 |
cs->status = STATUS_INACTIVE; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
766 |
cs->command_queue = NULL; |
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 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
769 |
// Clean the client_info memory |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
770 |
memset(_network_client_info, 0, sizeof(_network_client_info)); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
771 |
memset(_network_player_info, 0, sizeof(_network_player_info)); |
734
f4ad2f5805fd
(svn r1186) -Fix: [Network] You can now join a company on a server where a company
truelight
parents:
732
diff
changeset
|
772 |
_network_lobby_company_count = 0; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
773 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
774 |
_sync_frame = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
775 |
_network_first_time = true; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
776 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
777 |
_network_reconnect = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
778 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
779 |
InitPlayerRandoms(); |
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 |
NetworkUDPInitialize(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
782 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
783 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
784 |
// Query a server to fetch his game-info |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
785 |
// 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
|
786 |
// else only the client_info is fetched |
1329 | 787 |
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
|
788 |
{ |
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
|
789 |
if (!_network_available) return NULL; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
790 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
791 |
NetworkDisconnect(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
792 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
793 |
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
|
794 |
return NetworkUDPQueryServer(host, port); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
795 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
796 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
797 |
NetworkInitialize(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
798 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
799 |
_network_server = false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
800 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
801 |
// Try to connect |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
802 |
_networking = NetworkConnect(host, port); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
803 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
804 |
// 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
|
805 |
// _network_last_port = port; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
806 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
807 |
// We are connected |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
808 |
if (_networking) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
809 |
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
|
810 |
return NULL; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
811 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
812 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
813 |
// No networking, close everything down again |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
814 |
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
|
815 |
return NULL; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
816 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
817 |
|
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
|
818 |
/* Validates an address entered as a string and adds the server to |
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
|
819 |
* the list. If you use this functions, the games will be marked |
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
|
820 |
* as manually added. */ |
1329 | 821 |
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
|
822 |
{ |
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
|
823 |
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
|
824 |
NetworkGameList *item; |
1329 | 825 |
const char *port = NULL; |
826 |
const char *player = NULL; |
|
827 |
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
|
828 |
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
|
829 |
|
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
830 |
ttd_strlcpy(host, b, lengthof(host)); |
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
831 |
|
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
|
832 |
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
|
833 |
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
|
834 |
|
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
835 |
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
|
836 |
|
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
|
837 |
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
|
838 |
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
|
839 |
|
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
|
840 |
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
|
841 |
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
|
842 |
} |
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
|
843 |
} |
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
|
844 |
|
746 | 845 |
/* 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
|
846 |
* 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
|
847 |
* 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
|
848 |
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
|
849 |
{ |
959
e6a3bbda610f
(svn r1451) Fix some of the signed/unsigned comparison warnings
tron
parents:
903
diff
changeset
|
850 |
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
|
851 |
NetworkGameList *item = _network_game_list; |
841
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
852 |
while (item != NULL && i != lengthof(_network_host_list)) { |
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
|
853 |
if (item->manually) |
895
4d8e39aba340
(svn r1381) Fix: [ 1095143 ] Servers list now also saves the port
dominik
parents:
863
diff
changeset
|
854 |
_network_host_list[i++] = str_fmt("%s:%i", item->info.hostname, item->port); |
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
|
855 |
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
|
856 |
} |
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
|
857 |
|
841
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
858 |
for (; i < lengthof(_network_host_list); i++) { |
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
|
859 |
_network_host_list[i] = strdup(""); |
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
|
860 |
} |
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
|
861 |
} |
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
|
862 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
863 |
// Used by clients, to connect to a server |
1329 | 864 |
bool NetworkClientConnectGame(const char* host, unsigned short port) |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
865 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
866 |
if (!_network_available) return false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
867 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
868 |
if (port == 0) return false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
869 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
870 |
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
|
871 |
_network_last_port = port; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
872 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
873 |
NetworkDisconnect(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
874 |
NetworkUDPClose(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
875 |
NetworkInitialize(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
876 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
877 |
// Try to connect |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
878 |
_networking = NetworkConnect(host, port); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
879 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
880 |
// We are connected |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
881 |
if (_networking) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
882 |
IConsoleCmdExec("exec scripts/on_client.scr 0"); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
883 |
NetworkClient_Connected(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
884 |
} else { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
885 |
// Connecting failed |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
886 |
NetworkError(STR_NETWORK_ERR_NOCONNECTION); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
887 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
888 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
889 |
return _networking; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
890 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
891 |
|
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
892 |
static void NetworkInitGameInfo(void) |
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 |
NetworkClientInfo *ci; |
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 |
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
|
897 |
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
|
898 |
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
|
899 |
if (_network_game_info.server_name[0] == '\0') |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
900 |
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
|
901 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
902 |
// The server is a client too ;) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
903 |
if (_network_dedicated) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
904 |
_network_game_info.clients_on = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
905 |
_network_game_info.dedicated = true; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
906 |
} else { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
907 |
_network_game_info.clients_on = 1; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
908 |
_network_game_info.dedicated = false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
909 |
} |
693
5b1c1929bb91
(svn r1137) -Fix: [Network] Replaces all strncpy with ttd_strlcpy.. windows really
truelight
parents:
683
diff
changeset
|
910 |
ttd_strlcpy(_network_game_info.server_revision, _openttd_revision, sizeof(_network_game_info.server_revision)); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
911 |
_network_game_info.spectators_on = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
912 |
_network_game_info.game_date = _date; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
913 |
_network_game_info.start_date = ConvertIntDate(_patches.starting_date); |
863
6a1444534f62
(svn r1344) Use MapSize[XY]() (or MapSize()/MapMax[XY]() where appropriate) instead of TILES_[XY]
tron
parents:
841
diff
changeset
|
914 |
_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
|
915 |
_network_game_info.map_height = MapSizeY(); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
916 |
_network_game_info.map_set = _opt.landscape; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
917 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
918 |
if (_network_game_info.server_password[0] == '\0') { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
919 |
_network_game_info.use_password = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
920 |
} else { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
921 |
_network_game_info.use_password = 1; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
922 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
923 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
924 |
// 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
|
925 |
// The index is NETWORK_SERVER_INDEX ( = 1) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
926 |
ci = &_network_client_info[MAX_CLIENT_INFO - 1]; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
927 |
memset(ci, 0, sizeof(*ci)); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
928 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
929 |
ci->client_index = NETWORK_SERVER_INDEX; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
930 |
if (_network_dedicated) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
931 |
ci->client_playas = OWNER_SPECTATOR; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
932 |
else |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
933 |
ci->client_playas = _local_player + 1; |
693
5b1c1929bb91
(svn r1137) -Fix: [Network] Replaces all strncpy with ttd_strlcpy.. windows really
truelight
parents:
683
diff
changeset
|
934 |
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
|
935 |
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
|
936 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
937 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
938 |
bool NetworkServerStart(void) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
939 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
940 |
if (!_network_available) return false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
941 |
|
629
4bb1f0fb0109
(svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents:
619
diff
changeset
|
942 |
/* Call the pre-scripts */ |
4bb1f0fb0109
(svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents:
619
diff
changeset
|
943 |
IConsoleCmdExec("exec scripts/pre_server.scr 0"); |
4bb1f0fb0109
(svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents:
619
diff
changeset
|
944 |
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
|
945 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
946 |
NetworkInitialize(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
947 |
if (!NetworkListen()) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
948 |
return false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
949 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
950 |
// Try to start UDP-server |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
951 |
_network_udp_server = true; |
764
7e1e17b7c7d4
(svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents:
746
diff
changeset
|
952 |
_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
|
953 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
954 |
_network_server = true; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
955 |
_networking = true; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
956 |
_frame_counter = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
957 |
_frame_counter_server = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
958 |
_frame_counter_max = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
959 |
_network_own_client_index = NETWORK_SERVER_INDEX; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
960 |
|
683
3bd410628868
(svn r1124) -Fix: [Network] Also the server now has the option Give Money in
truelight
parents:
679
diff
changeset
|
961 |
if (!_network_dedicated) |
3bd410628868
(svn r1124) -Fix: [Network] Also the server now has the option Give Money in
truelight
parents:
679
diff
changeset
|
962 |
_network_playas = 1; |
3bd410628868
(svn r1124) -Fix: [Network] Also the server now has the option Give Money in
truelight
parents:
679
diff
changeset
|
963 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
964 |
_network_clients_connected = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
965 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
966 |
NetworkInitGameInfo(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
967 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
968 |
// execute server initialization script |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
969 |
IConsoleCmdExec("exec scripts/on_server.scr 0"); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
970 |
// if the server is dedicated ... add some other script |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
971 |
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
|
972 |
|
1fe298df8526
(svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents:
663
diff
changeset
|
973 |
/* Try to register us to the master server */ |
1fe298df8526
(svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents:
663
diff
changeset
|
974 |
_network_last_advertise_date = 0; |
1fe298df8526
(svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents:
663
diff
changeset
|
975 |
NetworkUDPAdvertise(); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
976 |
return true; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
977 |
} |
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 |
// The server is rebooting... |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
980 |
// 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
|
981 |
void NetworkReboot(void) |
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 |
if (_network_server) { |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
984 |
NetworkClientState *cs; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
985 |
FOR_ALL_CLIENTS(cs) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
986 |
SEND_COMMAND(PACKET_SERVER_NEWGAME)(cs); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
987 |
NetworkSend_Packets(cs); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
988 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
989 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
990 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
991 |
NetworkClose(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
992 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
993 |
// Free all queued commands |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
994 |
while (_local_command_queue != NULL) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
995 |
CommandPacket *p = _local_command_queue; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
996 |
_local_command_queue = _local_command_queue->next; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
997 |
free(p); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
998 |
} |
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 |
_networking = false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1001 |
_network_server = false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1002 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1003 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1004 |
// We want to disconnect from the host/clients |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1005 |
void NetworkDisconnect(void) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1006 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1007 |
if (_network_server) { |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
1008 |
NetworkClientState *cs; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1009 |
FOR_ALL_CLIENTS(cs) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1010 |
SEND_COMMAND(PACKET_SERVER_SHUTDOWN)(cs); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1011 |
NetworkSend_Packets(cs); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1012 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1013 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1014 |
|
765
18104986675d
(svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents:
764
diff
changeset
|
1015 |
NetworkUDPRemoveAdvertise(); |
18104986675d
(svn r1228) -Add: [Network] When a server normally shuts down, it removed itself
truelight
parents:
764
diff
changeset
|
1016 |
|
598
b8c5ae66f9ff
(svn r1022) -Fix: [Network] [ 1083690 ] The password dialog made the network
truelight
parents:
543
diff
changeset
|
1017 |
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0); |
b8c5ae66f9ff
(svn r1022) -Fix: [Network] [ 1083690 ] The password dialog made the network
truelight
parents:
543
diff
changeset
|
1018 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1019 |
NetworkClose(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1020 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1021 |
// Free all queued commands |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1022 |
while (_local_command_queue != NULL) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1023 |
CommandPacket *p = _local_command_queue; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1024 |
_local_command_queue = _local_command_queue->next; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1025 |
free(p); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1026 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1027 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1028 |
if (_networking && !_network_server) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1029 |
memcpy(&_patches, &network_tmp_patches, sizeof(_patches)); |
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 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1032 |
_networking = false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1033 |
_network_server = false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1034 |
} |
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 |
// Receives something from the network |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
1037 |
static bool NetworkReceive(void) |
0 | 1038 |
{ |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
1039 |
NetworkClientState *cs; |
0 | 1040 |
int n; |
1041 |
fd_set read_fd, write_fd; |
|
1042 |
struct timeval tv; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1043 |
|
0 | 1044 |
FD_ZERO(&read_fd); |
1045 |
FD_ZERO(&write_fd); |
|
1046 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1047 |
FOR_ALL_CLIENTS(cs) { |
0 | 1048 |
FD_SET(cs->socket, &read_fd); |
1049 |
FD_SET(cs->socket, &write_fd); |
|
1050 |
} |
|
1051 |
||
1052 |
// take care of listener port |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1053 |
if (_network_server) { |
0 | 1054 |
FD_SET(_listensocket, &read_fd); |
1055 |
} |
|
1056 |
||
1057 |
tv.tv_sec = tv.tv_usec = 0; // don't block at all. |
|
1058 |
#if !defined(__MORPHOS__) && !defined(__AMIGA__) |
|
1059 |
n = select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv); |
|
1060 |
#else |
|
1061 |
n = WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL); |
|
1062 |
#endif |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1063 |
if (n == -1 && !_network_server) NetworkError(STR_NETWORK_ERR_LOSTCONNECTION); |
0 | 1064 |
|
1065 |
// accept clients.. |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1066 |
if (_network_server && FD_ISSET(_listensocket, &read_fd)) |
0 | 1067 |
NetworkAcceptClients(); |
1068 |
||
1069 |
// read stuff from clients |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1070 |
FOR_ALL_CLIENTS(cs) { |
0 | 1071 |
cs->writable = !!FD_ISSET(cs->socket, &write_fd); |
1072 |
if (FD_ISSET(cs->socket, &read_fd)) { |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1073 |
if (_network_server) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1074 |
NetworkServer_ReadPackets(cs); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1075 |
else { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1076 |
byte res; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1077 |
// The client already was quiting! |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1078 |
if (cs->quited) return false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1079 |
if ((res = NetworkClient_ReadPackets(cs)) != NETWORK_RECV_STATUS_OKAY) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1080 |
// 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
|
1081 |
// close the client and drop back to main menu |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1082 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1083 |
NetworkClientError(res, cs); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1084 |
return false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1085 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1086 |
} |
0 | 1087 |
} |
1088 |
} |
|
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 |
} |
0 | 1091 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1092 |
// This sends all buffered commands (if possible) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1093 |
static void NetworkSend(void) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1094 |
{ |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
1095 |
NetworkClientState *cs; |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1096 |
FOR_ALL_CLIENTS(cs) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1097 |
if (cs->writable) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1098 |
NetworkSend_Packets(cs); |
0 | 1099 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1100 |
if (cs->status == STATUS_MAP) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1101 |
// 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
|
1102 |
SEND_COMMAND(PACKET_SERVER_MAP)(cs); |
0 | 1103 |
} |
1104 |
} |
|
1105 |
} |
|
1106 |
} |
|
1107 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1108 |
// Handle the local-command-queue |
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
1109 |
static void NetworkHandleLocalQueue(void) |
0 | 1110 |
{ |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1111 |
if (_local_command_queue != NULL) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1112 |
CommandPacket *cp; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1113 |
CommandPacket *cp_prev; |
0 | 1114 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1115 |
cp = _local_command_queue; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1116 |
cp_prev = NULL; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1117 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1118 |
while (cp != NULL) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1119 |
if (_frame_counter > cp->frame) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1120 |
// We can execute this command |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1121 |
NetworkExecuteCommand(cp); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1122 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1123 |
if (cp_prev != NULL) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1124 |
cp_prev->next = cp->next; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1125 |
free(cp); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1126 |
cp = cp_prev->next; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1127 |
} else { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1128 |
// This means we are at our first packet |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1129 |
_local_command_queue = cp->next; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1130 |
free(cp); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1131 |
cp = _local_command_queue; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1132 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1133 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1134 |
} else { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1135 |
// Command is in the future, skip to next |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1136 |
// (commands don't have to be in order in the queue!!) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1137 |
cp_prev = cp; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1138 |
cp = cp->next; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1139 |
} |
0 | 1140 |
} |
1141 |
} |
|
1142 |
} |
|
1143 |
||
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
1144 |
static bool NetworkDoClientLoop(void) |
0 | 1145 |
{ |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1146 |
_frame_counter++; |
0 | 1147 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1148 |
NetworkHandleLocalQueue(); |
211 | 1149 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1150 |
StateGameLoop(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1151 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1152 |
// Check if we are in sync! |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1153 |
if (_sync_frame != 0) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1154 |
if (_sync_frame == _frame_counter) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1155 |
#ifdef NETWORK_SEND_DOUBLE_SEED |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1156 |
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
|
1157 |
#else |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1158 |
if (_sync_seed_1 != _random_seeds[0][0]) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1159 |
#endif |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1160 |
NetworkError(STR_NETWORK_ERR_DESYNC); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1161 |
DEBUG(net, 0)("[NET] Sync error detected!"); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1162 |
NetworkClientError(NETWORK_RECV_STATUS_DESYNC, DEREF_CLIENT(0)); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1163 |
return false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1164 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1165 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1166 |
// 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
|
1167 |
// 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
|
1168 |
// frame as he is.. so we can start playing! |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1169 |
if (_network_first_time) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1170 |
_network_first_time = false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1171 |
SEND_COMMAND(PACKET_CLIENT_ACK)(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1172 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1173 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1174 |
_sync_frame = 0; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1175 |
} else if (_sync_frame < _frame_counter) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1176 |
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
|
1177 |
_sync_frame = 0; |
105 | 1178 |
} |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1179 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1180 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1181 |
return true; |
0 | 1182 |
} |
1183 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1184 |
// We have to do some UDP checking |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1185 |
void NetworkUDPGameLoop(void) |
211 | 1186 |
{ |
764
7e1e17b7c7d4
(svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents:
746
diff
changeset
|
1187 |
if (_network_udp_server) { |
7e1e17b7c7d4
(svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents:
746
diff
changeset
|
1188 |
NetworkUDPReceive(_udp_server_socket); |
7e1e17b7c7d4
(svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents:
746
diff
changeset
|
1189 |
if (_udp_master_socket != INVALID_SOCKET) { |
7e1e17b7c7d4
(svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents:
746
diff
changeset
|
1190 |
NetworkUDPReceive(_udp_master_socket); |
7e1e17b7c7d4
(svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents:
746
diff
changeset
|
1191 |
} |
7e1e17b7c7d4
(svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents:
746
diff
changeset
|
1192 |
} |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1193 |
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
|
1194 |
NetworkUDPReceive(_udp_client_socket); |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1195 |
if (_network_udp_broadcast > 0) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1196 |
_network_udp_broadcast--; |
0 | 1197 |
} |
1198 |
} |
|
1199 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1200 |
// The main loop called from ttd.c |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1201 |
// Here we also have to do StateGameLoop if needed! |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1202 |
void NetworkGameLoop(void) |
211 | 1203 |
{ |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1204 |
if (!_networking) return; |
105 | 1205 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1206 |
if (!NetworkReceive()) return; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1207 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1208 |
if (_network_server) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1209 |
// We first increase the _frame_counter |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1210 |
_frame_counter++; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1211 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1212 |
NetworkHandleLocalQueue(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1213 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1214 |
// Then we make the frame |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1215 |
StateGameLoop(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1216 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1217 |
_sync_seed_1 = _random_seeds[0][0]; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1218 |
#ifdef NETWORK_SEND_DOUBLE_SEED |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1219 |
_sync_seed_2 = _random_seeds[0][1]; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1220 |
#endif |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1221 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1222 |
NetworkServer_Tick(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1223 |
} else { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1224 |
// Client |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1225 |
|
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1226 |
// 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
|
1227 |
if (_frame_counter_server > _frame_counter) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1228 |
while (_frame_counter_server > _frame_counter) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1229 |
if (!NetworkDoClientLoop()) break; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1230 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1231 |
} else { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1232 |
// Else, keep on going till _frame_counter_max |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1233 |
if (_frame_counter_max > _frame_counter) { |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1234 |
NetworkDoClientLoop(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1235 |
} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1236 |
} |
0 | 1237 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1238 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1239 |
NetworkSend(); |
0 | 1240 |
} |
1241 |
||
716
8af847728d5b
(svn r1168) -Cleanup: [Network] Cleaned the network code a bit. Added 'const'
truelight
parents:
710
diff
changeset
|
1242 |
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
|
1243 |
{ |
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1244 |
md5_state_t state; |
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1245 |
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
|
1246 |
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
|
1247 |
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
|
1248 |
int di; |
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1249 |
|
607
1ae7deaa77d9
(svn r1031) -Fix: [Network] The unique-id was not as unique as I though it was..
truelight
parents:
602
diff
changeset
|
1250 |
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
|
1251 |
|
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1252 |
/* Generate the MD5 hash */ |
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1253 |
md5_init(&state); |
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1254 |
md5_append(&state, coding_string, strlen(coding_string)); |
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1255 |
md5_finish(&state, digest); |
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1256 |
|
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1257 |
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
|
1258 |
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
|
1259 |
|
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1260 |
/* _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
|
1261 |
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
|
1262 |
} |
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1263 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1264 |
// 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
|
1265 |
void NetworkStartUp(void) |
211 | 1266 |
{ |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1267 |
DEBUG(net, 3) ("[NET][Core] Starting network..."); |
770
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1268 |
|
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1269 |
#if defined(__MORPHOS__) || defined(__AMIGA__) |
841
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
1270 |
/* |
770
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1271 |
* 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
|
1272 |
* network related function, else: crash. |
841
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
1273 |
*/ |
770
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1274 |
{ |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1275 |
DEBUG(misc,3) ("[NET][Core] Loading bsd socket library"); |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1276 |
if (!(SocketBase = OpenLibrary("bsdsocket.library", 4))) { |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1277 |
DEBUG(net, 0) ("[NET][Core] Error: couldn't open bsdsocket.library version 4. Network not available."); |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1278 |
_network_available = false; |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1279 |
return; |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1280 |
} |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1281 |
|
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1282 |
#if defined(__AMIGA__) |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1283 |
// for usleep() implementation (only required for legacy AmigaOS builds) |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1284 |
if ( (TimerPort = CreateMsgPort()) ) { |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1285 |
if ( (TimerRequest = (struct timerequest *) CreateIORequest(TimerPort, sizeof(struct timerequest))) ) { |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1286 |
if ( OpenDevice("timer.device", UNIT_MICROHZ, (struct IORequest *) TimerRequest, 0) == 0 ) { |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1287 |
if ( !(TimerBase = TimerRequest->tr_node.io_Device) ) { |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1288 |
// free ressources... |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1289 |
DEBUG(net, 0) ("[NET][Core] Error: couldn't initialize timer. Network not available."); |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1290 |
_network_available = false; |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1291 |
return; |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1292 |
} |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1293 |
} |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1294 |
} |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1295 |
} |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1296 |
#endif // __AMIGA__ |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1297 |
} |
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1298 |
#endif // __MORPHOS__ / __AMIGA__ |
841
4874b9ce2765
(svn r1322) -Add: banning system (mostly tnx to guru3)
truelight
parents:
826
diff
changeset
|
1299 |
|
770
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1300 |
// Network is available |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1301 |
_network_available = true; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1302 |
_network_dedicated = false; |
668
1fe298df8526
(svn r1106) -Add: [Network] Added master-server protocol and advertise to
truelight
parents:
663
diff
changeset
|
1303 |
_network_last_advertise_date = 0; |
764
7e1e17b7c7d4
(svn r1227) -Add: Ingame Server-list (select Internet, then Find Servers)
truelight
parents:
746
diff
changeset
|
1304 |
_network_advertise_retries = 0; |
105 | 1305 |
|
629
4bb1f0fb0109
(svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents:
619
diff
changeset
|
1306 |
/* Load the ip from the openttd.cfg */ |
4bb1f0fb0109
(svn r1059) -Fix: [Console] Renamed 'set port' to 'set server_port'
truelight
parents:
619
diff
changeset
|
1307 |
_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
|
1308 |
/* 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
|
1309 |
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
|
1310 |
|
602
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1311 |
/* Generate an unique id when there is none yet */ |
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1312 |
if (_network_unique_id[0] == '\0') |
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1313 |
NetworkGenerateUniqueId(); |
f0afb4018788
(svn r1026) -Add: [Network] Added unique id, so in network, each client has an
truelight
parents:
598
diff
changeset
|
1314 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1315 |
memset(&_network_game_info, 0, sizeof(_network_game_info)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1316 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1317 |
/* XXX - Hard number here, because the strings can currently handle no more |
826 | 1318 |
than 10 clients -- TrueLight */ |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1319 |
_network_game_info.clients_max = 10; |
105 | 1320 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1321 |
// Let's load the network in windows |
211 | 1322 |
#if defined(WIN32) |
1323 |
{ |
|
1324 |
WSADATA wsa; |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1325 |
DEBUG(net, 3) ("[NET][Core] Loading windows socket library"); |
211 | 1326 |
if (WSAStartup(MAKEWORD(2,0), &wsa) != 0) { |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1327 |
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
|
1328 |
_network_available = false; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1329 |
return; |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1330 |
} |
211 | 1331 |
} |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1332 |
#endif // WIN32 |
211 | 1333 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1334 |
NetworkInitialize(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1335 |
DEBUG(net, 3) ("[NET][Core] Network online. Multiplayer available."); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1336 |
NetworkFindIPs(); |
211 | 1337 |
} |
1338 |
||
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1339 |
// This shuts the network down |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1340 |
void NetworkShutDown(void) |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1341 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1342 |
DEBUG(net, 3) ("[NET][Core] Shutting down the network."); |
211 | 1343 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1344 |
_network_available = false; |
211 | 1345 |
|
1346 |
#if defined(__MORPHOS__) || defined(__AMIGA__) |
|
1347 |
{ |
|
1348 |
// free allocated ressources |
|
770
c2eacca29601
(svn r1236) MorphOS: added make release like in OSX (tokai)
bjarni
parents:
765
diff
changeset
|
1349 |
#if defined(__AMIGA__) |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1350 |
if (TimerBase) { CloseDevice((struct IORequest *) TimerRequest); } |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1351 |
if (TimerRequest) { DeleteIORequest(TimerRequest); } |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1352 |
if (TimerPort) { DeleteMsgPort(TimerPort); } |
211 | 1353 |
#endif |
1354 |
||
1355 |
if (SocketBase) { |
|
1356 |
CloseLibrary(SocketBase); |
|
1357 |
} |
|
144
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1358 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1359 |
#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
|
1360 |
|
211 | 1361 |
#if defined(WIN32) |
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1362 |
{ |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1363 |
WSACleanup(); |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1364 |
} |
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
|
1365 |
#endif |
105 | 1366 |
} |
1367 |
||
773
a376cebd3dc0
(svn r1239) -Feature: Added gui option of setting company password. It can be found in the 'company information' window
darkvater
parents:
770
diff
changeset
|
1368 |
void NetworkChangeCompanyPassword(const char *str) |
a376cebd3dc0
(svn r1239) -Feature: Added gui option of setting company password. It can be found in the 'company information' window
darkvater
parents:
770
diff
changeset
|
1369 |
{ |
a376cebd3dc0
(svn r1239) -Feature: Added gui option of setting company password. It can be found in the 'company information' window
darkvater
parents:
770
diff
changeset
|
1370 |
if (strncmp(str, "*", sizeof(_network_player_info[_local_player].password)) == 0) { |
a376cebd3dc0
(svn r1239) -Feature: Added gui option of setting company password. It can be found in the 'company information' window
darkvater
parents:
770
diff
changeset
|
1371 |
_network_player_info[_local_player].password[0] = '\0'; |
a376cebd3dc0
(svn r1239) -Feature: Added gui option of setting company password. It can be found in the 'company information' window
darkvater
parents:
770
diff
changeset
|
1372 |
IConsolePrint(_iconsole_color_warning, "Company password protection removed."); |
a376cebd3dc0
(svn r1239) -Feature: Added gui option of setting company password. It can be found in the 'company information' window
darkvater
parents:
770
diff
changeset
|
1373 |
} else { |
a376cebd3dc0
(svn r1239) -Feature: Added gui option of setting company password. It can be found in the 'company information' window
darkvater
parents:
770
diff
changeset
|
1374 |
ttd_strlcpy(_network_player_info[_local_player].password, str, sizeof(_network_player_info[_local_player].password)); |
a376cebd3dc0
(svn r1239) -Feature: Added gui option of setting company password. It can be found in the 'company information' window
darkvater
parents:
770
diff
changeset
|
1375 |
IConsolePrintF(_iconsole_color_warning, "Company protected with password '%s'.", _network_player_info[_local_player].password); |
a376cebd3dc0
(svn r1239) -Feature: Added gui option of setting company password. It can be found in the 'company information' window
darkvater
parents:
770
diff
changeset
|
1376 |
} |
a376cebd3dc0
(svn r1239) -Feature: Added gui option of setting company password. It can be found in the 'company information' window
darkvater
parents:
770
diff
changeset
|
1377 |
|
a376cebd3dc0
(svn r1239) -Feature: Added gui option of setting company password. It can be found in the 'company information' window
darkvater
parents:
770
diff
changeset
|
1378 |
if (!_network_server) |
a376cebd3dc0
(svn r1239) -Feature: Added gui option of setting company password. It can be found in the 'company information' window
darkvater
parents:
770
diff
changeset
|
1379 |
SEND_COMMAND(PACKET_CLIENT_SET_PASSWORD)(_network_player_info[_local_player].password); |
a376cebd3dc0
(svn r1239) -Feature: Added gui option of setting company password. It can be found in the 'company information' window
darkvater
parents:
770
diff
changeset
|
1380 |
} |
a376cebd3dc0
(svn r1239) -Feature: Added gui option of setting company password. It can be found in the 'company information' window
darkvater
parents:
770
diff
changeset
|
1381 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1382 |
#else |
211 | 1383 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1384 |
void ParseConnectionString(const byte **player, const byte **port, byte *connection_string) {} |
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1385 |
void NetworkUpdateClientInfo(uint16 client_index) {} |
105 | 1386 |
|
543
946badd71033
(svn r942) -Merged branch/network back into the trunk
truelight
parents:
534
diff
changeset
|
1387 |
#endif /* ENABLE_NETWORK */ |