author | truelight |
Wed, 15 Sep 2004 18:36:33 +0000 | |
changeset 260 | 32fcaaf9f4ef |
parent 256 | f7c6267731fb |
child 262 | 3076b77dce21 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
105 | 3 |
#include "gui.h" |
0 | 4 |
#include "command.h" |
5 |
#include "player.h" |
|
144
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
6 |
#include "console.h" |
239 | 7 |
#include "economy.h" |
0 | 8 |
|
9 |
#if defined(WIN32) |
|
10 |
# include <windows.h> |
|
11 |
# include <winsock.h> |
|
12 |
||
13 |
# pragma comment (lib, "ws2_32.lib") |
|
14 |
# define ENABLE_NETWORK |
|
15 |
# define GET_LAST_ERROR() WSAGetLastError() |
|
16 |
# define EWOULDBLOCK WSAEWOULDBLOCK |
|
17 |
#endif |
|
18 |
||
19 |
#if defined(UNIX) |
|
20 |
// Make compatible with WIN32 names |
|
21 |
# define SOCKET int |
|
22 |
# define INVALID_SOCKET -1 |
|
23 |
// we need different defines for MorphOS and AmigaOS |
|
24 |
#if !defined(__MORPHOS__) && !defined(__AMIGA__) |
|
25 |
# define ioctlsocket ioctl |
|
26 |
# define closesocket close |
|
27 |
# define GET_LAST_ERROR() errno |
|
28 |
#endif |
|
29 |
// Need this for FIONREAD on solaris |
|
30 |
# define BSD_COMP |
|
31 |
# include <unistd.h> |
|
32 |
# include <sys/ioctl.h> |
|
33 |
||
34 |
// Socket stuff |
|
35 |
# include <sys/socket.h> |
|
36 |
# include <netinet/in.h> |
|
37 |
# include <arpa/inet.h> |
|
38 |
# include <errno.h> |
|
39 |
# include <sys/time.h> |
|
40 |
// NetDB |
|
41 |
# include <netdb.h> |
|
42 |
||
43 |
# ifndef TCP_NODELAY |
|
44 |
# define TCP_NODELAY 0x0001 |
|
45 |
# endif |
|
46 |
||
47 |
#endif |
|
48 |
||
49 |
||
50 |
#if defined(__MORPHOS__) || defined(__AMIGA__) |
|
51 |
# include <exec/types.h> |
|
52 |
# include <proto/exec.h> // required for Open/CloseLibrary() |
|
53 |
# if defined(__MORPHOS__) |
|
54 |
# include <sys/filio.h> // FION#? defines |
|
55 |
# else // __AMIGA__ |
|
56 |
# include <proto/socket.h> |
|
57 |
# endif |
|
58 |
||
59 |
// make source compatible with bsdsocket.library functions |
|
60 |
# define closesocket(s) CloseSocket(s) |
|
61 |
# define GET_LAST_ERROR() Errno() |
|
62 |
# define ioctlsocket(s,request,status) IoctlSocket((LONG)s,(ULONG)request,(char*)status) |
|
63 |
||
64 |
struct Library *SocketBase = NULL; |
|
144
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
65 |
|
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
|
66 |
#if !defined(__MORPHOS__) |
144
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
67 |
// usleep() implementation |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
68 |
#include <devices/timer.h> |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
69 |
#include <dos/dos.h> |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
70 |
|
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
71 |
struct Device *TimerBase = NULL; |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
72 |
struct MsgPort *TimerPort = NULL; |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
73 |
struct timerequest *TimerRequest = NULL; |
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
|
74 |
#endif |
144
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
75 |
|
0 | 76 |
#endif /* __MORPHOS__ || __AMIGA__ */ |
77 |
||
78 |
||
79 |
#define SEND_MTU 1460 |
|
80 |
||
81 |
#if defined(ENABLE_NETWORK) |
|
82 |
||
206 | 83 |
enum { |
84 |
PACKET_TYPE_WELCOME = 0, |
|
85 |
PACKET_TYPE_READY, |
|
86 |
PACKET_TYPE_ACK, |
|
87 |
PACKET_TYPE_SYNC, |
|
208 | 88 |
PACKET_TYPE_FSYNC, |
206 | 89 |
PACKET_TYPE_XMIT, |
90 |
PACKET_TYPE_COMMAND, |
|
239 | 91 |
PACKET_TYPE_EVENT, |
206 | 92 |
}; |
93 |
||
0 | 94 |
// sent from client -> server whenever the client wants to exec a command. |
95 |
// send from server -> client when another player execs a command. |
|
96 |
typedef struct CommandPacket { |
|
97 |
byte packet_length; |
|
98 |
byte packet_type; |
|
99 |
uint16 cmd; |
|
100 |
uint32 p1,p2; |
|
101 |
TileIndex tile; |
|
102 |
byte player;// player id, this is checked by the server. |
|
103 |
byte when; // offset from the current max_frame value minus 1. this is set by the server. |
|
104 |
uint32 dp[8]; |
|
105 |
} CommandPacket; |
|
106 |
||
239 | 107 |
typedef struct EventPacket { |
108 |
byte packet_length; |
|
109 |
byte packet_type; |
|
110 |
byte event_type; |
|
111 |
byte data_start; |
|
112 |
} EventPacket; |
|
113 |
||
0 | 114 |
#define COMMAND_PACKET_BASE_SIZE (sizeof(CommandPacket) - 8 * sizeof(uint32)) |
115 |
||
116 |
// sent from server -> client periodically to tell the client about the current tick in the server |
|
117 |
// and how far the client may progress. |
|
118 |
typedef struct SyncPacket { |
|
119 |
byte packet_length; |
|
120 |
byte packet_type; |
|
121 |
byte frames; // how many more frames may the client execute? this is relative to the old value of max. |
|
122 |
byte server; // where is the server currently executing? this is negatively relative to the old value of max. |
|
123 |
uint32 random_seed_1; // current random state at server. used to detect out of sync. |
|
124 |
uint32 random_seed_2; |
|
125 |
} SyncPacket; |
|
126 |
||
208 | 127 |
typedef struct FrameSyncPacket { |
128 |
byte packet_length; |
|
129 |
byte packet_type; |
|
130 |
byte frames; // where is the server currently executing? this is negatively relative to the old value of max. |
|
131 |
} FrameSyncPacket; |
|
132 |
||
0 | 133 |
// sent from server -> client as an acknowledgement that the server received the command. |
134 |
// the command will be executed at the current value of "max". |
|
135 |
typedef struct AckPacket { |
|
136 |
byte packet_length; |
|
137 |
byte packet_type; |
|
208 | 138 |
int16 when; |
0 | 139 |
} AckPacket; |
140 |
||
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
141 |
typedef struct ReadyPacket { |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
142 |
byte packet_length; |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
143 |
byte packet_type; |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
144 |
} ReadyPacket; |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
145 |
|
0 | 146 |
typedef struct FilePacketHdr { |
147 |
byte packet_length; |
|
148 |
byte packet_type; |
|
149 |
} FilePacketHdr; |
|
150 |
||
151 |
// sent from server to client when the client has joined. |
|
152 |
typedef struct WelcomePacket { |
|
153 |
byte packet_length; |
|
154 |
byte packet_type; |
|
206 | 155 |
uint32 player_seeds[MAX_PLAYERS][2]; |
156 |
uint32 frames_max; |
|
157 |
uint32 frames_srv; |
|
158 |
uint32 frames_cnt; |
|
0 | 159 |
} WelcomePacket; |
160 |
||
161 |
typedef struct Packet Packet; |
|
162 |
struct Packet { |
|
163 |
Packet *next; // this one has to be the first element. |
|
164 |
uint siz; |
|
165 |
byte buf[SEND_MTU]; // packet payload |
|
166 |
}; |
|
167 |
||
168 |
typedef struct ClientState { |
|
169 |
int socket; |
|
170 |
bool inactive; // disable sending of commands/syncs to client |
|
171 |
bool writable; |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
172 |
bool ready; |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
173 |
uint timeout; |
0 | 174 |
uint xmitpos; |
175 |
||
176 |
uint eaten; |
|
177 |
Packet *head, **last; |
|
178 |
||
179 |
uint buflen; // receive buffer len |
|
206 | 180 |
byte buf[1024]; // receive buffer |
0 | 181 |
} ClientState; |
182 |
||
183 |
||
184 |
typedef struct QueuedCommand QueuedCommand; |
|
185 |
struct QueuedCommand { |
|
186 |
QueuedCommand *next; |
|
187 |
CommandPacket cp; |
|
188 |
CommandCallback *callback; |
|
189 |
uint32 cmd; |
|
206 | 190 |
uint32 frame; |
0 | 191 |
}; |
192 |
||
193 |
typedef struct CommandQueue CommandQueue; |
|
194 |
struct CommandQueue { |
|
195 |
QueuedCommand *head, **last; |
|
196 |
}; |
|
197 |
||
198 |
#define MAX_CLIENTS (MAX_PLAYERS + 1) |
|
199 |
||
200 |
// packets waiting to be executed, for each of the players. |
|
201 |
// this list is sorted in frame order, so the item on the front will be executed first. |
|
202 |
static CommandQueue _command_queue; |
|
203 |
||
204 |
// in the client, this is the list of commands that have not yet been acked. |
|
205 |
// when it is acked, it will be moved to the appropriate position at the end of the player queue. |
|
206 |
static CommandQueue _ack_queue; |
|
207 |
||
208 |
static ClientState _clients[MAX_CLIENTS]; |
|
209 |
static int _num_clients; |
|
210 |
||
211 |
// keep a history of the 16 most recent seeds to be able to capture out of sync errors. |
|
212 |
static uint32 _my_seed_list[16][2]; |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
213 |
static bool _network_ready_sent; |
208 | 214 |
static uint32 _frame_fsync_last; |
0 | 215 |
|
216 |
typedef struct FutureSeeds { |
|
206 | 217 |
uint32 frame; |
0 | 218 |
uint32 seed[2]; |
219 |
} FutureSeeds; |
|
220 |
||
221 |
// remember some future seeds that the server sent to us. |
|
222 |
static FutureSeeds _future_seed[8]; |
|
175
d6cccece959a
(svn r176) -Fix: network_gui const and warnings fixes (Tron)
darkvater
parents:
173
diff
changeset
|
223 |
static uint _num_future_seed; |
0 | 224 |
|
105 | 225 |
static SOCKET _listensocket; // tcp socket |
0 | 226 |
|
105 | 227 |
static SOCKET _udp_client_socket; // udp server socket |
228 |
static SOCKET _udp_server_socket; // udp client socket |
|
0 | 229 |
|
230 |
typedef struct UDPPacket { |
|
231 |
byte command_code; |
|
232 |
byte data_len; |
|
233 |
byte command_check; |
|
234 |
byte data[255]; |
|
235 |
} UDPPacket; |
|
236 |
||
237 |
enum { |
|
238 |
NET_UDPCMD_SERVERSEARCH = 1, |
|
239 |
NET_UDPCMD_SERVERACTIVE, |
|
105 | 240 |
NET_UDPCMD_GETSERVERINFO, |
241 |
NET_UDPCMD_SERVERINFO, |
|
0 | 242 |
}; |
243 |
||
105 | 244 |
void NetworkUDPSend(bool client, struct sockaddr_in recv,struct UDPPacket packet); |
213
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
245 |
static void HandleCommandPacket(ClientState *cs, CommandPacket *np); |
206 | 246 |
static void CloseClient(ClientState *cs); |
247 |
void NetworkSendWelcome(ClientState *cs, bool direct); |
|
0 | 248 |
|
105 | 249 |
uint32 _network_ip_list[10]; // network ip list |
0 | 250 |
|
251 |
// this is set to point to the savegame |
|
252 |
static byte *_transmit_file; |
|
253 |
static size_t _transmit_file_size; |
|
254 |
||
255 |
static FILE *_recv_file; |
|
256 |
||
105 | 257 |
/* multi os compatible sleep function */ |
258 |
void CSleep(int milliseconds) { |
|
259 |
#if defined(WIN32) |
|
260 |
Sleep(milliseconds); |
|
261 |
#endif |
|
262 |
#if defined(UNIX) |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
263 |
#if !defined(__BEOS__) && !defined(__MORPHOS__) && !defined(__AMIGAOS__) |
105 | 264 |
usleep(milliseconds*1000); |
265 |
#endif |
|
266 |
#ifdef __BEOS__ |
|
267 |
snooze(milliseconds*1000); |
|
268 |
#endif |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
269 |
#if defined(__MORPHOS__) |
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
|
270 |
usleep(milliseconds*1000); |
df7c566d219f
(svn r174) -Network: [ 023268] small network.c changes/cleanups (MorphOS/AmigaOS) and one compiler warning fix (MorphOS) (tokai)
darkvater
parents:
172
diff
changeset
|
271 |
#endif |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
272 |
#if defined(__AMIGAOS__) && !defined(__MORPHOS__) |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
273 |
{ |
144
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
274 |
ULONG signals; |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
275 |
ULONG TimerSigBit = 1 << TimerPort->mp_SigBit; |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
276 |
|
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
277 |
// send IORequest |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
278 |
TimerRequest->tr_node.io_Command = TR_ADDREQUEST; |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
279 |
TimerRequest->tr_time.tv_secs = (milliseconds * 1000) / 1000000; |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
280 |
TimerRequest->tr_time.tv_micro = (milliseconds * 1000) % 1000000; |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
281 |
SendIO((struct IORequest *)TimerRequest); |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
282 |
|
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
283 |
if ( !((signals = Wait(TimerSigBit|SIGBREAKF_CTRL_C)) & TimerSigBit) ) { |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
284 |
AbortIO((struct IORequest *)TimerRequest); |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
285 |
} |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
286 |
WaitIO((struct IORequest *)TimerRequest); |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
287 |
} |
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
|
288 |
#endif // __AMIGAOS__ && !__MORPHOS__ |
105 | 289 |
#endif |
290 |
} |
|
291 |
||
0 | 292 |
////////////////////////////////////////////////////////////////////// |
293 |
||
105 | 294 |
// ****************************** // |
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
295 |
// * Network Error Handlers * // |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
296 |
// ****************************** // |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
297 |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
298 |
static void NetworkHandleSaveGameError() |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
299 |
{ |
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
300 |
_networking_sync = false; |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
301 |
_networking_queuing = true; |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
302 |
_switch_mode = SM_MENU; |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
303 |
_switch_mode_errorstr = STR_NETWORK_ERR_SAVEGAMEERROR; |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
304 |
} |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
305 |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
306 |
static void NetworkHandleConnectionLost() |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
307 |
{ |
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
308 |
_networking_sync = false; |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
309 |
_networking_queuing = true; |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
310 |
_switch_mode = SM_MENU; |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
311 |
_switch_mode_errorstr = STR_NETWORK_ERR_LOSTCONNECTION; |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
312 |
} |
211 | 313 |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
314 |
static void NetworkHandleDeSync() |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
315 |
{ |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
316 |
DEBUG(net, 0) ("[NET] Fatal ERROR: network sync error at frame %i", _frame_counter); |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
317 |
{ |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
318 |
int i; |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
319 |
for (i=15; i>=0; i--) DEBUG(net,0) ("[NET] frame %i: [0]=%i, [1]=%i",_frame_counter-(i+1),_my_seed_list[i][0],_my_seed_list[i][1]); |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
320 |
for (i=0; i<8; i++) DEBUG(net,0) ("[NET] frame %i: [0]=%i, [1]=%i",_frame_counter+i,_future_seed[i].seed[0],_future_seed[i].seed[1]); |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
321 |
} |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
322 |
_networking_sync = false; |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
323 |
_networking_queuing = true; |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
324 |
_switch_mode = SM_MENU; |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
325 |
_switch_mode_errorstr = STR_NETWORK_ERR_DESYNC; |
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
326 |
} |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
327 |
|
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
328 |
// ****************************** // |
105 | 329 |
// * TCP Packets and Handlers * // |
330 |
// ****************************** // |
|
331 |
||
0 | 332 |
static QueuedCommand *AllocQueuedCommand(CommandQueue *nq) |
333 |
{ |
|
334 |
QueuedCommand *qp = (QueuedCommand*)calloc(sizeof(QueuedCommand), 1); |
|
335 |
assert(qp); |
|
336 |
*nq->last = qp; |
|
337 |
nq->last = &qp->next; |
|
338 |
return qp; |
|
339 |
} |
|
340 |
||
211 | 341 |
static void QueueClear(CommandQueue *nq) |
342 |
{ |
|
206 | 343 |
QueuedCommand *qp; |
344 |
while ((qp=nq->head)) { |
|
345 |
// unlink it. |
|
346 |
if (!(nq->head = qp->next)) nq->last = &nq->head; |
|
347 |
free(qp); |
|
348 |
} |
|
349 |
nq->last = &nq->head; |
|
350 |
} |
|
351 |
||
208 | 352 |
static int GetNextSyncFrame() |
353 |
{ |
|
354 |
uint32 newframe; |
|
239 | 355 |
if (_frame_fsync_last == 0) return -5; |
256
f7c6267731fb
(svn r261) -Workaround: unplayeable network-game on busy maps because of wrong setup of _current_player. This is only a workaround that can cause desyncs too but not that often
signde
parents:
247
diff
changeset
|
356 |
newframe = (_frame_fsync_last + 10); // do not use a multiple of 4 since that screws up sync-packets |
239 | 357 |
if ( (newframe + 4) > _frame_counter_max) return -5; |
208 | 358 |
return (_frame_counter_max - newframe); |
359 |
||
360 |
} |
|
361 |
||
0 | 362 |
// go through the player queues for each player and see if there are any pending commands |
363 |
// that should be executed this frame. if there are, execute them. |
|
364 |
void NetworkProcessCommands() |
|
365 |
{ |
|
366 |
CommandQueue *nq; |
|
367 |
QueuedCommand *qp; |
|
260
32fcaaf9f4ef
(svn r266) -Fix: hopefully fixed the desync problem nicely (and reverted the
truelight
parents:
256
diff
changeset
|
368 |
byte old_player; |
0 | 369 |
|
370 |
// queue mode ? |
|
371 |
if (_networking_queuing) |
|
372 |
return; |
|
373 |
||
374 |
nq = &_command_queue; |
|
375 |
while ( (qp=nq->head) && (!_networking_sync || qp->frame <= _frame_counter)) { |
|
376 |
// unlink it. |
|
377 |
if (!(nq->head = qp->next)) nq->last = &nq->head; |
|
378 |
||
379 |
if (qp->frame < _frame_counter && _networking_sync) { |
|
239 | 380 |
DEBUG(net,0) ("warning: !qp->cp.frame < _frame_counter, %d < %d [%d]\n", qp->frame, _frame_counter, _frame_counter_srv+4); |
0 | 381 |
} |
382 |
||
383 |
// run the command |
|
260
32fcaaf9f4ef
(svn r266) -Fix: hopefully fixed the desync problem nicely (and reverted the
truelight
parents:
256
diff
changeset
|
384 |
old_player = _current_player; |
0 | 385 |
_current_player = qp->cp.player; |
386 |
memcpy(_decode_parameters, qp->cp.dp, (qp->cp.packet_length - COMMAND_PACKET_BASE_SIZE)); |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
387 |
|
0 | 388 |
DoCommandP(qp->cp.tile, qp->cp.p1, qp->cp.p2, qp->callback, qp->cmd | CMD_DONT_NETWORK); |
389 |
free(qp); |
|
260
32fcaaf9f4ef
(svn r266) -Fix: hopefully fixed the desync problem nicely (and reverted the
truelight
parents:
256
diff
changeset
|
390 |
_current_player = old_player; |
0 | 391 |
} |
392 |
||
393 |
if (!_networking_server) { |
|
394 |
// remember the random seed so we can check if we're out of sync. |
|
395 |
_my_seed_list[_frame_counter & 15][0] = _sync_seed_1; |
|
396 |
_my_seed_list[_frame_counter & 15][1] = _sync_seed_2; |
|
397 |
||
398 |
while (_num_future_seed) { |
|
399 |
assert(_future_seed[0].frame >= _frame_counter); |
|
400 |
if (_future_seed[0].frame != _frame_counter) break; |
|
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
401 |
if (_future_seed[0].seed[0] != _sync_seed_1 ||_future_seed[0].seed[1] != _sync_seed_2) NetworkHandleDeSync(); |
0 | 402 |
memcpy_overlapping(_future_seed, _future_seed + 1, --_num_future_seed * sizeof(FutureSeeds)); |
403 |
} |
|
404 |
} |
|
405 |
} |
|
406 |
||
407 |
// send a packet to a client |
|
408 |
static void SendBytes(ClientState *cs, void *bytes, uint len) |
|
409 |
{ |
|
410 |
byte *b = (byte*)bytes; |
|
411 |
uint n; |
|
412 |
Packet *p; |
|
413 |
||
414 |
assert(len != 0); |
|
415 |
||
416 |
// see if there's space in the last packet? |
|
417 |
if (!cs->head || (p = (Packet*)cs->last, p->siz == sizeof(p->buf))) |
|
418 |
p = NULL; |
|
419 |
||
420 |
do { |
|
421 |
if (!p) { |
|
422 |
// need to allocate a new packet buffer. |
|
423 |
p = (Packet*)malloc(sizeof(Packet)); |
|
424 |
||
425 |
// insert at the end of the linked list. |
|
426 |
*cs->last = p; |
|
427 |
cs->last = &p->next; |
|
428 |
p->next = NULL; |
|
429 |
p->siz = 0; |
|
430 |
} |
|
431 |
||
432 |
// copy bytes to packet. |
|
433 |
n = minu(sizeof(p->buf) - p->siz, len); |
|
434 |
memcpy(p->buf + p->siz, b, n); |
|
435 |
p->siz += n; |
|
436 |
b += n; |
|
437 |
p = NULL; |
|
438 |
} while (len -= n); |
|
439 |
} |
|
440 |
||
206 | 441 |
// send data direct to a client |
442 |
static void SendDirectBytes(ClientState *cs, void *bytes, uint len) |
|
443 |
{ |
|
444 |
char *buf = (char*)bytes; |
|
445 |
uint n; |
|
446 |
||
447 |
n = send(cs->socket, buf, len, 0); |
|
448 |
if (n == -1) { |
|
449 |
int err = GET_LAST_ERROR(); |
|
450 |
DEBUG(net, 0) ("[NET] send() failed with error %d", err); |
|
451 |
CloseClient(cs); |
|
452 |
} |
|
453 |
} |
|
454 |
||
0 | 455 |
// client: |
456 |
// add it to the client's ack queue, and send the command to the server |
|
457 |
// server: |
|
458 |
// add it to the server's player queue, and send it to all clients. |
|
459 |
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) |
|
460 |
{ |
|
461 |
int nump; |
|
462 |
QueuedCommand *qp; |
|
463 |
ClientState *cs; |
|
464 |
||
213
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
465 |
if (!(cmd & CMD_NET_INSTANT)) { |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
466 |
qp = AllocQueuedCommand(_networking_server ? &_command_queue : &_ack_queue); |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
467 |
} else { |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
468 |
qp = (QueuedCommand*)calloc(sizeof(QueuedCommand), 1); |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
469 |
} |
206 | 470 |
qp->cp.packet_type = PACKET_TYPE_COMMAND; |
0 | 471 |
qp->cp.tile = tile; |
472 |
qp->cp.p1 = p1; |
|
473 |
qp->cp.p2 = p2; |
|
474 |
qp->cp.cmd = (uint16)cmd; |
|
475 |
qp->cp.player = _local_player; |
|
476 |
qp->cp.when = 0; |
|
477 |
qp->cmd = cmd; |
|
478 |
qp->callback = callback; |
|
479 |
||
480 |
// so the server knows when to execute it. |
|
208 | 481 |
qp->frame = _frame_counter_max - GetNextSyncFrame(); |
0 | 482 |
|
483 |
// calculate the amount of extra bytes. |
|
484 |
nump = 8; |
|
485 |
while ( nump != 0 && ((uint32*)_decode_parameters)[nump-1] == 0) nump--; |
|
486 |
qp->cp.packet_length = COMMAND_PACKET_BASE_SIZE + nump * sizeof(uint32); |
|
487 |
if (nump != 0) memcpy(qp->cp.dp, _decode_parameters, nump * sizeof(uint32)); |
|
488 |
||
489 |
#if defined(TTD_BIG_ENDIAN) |
|
490 |
// need to convert the command to little endian before sending it. |
|
491 |
{ |
|
492 |
CommandPacket cp; |
|
493 |
cp = qp->cp; |
|
494 |
cp.cmd = TO_LE16(cp.cmd); |
|
495 |
cp.tile = TO_LE16(cp.tile); |
|
496 |
cp.p1 = TO_LE32(cp.p1); |
|
497 |
cp.p2 = TO_LE32(cp.p2); |
|
498 |
for(cs=_clients; cs->socket != INVALID_SOCKET; cs++) if (!cs->inactive) SendBytes(cs, &cp, cp.packet_length); |
|
499 |
} |
|
500 |
#else |
|
501 |
// send it to the peers |
|
502 |
for(cs=_clients; cs->socket != INVALID_SOCKET; cs++) if (!cs->inactive) SendBytes(cs, &qp->cp, qp->cp.packet_length); |
|
503 |
||
504 |
#endif |
|
213
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
505 |
if (cmd & CMD_NET_INSTANT) { |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
506 |
free(qp); |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
507 |
} |
0 | 508 |
} |
509 |
||
239 | 510 |
void NetworkSendEvent(uint16 type, uint16 data_len, void * data) |
511 |
{ |
|
512 |
EventPacket * ep; |
|
513 |
ClientState *cs; |
|
260
32fcaaf9f4ef
(svn r266) -Fix: hopefully fixed the desync problem nicely (and reverted the
truelight
parents:
256
diff
changeset
|
514 |
|
239 | 515 |
// encode the event ... add its data |
516 |
ep=malloc(data_len+sizeof(EventPacket)-1); |
|
517 |
ep->event_type = type; |
|
518 |
ep->packet_length = data_len+sizeof(EventPacket)-1; |
|
519 |
ep->packet_type = PACKET_TYPE_EVENT; |
|
520 |
memcpy(&ep->data_start,data,data_len); |
|
521 |
||
522 |
// send it to the peers |
|
523 |
for(cs=_clients; cs->socket != INVALID_SOCKET; cs++) if (!cs->inactive) SendBytes(cs, ep, ep->packet_length); |
|
524 |
||
525 |
// free the temp packet |
|
526 |
free(ep); |
|
527 |
} |
|
528 |
||
0 | 529 |
// client: |
530 |
// server sends a command from another player that we should execute. |
|
531 |
// put it in the command queue. |
|
532 |
// |
|
533 |
// server: |
|
534 |
// client sends a command that it wants to execute. |
|
535 |
// fill the when field so the client knows when to execute it. |
|
536 |
// put it in the appropriate player queue. |
|
537 |
// send it to all other clients. |
|
538 |
// send an ack packet to the actual client. |
|
539 |
||
540 |
static void HandleCommandPacket(ClientState *cs, CommandPacket *np) |
|
541 |
{ |
|
542 |
QueuedCommand *qp; |
|
543 |
ClientState *c; |
|
544 |
AckPacket ap; |
|
213
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
545 |
uint16 cmd; |
0 | 546 |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
547 |
DEBUG(net, 2) ("[NET] cmd size %d", np->packet_length); |
0 | 548 |
assert(np->packet_length >= COMMAND_PACKET_BASE_SIZE); |
549 |
||
213
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
550 |
cmd = np->cmd; |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
551 |
#if defined(TTD_BIG_ENDIAN) |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
552 |
cmd = TO_LE16(cmd); |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
553 |
#endif |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
554 |
|
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
555 |
if (!(cmd & CMD_NET_INSTANT)) { |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
556 |
// put it into the command queue |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
557 |
qp = AllocQueuedCommand(&_command_queue); |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
558 |
} else { |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
559 |
qp = (QueuedCommand*)calloc(sizeof(QueuedCommand), 1); |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
560 |
} |
0 | 561 |
qp->cp = *np; |
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
220
diff
changeset
|
562 |
|
208 | 563 |
qp->frame = _frame_counter_max - GetNextSyncFrame(); |
0 | 564 |
|
565 |
qp->callback = NULL; |
|
566 |
||
567 |
// extra params |
|
568 |
memcpy(&qp->cp.dp, np->dp, np->packet_length - COMMAND_PACKET_BASE_SIZE); |
|
569 |
||
206 | 570 |
ap.packet_type = PACKET_TYPE_ACK; |
208 | 571 |
ap.when = GetNextSyncFrame(); |
206 | 572 |
ap.packet_length = sizeof(AckPacket); |
208 | 573 |
DEBUG(net,4)("[NET] NewACK: frame=%i %i",ap.when,_frame_counter_max - GetNextSyncFrame()); |
0 | 574 |
|
575 |
// send it to the peers |
|
576 |
if (_networking_server) { |
|
577 |
for(c=_clients; c->socket != INVALID_SOCKET; c++) { |
|
578 |
if (c == cs) { |
|
213
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
579 |
if (!(cmd & CMD_NET_INSTANT)) SendDirectBytes(c, &ap, ap.packet_length); |
0 | 580 |
} else { |
581 |
if (!cs->inactive) SendBytes(c, &qp->cp, qp->cp.packet_length); |
|
582 |
} |
|
583 |
} |
|
584 |
} |
|
585 |
||
586 |
// convert from little endian to big endian? |
|
587 |
#if defined(TTD_BIG_ENDIAN) |
|
588 |
qp->cp.cmd = TO_LE16(qp->cp.cmd); |
|
589 |
qp->cp.tile = TO_LE16(qp->cp.tile); |
|
590 |
qp->cp.p1 = TO_LE32(qp->cp.p1); |
|
591 |
qp->cp.p2 = TO_LE32(qp->cp.p2); |
|
592 |
#endif |
|
593 |
||
594 |
qp->cmd = qp->cp.cmd; |
|
213
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
595 |
|
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
596 |
if (cmd & CMD_NET_INSTANT) { |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
597 |
byte p = _current_player; |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
598 |
_current_player = qp->cp.player; |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
599 |
memcpy(_decode_parameters, qp->cp.dp, (qp->cp.packet_length - COMMAND_PACKET_BASE_SIZE)); |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
600 |
DoCommandP(qp->cp.tile, qp->cp.p1, qp->cp.p2, qp->callback, qp->cmd | CMD_DONT_NETWORK); |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
601 |
free(qp); |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
602 |
_current_player = p; |
770e504a6e51
(svn r214) -Feature: CMD_NET_INSTANT [just in time command handling over network] (sign_de)
darkvater
parents:
211
diff
changeset
|
603 |
} |
0 | 604 |
} |
605 |
||
239 | 606 |
static void HandleEventPacket(EventPacket *ep) |
607 |
{ |
|
608 |
switch (ep->event_type) { |
|
609 |
case NET_EVENT_SUBSIDY: |
|
610 |
RemoteSubsidyAdd((Subsidy *)&ep->data_start); |
|
611 |
break; |
|
612 |
} |
|
613 |
} |
|
614 |
||
0 | 615 |
// sent from server -> client periodically to tell the client about the current tick in the server |
616 |
// and how far the client may progress. |
|
617 |
static void HandleSyncPacket(SyncPacket *sp) |
|
618 |
{ |
|
619 |
uint32 s1,s2; |
|
620 |
||
621 |
_frame_counter_srv = _frame_counter_max - sp->server; |
|
622 |
_frame_counter_max += sp->frames; |
|
623 |
||
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
624 |
// reset network ready packet state |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
625 |
_network_ready_sent = false; |
0 | 626 |
|
627 |
// queueing only? |
|
628 |
if (_networking_queuing || _frame_counter == 0) |
|
629 |
return; |
|
630 |
||
631 |
s1 = TO_LE32(sp->random_seed_1); |
|
632 |
s2 = TO_LE32(sp->random_seed_2); |
|
633 |
||
206 | 634 |
DEBUG(net, 3) ("[NET] sync seeds: frame=%i 1=%i 2=%i",_frame_counter, sp->random_seed_1, sp->random_seed_2); |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
635 |
|
0 | 636 |
if (_frame_counter_srv <= _frame_counter) { |
637 |
// we are ahead of the server check if the seed is in our list. |
|
638 |
if (_frame_counter_srv + 16 > _frame_counter) { |
|
639 |
// the random seed exists in our array check it. |
|
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
640 |
if (s1 != _my_seed_list[_frame_counter_srv & 0xF][0] || s2 != _my_seed_list[_frame_counter_srv & 0xF][1]) NetworkHandleDeSync(); |
0 | 641 |
} |
642 |
} else { |
|
643 |
// the server's frame has not been executed yet. store the server's seed in a list. |
|
644 |
if (_num_future_seed < lengthof(_future_seed)) { |
|
645 |
_future_seed[_num_future_seed].frame = _frame_counter_srv; |
|
646 |
_future_seed[_num_future_seed].seed[0] = s1; |
|
647 |
_future_seed[_num_future_seed].seed[1] = s2; |
|
648 |
_num_future_seed++; |
|
649 |
} |
|
650 |
} |
|
651 |
} |
|
652 |
||
208 | 653 |
static void HandleFSyncPacket(FrameSyncPacket *fsp) |
654 |
{ |
|
655 |
DEBUG(net,3)("[NET] FSYNC: srv=%i %i",fsp->frames,(_frame_counter_max - fsp->frames)); |
|
656 |
if (fsp->frames < 4) return; |
|
657 |
_frame_fsync_last = _frame_counter_srv = _frame_counter_max - fsp->frames; |
|
658 |
} |
|
659 |
||
0 | 660 |
// sent from server -> client as an acknowledgement that the server received the command. |
661 |
// the command will be executed at the current value of "max". |
|
206 | 662 |
static void HandleAckPacket(AckPacket * ap) |
0 | 663 |
{ |
664 |
QueuedCommand *q; |
|
665 |
// move a packet from the ack queue to the end of this player's queue. |
|
666 |
q = _ack_queue.head; |
|
667 |
assert(q); |
|
668 |
if (!(_ack_queue.head = q->next)) _ack_queue.last = &_ack_queue.head; |
|
669 |
q->next = NULL; |
|
206 | 670 |
q->frame = (_frame_counter_max - (ap->when)); |
0 | 671 |
|
672 |
*_command_queue.last = q; |
|
673 |
_command_queue.last = &q->next; |
|
674 |
||
206 | 675 |
DEBUG(net, 2) ("[NET] ack [frame=%i]",q->frame); |
0 | 676 |
} |
677 |
||
678 |
static void HandleFilePacket(FilePacketHdr *fp) |
|
679 |
{ |
|
680 |
int n = fp->packet_length - sizeof(FilePacketHdr); |
|
681 |
if (n == 0) { |
|
682 |
assert(_networking_queuing); |
|
683 |
assert(!_networking_sync); |
|
684 |
// eof |
|
685 |
if (_recv_file) { fclose(_recv_file); _recv_file = NULL; } |
|
686 |
||
687 |
// attempt loading the game. |
|
688 |
_game_mode = GM_NORMAL; |
|
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
689 |
if (SaveOrLoad("networkc.tmp", SL_LOAD) != SL_OK) { |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
690 |
NetworkCoreDisconnect(); |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
691 |
NetworkHandleSaveGameError(); |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
692 |
return; |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
693 |
} |
0 | 694 |
// sync to server. |
695 |
_networking_queuing = false; |
|
105 | 696 |
NetworkStartSync(false); |
0 | 697 |
|
698 |
if (_network_playas == 0) { |
|
699 |
// send a command to make a new player |
|
700 |
_local_player = 0; |
|
701 |
NetworkSendCommand(0, 0, 0, CMD_PLAYER_CTRL, NULL); |
|
1 | 702 |
_local_player = OWNER_SPECTATOR; |
0 | 703 |
} else { |
704 |
// take control over an existing company |
|
705 |
if (DEREF_PLAYER(_network_playas-1)->is_active) |
|
706 |
_local_player = _network_playas-1; |
|
707 |
else |
|
1 | 708 |
_local_player = OWNER_SPECTATOR; |
0 | 709 |
} |
710 |
||
711 |
} else { |
|
712 |
if(!_recv_file) { |
|
713 |
_recv_file = fopen("networkc.tmp", "wb"); |
|
714 |
if (!_recv_file) error("can't open savefile"); |
|
715 |
} |
|
716 |
fwrite( (char*)fp + sizeof(*fp), n, 1, _recv_file); |
|
717 |
} |
|
718 |
} |
|
719 |
||
211 | 720 |
static void HandleWelcomePacket(WelcomePacket *wp) |
721 |
{ |
|
206 | 722 |
int i; |
723 |
for (i=0; i<MAX_PLAYERS; i++) { |
|
724 |
_player_seeds[i][0]=wp->player_seeds[i][0]; |
|
725 |
_player_seeds[i][1]=wp->player_seeds[i][1]; |
|
726 |
} |
|
727 |
if (wp->frames_srv != 0) { |
|
728 |
_frame_counter_max = wp->frames_max; |
|
729 |
_frame_counter_srv = wp->frames_srv; |
|
730 |
} |
|
731 |
if (wp->frames_cnt != 0) { |
|
732 |
_frame_counter = wp->frames_cnt; |
|
733 |
} |
|
734 |
} |
|
735 |
||
736 |
static void HandleReadyPacket(ReadyPacket *rp, ClientState *cs) |
|
737 |
{ |
|
738 |
cs->ready=true; |
|
739 |
cs->timeout=_network_client_timeout; |
|
740 |
} |
|
741 |
||
742 |
||
0 | 743 |
static void CloseClient(ClientState *cs) |
744 |
{ |
|
745 |
Packet *p, *next; |
|
746 |
||
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
747 |
DEBUG(net, 1) ("[NET][TCP] closed client connection"); |
0 | 748 |
|
749 |
assert(cs->socket != INVALID_SOCKET); |
|
750 |
||
751 |
closesocket(cs->socket); |
|
752 |
||
753 |
// free buffers |
|
754 |
for(p = cs->head; p; p=next) { |
|
755 |
next = p->next; |
|
756 |
free(p); |
|
757 |
} |
|
758 |
||
759 |
// copy up structs... |
|
760 |
while ((cs+1)->socket != INVALID_SOCKET) { |
|
761 |
*cs = *(cs+1); |
|
762 |
cs++; |
|
763 |
} |
|
764 |
cs->socket = INVALID_SOCKET; |
|
765 |
||
105 | 766 |
if (_networking_server) _network_game.players_on--; |
767 |
||
0 | 768 |
_num_clients--; |
769 |
} |
|
770 |
||
771 |
#define NETWORK_BUFFER_SIZE 4096 |
|
772 |
static bool ReadPackets(ClientState *cs) |
|
773 |
{ |
|
774 |
byte network_buffer[NETWORK_BUFFER_SIZE]; |
|
775 |
uint pos,size; |
|
776 |
unsigned long recv_bytes; |
|
777 |
||
778 |
size = cs->buflen; |
|
779 |
||
780 |
for(;;) { |
|
781 |
if (size != 0) memcpy(network_buffer, cs->buf, size); |
|
782 |
||
783 |
recv_bytes = recv(cs->socket, (char*)network_buffer + size, sizeof(network_buffer) - size, 0); |
|
784 |
if ( recv_bytes == (unsigned long)-1) { |
|
785 |
int err = GET_LAST_ERROR(); |
|
786 |
if (err == EWOULDBLOCK) break; |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
787 |
DEBUG(net, 0) ("[NET] recv() failed with error %d", err); |
0 | 788 |
CloseClient(cs); |
789 |
return false; |
|
790 |
} |
|
791 |
// no more bytes for now? |
|
792 |
if (recv_bytes == 0) |
|
793 |
break; |
|
794 |
||
795 |
size += recv_bytes; // number of bytes read. |
|
796 |
pos = 0; |
|
797 |
while (size >= 2) { |
|
798 |
byte *packet = network_buffer + pos; |
|
799 |
// whole packet not there yet? |
|
800 |
if (size < packet[0]) break; |
|
801 |
size -= packet[0]; |
|
802 |
pos += packet[0]; |
|
803 |
switch(packet[1]) { |
|
206 | 804 |
case PACKET_TYPE_WELCOME: |
805 |
HandleWelcomePacket((WelcomePacket *)packet); |
|
806 |
break; |
|
807 |
case PACKET_TYPE_COMMAND: |
|
0 | 808 |
HandleCommandPacket(cs, (CommandPacket*)packet); |
809 |
break; |
|
206 | 810 |
case PACKET_TYPE_SYNC: |
0 | 811 |
assert(_networking_sync || _networking_queuing); |
812 |
assert(!_networking_server); |
|
813 |
HandleSyncPacket((SyncPacket*)packet); |
|
814 |
break; |
|
208 | 815 |
case PACKET_TYPE_FSYNC: |
816 |
HandleFSyncPacket((FrameSyncPacket *)packet); |
|
817 |
break; |
|
206 | 818 |
case PACKET_TYPE_ACK: |
0 | 819 |
assert(!_networking_server); |
206 | 820 |
HandleAckPacket((AckPacket*)packet); |
0 | 821 |
break; |
206 | 822 |
case PACKET_TYPE_XMIT: |
0 | 823 |
HandleFilePacket((FilePacketHdr*)packet); |
824 |
break; |
|
206 | 825 |
case PACKET_TYPE_READY: |
826 |
HandleReadyPacket((ReadyPacket*)packet, cs); |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
827 |
break; |
239 | 828 |
case PACKET_TYPE_EVENT: |
829 |
HandleEventPacket((EventPacket*)packet); |
|
830 |
break; |
|
0 | 831 |
default: |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
832 |
DEBUG (net,0) ("net: unknown packet type"); |
0 | 833 |
} |
834 |
} |
|
835 |
||
175
d6cccece959a
(svn r176) -Fix: network_gui const and warnings fixes (Tron)
darkvater
parents:
173
diff
changeset
|
836 |
assert(size < sizeof(cs->buf)); |
0 | 837 |
|
838 |
memcpy(cs->buf, network_buffer + pos, size); |
|
839 |
} |
|
840 |
||
841 |
cs->buflen = size; |
|
842 |
||
843 |
return true; |
|
844 |
} |
|
845 |
||
846 |
||
847 |
static bool SendPackets(ClientState *cs) |
|
848 |
{ |
|
849 |
Packet *p; |
|
850 |
int n; |
|
851 |
uint nskip = cs->eaten, nsent = nskip; |
|
852 |
||
853 |
// try sending as much as possible. |
|
854 |
for(p=cs->head; p ;p = p->next) { |
|
855 |
if (p->siz) { |
|
856 |
assert(nskip < p->siz); |
|
857 |
||
858 |
n = send(cs->socket, p->buf + nskip, p->siz - nskip, 0); |
|
859 |
if (n == -1) { |
|
860 |
int err = GET_LAST_ERROR(); |
|
861 |
if (err == EWOULDBLOCK) break; |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
862 |
DEBUG(net, 0) ("[NET] send() failed with error %d", err); |
0 | 863 |
CloseClient(cs); |
864 |
return false; |
|
865 |
} |
|
866 |
nsent += n; |
|
867 |
// send was not able to send it all? then we assume that the os buffer is full and break. |
|
868 |
if (nskip + n != p->siz) |
|
869 |
break; |
|
870 |
nskip = 0; |
|
871 |
} |
|
872 |
} |
|
873 |
||
874 |
// nsent bytes in the linked list are not invalid. free as many buffers as possible. |
|
875 |
// don't actually free the last buffer. |
|
876 |
while (nsent) { |
|
877 |
p = cs->head; |
|
878 |
assert(p->siz != 0); |
|
879 |
||
880 |
// some bytes of the packet are still unsent. |
|
881 |
if ( (int)(nsent - p->siz) < 0) |
|
882 |
break; |
|
883 |
nsent -= p->siz; |
|
884 |
p->siz = 0; |
|
885 |
if (p->next) { |
|
886 |
cs->head = p->next; |
|
887 |
free(p); |
|
888 |
} |
|
889 |
} |
|
890 |
||
891 |
cs->eaten = nsent; |
|
892 |
||
893 |
return true; |
|
894 |
} |
|
895 |
||
896 |
// transmit the file.. |
|
897 |
static void SendXmit(ClientState *cs) |
|
898 |
{ |
|
899 |
uint pos, n; |
|
900 |
FilePacketHdr hdr; |
|
901 |
int p; |
|
902 |
||
903 |
// if too many unsent bytes left in buffer, don't send more. |
|
904 |
if (cs->head && cs->head->next) |
|
905 |
return; |
|
906 |
||
907 |
pos = cs->xmitpos - 1; |
|
908 |
||
909 |
p = 20; |
|
910 |
do { |
|
911 |
// compute size of data to xmit |
|
912 |
n = minu(_transmit_file_size - pos, 248); |
|
913 |
||
914 |
hdr.packet_length = n + sizeof(hdr); |
|
206 | 915 |
hdr.packet_type = PACKET_TYPE_XMIT; |
0 | 916 |
SendBytes(cs, &hdr, sizeof(hdr)); |
917 |
||
918 |
if (n == 0) { |
|
919 |
pos = -1; // eof |
|
920 |
break; |
|
921 |
} |
|
922 |
SendBytes(cs, _transmit_file + pos, n); |
|
923 |
pos += n; |
|
924 |
} while (--p); |
|
925 |
||
926 |
cs->xmitpos = pos + 1; |
|
927 |
||
206 | 928 |
if (cs->xmitpos == 0) { |
929 |
NetworkSendWelcome(cs,false); |
|
930 |
} |
|
931 |
||
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
932 |
DEBUG(net, 2) ("[NET] client xmit at %d", pos + 1); |
0 | 933 |
} |
934 |
||
935 |
static ClientState *AllocClient(SOCKET s) |
|
936 |
{ |
|
937 |
ClientState *cs; |
|
938 |
||
939 |
if (_num_clients == MAX_CLIENTS) |
|
940 |
return NULL; |
|
941 |
||
105 | 942 |
if (_networking_server) _network_game.players_on++; |
943 |
||
0 | 944 |
cs = &_clients[_num_clients++]; |
945 |
memset(cs, 0, sizeof(*cs)); |
|
946 |
cs->last = &cs->head; |
|
947 |
cs->socket = s; |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
948 |
cs->timeout = _network_client_timeout; |
0 | 949 |
return cs; |
950 |
} |
|
951 |
||
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
952 |
void NetworkSendReadyPacket() |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
953 |
{ |
206 | 954 |
if ((!_network_ready_sent) && (_frame_counter + _network_ready_ahead >= _frame_counter_max)) { |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
955 |
ReadyPacket *rp = malloc(sizeof(rp)); |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
956 |
ClientState *c = _clients; |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
957 |
|
206 | 958 |
rp->packet_type = PACKET_TYPE_READY; |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
959 |
rp->packet_length = sizeof(rp); |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
960 |
SendBytes(c, rp, sizeof(rp)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
961 |
_network_ready_sent = true; |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
962 |
} |
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
963 |
} |
0 | 964 |
|
206 | 965 |
void NetworkSendSyncPackets() |
966 |
{ |
|
967 |
ClientState *cs; |
|
968 |
uint32 new_max; |
|
969 |
SyncPacket sp; |
|
970 |
||
971 |
new_max = _frame_counter + (int)_network_sync_freq; |
|
972 |
||
973 |
DEBUG(net,3) ("net: serv: sync frame=%i,max=%i, seed1=%i, seed2=%i",new_max,_sync_seed_1,_sync_seed_2); |
|
974 |
||
975 |
sp.packet_length = sizeof(sp); |
|
976 |
sp.packet_type = PACKET_TYPE_SYNC; |
|
977 |
sp.frames = new_max - _frame_counter_max; |
|
978 |
sp.server = _frame_counter_max - _frame_counter; |
|
979 |
sp.random_seed_1 = TO_LE32(_sync_seed_1); |
|
980 |
sp.random_seed_2 = TO_LE32(_sync_seed_2); |
|
981 |
_frame_counter_max = new_max; |
|
982 |
||
983 |
// send it to all the clients and mark them unready |
|
984 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
985 |
cs->ready=false; |
|
208 | 986 |
SendBytes(cs, &sp, sp.packet_length); |
987 |
} |
|
988 |
||
989 |
} |
|
990 |
||
991 |
void NetworkSendFrameSyncPackets() |
|
992 |
{ |
|
993 |
ClientState *cs; |
|
994 |
FrameSyncPacket fsp; |
|
995 |
if ((_frame_counter + 4) < _frame_counter_max) if ((_frame_fsync_last + 4 < _frame_counter)) { |
|
996 |
// this packet mantains some information about on which frame the server is |
|
997 |
fsp.frames = _frame_counter_max - _frame_counter; |
|
998 |
fsp.packet_type = PACKET_TYPE_FSYNC; |
|
999 |
fsp.packet_length = sizeof (FrameSyncPacket); |
|
1000 |
// send it to all the clients and mark them unready |
|
1001 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
1002 |
cs->ready=false; |
|
1003 |
SendBytes(cs, &fsp, fsp.packet_length); |
|
1004 |
} |
|
1005 |
_frame_fsync_last = _frame_counter; |
|
206 | 1006 |
} |
1007 |
||
1008 |
} |
|
1009 |
||
1010 |
void NetworkSendWelcome(ClientState *cs, bool direct) { |
|
1011 |
WelcomePacket wp; |
|
1012 |
int i; |
|
1013 |
wp.packet_type = PACKET_TYPE_WELCOME; |
|
1014 |
wp.packet_length = sizeof(WelcomePacket); |
|
1015 |
for (i=0; i<MAX_PLAYERS; i++) { |
|
1016 |
wp.player_seeds[i][0]=_player_seeds[i][0]; |
|
1017 |
wp.player_seeds[i][1]=_player_seeds[i][1]; |
|
1018 |
} |
|
1019 |
if (direct) { |
|
1020 |
wp.frames_max=0; |
|
1021 |
wp.frames_srv=0; |
|
1022 |
wp.frames_cnt=_frame_counter; |
|
1023 |
SendDirectBytes(cs,(void *)&wp,wp.packet_length); |
|
1024 |
} else { |
|
1025 |
wp.frames_max=_frame_counter_max; |
|
1026 |
wp.frames_srv=_frame_counter_srv; |
|
1027 |
wp.frames_cnt=0; |
|
1028 |
SendBytes(cs,(void *)&wp,wp.packet_length); |
|
1029 |
} |
|
1030 |
} |
|
1031 |
||
0 | 1032 |
static void NetworkAcceptClients() |
1033 |
{ |
|
1034 |
struct sockaddr_in sin; |
|
1035 |
SOCKET s; |
|
1036 |
ClientState *cs; |
|
1037 |
#ifndef __MORPHOS__ |
|
1038 |
int sin_len; |
|
1039 |
#else |
|
1040 |
LONG sin_len; // for some reason we need a 'LONG' under MorphOS |
|
1041 |
#endif |
|
1042 |
||
1043 |
assert(_listensocket != INVALID_SOCKET); |
|
1044 |
||
1045 |
for(;;) { |
|
1046 |
sin_len = sizeof(sin); |
|
1047 |
s = accept(_listensocket, (struct sockaddr*)&sin, &sin_len); |
|
1048 |
if (s == INVALID_SOCKET) return; |
|
1049 |
||
1050 |
// set nonblocking mode for client socket |
|
1051 |
{ unsigned long blocking = 1; ioctlsocket(s, FIONBIO, &blocking); } |
|
1052 |
||
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1053 |
DEBUG(net, 1) ("[NET] got client from %s", inet_ntoa(sin.sin_addr)); |
0 | 1054 |
|
1055 |
// set nodelay |
|
1056 |
{int b = 1; setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b));} |
|
1057 |
||
1058 |
cs = AllocClient(s); |
|
1059 |
if (cs == NULL) { |
|
1060 |
// no more clients allowed? |
|
1061 |
closesocket(s); |
|
1062 |
continue; |
|
1063 |
} |
|
1064 |
||
1065 |
if (_networking_sync) { |
|
1066 |
// a new client has connected. it needs a snapshot. |
|
1067 |
cs->inactive = true; |
|
1068 |
} |
|
1069 |
} |
|
1070 |
||
1071 |
// when a new client has joined. it needs different information depending on if it's at the game menu or in an active game. |
|
1072 |
// Game menu: |
|
1073 |
// - list of players already in the game (name, company name, face, color) |
|
1074 |
// - list of game settings and patch settings |
|
1075 |
// Active game: |
|
1076 |
// - the state of the world (includes player name, company name, player face, player color) |
|
1077 |
// - list of the patch settings |
|
1078 |
||
1079 |
// Networking can be in several "states". |
|
1080 |
// * not sync - games don't need to be in sync, and frame counter is not synced. for example intro screen. all commands are executed immediately. |
|
1081 |
// * sync - games are in sync |
|
1082 |
} |
|
1083 |
||
1084 |
static void SendQueuedCommandsToNewClient(ClientState *cs) |
|
1085 |
{ |
|
1086 |
// send the commands in the server queue to the new client. |
|
1087 |
QueuedCommand *qp; |
|
1088 |
SyncPacket sp; |
|
206 | 1089 |
uint32 frame; |
0 | 1090 |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1091 |
DEBUG(net, 2) ("[NET] sending queued commands to client"); |
0 | 1092 |
|
1093 |
sp.packet_length = sizeof(sp); |
|
206 | 1094 |
sp.packet_type = PACKET_TYPE_SYNC; |
0 | 1095 |
sp.random_seed_1 = sp.random_seed_2 = 0; |
1096 |
sp.server = 0; |
|
1097 |
||
1098 |
frame = _frame_counter; |
|
1099 |
||
1100 |
for(qp=_command_queue.head; qp; qp = qp->next) { |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1101 |
DEBUG(net, 4) ("[NET] sending cmd to be executed at %d (old %d)", qp->frame, frame); |
0 | 1102 |
if (qp->frame > frame) { |
1103 |
assert(qp->frame <= _frame_counter_max); |
|
1104 |
sp.frames = qp->frame - frame; |
|
1105 |
frame = qp->frame; |
|
1106 |
SendBytes(cs, &sp, sizeof(sp)); |
|
1107 |
} |
|
1108 |
SendBytes(cs, &qp->cp, qp->cp.packet_length); |
|
1109 |
} |
|
1110 |
||
1111 |
if (frame < _frame_counter_max) { |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1112 |
DEBUG(net, 4) ("[NET] sending queued sync %d (%d)", _frame_counter_max, frame); |
0 | 1113 |
sp.frames = _frame_counter_max - frame; |
1114 |
SendBytes(cs, &sp, sizeof(sp)); |
|
1115 |
} |
|
1116 |
||
1117 |
} |
|
1118 |
||
211 | 1119 |
bool NetworkCheckClientReady() |
1120 |
{ |
|
206 | 1121 |
bool ready_all = true; |
1122 |
uint16 count = 0; |
|
1123 |
ClientState *cs; |
|
1124 |
||
1125 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
1126 |
count++; |
|
1127 |
ready_all = ready_all && (cs->ready || cs->inactive || (cs->xmitpos>0)); |
|
1128 |
if (!cs->ready) cs->timeout-=1; |
|
1129 |
if (cs->timeout == 0) { |
|
1130 |
SET_DPARAM16(0,count); |
|
1131 |
ShowErrorMessage(-1,STR_NETWORK_ERR_TIMEOUT,0,0); |
|
1132 |
CloseClient(cs); |
|
1133 |
} |
|
1134 |
} |
|
1135 |
return ready_all; |
|
1136 |
} |
|
1137 |
||
105 | 1138 |
// ************************** // |
1139 |
// * TCP Networking * // |
|
1140 |
// ************************** // |
|
1141 |
||
211 | 1142 |
unsigned long NetworkResolveHost(const char *hostname) |
1143 |
{ |
|
144
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1144 |
struct hostent* remotehost; |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1145 |
|
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1146 |
if ((hostname[0]<0x30) || (hostname[0]>0x39)) { |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1147 |
// seems to be an hostname [first character is no number] |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1148 |
remotehost = gethostbyname(hostname); |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1149 |
if (remotehost == NULL) { |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1150 |
DEBUG(net, 1) ("[NET][IP] cannot resolve %s", hostname); |
144
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1151 |
return 0; |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1152 |
} else { |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1153 |
DEBUG(net, 1) ("[NET][IP] resolved %s to %s",hostname, inet_ntoa(*(struct in_addr *) remotehost->h_addr_list[0])); |
144
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1154 |
return inet_addr(inet_ntoa(*(struct in_addr *) remotehost->h_addr_list[0])); |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1155 |
} |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1156 |
} else { |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1157 |
// seems to be an ip [first character is a number] |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1158 |
return inet_addr(hostname); |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1159 |
} |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1160 |
|
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1161 |
} |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1162 |
|
105 | 1163 |
bool NetworkConnect(const char *hostname, int port) |
1164 |
{ |
|
1165 |
SOCKET s; |
|
1166 |
struct sockaddr_in sin; |
|
1167 |
int b; |
|
1168 |
||
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1169 |
DEBUG(net, 1) ("[NET][TCP] Connecting to %s %d", hostname, port); |
105 | 1170 |
|
1171 |
s = socket(AF_INET, SOCK_STREAM, 0); |
|
1172 |
if (s == INVALID_SOCKET) error("socket() failed"); |
|
1173 |
||
1174 |
b = 1; |
|
1175 |
setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b)); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1176 |
|
105 | 1177 |
sin.sin_family = AF_INET; |
144
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1178 |
sin.sin_addr.s_addr = NetworkResolveHost(hostname); |
105 | 1179 |
sin.sin_port = htons(port); |
1180 |
||
1181 |
if (connect(s, (struct sockaddr*) &sin, sizeof(sin)) != 0) { |
|
1182 |
NetworkClose(true); |
|
1183 |
return false; |
|
1184 |
} |
|
1185 |
||
1186 |
// set nonblocking mode for socket.. |
|
1187 |
{ unsigned long blocking = 1; ioctlsocket(s, FIONBIO, &blocking); } |
|
1188 |
||
1189 |
// in client mode, only the first client field is used. it's pointing to the server. |
|
1190 |
AllocClient(s); |
|
1191 |
||
1192 |
// queue packets.. because we're waiting for the savegame. |
|
1193 |
_networking_queuing = true; |
|
1194 |
_frame_counter_max = 0; |
|
1195 |
||
1196 |
return true; |
|
1197 |
} |
|
1198 |
||
1199 |
void NetworkListen() |
|
1200 |
{ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1201 |
|
105 | 1202 |
SOCKET ls; |
1203 |
struct sockaddr_in sin; |
|
1204 |
int port; |
|
1205 |
||
1206 |
port = _network_server_port; |
|
1207 |
||
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1208 |
DEBUG(net, 1) ("[NET][TCP] listening on port %d", port); |
105 | 1209 |
|
1210 |
ls = socket(AF_INET, SOCK_STREAM, 0); |
|
1211 |
if (ls == INVALID_SOCKET) |
|
1212 |
error("socket() on listen socket failed"); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1213 |
|
105 | 1214 |
// reuse the socket |
1215 |
{ |
|
1216 |
int reuse = 1; if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) == -1) |
|
1217 |
error("setsockopt() on listen socket failed"); |
|
1218 |
} |
|
1219 |
||
1220 |
// set nonblocking mode for socket |
|
1221 |
{ unsigned long blocking = 1; ioctlsocket(ls, FIONBIO, &blocking); } |
|
1222 |
||
1223 |
sin.sin_family = AF_INET; |
|
1224 |
sin.sin_addr.s_addr = 0; |
|
1225 |
sin.sin_port = htons(port); |
|
1226 |
||
1227 |
if (bind(ls, (struct sockaddr*)&sin, sizeof(sin)) != 0) |
|
1228 |
error("bind() failed"); |
|
1229 |
||
1230 |
if (listen(ls, 1) != 0) |
|
1231 |
error("listen() failed"); |
|
1232 |
||
1233 |
_listensocket = ls; |
|
1234 |
} |
|
1235 |
||
0 | 1236 |
void NetworkReceive() |
1237 |
{ |
|
1238 |
ClientState *cs; |
|
1239 |
int n; |
|
1240 |
fd_set read_fd, write_fd; |
|
1241 |
struct timeval tv; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1242 |
|
0 | 1243 |
FD_ZERO(&read_fd); |
1244 |
FD_ZERO(&write_fd); |
|
1245 |
||
1246 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
1247 |
FD_SET(cs->socket, &read_fd); |
|
1248 |
FD_SET(cs->socket, &write_fd); |
|
1249 |
} |
|
1250 |
||
1251 |
// take care of listener port |
|
1252 |
if (_networking_server) { |
|
1253 |
FD_SET(_listensocket, &read_fd); |
|
1254 |
} |
|
1255 |
||
1256 |
tv.tv_sec = tv.tv_usec = 0; // don't block at all. |
|
1257 |
#if !defined(__MORPHOS__) && !defined(__AMIGA__) |
|
1258 |
n = select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv); |
|
1259 |
#else |
|
1260 |
n = WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL); |
|
1261 |
#endif |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1262 |
if ((n == -1) && (!_networking_server)) NetworkHandleConnectionLost(); |
0 | 1263 |
|
1264 |
// accept clients.. |
|
1265 |
if (_networking_server && FD_ISSET(_listensocket, &read_fd)) |
|
1266 |
NetworkAcceptClients(); |
|
1267 |
||
1268 |
// read stuff from clients |
|
1269 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
1270 |
cs->writable = !!FD_ISSET(cs->socket, &write_fd); |
|
1271 |
if (FD_ISSET(cs->socket, &read_fd)) { |
|
1272 |
if (!ReadPackets(cs)) |
|
1273 |
cs--; |
|
1274 |
} |
|
1275 |
} |
|
1276 |
||
1277 |
// if we're a server, and any client needs a snapshot, create a snapshot and send all commands from the server queue to the client. |
|
1278 |
if (_networking_server && _transmit_file == NULL) { |
|
1279 |
bool didsave = false; |
|
1280 |
||
1281 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
1282 |
if (cs->inactive) { |
|
1283 |
cs->inactive = false; |
|
1284 |
// found a client waiting for a snapshot. make a snapshot. |
|
1285 |
if (!didsave) { |
|
1286 |
char filename[256]; |
|
1287 |
sprintf(filename, "%snetwork.tmp", _path.autosave_dir); |
|
1288 |
didsave = true; |
|
1289 |
if (SaveOrLoad(filename, SL_SAVE) != SL_OK) error("network savedump failed"); |
|
1290 |
_transmit_file = ReadFileToMem(filename, &_transmit_file_size, 500000); |
|
1291 |
if (_transmit_file == NULL) error("network savedump failed to load"); |
|
1292 |
} |
|
1293 |
// and start sending the file.. |
|
1294 |
cs->xmitpos = 1; |
|
1295 |
||
1296 |
// send queue of commands to client. |
|
1297 |
SendQueuedCommandsToNewClient(cs); |
|
206 | 1298 |
|
1299 |
NetworkSendWelcome(cs, true); |
|
0 | 1300 |
} |
1301 |
} |
|
1302 |
} |
|
1303 |
} |
|
1304 |
||
1305 |
void NetworkSend() |
|
1306 |
{ |
|
1307 |
ClientState *cs; |
|
1308 |
void *free_xmit; |
|
1309 |
||
1310 |
free_xmit = _transmit_file; |
|
1311 |
||
1312 |
// send stuff to all clients |
|
1313 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
1314 |
if (cs->xmitpos) { |
|
1315 |
if (cs->writable) |
|
1316 |
SendXmit(cs); |
|
1317 |
free_xmit = NULL; |
|
1318 |
} |
|
1319 |
if (cs->writable) { |
|
1320 |
if (!SendPackets(cs)) cs--; |
|
1321 |
} |
|
1322 |
} |
|
1323 |
||
1324 |
// no clients left that xmit the file, free it. |
|
1325 |
if (free_xmit) { |
|
1326 |
_transmit_file = NULL; |
|
1327 |
free(free_xmit); |
|
1328 |
} |
|
1329 |
} |
|
1330 |
||
1331 |
||
105 | 1332 |
void NetworkInitialize() |
0 | 1333 |
{ |
1334 |
ClientState *cs; |
|
1 | 1335 |
|
206 | 1336 |
QueueClear(&_command_queue); |
1337 |
QueueClear(&_ack_queue); |
|
0 | 1338 |
_command_queue.last = &_command_queue.head; |
211 | 1339 |
_network_game_list = NULL; |
0 | 1340 |
|
1341 |
// invalidate all clients |
|
1342 |
for(cs=_clients; cs != &_clients[MAX_CLIENTS]; cs++) |
|
1343 |
cs->socket = INVALID_SOCKET; |
|
105 | 1344 |
|
1345 |
} |
|
1346 |
||
211 | 1347 |
void NetworkClose(bool client) |
1348 |
{ |
|
105 | 1349 |
|
1350 |
ClientState *cs; |
|
1351 |
// invalidate all clients |
|
1352 |
||
1353 |
for(cs=_clients; cs != &_clients[MAX_CLIENTS]; cs++) if (cs->socket != INVALID_SOCKET) { |
|
1354 |
CloseClient(cs); |
|
1355 |
} |
|
1356 |
||
1357 |
if (!client) { |
|
1358 |
// if in servermode --> close listener |
|
1359 |
closesocket(_listensocket); |
|
1360 |
_listensocket= INVALID_SOCKET; |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1361 |
DEBUG(net, 1) ("[NET][TCP] closed listener on port %i", _network_server_port); |
211 | 1362 |
} |
0 | 1363 |
} |
1364 |
||
1365 |
void NetworkShutdown() |
|
1366 |
{ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1367 |
_networking_server = false; |
169
4081f1d1f393
(svn r170) -Fix: [1022902] network fix .. console fix bug [1018523]. Fixes some variable initialization errors and a console.h warning (sign_de)
darkvater
parents:
144
diff
changeset
|
1368 |
_networking = false; |
4081f1d1f393
(svn r170) -Fix: [1022902] network fix .. console fix bug [1018523]. Fixes some variable initialization errors and a console.h warning (sign_de)
darkvater
parents:
144
diff
changeset
|
1369 |
_networking_sync = false; |
4081f1d1f393
(svn r170) -Fix: [1022902] network fix .. console fix bug [1018523]. Fixes some variable initialization errors and a console.h warning (sign_de)
darkvater
parents:
144
diff
changeset
|
1370 |
_frame_counter = 0; |
4081f1d1f393
(svn r170) -Fix: [1022902] network fix .. console fix bug [1018523]. Fixes some variable initialization errors and a console.h warning (sign_de)
darkvater
parents:
144
diff
changeset
|
1371 |
_frame_counter_max = 0; |
4081f1d1f393
(svn r170) -Fix: [1022902] network fix .. console fix bug [1018523]. Fixes some variable initialization errors and a console.h warning (sign_de)
darkvater
parents:
144
diff
changeset
|
1372 |
_frame_counter_srv = 0; |
0 | 1373 |
} |
1374 |
||
1375 |
// switch to synced mode. |
|
105 | 1376 |
void NetworkStartSync(bool fcreset) |
0 | 1377 |
{ |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1378 |
DEBUG(net, 3) ("[NET][SYNC] switching to synced game mode"); |
0 | 1379 |
_networking_sync = true; |
1380 |
_frame_counter = 0; |
|
211 | 1381 |
|
105 | 1382 |
if (fcreset) { |
1383 |
_frame_counter_max = 0; |
|
1384 |
_frame_counter_srv = 0; |
|
208 | 1385 |
_frame_fsync_last = 0; |
105 | 1386 |
} |
0 | 1387 |
_num_future_seed = 0; |
1388 |
_sync_seed_1 = _sync_seed_2 = 0; |
|
1389 |
memset(_my_seed_list, 0, sizeof(_my_seed_list)); |
|
1390 |
} |
|
1391 |
||
1392 |
// ************************** // |
|
1393 |
// * UDP Network Extensions * // |
|
1394 |
// ************************** // |
|
1395 |
||
105 | 1396 |
void NetworkUDPListen(bool client) |
0 | 1397 |
{ |
1398 |
SOCKET udp; |
|
1399 |
struct sockaddr_in sin; |
|
105 | 1400 |
int port; |
0 | 1401 |
|
105 | 1402 |
if (client) { port = _network_client_port; } else { port = _network_server_port; }; |
1403 |
||
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1404 |
DEBUG(net, 1) ("[NET][UDP] listening on port %i", port); |
0 | 1405 |
|
1406 |
udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1407 |
|
105 | 1408 |
// this disables network |
1409 |
_network_available = !(udp == INVALID_SOCKET); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1410 |
|
0 | 1411 |
// set nonblocking mode for socket |
1412 |
{ unsigned long blocking = 1; ioctlsocket(udp, FIONBIO, &blocking); } |
|
1413 |
||
1414 |
sin.sin_family = AF_INET; |
|
1415 |
sin.sin_addr.s_addr = 0; |
|
1416 |
sin.sin_port = htons(port); |
|
1417 |
||
1418 |
if (bind(udp, (struct sockaddr*)&sin, sizeof(sin)) != 0) |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1419 |
DEBUG(net, 1) ("[NET][UDP] error: bind failed on port %i", port); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1420 |
|
0 | 1421 |
|
1422 |
// enable broadcasting |
|
1423 |
{ unsigned long val=1; setsockopt(udp, SOL_SOCKET, SO_BROADCAST, (char *) &val , sizeof(val)); } |
|
105 | 1424 |
// allow reusing |
1425 |
{ unsigned long val=1; setsockopt(udp, SOL_SOCKET, SO_REUSEADDR, (char *) &val , sizeof(val)); } |
|
1426 |
||
1427 |
if (client) { _udp_client_socket = udp; } else { _udp_server_socket = udp; } ; |
|
0 | 1428 |
|
1429 |
} |
|
1430 |
||
211 | 1431 |
void NetworkUDPClose(bool client) |
1432 |
{ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1433 |
if (client) { |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1434 |
DEBUG(net, 1) ("[NET][UDP] closed listener on port %i", _network_client_port); |
105 | 1435 |
closesocket(_udp_client_socket); |
1436 |
_udp_client_socket = INVALID_SOCKET; |
|
1437 |
} else { |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1438 |
DEBUG(net, 1) ("[NET][UDP] closed listener on port %i", _network_server_port); |
105 | 1439 |
closesocket(_udp_server_socket); |
1440 |
_udp_server_socket = INVALID_SOCKET; |
|
1441 |
}; |
|
1442 |
} |
|
1443 |
||
211 | 1444 |
void NetworkUDPReceive(bool client) |
1445 |
{ |
|
0 | 1446 |
struct sockaddr_in client_addr; |
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
|
1447 |
#ifndef __MORPHOS__ |
0 | 1448 |
int client_len; |
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
|
1449 |
#else |
df7c566d219f
(svn r174) -Network: [ 023268] small network.c changes/cleanups (MorphOS/AmigaOS) and one compiler warning fix (MorphOS) (tokai)
darkvater
parents:
172
diff
changeset
|
1450 |
LONG client_len; // for some reason we need a 'LONG' under MorphOS |
df7c566d219f
(svn r174) -Network: [ 023268] small network.c changes/cleanups (MorphOS/AmigaOS) and one compiler warning fix (MorphOS) (tokai)
darkvater
parents:
172
diff
changeset
|
1451 |
#endif |
0 | 1452 |
int nbytes; |
1453 |
struct UDPPacket packet; |
|
1454 |
int packet_len; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1455 |
|
105 | 1456 |
SOCKET udp; |
1457 |
if (client) udp=_udp_client_socket; else udp=_udp_server_socket; |
|
1458 |
||
0 | 1459 |
packet_len = sizeof(packet); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1460 |
client_len = sizeof(client_addr); |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1461 |
|
105 | 1462 |
nbytes = recvfrom(udp, (char *) &packet, packet_len , 0, (struct sockaddr *) &client_addr, &client_len); |
0 | 1463 |
if (nbytes>0) { |
1464 |
if (packet.command_code==packet.command_check) switch (packet.command_code) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1465 |
|
105 | 1466 |
case NET_UDPCMD_SERVERSEARCH: |
1467 |
if (!client) { |
|
1468 |
packet.command_check=packet.command_code=NET_UDPCMD_SERVERINFO; |
|
1469 |
memcpy(&packet.data,&_network_game,sizeof(_network_game)); |
|
1470 |
packet.data_len=sizeof(_network_game); |
|
1471 |
NetworkUDPSend(client,client_addr, packet); |
|
1472 |
} |
|
1473 |
break; |
|
1474 |
case NET_UDPCMD_GETSERVERINFO: |
|
1475 |
if (!client) { |
|
1476 |
packet.command_check=packet.command_code=NET_UDPCMD_SERVERINFO; |
|
1477 |
memcpy(&packet.data,&_network_game,sizeof(_network_game)); |
|
1478 |
packet.data_len=sizeof(_network_game); |
|
1479 |
NetworkUDPSend(client,client_addr, packet); |
|
0 | 1480 |
} |
1481 |
break; |
|
105 | 1482 |
case NET_UDPCMD_SERVERINFO: |
1483 |
if (client) { |
|
1484 |
NetworkGameList * item; |
|
1485 |
||
1486 |
item = (NetworkGameList *) NetworkGameListAdd(); |
|
1487 |
item -> ip = inet_addr(inet_ntoa(client_addr.sin_addr)); |
|
1488 |
item -> port = ntohs(client_addr.sin_port); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1489 |
|
105 | 1490 |
memcpy(item,&packet.data,packet.data_len); |
1491 |
} |
|
1492 |
break; |
|
0 | 1493 |
} |
1494 |
} |
|
1495 |
} |
|
1496 |
||
211 | 1497 |
void NetworkUDPBroadCast(bool client, struct UDPPacket packet) |
1498 |
{ |
|
0 | 1499 |
int i=0, res; |
1500 |
struct sockaddr_in out_addr; |
|
1501 |
uint32 bcaddr; |
|
1502 |
byte * bcptr; |
|
105 | 1503 |
|
1504 |
SOCKET udp; |
|
1505 |
if (client) udp=_udp_client_socket; else udp=_udp_server_socket; |
|
1506 |
||
0 | 1507 |
while (_network_ip_list[i]!=0) { |
1508 |
bcaddr=_network_ip_list[i]; |
|
1509 |
out_addr.sin_family = AF_INET; |
|
105 | 1510 |
if (client) { out_addr.sin_port = htons(_network_server_port); } else { out_addr.sin_port = htons(_network_client_port); }; |
0 | 1511 |
bcptr = (byte *) &bcaddr; |
1512 |
bcptr[3]=255; |
|
1513 |
out_addr.sin_addr.s_addr = bcaddr; |
|
105 | 1514 |
res=sendto(udp,(char *) &packet,sizeof(packet),0,(struct sockaddr *) &out_addr,sizeof(out_addr)); |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1515 |
if (res==-1) DEBUG(net, 1)("udp: broadcast error: %i",GET_LAST_ERROR()); |
0 | 1516 |
i++; |
1517 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1518 |
|
0 | 1519 |
} |
1520 |
||
211 | 1521 |
void NetworkUDPSend(bool client, struct sockaddr_in recv,struct UDPPacket packet) |
1522 |
{ |
|
105 | 1523 |
SOCKET udp; |
1524 |
if (client) udp=_udp_client_socket; else udp=_udp_server_socket; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1525 |
|
105 | 1526 |
sendto(udp,(char *) &packet,sizeof(packet),0,(struct sockaddr *) &recv,sizeof(recv)); |
0 | 1527 |
} |
1528 |
||
105 | 1529 |
|
211 | 1530 |
bool NetworkUDPSearchGame(const byte ** _network_detected_serverip, unsigned short * _network_detected_serverport) |
1531 |
{ |
|
0 | 1532 |
struct UDPPacket packet; |
1533 |
int timeout=3000; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1534 |
|
105 | 1535 |
NetworkGameListClear(); |
1536 |
||
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1537 |
DEBUG(net, 0) ("[NET][UDP] searching server"); |
105 | 1538 |
*_network_detected_serverip = "255.255.255.255"; |
1539 |
*_network_detected_serverport = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1540 |
|
0 | 1541 |
packet.command_check=packet.command_code=NET_UDPCMD_SERVERSEARCH; |
1542 |
packet.data_len=0; |
|
105 | 1543 |
NetworkUDPBroadCast(true, packet); |
0 | 1544 |
while (timeout>=0) { |
1545 |
CSleep(100); |
|
1546 |
timeout-=100; |
|
105 | 1547 |
NetworkUDPReceive(true); |
1548 |
||
1549 |
if (_network_game_count>0) { |
|
1550 |
NetworkGameList * item; |
|
1551 |
item = (NetworkGameList *) NetworkGameListItem(0); |
|
1552 |
*_network_detected_serverip=inet_ntoa(*(struct in_addr *) &item->ip); |
|
1553 |
*_network_detected_serverport=item->port; |
|
1554 |
timeout=-1; |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1555 |
DEBUG(net, 0) ("[NET][UDP] server found on %s", *_network_detected_serverip); |
105 | 1556 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1557 |
|
0 | 1558 |
} |
105 | 1559 |
|
175
d6cccece959a
(svn r176) -Fix: network_gui const and warnings fixes (Tron)
darkvater
parents:
173
diff
changeset
|
1560 |
return (*_network_detected_serverport>0); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1561 |
|
0 | 1562 |
} |
1563 |
||
1564 |
||
105 | 1565 |
// *************************** // |
1566 |
// * New Network Core System * // |
|
1567 |
// *************************** // |
|
1568 |
||
211 | 1569 |
void NetworkIPListInit() |
1570 |
{ |
|
105 | 1571 |
struct hostent* he = NULL; |
1572 |
char hostname[250]; |
|
1573 |
uint32 bcaddr; |
|
1574 |
int i=0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1575 |
|
105 | 1576 |
gethostname(hostname,250); |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1577 |
DEBUG(net, 2) ("[NET][IP] init for host %s", hostname); |
105 | 1578 |
he=gethostbyname((char *) hostname); |
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
106
diff
changeset
|
1579 |
|
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
106
diff
changeset
|
1580 |
if (he == NULL) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
106
diff
changeset
|
1581 |
he = gethostbyname("localhost"); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
106
diff
changeset
|
1582 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1583 |
|
110
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
106
diff
changeset
|
1584 |
if (he == NULL) { |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
106
diff
changeset
|
1585 |
bcaddr = inet_addr("127.0.0.1"); |
a22a6b07904b
(svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents:
106
diff
changeset
|
1586 |
he = gethostbyaddr(inet_ntoa(*(struct in_addr *) &bcaddr), sizeof(bcaddr), AF_INET); |
105 | 1587 |
} |
1588 |
||
1589 |
if (he == NULL) { |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1590 |
DEBUG(net, 2) ("[NET][IP] cannot resolve %s", hostname); |
105 | 1591 |
} else { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1592 |
while(he->h_addr_list[i]) { |
105 | 1593 |
bcaddr = inet_addr(inet_ntoa(*(struct in_addr *) he->h_addr_list[i])); |
1594 |
_network_ip_list[i]=bcaddr; |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1595 |
DEBUG(net, 2) ("[NET][IP] add %s",inet_ntoa(*(struct in_addr *) he->h_addr_list[i])); |
105 | 1596 |
i++; |
1597 |
} |
|
1598 |
||
1599 |
} |
|
1600 |
_network_ip_list[i]=0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1601 |
|
105 | 1602 |
} |
1603 |
||
1604 |
/* *************************************************** */ |
|
1605 |
||
211 | 1606 |
void NetworkCoreInit() |
105 | 1607 |
{ |
211 | 1608 |
DEBUG(net, 3) ("[NET][Core] init()"); |
220
dc1710f96b39
(svn r221) -Feature: console command and variable hooking
signde
parents:
218
diff
changeset
|
1609 |
_network_available = true; |
dc1710f96b39
(svn r221) -Feature: console command and variable hooking
signde
parents:
218
diff
changeset
|
1610 |
_network_client_timeout = 300; |
dc1710f96b39
(svn r221) -Feature: console command and variable hooking
signde
parents:
218
diff
changeset
|
1611 |
_network_ready_ahead = 1; |
105 | 1612 |
|
211 | 1613 |
// [win32] winsock startup |
105 | 1614 |
|
211 | 1615 |
#if defined(WIN32) |
1616 |
{ |
|
1617 |
WSADATA wsa; |
|
1618 |
DEBUG(net, 3) ("[NET][Core] using windows socket library"); |
|
1619 |
if (WSAStartup(MAKEWORD(2,0), &wsa) != 0) { |
|
1620 |
DEBUG(net, 3) ("[NET][Core] error: WSAStartup failed"); |
|
1621 |
_network_available=false; |
|
1622 |
} |
|
1623 |
} |
|
1624 |
#else |
|
144
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1625 |
|
211 | 1626 |
// [morphos/amigaos] bsd-socket startup |
1627 |
||
1628 |
#if defined(__MORPHOS__) || defined(__AMIGA__) |
|
1629 |
{ |
|
1630 |
DEBUG(misc,3) ("[NET][Core] using bsd socket library"); |
|
1631 |
if (!(SocketBase = OpenLibrary("bsdsocket.library", 4))) { |
|
1632 |
DEBUG(net, 3) ("[NET][Core] Couldn't open bsdsocket.library version 4."); |
|
1633 |
_network_available=false; |
|
1634 |
} |
|
1635 |
||
1636 |
#if !defined(__MORPHOS__) |
|
1637 |
// for usleep() implementation (only required for legacy AmigaOS builds) |
|
1638 |
if ( (TimerPort = CreateMsgPort()) ) { |
|
1639 |
if ( (TimerRequest = (struct timerequest *) CreateIORequest(TimerPort, sizeof(struct timerequest))) ) { |
|
1640 |
if ( OpenDevice("timer.device", UNIT_MICROHZ, (struct IORequest *) TimerRequest, 0) == 0 ) { |
|
1641 |
if ( !(TimerBase = TimerRequest->tr_node.io_Device) ) { |
|
1642 |
// free ressources... |
|
1643 |
DEBUG(net, 3) ("[NET][Core] Couldn't initialize timer."); |
|
1644 |
_network_available=false; |
|
1645 |
} |
|
144
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1646 |
} |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1647 |
} |
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1648 |
} |
211 | 1649 |
#endif |
1650 |
||
1651 |
} |
|
1652 |
#else |
|
1653 |
||
1654 |
// [linux/macos] unix-socket startup |
|
1655 |
||
1656 |
DEBUG(net, 3) ("[NET][Core] using unix socket library"); |
|
1657 |
||
1658 |
#endif |
|
1659 |
||
1660 |
#endif |
|
1661 |
||
1662 |
||
1663 |
if (_network_available) { |
|
1664 |
DEBUG(net, 3) ("[NET][Core] OK: multiplayer available"); |
|
1665 |
// initiate network ip list |
|
1666 |
NetworkIPListInit(); |
|
1667 |
} else |
|
1668 |
DEBUG(net, 3) ("[NET][Core] FAILED: multiplayer not available"); |
|
1669 |
} |
|
1670 |
||
1671 |
/* *************************************************** */ |
|
1672 |
||
1673 |
void NetworkCoreShutdown() |
|
1674 |
{ |
|
1675 |
DEBUG(net, 3) ("[NET][Core] shutdown()"); |
|
1676 |
||
1677 |
#if defined(__MORPHOS__) || defined(__AMIGA__) |
|
1678 |
{ |
|
1679 |
// free allocated ressources |
|
1680 |
#if !defined(__MORPHOS__) |
|
1681 |
if (TimerBase) { CloseDevice((struct IORequest *) TimerRequest); } |
|
1682 |
if (TimerRequest) { DeleteIORequest(TimerRequest); } |
|
1683 |
if (TimerPort) { DeleteMsgPort(TimerPort); } |
|
1684 |
#endif |
|
1685 |
||
1686 |
if (SocketBase) { |
|
1687 |
CloseLibrary(SocketBase); |
|
1688 |
} |
|
144
6b42494d405e
(svn r145) -Fix: [1016921] Network Name Resolution for not only connecting with IP. (sign_de)
darkvater
parents:
110
diff
changeset
|
1689 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1690 |
#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
|
1691 |
|
211 | 1692 |
#if defined(WIN32) |
1693 |
{ WSACleanup();} |
|
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
|
1694 |
#endif |
105 | 1695 |
} |
1696 |
||
1697 |
/* *************************************************** */ |
|
1698 |
||
228
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1699 |
void ParseConnectionString(const byte **player, const byte **port, byte *connection_string) |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1700 |
{ |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1701 |
byte c = 0; |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1702 |
while (connection_string[c] != '\0') { |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1703 |
if (connection_string[c] == '#') { |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1704 |
*player = &connection_string[c+1]; |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1705 |
connection_string[c] = '\0'; |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1706 |
} |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1707 |
if (connection_string[c] == ':') { |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1708 |
*port = &connection_string[c+1]; |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1709 |
connection_string[c] = '\0'; |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1710 |
} |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1711 |
c++; |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1712 |
} |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1713 |
} |
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1714 |
|
175
d6cccece959a
(svn r176) -Fix: network_gui const and warnings fixes (Tron)
darkvater
parents:
173
diff
changeset
|
1715 |
bool NetworkCoreConnectGame(const byte* b, unsigned short port) |
105 | 1716 |
{ |
1717 |
if (!_network_available) return false; |
|
1718 |
||
175
d6cccece959a
(svn r176) -Fix: network_gui const and warnings fixes (Tron)
darkvater
parents:
173
diff
changeset
|
1719 |
if (strcmp(b,"auto")==0) { |
105 | 1720 |
// do autodetect |
1721 |
NetworkUDPSearchGame(&b, &port); |
|
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
1722 |
} |
105 | 1723 |
|
1724 |
if (port==0) { |
|
1725 |
// autodetection failed |
|
1726 |
if (_networking_override) NetworkLobbyShutdown(); |
|
1727 |
ShowErrorMessage(-1, STR_NETWORK_ERR_NOSERVER, 0, 0); |
|
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
1728 |
_switch_mode_errorstr = STR_NETWORK_ERR_NOSERVER; |
105 | 1729 |
return false; |
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
1730 |
} |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
1731 |
|
105 | 1732 |
NetworkInitialize(); |
1733 |
_networking = NetworkConnect(b, port); |
|
1734 |
if (_networking) { |
|
1735 |
NetworkLobbyShutdown(); |
|
247
1cbc32ff06eb
(svn r248) -Feature: console script files "exec myscript.file"
darkvater
parents:
239
diff
changeset
|
1736 |
IConsoleCmdExec("exec scripts/on_client.scr 0"); |
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
1737 |
} else { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1738 |
if (_networking_override) |
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
1739 |
NetworkLobbyShutdown(); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1740 |
|
105 | 1741 |
ShowErrorMessage(-1, STR_NETWORK_ERR_NOCONNECTION,0,0); |
172
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
1742 |
_switch_mode_errorstr = STR_NETWORK_ERR_NOCONNECTION; |
8d8b3383470d
(svn r173) -Network: [1023231] Debug Code + ErrorBoxes + Load Game/Scenario. Added some more desync debug code and replaced some of the error() calls with some better error boxes. Hopefully find desyncs easier (sign_de)
darkvater
parents:
169
diff
changeset
|
1743 |
} |
105 | 1744 |
return _networking; |
1745 |
} |
|
1746 |
||
1747 |
/* *************************************************** */ |
|
1748 |
||
211 | 1749 |
bool NetworkCoreConnectGameStruct(NetworkGameList * item) |
1750 |
{ |
|
1751 |
return NetworkCoreConnectGame(inet_ntoa(*(struct in_addr *) &item->ip),item->port); |
|
1752 |
} |
|
1753 |
||
1754 |
/* *************************************************** */ |
|
1755 |
||
105 | 1756 |
bool NetworkCoreStartGame() |
1757 |
{ |
|
1758 |
if (!_network_available) return false; |
|
1759 |
NetworkLobbyShutdown(); |
|
1760 |
NetworkInitialize(); |
|
1761 |
NetworkListen(); |
|
1762 |
NetworkUDPListen(false); |
|
1763 |
_networking_server = true; |
|
1764 |
_networking = true; |
|
1765 |
NetworkGameFillDefaults(); // clears the network game info |
|
1766 |
_network_game.players_on++; // the serverplayer is online |
|
247
1cbc32ff06eb
(svn r248) -Feature: console script files "exec myscript.file"
darkvater
parents:
239
diff
changeset
|
1767 |
// execute server initialization script |
1cbc32ff06eb
(svn r248) -Feature: console script files "exec myscript.file"
darkvater
parents:
239
diff
changeset
|
1768 |
IConsoleCmdExec("exec scripts/on_server.scr 0"); |
105 | 1769 |
return true; |
1770 |
} |
|
1771 |
||
1772 |
/* *************************************************** */ |
|
1773 |
||
1774 |
void NetworkCoreDisconnect() |
|
1775 |
{ |
|
1776 |
/* terminate server */ |
|
1777 |
if (_networking_server) { |
|
1778 |
NetworkUDPClose(false); |
|
1779 |
NetworkClose(false); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1780 |
} |
105 | 1781 |
|
1782 |
/* terminate client connection */ |
|
1783 |
else if (_networking) { |
|
1784 |
NetworkClose(true); |
|
1785 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1786 |
|
105 | 1787 |
NetworkShutdown(); |
1788 |
} |
|
1789 |
||
1790 |
/* *************************************************** */ |
|
1791 |
||
211 | 1792 |
void NetworkCoreLoop(bool incomming) |
1793 |
{ |
|
1794 |
if (incomming) { |
|
1795 |
// incomming |
|
1796 |
if ( _udp_client_socket != INVALID_SOCKET ) NetworkUDPReceive(true); |
|
1797 |
if ( _udp_server_socket != INVALID_SOCKET ) NetworkUDPReceive(false); |
|
105 | 1798 |
|
211 | 1799 |
if (_networking) |
1800 |
NetworkReceive(); |
|
105 | 1801 |
|
1802 |
} else { |
|
211 | 1803 |
if ( _udp_client_socket != INVALID_SOCKET ) NetworkUDPReceive(true); |
1804 |
if ( _udp_server_socket != INVALID_SOCKET ) NetworkUDPReceive(false); |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1805 |
|
211 | 1806 |
if (_networking) |
1807 |
NetworkSend(); |
|
105 | 1808 |
} |
1809 |
} |
|
1810 |
||
211 | 1811 |
void NetworkLobbyInit() |
1812 |
{ |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1813 |
DEBUG(net, 3) ("[NET][Lobby] init()"); |
105 | 1814 |
NetworkUDPListen(true); |
1815 |
} |
|
1816 |
||
211 | 1817 |
void NetworkLobbyShutdown() |
1818 |
{ |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1819 |
DEBUG(net, 3) ("[NET][Lobby] shutdown()"); |
105 | 1820 |
NetworkUDPClose(true); |
1821 |
} |
|
1822 |
||
1823 |
||
1824 |
// ******************************** // |
|
1825 |
// * Network Game List Extensions * // |
|
1826 |
// ******************************** // |
|
1827 |
||
211 | 1828 |
void NetworkGameListClear() |
1829 |
{ |
|
1830 |
NetworkGameList * item; |
|
1831 |
NetworkGameList * next; |
|
105 | 1832 |
|
211 | 1833 |
DEBUG(net, 4) ("[NET][G-List] cleared server list"); |
1834 |
||
1835 |
item = _network_game_list; |
|
1836 |
||
1837 |
while (item != NULL) { |
|
1838 |
next = (NetworkGameList *) item -> _next; |
|
1839 |
free (item); |
|
1840 |
item = next; |
|
105 | 1841 |
} |
211 | 1842 |
_network_game_list=NULL; |
1843 |
_network_game_count=0; |
|
105 | 1844 |
} |
1845 |
||
211 | 1846 |
NetworkGameList * NetworkGameListAdd() |
1847 |
{ |
|
1848 |
NetworkGameList * item; |
|
1849 |
NetworkGameList * before; |
|
105 | 1850 |
|
211 | 1851 |
DEBUG(net, 4) ("[NET][G-List] added server to list"); |
105 | 1852 |
|
211 | 1853 |
item = _network_game_list; |
105 | 1854 |
before = item; |
211 | 1855 |
while (item != NULL) { |
1856 |
before = item; |
|
1857 |
item = (NetworkGameList *) item -> _next; |
|
105 | 1858 |
} |
211 | 1859 |
|
1860 |
item = malloc(sizeof(NetworkGameList)); |
|
1861 |
item -> _next = NULL; |
|
1862 |
||
1863 |
if (before == NULL) { |
|
1864 |
_network_game_list = item; |
|
1865 |
} else |
|
1866 |
before -> _next = item; |
|
1867 |
||
1868 |
_network_game_count++; |
|
1869 |
return item; |
|
105 | 1870 |
} |
1871 |
||
211 | 1872 |
void NetworkGameListFromLAN() |
1873 |
{ |
|
105 | 1874 |
struct UDPPacket packet; |
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1875 |
DEBUG(net, 2) ("[NET][G-List] searching server over lan"); |
105 | 1876 |
NetworkGameListClear(); |
1877 |
packet.command_check=packet.command_code=NET_UDPCMD_SERVERSEARCH; |
|
1878 |
packet.data_len=0; |
|
1879 |
NetworkUDPBroadCast(true,packet); |
|
1880 |
} |
|
1881 |
||
211 | 1882 |
void NetworkGameListFromInternet() |
1883 |
{ |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1884 |
DEBUG(net, 2) ("[NET][G-List] searching servers over internet"); |
105 | 1885 |
NetworkGameListClear(); |
1886 |
||
1887 |
// **TODO** masterserver communication [internet protocol list] |
|
1888 |
} |
|
1889 |
||
211 | 1890 |
NetworkGameList * NetworkGameListItem(uint16 index) |
1891 |
{ |
|
1892 |
NetworkGameList * item; |
|
1893 |
NetworkGameList * next; |
|
1894 |
uint16 cnt = 0; |
|
105 | 1895 |
|
211 | 1896 |
item = _network_game_list; |
105 | 1897 |
|
211 | 1898 |
while ((item != NULL) && (cnt != index)) { |
1899 |
next = (NetworkGameList *) item -> _next; |
|
1900 |
item = next; |
|
1901 |
cnt++; |
|
105 | 1902 |
} |
1903 |
||
211 | 1904 |
return item; |
105 | 1905 |
} |
1906 |
||
1907 |
// *************************** // |
|
1908 |
// * Network Game Extensions * // |
|
1909 |
// *************************** // |
|
1910 |
||
211 | 1911 |
void NetworkGameFillDefaults() |
1912 |
{ |
|
105 | 1913 |
NetworkGameInfo * game = &_network_game; |
1914 |
#if defined(WITH_REV) |
|
1915 |
extern char _openttd_revision[]; |
|
1916 |
#endif |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1917 |
|
185
646403e35006
(svn r186) -Add: debug 'net' for network debug (sign_de)
truelight
parents:
177
diff
changeset
|
1918 |
DEBUG(net, 4) ("[NET][G-Info] setting defaults"); |
105 | 1919 |
|
1920 |
ttd_strlcpy(game->server_name,"OpenTTD Game",13); |
|
1921 |
game->game_password[0]='\0'; |
|
1922 |
game->map_name[0]='\0'; |
|
1923 |
#if defined(WITH_REV) |
|
1924 |
ttd_strlcpy(game->server_revision,_openttd_revision,strlen(_openttd_revision)); |
|
1925 |
#else |
|
1926 |
ttd_strlcpy(game->server_revision,"norev000",strlen("norev000")); |
|
1927 |
#endif |
|
1928 |
game->game_date=0; |
|
1929 |
||
1930 |
game->map_height=0; |
|
1931 |
game->map_width=0; |
|
1932 |
game->map_set=0; |
|
1933 |
||
1934 |
game->players_max=8; |
|
1935 |
game->players_on=0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
188
diff
changeset
|
1936 |
|
105 | 1937 |
game->server_lang=_dynlang.curr; |
1938 |
} |
|
1939 |
||
211 | 1940 |
void NetworkGameChangeDate(uint16 newdate) |
1941 |
{ |
|
1942 |
if (_networking_server) |
|
105 | 1943 |
_network_game.game_date = newdate; |
1944 |
} |
|
1945 |
||
8
9ab81ef450f2
(svn r9) Fixed a couple of warnings and minor coding issues
dominik
parents:
1
diff
changeset
|
1946 |
#else // not ENABLE_NETWORK |
0 | 1947 |
|
1948 |
// stubs |
|
105 | 1949 |
void NetworkInitialize() {} |
0 | 1950 |
void NetworkShutdown() {} |
105 | 1951 |
void NetworkListen() {} |
0 | 1952 |
void NetworkConnect(const char *hostname, int port) {} |
1953 |
void NetworkReceive() {} |
|
1954 |
void NetworkSend() {} |
|
1955 |
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) {} |
|
239 | 1956 |
void NetworkSendEvent(uint16 type, uint16 data_len, void * data) {}; |
0 | 1957 |
void NetworkProcessCommands() {} |
106
51354fbb3e8a
(svn r107) Small fix for compiling without networking
dominik
parents:
105
diff
changeset
|
1958 |
void NetworkStartSync(bool fcreset) {} |
208 | 1959 |
void NetworkSendReadyPacket() {} |
1960 |
void NetworkSendSyncPackets() {} |
|
1961 |
void NetworkSendFrameSyncPackets() {} |
|
1962 |
bool NetworkCheckClientReady() { return true; } |
|
105 | 1963 |
void NetworkCoreInit() { _network_available=false; }; |
1964 |
void NetworkCoreShutdown() {}; |
|
1965 |
void NetworkCoreDisconnect() {}; |
|
1966 |
void NetworkCoreLoop(bool incomming) {}; |
|
228
f65dec6727d9
(svn r229) -Fix: Some more const stuff fixed .(Tron)
darkvater
parents:
222
diff
changeset
|
1967 |
void ParseConnectionString(const byte **player, const byte **port, byte *connection_string) {}; |
177
71fec20dd4a8
(svn r178) -Fix: small network fix if network is not enabled
darkvater
parents:
175
diff
changeset
|
1968 |
bool NetworkCoreConnectGame(const byte* b, unsigned short port) {return false;}; |
106
51354fbb3e8a
(svn r107) Small fix for compiling without networking
dominik
parents:
105
diff
changeset
|
1969 |
bool NetworkCoreStartGame() {return false;}; |
105 | 1970 |
void NetworkLobbyShutdown() {}; |
1971 |
void NetworkLobbyInit() {}; |
|
1972 |
void NetworkGameListClear() {}; |
|
218 | 1973 |
NetworkGameList * NetworkGameListAdd() {return NULL;}; |
105 | 1974 |
void NetworkGameListFromLAN() {}; |
1975 |
void NetworkGameListFromInternet() {}; |
|
1976 |
void NetworkGameFillDefaults() {}; |
|
218 | 1977 |
NetworkGameList * NetworkGameListItem(uint16 index) {return NULL;}; |
220
dc1710f96b39
(svn r221) -Feature: console command and variable hooking
signde
parents:
218
diff
changeset
|
1978 |
bool NetworkCoreConnectGameStruct(NetworkGameList * item) {return false;}; |
105 | 1979 |
void NetworkGameChangeDate(uint16 newdate) {}; |
1980 |
||
1981 |
#endif |