author | dominik |
Sun, 22 Aug 2004 10:23:37 +0000 | |
changeset 105 | 44e894da0fef |
parent 81 | 1759d41e3d43 |
child 106 | 51354fbb3e8a |
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" |
|
6 |
||
7 |
#if defined(WIN32) |
|
8 |
# include <windows.h> |
|
9 |
# include <winsock.h> |
|
10 |
||
11 |
# pragma comment (lib, "ws2_32.lib") |
|
12 |
# define ENABLE_NETWORK |
|
13 |
# define GET_LAST_ERROR() WSAGetLastError() |
|
14 |
# define EWOULDBLOCK WSAEWOULDBLOCK |
|
15 |
#endif |
|
16 |
||
17 |
#if defined(UNIX) |
|
18 |
// Make compatible with WIN32 names |
|
19 |
# define SOCKET int |
|
20 |
# define INVALID_SOCKET -1 |
|
21 |
// we need different defines for MorphOS and AmigaOS |
|
22 |
#if !defined(__MORPHOS__) && !defined(__AMIGA__) |
|
23 |
# define ioctlsocket ioctl |
|
24 |
# define closesocket close |
|
25 |
# define GET_LAST_ERROR() errno |
|
26 |
#endif |
|
27 |
// Need this for FIONREAD on solaris |
|
28 |
# define BSD_COMP |
|
29 |
# include <unistd.h> |
|
30 |
# include <sys/ioctl.h> |
|
31 |
||
32 |
// Socket stuff |
|
33 |
# include <sys/socket.h> |
|
34 |
# include <netinet/in.h> |
|
35 |
# include <arpa/inet.h> |
|
36 |
# include <errno.h> |
|
37 |
# include <sys/time.h> |
|
38 |
// NetDB |
|
39 |
# include <netdb.h> |
|
40 |
||
41 |
# ifndef TCP_NODELAY |
|
42 |
# define TCP_NODELAY 0x0001 |
|
43 |
# endif |
|
44 |
||
45 |
#endif |
|
46 |
||
47 |
||
48 |
#if defined(__MORPHOS__) || defined(__AMIGA__) |
|
49 |
# include <exec/types.h> |
|
50 |
# include <proto/exec.h> // required for Open/CloseLibrary() |
|
51 |
# if defined(__MORPHOS__) |
|
52 |
# include <sys/filio.h> // FION#? defines |
|
53 |
# else // __AMIGA__ |
|
54 |
# include <proto/socket.h> |
|
55 |
# endif |
|
56 |
||
57 |
// make source compatible with bsdsocket.library functions |
|
58 |
# define closesocket(s) CloseSocket(s) |
|
59 |
# define GET_LAST_ERROR() Errno() |
|
60 |
# define ioctlsocket(s,request,status) IoctlSocket((LONG)s,(ULONG)request,(char*)status) |
|
61 |
||
62 |
struct Library *SocketBase = NULL; |
|
63 |
#endif /* __MORPHOS__ || __AMIGA__ */ |
|
64 |
||
65 |
||
66 |
#define SEND_MTU 1460 |
|
67 |
||
68 |
#if defined(ENABLE_NETWORK) |
|
69 |
||
70 |
// sent from client -> server whenever the client wants to exec a command. |
|
71 |
// send from server -> client when another player execs a command. |
|
72 |
typedef struct CommandPacket { |
|
73 |
byte packet_length; |
|
74 |
byte packet_type; |
|
75 |
uint16 cmd; |
|
76 |
uint32 p1,p2; |
|
77 |
TileIndex tile; |
|
78 |
byte player;// player id, this is checked by the server. |
|
79 |
byte when; // offset from the current max_frame value minus 1. this is set by the server. |
|
80 |
uint32 dp[8]; |
|
81 |
} CommandPacket; |
|
82 |
||
83 |
assert_compile(sizeof(CommandPacket) == 16 + 32); |
|
84 |
||
85 |
#define COMMAND_PACKET_BASE_SIZE (sizeof(CommandPacket) - 8 * sizeof(uint32)) |
|
86 |
||
87 |
// sent from server -> client periodically to tell the client about the current tick in the server |
|
88 |
// and how far the client may progress. |
|
89 |
typedef struct SyncPacket { |
|
90 |
byte packet_length; |
|
91 |
byte packet_type; |
|
92 |
byte frames; // how many more frames may the client execute? this is relative to the old value of max. |
|
93 |
byte server; // where is the server currently executing? this is negatively relative to the old value of max. |
|
94 |
uint32 random_seed_1; // current random state at server. used to detect out of sync. |
|
95 |
uint32 random_seed_2; |
|
96 |
} SyncPacket; |
|
97 |
||
98 |
assert_compile(sizeof(SyncPacket) == 12); |
|
99 |
||
100 |
// sent from server -> client as an acknowledgement that the server received the command. |
|
101 |
// the command will be executed at the current value of "max". |
|
102 |
typedef struct AckPacket { |
|
103 |
byte packet_length; |
|
104 |
byte packet_type; |
|
105 |
} AckPacket; |
|
106 |
||
107 |
typedef struct FilePacketHdr { |
|
108 |
byte packet_length; |
|
109 |
byte packet_type; |
|
110 |
byte unused[2]; |
|
111 |
} FilePacketHdr; |
|
112 |
||
113 |
assert_compile(sizeof(FilePacketHdr) == 4); |
|
114 |
||
115 |
// sent from server to client when the client has joined. |
|
116 |
typedef struct WelcomePacket { |
|
117 |
byte packet_length; |
|
118 |
byte packet_type; |
|
119 |
byte unused[2]; |
|
120 |
} WelcomePacket; |
|
121 |
||
122 |
typedef struct Packet Packet; |
|
123 |
struct Packet { |
|
124 |
Packet *next; // this one has to be the first element. |
|
125 |
uint siz; |
|
126 |
byte buf[SEND_MTU]; // packet payload |
|
127 |
}; |
|
128 |
||
129 |
typedef struct ClientState { |
|
130 |
int socket; |
|
131 |
bool inactive; // disable sending of commands/syncs to client |
|
132 |
bool writable; |
|
133 |
uint xmitpos; |
|
134 |
||
135 |
uint eaten; |
|
136 |
Packet *head, **last; |
|
137 |
||
138 |
uint buflen; // receive buffer len |
|
139 |
byte buf[256]; // receive buffer |
|
140 |
} ClientState; |
|
141 |
||
142 |
||
143 |
static uint _not_packet; |
|
144 |
||
145 |
typedef struct QueuedCommand QueuedCommand; |
|
146 |
struct QueuedCommand { |
|
147 |
QueuedCommand *next; |
|
148 |
CommandPacket cp; |
|
149 |
CommandCallback *callback; |
|
150 |
uint32 cmd; |
|
151 |
int32 frame; |
|
152 |
}; |
|
153 |
||
154 |
typedef struct CommandQueue CommandQueue; |
|
155 |
struct CommandQueue { |
|
156 |
QueuedCommand *head, **last; |
|
157 |
}; |
|
158 |
||
159 |
#define MAX_CLIENTS (MAX_PLAYERS + 1) |
|
160 |
||
161 |
// packets waiting to be executed, for each of the players. |
|
162 |
// this list is sorted in frame order, so the item on the front will be executed first. |
|
163 |
static CommandQueue _command_queue; |
|
164 |
||
165 |
// in the client, this is the list of commands that have not yet been acked. |
|
166 |
// when it is acked, it will be moved to the appropriate position at the end of the player queue. |
|
167 |
static CommandQueue _ack_queue; |
|
168 |
||
169 |
static ClientState _clients[MAX_CLIENTS]; |
|
170 |
static int _num_clients; |
|
171 |
||
172 |
// keep a history of the 16 most recent seeds to be able to capture out of sync errors. |
|
173 |
static uint32 _my_seed_list[16][2]; |
|
174 |
||
175 |
typedef struct FutureSeeds { |
|
176 |
int32 frame; |
|
177 |
uint32 seed[2]; |
|
178 |
} FutureSeeds; |
|
179 |
||
180 |
// remember some future seeds that the server sent to us. |
|
181 |
static FutureSeeds _future_seed[8]; |
|
182 |
static int _num_future_seed; |
|
183 |
||
105 | 184 |
static SOCKET _listensocket; // tcp socket |
0 | 185 |
|
105 | 186 |
static SOCKET _udp_client_socket; // udp server socket |
187 |
static SOCKET _udp_server_socket; // udp client socket |
|
0 | 188 |
|
189 |
typedef struct UDPPacket { |
|
190 |
byte command_code; |
|
191 |
byte data_len; |
|
192 |
byte command_check; |
|
193 |
byte data[255]; |
|
194 |
} UDPPacket; |
|
195 |
||
196 |
enum { |
|
197 |
NET_UDPCMD_SERVERSEARCH = 1, |
|
198 |
NET_UDPCMD_SERVERACTIVE, |
|
105 | 199 |
NET_UDPCMD_GETSERVERINFO, |
200 |
NET_UDPCMD_SERVERINFO, |
|
0 | 201 |
}; |
202 |
||
105 | 203 |
void NetworkUDPSend(bool client, struct sockaddr_in recv,struct UDPPacket packet); |
0 | 204 |
|
105 | 205 |
uint32 _network_ip_list[10]; // network ip list |
0 | 206 |
|
207 |
// this is set to point to the savegame |
|
208 |
static byte *_transmit_file; |
|
209 |
static size_t _transmit_file_size; |
|
210 |
||
211 |
static FILE *_recv_file; |
|
212 |
||
105 | 213 |
typedef struct NetworkGameInfo { |
214 |
char server_name[40]; // name of the game |
|
215 |
char server_revision[8]; // server game version |
|
216 |
byte server_lang; // langid |
|
217 |
byte players_max; // max players allowed on server |
|
218 |
byte players_on; // current count of players on server |
|
219 |
uint16 game_date; // current date |
|
220 |
char game_password[10]; // should fit ... 14 chars |
|
221 |
char map_name[40]; // map which is played ["random" for a randomized map] |
|
222 |
uint map_width; // map width / 8 |
|
223 |
uint map_height; // map height / 8 |
|
224 |
byte map_set; // graphical set |
|
225 |
} NetworkGameInfo; |
|
226 |
||
227 |
typedef struct NetworkGameList { |
|
228 |
NetworkGameInfo item; |
|
229 |
uint32 ip; |
|
230 |
uint16 port; |
|
231 |
char * _next; |
|
232 |
} NetworkGameList; |
|
233 |
||
234 |
static NetworkGameInfo _network_game; |
|
235 |
static NetworkGameList * _network_game_list = NULL; |
|
236 |
||
237 |
/* multi os compatible sleep function */ |
|
238 |
void CSleep(int milliseconds) { |
|
239 |
#if defined(WIN32) |
|
240 |
Sleep(milliseconds); |
|
241 |
#endif |
|
242 |
#if defined(UNIX) |
|
243 |
#ifndef __BEOS__ |
|
244 |
usleep(milliseconds*1000); |
|
245 |
#endif |
|
246 |
#ifdef __BEOS__ |
|
247 |
snooze(milliseconds*1000); |
|
248 |
#endif |
|
249 |
#endif |
|
250 |
} |
|
251 |
||
0 | 252 |
////////////////////////////////////////////////////////////////////// |
253 |
||
105 | 254 |
// ****************************** // |
255 |
// * TCP Packets and Handlers * // |
|
256 |
// ****************************** // |
|
257 |
||
0 | 258 |
static QueuedCommand *AllocQueuedCommand(CommandQueue *nq) |
259 |
{ |
|
260 |
QueuedCommand *qp = (QueuedCommand*)calloc(sizeof(QueuedCommand), 1); |
|
261 |
assert(qp); |
|
262 |
*nq->last = qp; |
|
263 |
nq->last = &qp->next; |
|
264 |
return qp; |
|
265 |
} |
|
266 |
||
267 |
// go through the player queues for each player and see if there are any pending commands |
|
268 |
// that should be executed this frame. if there are, execute them. |
|
269 |
void NetworkProcessCommands() |
|
270 |
{ |
|
271 |
CommandQueue *nq; |
|
272 |
QueuedCommand *qp; |
|
273 |
||
274 |
// queue mode ? |
|
275 |
if (_networking_queuing) |
|
276 |
return; |
|
277 |
||
278 |
nq = &_command_queue; |
|
279 |
while ( (qp=nq->head) && (!_networking_sync || qp->frame <= _frame_counter)) { |
|
280 |
// unlink it. |
|
281 |
if (!(nq->head = qp->next)) nq->last = &nq->head; |
|
282 |
||
283 |
if (qp->frame < _frame_counter && _networking_sync) { |
|
284 |
error("!qp->cp.frame < _frame_counter, %d < %d\n", qp->frame, _frame_counter); |
|
285 |
} |
|
286 |
||
287 |
// run the command |
|
288 |
_current_player = qp->cp.player; |
|
289 |
memcpy(_decode_parameters, qp->cp.dp, (qp->cp.packet_length - COMMAND_PACKET_BASE_SIZE)); |
|
290 |
DoCommandP(qp->cp.tile, qp->cp.p1, qp->cp.p2, qp->callback, qp->cmd | CMD_DONT_NETWORK); |
|
291 |
free(qp); |
|
292 |
} |
|
293 |
||
294 |
if (!_networking_server) { |
|
295 |
// remember the random seed so we can check if we're out of sync. |
|
296 |
_my_seed_list[_frame_counter & 15][0] = _sync_seed_1; |
|
297 |
_my_seed_list[_frame_counter & 15][1] = _sync_seed_2; |
|
298 |
||
299 |
while (_num_future_seed) { |
|
300 |
assert(_future_seed[0].frame >= _frame_counter); |
|
301 |
if (_future_seed[0].frame != _frame_counter) break; |
|
302 |
if (_future_seed[0].seed[0] != _sync_seed_1 ||_future_seed[0].seed[1] != _sync_seed_2) |
|
303 |
error("!network sync error"); |
|
304 |
memcpy_overlapping(_future_seed, _future_seed + 1, --_num_future_seed * sizeof(FutureSeeds)); |
|
305 |
} |
|
306 |
} |
|
307 |
} |
|
308 |
||
309 |
// send a packet to a client |
|
310 |
static void SendBytes(ClientState *cs, void *bytes, uint len) |
|
311 |
{ |
|
312 |
byte *b = (byte*)bytes; |
|
313 |
uint n; |
|
314 |
Packet *p; |
|
315 |
||
316 |
assert(len != 0); |
|
317 |
||
318 |
// see if there's space in the last packet? |
|
319 |
if (!cs->head || (p = (Packet*)cs->last, p->siz == sizeof(p->buf))) |
|
320 |
p = NULL; |
|
321 |
||
322 |
do { |
|
323 |
if (!p) { |
|
324 |
// need to allocate a new packet buffer. |
|
325 |
p = (Packet*)malloc(sizeof(Packet)); |
|
326 |
||
327 |
// insert at the end of the linked list. |
|
328 |
*cs->last = p; |
|
329 |
cs->last = &p->next; |
|
330 |
p->next = NULL; |
|
331 |
p->siz = 0; |
|
332 |
} |
|
333 |
||
334 |
// copy bytes to packet. |
|
335 |
n = minu(sizeof(p->buf) - p->siz, len); |
|
336 |
memcpy(p->buf + p->siz, b, n); |
|
337 |
p->siz += n; |
|
338 |
b += n; |
|
339 |
p = NULL; |
|
340 |
} while (len -= n); |
|
341 |
} |
|
342 |
||
343 |
// client: |
|
344 |
// add it to the client's ack queue, and send the command to the server |
|
345 |
// server: |
|
346 |
// add it to the server's player queue, and send it to all clients. |
|
347 |
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) |
|
348 |
{ |
|
349 |
int nump; |
|
350 |
QueuedCommand *qp; |
|
351 |
ClientState *cs; |
|
352 |
||
353 |
qp = AllocQueuedCommand(_networking_server ? &_command_queue : &_ack_queue); |
|
354 |
qp->cp.packet_type = 0; |
|
355 |
qp->cp.tile = tile; |
|
356 |
qp->cp.p1 = p1; |
|
357 |
qp->cp.p2 = p2; |
|
358 |
qp->cp.cmd = (uint16)cmd; |
|
359 |
qp->cp.player = _local_player; |
|
360 |
qp->cp.when = 0; |
|
361 |
qp->cmd = cmd; |
|
362 |
qp->callback = callback; |
|
363 |
||
364 |
// so the server knows when to execute it. |
|
365 |
qp->frame = _frame_counter_max; |
|
366 |
||
367 |
// calculate the amount of extra bytes. |
|
368 |
nump = 8; |
|
369 |
while ( nump != 0 && ((uint32*)_decode_parameters)[nump-1] == 0) nump--; |
|
370 |
qp->cp.packet_length = COMMAND_PACKET_BASE_SIZE + nump * sizeof(uint32); |
|
371 |
if (nump != 0) memcpy(qp->cp.dp, _decode_parameters, nump * sizeof(uint32)); |
|
372 |
||
373 |
#if defined(TTD_BIG_ENDIAN) |
|
374 |
// need to convert the command to little endian before sending it. |
|
375 |
{ |
|
376 |
CommandPacket cp; |
|
377 |
cp = qp->cp; |
|
378 |
cp.cmd = TO_LE16(cp.cmd); |
|
379 |
cp.tile = TO_LE16(cp.tile); |
|
380 |
cp.p1 = TO_LE32(cp.p1); |
|
381 |
cp.p2 = TO_LE32(cp.p2); |
|
382 |
for(cs=_clients; cs->socket != INVALID_SOCKET; cs++) if (!cs->inactive) SendBytes(cs, &cp, cp.packet_length); |
|
383 |
} |
|
384 |
#else |
|
385 |
// send it to the peers |
|
386 |
for(cs=_clients; cs->socket != INVALID_SOCKET; cs++) if (!cs->inactive) SendBytes(cs, &qp->cp, qp->cp.packet_length); |
|
387 |
||
388 |
#endif |
|
389 |
} |
|
390 |
||
391 |
// client: |
|
392 |
// server sends a command from another player that we should execute. |
|
393 |
// put it in the command queue. |
|
394 |
// |
|
395 |
// server: |
|
396 |
// client sends a command that it wants to execute. |
|
397 |
// fill the when field so the client knows when to execute it. |
|
398 |
// put it in the appropriate player queue. |
|
399 |
// send it to all other clients. |
|
400 |
// send an ack packet to the actual client. |
|
401 |
||
402 |
static void HandleCommandPacket(ClientState *cs, CommandPacket *np) |
|
403 |
{ |
|
404 |
QueuedCommand *qp; |
|
405 |
ClientState *c; |
|
406 |
AckPacket ap; |
|
407 |
||
408 |
printf("net: cmd size %d\n", np->packet_length); |
|
409 |
||
410 |
assert(np->packet_length >= COMMAND_PACKET_BASE_SIZE); |
|
411 |
||
412 |
// put it into the command queue |
|
413 |
qp = AllocQueuedCommand(&_command_queue); |
|
414 |
qp->cp = *np; |
|
415 |
||
416 |
qp->frame = _frame_counter_max; |
|
417 |
||
418 |
qp->callback = NULL; |
|
419 |
||
420 |
// extra params |
|
421 |
memcpy(&qp->cp.dp, np->dp, np->packet_length - COMMAND_PACKET_BASE_SIZE); |
|
422 |
||
423 |
ap.packet_type = 2; |
|
424 |
ap.packet_length = 2; |
|
425 |
||
426 |
// send it to the peers |
|
427 |
if (_networking_server) { |
|
428 |
for(c=_clients; c->socket != INVALID_SOCKET; c++) { |
|
429 |
if (c == cs) { |
|
430 |
SendBytes(c, &ap, 2); |
|
431 |
} else { |
|
432 |
if (!cs->inactive) SendBytes(c, &qp->cp, qp->cp.packet_length); |
|
433 |
} |
|
434 |
} |
|
435 |
} |
|
436 |
||
437 |
// convert from little endian to big endian? |
|
438 |
#if defined(TTD_BIG_ENDIAN) |
|
439 |
qp->cp.cmd = TO_LE16(qp->cp.cmd); |
|
440 |
qp->cp.tile = TO_LE16(qp->cp.tile); |
|
441 |
qp->cp.p1 = TO_LE32(qp->cp.p1); |
|
442 |
qp->cp.p2 = TO_LE32(qp->cp.p2); |
|
443 |
#endif |
|
444 |
||
445 |
qp->cmd = qp->cp.cmd; |
|
446 |
} |
|
447 |
||
448 |
// sent from server -> client periodically to tell the client about the current tick in the server |
|
449 |
// and how far the client may progress. |
|
450 |
static void HandleSyncPacket(SyncPacket *sp) |
|
451 |
{ |
|
452 |
uint32 s1,s2; |
|
453 |
||
454 |
_frame_counter_srv = _frame_counter_max - sp->server; |
|
455 |
_frame_counter_max += sp->frames; |
|
456 |
||
457 |
printf("net: sync max=%d cur=%d server=%d\n", _frame_counter_max, _frame_counter, _frame_counter_srv); |
|
458 |
||
459 |
// queueing only? |
|
460 |
if (_networking_queuing || _frame_counter == 0) |
|
461 |
return; |
|
462 |
||
463 |
s1 = TO_LE32(sp->random_seed_1); |
|
464 |
s2 = TO_LE32(sp->random_seed_2); |
|
465 |
||
466 |
if (_frame_counter_srv <= _frame_counter) { |
|
467 |
// we are ahead of the server check if the seed is in our list. |
|
468 |
if (_frame_counter_srv + 16 > _frame_counter) { |
|
469 |
// the random seed exists in our array check it. |
|
470 |
if (s1 != _my_seed_list[_frame_counter_srv & 0xF][0] || s2 != _my_seed_list[_frame_counter_srv & 0xF][1]) |
|
471 |
error("!network is desynched\n"); |
|
472 |
} |
|
473 |
} else { |
|
474 |
// the server's frame has not been executed yet. store the server's seed in a list. |
|
475 |
if (_num_future_seed < lengthof(_future_seed)) { |
|
476 |
_future_seed[_num_future_seed].frame = _frame_counter_srv; |
|
477 |
_future_seed[_num_future_seed].seed[0] = s1; |
|
478 |
_future_seed[_num_future_seed].seed[1] = s2; |
|
479 |
_num_future_seed++; |
|
480 |
} |
|
481 |
} |
|
482 |
} |
|
483 |
||
484 |
// sent from server -> client as an acknowledgement that the server received the command. |
|
485 |
// the command will be executed at the current value of "max". |
|
486 |
static void HandleAckPacket() |
|
487 |
{ |
|
488 |
QueuedCommand *q; |
|
489 |
// move a packet from the ack queue to the end of this player's queue. |
|
490 |
q = _ack_queue.head; |
|
491 |
assert(q); |
|
492 |
if (!(_ack_queue.head = q->next)) _ack_queue.last = &_ack_queue.head; |
|
493 |
q->next = NULL; |
|
494 |
q->frame = _frame_counter_max; |
|
495 |
||
496 |
*_command_queue.last = q; |
|
497 |
_command_queue.last = &q->next; |
|
498 |
||
499 |
printf("net: ack\n"); |
|
500 |
} |
|
501 |
||
502 |
static void HandleFilePacket(FilePacketHdr *fp) |
|
503 |
{ |
|
504 |
int n = fp->packet_length - sizeof(FilePacketHdr); |
|
505 |
if (n == 0) { |
|
506 |
assert(_networking_queuing); |
|
507 |
assert(!_networking_sync); |
|
508 |
// eof |
|
509 |
if (_recv_file) { fclose(_recv_file); _recv_file = NULL; } |
|
510 |
||
511 |
// attempt loading the game. |
|
512 |
_game_mode = GM_NORMAL; |
|
513 |
if (SaveOrLoad("networkc.tmp", SL_LOAD) != SL_OK) error("network load failed"); |
|
514 |
||
515 |
// sync to server. |
|
516 |
_networking_queuing = false; |
|
105 | 517 |
NetworkStartSync(false); |
518 |
/* |
|
0 | 519 |
_networking_sync = true; |
520 |
_frame_counter = 0; // start executing at frame 0. |
|
521 |
_sync_seed_1 = _sync_seed_2 = 0; |
|
522 |
_num_future_seed = 0; |
|
105 | 523 |
memset(_my_seed_list, 0, sizeof(_my_seed_list)); */ |
0 | 524 |
|
525 |
if (_network_playas == 0) { |
|
526 |
// send a command to make a new player |
|
527 |
_local_player = 0; |
|
528 |
NetworkSendCommand(0, 0, 0, CMD_PLAYER_CTRL, NULL); |
|
1 | 529 |
_local_player = OWNER_SPECTATOR; |
0 | 530 |
} else { |
531 |
// take control over an existing company |
|
532 |
if (DEREF_PLAYER(_network_playas-1)->is_active) |
|
533 |
_local_player = _network_playas-1; |
|
534 |
else |
|
1 | 535 |
_local_player = OWNER_SPECTATOR; |
0 | 536 |
} |
537 |
||
538 |
} else { |
|
539 |
if(!_recv_file) { |
|
540 |
_recv_file = fopen("networkc.tmp", "wb"); |
|
541 |
if (!_recv_file) error("can't open savefile"); |
|
542 |
} |
|
543 |
fwrite( (char*)fp + sizeof(*fp), n, 1, _recv_file); |
|
544 |
} |
|
545 |
} |
|
546 |
||
547 |
static void CloseClient(ClientState *cs) |
|
548 |
{ |
|
549 |
Packet *p, *next; |
|
550 |
||
105 | 551 |
DEBUG(misc,1) ("[NET][TCP] closed client connection"); |
0 | 552 |
|
553 |
assert(cs->socket != INVALID_SOCKET); |
|
554 |
||
555 |
closesocket(cs->socket); |
|
556 |
||
557 |
// free buffers |
|
558 |
for(p = cs->head; p; p=next) { |
|
559 |
next = p->next; |
|
560 |
free(p); |
|
561 |
} |
|
562 |
||
563 |
// copy up structs... |
|
564 |
while ((cs+1)->socket != INVALID_SOCKET) { |
|
565 |
*cs = *(cs+1); |
|
566 |
cs++; |
|
567 |
} |
|
568 |
cs->socket = INVALID_SOCKET; |
|
569 |
||
105 | 570 |
if (_networking_server) _network_game.players_on--; |
571 |
||
0 | 572 |
_num_clients--; |
573 |
} |
|
574 |
||
575 |
#define NETWORK_BUFFER_SIZE 4096 |
|
576 |
static bool ReadPackets(ClientState *cs) |
|
577 |
{ |
|
578 |
byte network_buffer[NETWORK_BUFFER_SIZE]; |
|
579 |
uint pos,size; |
|
580 |
unsigned long recv_bytes; |
|
581 |
||
582 |
size = cs->buflen; |
|
583 |
||
584 |
for(;;) { |
|
585 |
if (size != 0) memcpy(network_buffer, cs->buf, size); |
|
586 |
||
587 |
recv_bytes = recv(cs->socket, (char*)network_buffer + size, sizeof(network_buffer) - size, 0); |
|
588 |
if ( recv_bytes == (unsigned long)-1) { |
|
589 |
int err = GET_LAST_ERROR(); |
|
590 |
if (err == EWOULDBLOCK) break; |
|
591 |
printf("net: recv() failed with error %d\n", err); |
|
592 |
CloseClient(cs); |
|
593 |
return false; |
|
594 |
} |
|
595 |
// no more bytes for now? |
|
596 |
if (recv_bytes == 0) |
|
597 |
break; |
|
598 |
||
599 |
size += recv_bytes; // number of bytes read. |
|
600 |
pos = 0; |
|
601 |
while (size >= 2) { |
|
602 |
byte *packet = network_buffer + pos; |
|
603 |
// whole packet not there yet? |
|
604 |
if (size < packet[0]) break; |
|
605 |
size -= packet[0]; |
|
606 |
pos += packet[0]; |
|
607 |
||
608 |
switch(packet[1]) { |
|
609 |
case 0: |
|
610 |
HandleCommandPacket(cs, (CommandPacket*)packet); |
|
611 |
break; |
|
612 |
case 1: |
|
613 |
assert(_networking_sync || _networking_queuing); |
|
614 |
assert(!_networking_server); |
|
615 |
HandleSyncPacket((SyncPacket*)packet); |
|
616 |
break; |
|
617 |
case 2: |
|
618 |
assert(!_networking_server); |
|
619 |
HandleAckPacket(); |
|
620 |
break; |
|
621 |
case 3: |
|
622 |
HandleFilePacket((FilePacketHdr*)packet); |
|
623 |
break; |
|
624 |
default: |
|
625 |
error("unknown packet type"); |
|
626 |
} |
|
627 |
} |
|
628 |
||
629 |
assert(size>=0 && size < sizeof(cs->buf)); |
|
630 |
||
631 |
memcpy(cs->buf, network_buffer + pos, size); |
|
632 |
} |
|
633 |
||
634 |
cs->buflen = size; |
|
635 |
||
636 |
return true; |
|
637 |
} |
|
638 |
||
639 |
||
640 |
static bool SendPackets(ClientState *cs) |
|
641 |
{ |
|
642 |
Packet *p; |
|
643 |
int n; |
|
644 |
uint nskip = cs->eaten, nsent = nskip; |
|
645 |
||
646 |
// try sending as much as possible. |
|
647 |
for(p=cs->head; p ;p = p->next) { |
|
648 |
if (p->siz) { |
|
649 |
assert(nskip < p->siz); |
|
650 |
||
651 |
n = send(cs->socket, p->buf + nskip, p->siz - nskip, 0); |
|
652 |
if (n == -1) { |
|
653 |
int err = GET_LAST_ERROR(); |
|
654 |
if (err == EWOULDBLOCK) break; |
|
655 |
printf("net: send() failed with error %d\n", err); |
|
656 |
CloseClient(cs); |
|
657 |
return false; |
|
658 |
} |
|
659 |
nsent += n; |
|
660 |
// send was not able to send it all? then we assume that the os buffer is full and break. |
|
661 |
if (nskip + n != p->siz) |
|
662 |
break; |
|
663 |
nskip = 0; |
|
664 |
} |
|
665 |
} |
|
666 |
||
667 |
// nsent bytes in the linked list are not invalid. free as many buffers as possible. |
|
668 |
// don't actually free the last buffer. |
|
669 |
while (nsent) { |
|
670 |
p = cs->head; |
|
671 |
assert(p->siz != 0); |
|
672 |
||
673 |
// some bytes of the packet are still unsent. |
|
674 |
if ( (int)(nsent - p->siz) < 0) |
|
675 |
break; |
|
676 |
nsent -= p->siz; |
|
677 |
p->siz = 0; |
|
678 |
if (p->next) { |
|
679 |
cs->head = p->next; |
|
680 |
free(p); |
|
681 |
} |
|
682 |
} |
|
683 |
||
684 |
cs->eaten = nsent; |
|
685 |
||
686 |
return true; |
|
687 |
} |
|
688 |
||
689 |
// transmit the file.. |
|
690 |
static void SendXmit(ClientState *cs) |
|
691 |
{ |
|
692 |
uint pos, n; |
|
693 |
FilePacketHdr hdr; |
|
694 |
int p; |
|
695 |
||
696 |
// if too many unsent bytes left in buffer, don't send more. |
|
697 |
if (cs->head && cs->head->next) |
|
698 |
return; |
|
699 |
||
700 |
pos = cs->xmitpos - 1; |
|
701 |
||
702 |
p = 20; |
|
703 |
do { |
|
704 |
// compute size of data to xmit |
|
705 |
n = minu(_transmit_file_size - pos, 248); |
|
706 |
||
707 |
hdr.packet_length = n + sizeof(hdr); |
|
708 |
hdr.packet_type = 3; |
|
709 |
hdr.unused[0] = hdr.unused[1] = 0; |
|
710 |
SendBytes(cs, &hdr, sizeof(hdr)); |
|
711 |
||
712 |
if (n == 0) { |
|
713 |
pos = -1; // eof |
|
714 |
break; |
|
715 |
} |
|
716 |
SendBytes(cs, _transmit_file + pos, n); |
|
717 |
pos += n; |
|
718 |
} while (--p); |
|
719 |
||
720 |
cs->xmitpos = pos + 1; |
|
721 |
||
722 |
printf("net: client xmit at %d\n", pos + 1); |
|
723 |
} |
|
724 |
||
725 |
static ClientState *AllocClient(SOCKET s) |
|
726 |
{ |
|
727 |
ClientState *cs; |
|
728 |
||
729 |
if (_num_clients == MAX_CLIENTS) |
|
730 |
return NULL; |
|
731 |
||
105 | 732 |
if (_networking_server) _network_game.players_on++; |
733 |
||
0 | 734 |
cs = &_clients[_num_clients++]; |
735 |
memset(cs, 0, sizeof(*cs)); |
|
736 |
cs->last = &cs->head; |
|
737 |
cs->socket = s; |
|
738 |
return cs; |
|
739 |
} |
|
740 |
||
741 |
||
742 |
static void NetworkAcceptClients() |
|
743 |
{ |
|
744 |
struct sockaddr_in sin; |
|
745 |
SOCKET s; |
|
746 |
ClientState *cs; |
|
747 |
#ifndef __MORPHOS__ |
|
748 |
int sin_len; |
|
749 |
#else |
|
750 |
LONG sin_len; // for some reason we need a 'LONG' under MorphOS |
|
751 |
#endif |
|
752 |
||
753 |
assert(_listensocket != INVALID_SOCKET); |
|
754 |
||
755 |
for(;;) { |
|
756 |
sin_len = sizeof(sin); |
|
757 |
s = accept(_listensocket, (struct sockaddr*)&sin, &sin_len); |
|
758 |
if (s == INVALID_SOCKET) return; |
|
759 |
||
760 |
// set nonblocking mode for client socket |
|
761 |
{ unsigned long blocking = 1; ioctlsocket(s, FIONBIO, &blocking); } |
|
762 |
||
763 |
printf("net: got client from %s\n", inet_ntoa(sin.sin_addr)); |
|
764 |
||
765 |
// set nodelay |
|
766 |
{int b = 1; setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b));} |
|
767 |
||
768 |
cs = AllocClient(s); |
|
769 |
if (cs == NULL) { |
|
770 |
// no more clients allowed? |
|
771 |
closesocket(s); |
|
772 |
continue; |
|
773 |
} |
|
774 |
||
775 |
if (_networking_sync) { |
|
776 |
// a new client has connected. it needs a snapshot. |
|
777 |
cs->inactive = true; |
|
778 |
} |
|
779 |
} |
|
780 |
||
781 |
// when a new client has joined. it needs different information depending on if it's at the game menu or in an active game. |
|
782 |
// Game menu: |
|
783 |
// - list of players already in the game (name, company name, face, color) |
|
784 |
// - list of game settings and patch settings |
|
785 |
// Active game: |
|
786 |
// - the state of the world (includes player name, company name, player face, player color) |
|
787 |
// - list of the patch settings |
|
788 |
||
789 |
// Networking can be in several "states". |
|
790 |
// * 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. |
|
791 |
// * sync - games are in sync |
|
792 |
} |
|
793 |
||
794 |
||
795 |
static void SendQueuedCommandsToNewClient(ClientState *cs) |
|
796 |
{ |
|
797 |
// send the commands in the server queue to the new client. |
|
798 |
QueuedCommand *qp; |
|
799 |
SyncPacket sp; |
|
800 |
int32 frame; |
|
801 |
||
802 |
printf("net: sending queued commands to client\n"); |
|
803 |
||
804 |
sp.packet_length = sizeof(sp); |
|
805 |
sp.packet_type = 1; |
|
806 |
sp.random_seed_1 = sp.random_seed_2 = 0; |
|
807 |
sp.server = 0; |
|
808 |
||
809 |
frame = _frame_counter; |
|
810 |
||
811 |
for(qp=_command_queue.head; qp; qp = qp->next) { |
|
812 |
printf("net: sending cmd to be executed at %d (old %d)\n", qp->frame, frame); |
|
813 |
if (qp->frame > frame) { |
|
814 |
assert(qp->frame <= _frame_counter_max); |
|
815 |
sp.frames = qp->frame - frame; |
|
816 |
frame = qp->frame; |
|
817 |
SendBytes(cs, &sp, sizeof(sp)); |
|
818 |
} |
|
819 |
SendBytes(cs, &qp->cp, qp->cp.packet_length); |
|
820 |
} |
|
821 |
||
822 |
if (frame < _frame_counter_max) { |
|
823 |
printf("net: sending queued sync %d (%d)\n", _frame_counter_max, frame); |
|
824 |
sp.frames = _frame_counter_max - frame; |
|
825 |
SendBytes(cs, &sp, sizeof(sp)); |
|
826 |
} |
|
827 |
||
828 |
} |
|
829 |
||
105 | 830 |
// ************************** // |
831 |
// * TCP Networking * // |
|
832 |
// ************************** // |
|
833 |
||
834 |
bool NetworkConnect(const char *hostname, int port) |
|
835 |
{ |
|
836 |
SOCKET s; |
|
837 |
struct sockaddr_in sin; |
|
838 |
int b; |
|
839 |
||
840 |
DEBUG(misc, 1) ("[NET][TCP] Connecting to %s %d", hostname, port); |
|
841 |
||
842 |
s = socket(AF_INET, SOCK_STREAM, 0); |
|
843 |
if (s == INVALID_SOCKET) error("socket() failed"); |
|
844 |
||
845 |
b = 1; |
|
846 |
setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b)); |
|
847 |
||
848 |
sin.sin_family = AF_INET; |
|
849 |
sin.sin_addr.s_addr = inet_addr(hostname); |
|
850 |
sin.sin_port = htons(port); |
|
851 |
||
852 |
if (connect(s, (struct sockaddr*) &sin, sizeof(sin)) != 0) { |
|
853 |
NetworkClose(true); |
|
854 |
return false; |
|
855 |
} |
|
856 |
||
857 |
// set nonblocking mode for socket.. |
|
858 |
{ unsigned long blocking = 1; ioctlsocket(s, FIONBIO, &blocking); } |
|
859 |
||
860 |
// in client mode, only the first client field is used. it's pointing to the server. |
|
861 |
AllocClient(s); |
|
862 |
||
863 |
// queue packets.. because we're waiting for the savegame. |
|
864 |
_networking_queuing = true; |
|
865 |
_frame_counter_max = 0; |
|
866 |
||
867 |
return true; |
|
868 |
} |
|
869 |
||
870 |
void NetworkListen() |
|
871 |
{ |
|
872 |
||
873 |
SOCKET ls; |
|
874 |
struct sockaddr_in sin; |
|
875 |
int port; |
|
876 |
||
877 |
port = _network_server_port; |
|
878 |
||
879 |
DEBUG(misc, 1) ("[NET][TCP] listening on port %d", port); |
|
880 |
||
881 |
ls = socket(AF_INET, SOCK_STREAM, 0); |
|
882 |
if (ls == INVALID_SOCKET) |
|
883 |
error("socket() on listen socket failed"); |
|
884 |
||
885 |
// reuse the socket |
|
886 |
{ |
|
887 |
int reuse = 1; if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) == -1) |
|
888 |
error("setsockopt() on listen socket failed"); |
|
889 |
} |
|
890 |
||
891 |
// set nonblocking mode for socket |
|
892 |
{ unsigned long blocking = 1; ioctlsocket(ls, FIONBIO, &blocking); } |
|
893 |
||
894 |
sin.sin_family = AF_INET; |
|
895 |
sin.sin_addr.s_addr = 0; |
|
896 |
sin.sin_port = htons(port); |
|
897 |
||
898 |
if (bind(ls, (struct sockaddr*)&sin, sizeof(sin)) != 0) |
|
899 |
error("bind() failed"); |
|
900 |
||
901 |
if (listen(ls, 1) != 0) |
|
902 |
error("listen() failed"); |
|
903 |
||
904 |
_listensocket = ls; |
|
905 |
} |
|
906 |
||
0 | 907 |
void NetworkReceive() |
908 |
{ |
|
909 |
ClientState *cs; |
|
910 |
int n; |
|
911 |
fd_set read_fd, write_fd; |
|
912 |
struct timeval tv; |
|
913 |
||
914 |
FD_ZERO(&read_fd); |
|
915 |
FD_ZERO(&write_fd); |
|
916 |
||
917 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
918 |
FD_SET(cs->socket, &read_fd); |
|
919 |
FD_SET(cs->socket, &write_fd); |
|
920 |
} |
|
921 |
||
922 |
// take care of listener port |
|
923 |
if (_networking_server) { |
|
924 |
FD_SET(_listensocket, &read_fd); |
|
925 |
} |
|
926 |
||
927 |
tv.tv_sec = tv.tv_usec = 0; // don't block at all. |
|
928 |
#if !defined(__MORPHOS__) && !defined(__AMIGA__) |
|
929 |
n = select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv); |
|
930 |
#else |
|
931 |
n = WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL); |
|
932 |
#endif |
|
933 |
if (n == -1) error("select failed"); |
|
934 |
||
935 |
// accept clients.. |
|
936 |
if (_networking_server && FD_ISSET(_listensocket, &read_fd)) |
|
937 |
NetworkAcceptClients(); |
|
938 |
||
939 |
// read stuff from clients |
|
940 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
941 |
cs->writable = !!FD_ISSET(cs->socket, &write_fd); |
|
942 |
if (FD_ISSET(cs->socket, &read_fd)) { |
|
943 |
if (!ReadPackets(cs)) |
|
944 |
cs--; |
|
945 |
} |
|
946 |
} |
|
947 |
||
948 |
// 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. |
|
949 |
if (_networking_server && _transmit_file == NULL) { |
|
950 |
bool didsave = false; |
|
951 |
||
952 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
953 |
if (cs->inactive) { |
|
954 |
cs->inactive = false; |
|
955 |
// found a client waiting for a snapshot. make a snapshot. |
|
956 |
if (!didsave) { |
|
957 |
char filename[256]; |
|
958 |
sprintf(filename, "%snetwork.tmp", _path.autosave_dir); |
|
959 |
didsave = true; |
|
960 |
if (SaveOrLoad(filename, SL_SAVE) != SL_OK) error("network savedump failed"); |
|
961 |
_transmit_file = ReadFileToMem(filename, &_transmit_file_size, 500000); |
|
962 |
if (_transmit_file == NULL) error("network savedump failed to load"); |
|
963 |
} |
|
964 |
// and start sending the file.. |
|
965 |
cs->xmitpos = 1; |
|
966 |
||
967 |
// send queue of commands to client. |
|
968 |
SendQueuedCommandsToNewClient(cs); |
|
969 |
} |
|
970 |
} |
|
971 |
} |
|
972 |
} |
|
973 |
||
974 |
void NetworkSend() |
|
975 |
{ |
|
976 |
ClientState *cs; |
|
977 |
void *free_xmit; |
|
978 |
||
979 |
// send sync packets? |
|
980 |
if (_networking_server && _networking_sync && !_pause) { |
|
981 |
if (++_not_packet >= _network_sync_freq) { |
|
982 |
SyncPacket sp; |
|
983 |
uint new_max; |
|
984 |
||
985 |
_not_packet = 0; |
|
986 |
||
987 |
new_max = max(_frame_counter + (int)_network_ahead_frames, _frame_counter_max); |
|
988 |
||
989 |
sp.packet_length = sizeof(sp); |
|
990 |
sp.packet_type = 1; |
|
991 |
sp.frames = new_max - _frame_counter_max; |
|
992 |
sp.server = _frame_counter_max - _frame_counter; |
|
993 |
sp.random_seed_1 = TO_LE32(_sync_seed_1); |
|
994 |
sp.random_seed_2 = TO_LE32(_sync_seed_2); |
|
995 |
_frame_counter_max = new_max; |
|
996 |
||
997 |
// send it to all the clients |
|
998 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) SendBytes(cs, &sp, sizeof(sp)); |
|
999 |
} |
|
1000 |
} |
|
1001 |
||
1002 |
free_xmit = _transmit_file; |
|
1003 |
||
1004 |
// send stuff to all clients |
|
1005 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
1006 |
if (cs->xmitpos) { |
|
1007 |
if (cs->writable) |
|
1008 |
SendXmit(cs); |
|
1009 |
free_xmit = NULL; |
|
1010 |
} |
|
1011 |
if (cs->writable) { |
|
1012 |
if (!SendPackets(cs)) cs--; |
|
1013 |
} |
|
1014 |
} |
|
1015 |
||
1016 |
// no clients left that xmit the file, free it. |
|
1017 |
if (free_xmit) { |
|
1018 |
_transmit_file = NULL; |
|
1019 |
free(free_xmit); |
|
1020 |
} |
|
1021 |
} |
|
1022 |
||
1023 |
||
105 | 1024 |
void NetworkInitialize() |
0 | 1025 |
{ |
1026 |
ClientState *cs; |
|
1 | 1027 |
|
0 | 1028 |
_command_queue.last = &_command_queue.head; |
1029 |
_ack_queue.last = &_ack_queue.head; |
|
1030 |
||
1031 |
// invalidate all clients |
|
1032 |
for(cs=_clients; cs != &_clients[MAX_CLIENTS]; cs++) |
|
1033 |
cs->socket = INVALID_SOCKET; |
|
105 | 1034 |
|
1035 |
} |
|
1036 |
||
1037 |
void NetworkClose(bool client) { |
|
1038 |
||
1039 |
ClientState *cs; |
|
1040 |
// invalidate all clients |
|
1041 |
||
1042 |
for(cs=_clients; cs != &_clients[MAX_CLIENTS]; cs++) if (cs->socket != INVALID_SOCKET) { |
|
1043 |
CloseClient(cs); |
|
1044 |
} |
|
1045 |
||
1046 |
if (!client) { |
|
1047 |
// if in servermode --> close listener |
|
1048 |
closesocket(_listensocket); |
|
1049 |
_listensocket= INVALID_SOCKET; |
|
1050 |
DEBUG(misc,1) ("[NET][TCP] closed listener on port %i", _network_server_port); |
|
1051 |
} |
|
0 | 1052 |
} |
1053 |
||
1054 |
void NetworkShutdown() |
|
1055 |
{ |
|
105 | 1056 |
|
0 | 1057 |
} |
1058 |
||
1059 |
// switch to synced mode. |
|
105 | 1060 |
void NetworkStartSync(bool fcreset) |
0 | 1061 |
{ |
105 | 1062 |
DEBUG(misc,3) ("[NET][SYNC] switching to synced game mode"); |
0 | 1063 |
_networking_sync = true; |
1064 |
_frame_counter = 0; |
|
105 | 1065 |
if (fcreset) { |
1066 |
_frame_counter_max = 0; |
|
1067 |
_frame_counter_srv = 0; |
|
1068 |
} |
|
0 | 1069 |
_num_future_seed = 0; |
1070 |
_sync_seed_1 = _sync_seed_2 = 0; |
|
1071 |
memset(_my_seed_list, 0, sizeof(_my_seed_list)); |
|
105 | 1072 |
|
0 | 1073 |
} |
1074 |
||
1075 |
// ************************** // |
|
1076 |
// * UDP Network Extensions * // |
|
1077 |
// ************************** // |
|
1078 |
||
105 | 1079 |
void NetworkUDPListen(bool client) |
0 | 1080 |
{ |
1081 |
SOCKET udp; |
|
1082 |
struct sockaddr_in sin; |
|
105 | 1083 |
int port; |
0 | 1084 |
|
105 | 1085 |
if (client) { port = _network_client_port; } else { port = _network_server_port; }; |
1086 |
||
1087 |
DEBUG(misc,1) ("[NET][UDP] listening on port %i", port); |
|
0 | 1088 |
|
1089 |
udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
|
105 | 1090 |
|
1091 |
// this disables network |
|
1092 |
_network_available = !(udp == INVALID_SOCKET); |
|
0 | 1093 |
|
1094 |
// set nonblocking mode for socket |
|
1095 |
{ unsigned long blocking = 1; ioctlsocket(udp, FIONBIO, &blocking); } |
|
1096 |
||
1097 |
sin.sin_family = AF_INET; |
|
1098 |
sin.sin_addr.s_addr = 0; |
|
1099 |
sin.sin_port = htons(port); |
|
1100 |
||
1101 |
if (bind(udp, (struct sockaddr*)&sin, sizeof(sin)) != 0) |
|
105 | 1102 |
DEBUG(misc,1) ("[NET][UDP] error: bind failed on port %i", port); |
1103 |
||
0 | 1104 |
|
1105 |
// enable broadcasting |
|
1106 |
{ unsigned long val=1; setsockopt(udp, SOL_SOCKET, SO_BROADCAST, (char *) &val , sizeof(val)); } |
|
105 | 1107 |
// allow reusing |
1108 |
{ unsigned long val=1; setsockopt(udp, SOL_SOCKET, SO_REUSEADDR, (char *) &val , sizeof(val)); } |
|
1109 |
||
1110 |
if (client) { _udp_client_socket = udp; } else { _udp_server_socket = udp; } ; |
|
0 | 1111 |
|
1112 |
} |
|
1113 |
||
105 | 1114 |
void NetworkUDPClose(bool client) { |
1115 |
if (client) { |
|
1116 |
DEBUG(misc,1) ("[NET][UDP] closed listener on port %i", _network_client_port); |
|
1117 |
closesocket(_udp_client_socket); |
|
1118 |
_udp_client_socket = INVALID_SOCKET; |
|
1119 |
} else { |
|
1120 |
DEBUG(misc,1) ("[NET][UDP] closed listener on port %i", _network_server_port); |
|
1121 |
closesocket(_udp_server_socket); |
|
1122 |
_udp_server_socket = INVALID_SOCKET; |
|
1123 |
}; |
|
1124 |
} |
|
1125 |
||
1126 |
void NetworkUDPReceive(bool client) { |
|
0 | 1127 |
struct sockaddr_in client_addr; |
1128 |
int client_len; |
|
1129 |
int nbytes; |
|
1130 |
struct UDPPacket packet; |
|
1131 |
int packet_len; |
|
1132 |
||
105 | 1133 |
SOCKET udp; |
1134 |
if (client) udp=_udp_client_socket; else udp=_udp_server_socket; |
|
1135 |
||
0 | 1136 |
packet_len = sizeof(packet); |
1137 |
client_len = sizeof(client_addr); |
|
1138 |
||
105 | 1139 |
nbytes = recvfrom(udp, (char *) &packet, packet_len , 0, (struct sockaddr *) &client_addr, &client_len); |
0 | 1140 |
if (nbytes>0) { |
1141 |
if (packet.command_code==packet.command_check) switch (packet.command_code) { |
|
1142 |
||
105 | 1143 |
case NET_UDPCMD_SERVERSEARCH: |
1144 |
if (!client) { |
|
1145 |
packet.command_check=packet.command_code=NET_UDPCMD_SERVERINFO; |
|
1146 |
memcpy(&packet.data,&_network_game,sizeof(_network_game)); |
|
1147 |
packet.data_len=sizeof(_network_game); |
|
1148 |
NetworkUDPSend(client,client_addr, packet); |
|
1149 |
} |
|
1150 |
break; |
|
1151 |
case NET_UDPCMD_GETSERVERINFO: |
|
1152 |
if (!client) { |
|
1153 |
packet.command_check=packet.command_code=NET_UDPCMD_SERVERINFO; |
|
1154 |
memcpy(&packet.data,&_network_game,sizeof(_network_game)); |
|
1155 |
packet.data_len=sizeof(_network_game); |
|
1156 |
NetworkUDPSend(client,client_addr, packet); |
|
0 | 1157 |
} |
1158 |
break; |
|
105 | 1159 |
case NET_UDPCMD_SERVERINFO: |
1160 |
if (client) { |
|
1161 |
NetworkGameList * item; |
|
1162 |
||
1163 |
item = (NetworkGameList *) NetworkGameListAdd(); |
|
1164 |
item -> ip = inet_addr(inet_ntoa(client_addr.sin_addr)); |
|
1165 |
item -> port = ntohs(client_addr.sin_port); |
|
1166 |
||
1167 |
memcpy(item,&packet.data,packet.data_len); |
|
1168 |
} |
|
1169 |
break; |
|
0 | 1170 |
} |
1171 |
} |
|
1172 |
} |
|
1173 |
||
81 | 1174 |
|
0 | 1175 |
|
105 | 1176 |
void NetworkUDPBroadCast(bool client, struct UDPPacket packet) { |
0 | 1177 |
int i=0, res; |
1178 |
struct sockaddr_in out_addr; |
|
1179 |
uint32 bcaddr; |
|
1180 |
byte * bcptr; |
|
105 | 1181 |
|
1182 |
SOCKET udp; |
|
1183 |
if (client) udp=_udp_client_socket; else udp=_udp_server_socket; |
|
1184 |
||
0 | 1185 |
while (_network_ip_list[i]!=0) { |
1186 |
bcaddr=_network_ip_list[i]; |
|
1187 |
out_addr.sin_family = AF_INET; |
|
105 | 1188 |
if (client) { out_addr.sin_port = htons(_network_server_port); } else { out_addr.sin_port = htons(_network_client_port); }; |
0 | 1189 |
bcptr = (byte *) &bcaddr; |
1190 |
bcptr[3]=255; |
|
1191 |
out_addr.sin_addr.s_addr = bcaddr; |
|
105 | 1192 |
res=sendto(udp,(char *) &packet,sizeof(packet),0,(struct sockaddr *) &out_addr,sizeof(out_addr)); |
0 | 1193 |
if (res==-1) error("udp: broadcast error: %i",GET_LAST_ERROR()); |
1194 |
i++; |
|
1195 |
} |
|
1196 |
||
1197 |
} |
|
1198 |
||
105 | 1199 |
void NetworkUDPSend(bool client, struct sockaddr_in recv,struct UDPPacket packet) { |
1200 |
||
1201 |
SOCKET udp; |
|
1202 |
if (client) udp=_udp_client_socket; else udp=_udp_server_socket; |
|
1203 |
||
1204 |
sendto(udp,(char *) &packet,sizeof(packet),0,(struct sockaddr *) &recv,sizeof(recv)); |
|
0 | 1205 |
} |
1206 |
||
105 | 1207 |
|
1208 |
bool NetworkUDPSearchGame(byte ** _network_detected_serverip, unsigned short * _network_detected_serverport) { |
|
0 | 1209 |
struct UDPPacket packet; |
1210 |
int timeout=3000; |
|
105 | 1211 |
|
1212 |
NetworkGameListClear(); |
|
1213 |
||
1214 |
DEBUG(misc,0) ("[NET][UDP] searching server"); |
|
1215 |
*_network_detected_serverip = "255.255.255.255"; |
|
1216 |
*_network_detected_serverport = 0; |
|
0 | 1217 |
|
1218 |
packet.command_check=packet.command_code=NET_UDPCMD_SERVERSEARCH; |
|
1219 |
packet.data_len=0; |
|
105 | 1220 |
NetworkUDPBroadCast(true, packet); |
0 | 1221 |
while (timeout>=0) { |
1222 |
CSleep(100); |
|
1223 |
timeout-=100; |
|
105 | 1224 |
NetworkUDPReceive(true); |
1225 |
||
1226 |
if (_network_game_count>0) { |
|
1227 |
NetworkGameList * item; |
|
1228 |
item = (NetworkGameList *) NetworkGameListItem(0); |
|
1229 |
*_network_detected_serverip=inet_ntoa(*(struct in_addr *) &item->ip); |
|
1230 |
*_network_detected_serverport=item->port; |
|
1231 |
timeout=-1; |
|
1232 |
DEBUG(misc,0) ("[NET][UDP] server found on %s", *_network_detected_serverip); |
|
1233 |
} |
|
1234 |
||
0 | 1235 |
} |
105 | 1236 |
|
0 | 1237 |
return (_network_detected_serverport>0); |
1238 |
||
1239 |
} |
|
1240 |
||
1241 |
||
105 | 1242 |
// *************************** // |
1243 |
// * New Network Core System * // |
|
1244 |
// *************************** // |
|
1245 |
||
1246 |
void NetworkIPListInit() { |
|
1247 |
struct hostent* he = NULL; |
|
1248 |
char hostname[250]; |
|
1249 |
uint32 bcaddr; |
|
1250 |
int i=0; |
|
1251 |
||
1252 |
gethostname(hostname,250); |
|
1253 |
DEBUG(misc,2) ("[NET][IP] init for host %s", hostname); |
|
1254 |
he=gethostbyname((char *) hostname); |
|
1255 |
||
1256 |
if (he == NULL) { |
|
1257 |
he = gethostbyname("localhost"); |
|
1258 |
} |
|
1259 |
||
1260 |
if (he == NULL) { |
|
1261 |
bcaddr = inet_addr("127.0.0.1"); |
|
1262 |
he = gethostbyaddr(inet_ntoa(*(struct in_addr *) &bcaddr), sizeof(bcaddr), AF_INET); |
|
1263 |
} |
|
1264 |
||
1265 |
if (he == NULL) { |
|
1266 |
DEBUG(misc, 2) ("[NET][IP] cannot resolve %s", hostname); |
|
1267 |
} else { |
|
1268 |
while(he->h_addr_list[i]) { |
|
1269 |
bcaddr = inet_addr(inet_ntoa(*(struct in_addr *) he->h_addr_list[i])); |
|
1270 |
_network_ip_list[i]=bcaddr; |
|
1271 |
DEBUG(misc,2) ("[NET][IP] add %s",inet_ntoa(*(struct in_addr *) he->h_addr_list[i])); |
|
1272 |
i++; |
|
1273 |
} |
|
1274 |
||
1275 |
} |
|
1276 |
_network_ip_list[i]=0; |
|
1277 |
||
1278 |
} |
|
1279 |
||
1280 |
/* *************************************************** */ |
|
1281 |
||
1282 |
void NetworkCoreInit() { |
|
1283 |
||
1284 |
DEBUG(misc,3) ("[NET][Core] init()"); |
|
1285 |
_network_available=true; |
|
1286 |
||
1287 |
// [win32] winsock startup |
|
1288 |
||
1289 |
#if defined(WIN32) |
|
1290 |
{ |
|
1291 |
WSADATA wsa; |
|
1292 |
DEBUG(misc,3) ("[NET][Core] using windows socket library"); |
|
1293 |
if (WSAStartup(MAKEWORD(2,0), &wsa) != 0) { |
|
1294 |
DEBUG(misc,3) ("[NET][Core] error: WSAStartup failed"); |
|
1295 |
_network_available=false; |
|
1296 |
} |
|
1297 |
} |
|
1298 |
#else |
|
1299 |
||
1300 |
// [morphos/amigaos] bsd-socket startup |
|
1301 |
||
1302 |
#if defined(__MORPHOS__) || defined(__AMIGA__) |
|
1303 |
{ |
|
1304 |
DEBUG(misc,3) ("[NET][Core] using bsd socket library"); |
|
1305 |
if (!(SocketBase = OpenLibrary("bsdsocket.library", 4))) { |
|
1306 |
DEBUG(misc,3) ("[NET][Core] Couldn't open bsdsocket.library version 4."); |
|
1307 |
_network_available=false; |
|
1308 |
} |
|
1309 |
} |
|
1310 |
#else |
|
1311 |
||
1312 |
// [linux/macos] unix-socket startup |
|
1313 |
||
1314 |
DEBUG(misc,3) ("[NET][Core] using unix socket library"); |
|
1315 |
||
1316 |
#endif |
|
1317 |
||
1318 |
#endif |
|
1319 |
||
1320 |
||
1321 |
if (_network_available) { |
|
1322 |
DEBUG(misc,3) ("[NET][Core] OK: multiplayer available"); |
|
1323 |
// initiate network ip list |
|
1324 |
NetworkIPListInit(); |
|
1325 |
} else { |
|
1326 |
DEBUG(misc,3) ("[NET][Core] FAILED: multiplayer not available"); |
|
1327 |
} |
|
1328 |
} |
|
1329 |
||
1330 |
/* *************************************************** */ |
|
1331 |
||
1332 |
void NetworkCoreShutdown() { |
|
1333 |
||
1334 |
DEBUG(misc,3) ("[NET][Core] shutdown()"); |
|
1335 |
||
1336 |
#if defined(__MORPHOS__) || defined(__AMIGA__) |
|
1337 |
{ |
|
1338 |
if (SocketBase) { |
|
1339 |
CloseLibrary(SocketBase); |
|
1340 |
} |
|
1341 |
} |
|
1342 |
#endif |
|
1343 |
||
1344 |
||
1345 |
#if defined(WIN32) |
|
1346 |
{ |
|
1347 |
WSACleanup(); |
|
1348 |
} |
|
1349 |
#endif |
|
1350 |
||
1351 |
} |
|
1352 |
||
1353 |
/* *************************************************** */ |
|
1354 |
||
1355 |
bool NetworkCoreConnectGame(byte* b, unsigned short port) |
|
1356 |
{ |
|
1357 |
if (!_network_available) return false; |
|
1358 |
||
1359 |
if (strcmp((char *) b,"auto")==0) { |
|
1360 |
// do autodetect |
|
1361 |
NetworkUDPSearchGame(&b, &port); |
|
1362 |
} |
|
1363 |
||
1364 |
if (port==0) { |
|
1365 |
// autodetection failed |
|
1366 |
if (_networking_override) NetworkLobbyShutdown(); |
|
1367 |
ShowErrorMessage(-1, STR_NETWORK_ERR_NOSERVER, 0, 0); |
|
1368 |
return false; |
|
1369 |
} |
|
1370 |
NetworkInitialize(); |
|
1371 |
_networking = NetworkConnect(b, port); |
|
1372 |
if (_networking) { |
|
1373 |
NetworkLobbyShutdown(); |
|
1374 |
} else { |
|
1375 |
if (_networking_override) NetworkLobbyShutdown(); |
|
1376 |
ShowErrorMessage(-1, STR_NETWORK_ERR_NOCONNECTION,0,0); |
|
1377 |
} |
|
1378 |
return _networking; |
|
1379 |
} |
|
1380 |
||
1381 |
/* *************************************************** */ |
|
1382 |
||
1383 |
bool NetworkCoreStartGame() |
|
1384 |
{ |
|
1385 |
if (!_network_available) return false; |
|
1386 |
NetworkLobbyShutdown(); |
|
1387 |
NetworkInitialize(); |
|
1388 |
NetworkListen(); |
|
1389 |
NetworkUDPListen(false); |
|
1390 |
_networking_server = true; |
|
1391 |
_networking = true; |
|
1392 |
NetworkGameFillDefaults(); // clears the network game info |
|
1393 |
_network_game.players_on++; // the serverplayer is online |
|
1394 |
return true; |
|
1395 |
} |
|
1396 |
||
1397 |
/* *************************************************** */ |
|
1398 |
||
1399 |
void NetworkCoreDisconnect() |
|
1400 |
{ |
|
1401 |
/* terminate server */ |
|
1402 |
if (_networking_server) { |
|
1403 |
NetworkUDPClose(false); |
|
1404 |
NetworkClose(false); |
|
1405 |
} |
|
1406 |
||
1407 |
/* terminate client connection */ |
|
1408 |
else if (_networking) { |
|
1409 |
NetworkClose(true); |
|
1410 |
} |
|
1411 |
||
1412 |
_networking_server = false; |
|
1413 |
_networking = false; |
|
1414 |
NetworkShutdown(); |
|
1415 |
} |
|
1416 |
||
1417 |
/* *************************************************** */ |
|
1418 |
||
1419 |
void NetworkCoreLoop(bool incomming) { |
|
1420 |
||
1421 |
||
1422 |
if (incomming) { |
|
1423 |
||
1424 |
// incomming |
|
1425 |
||
1426 |
if ( _udp_client_socket != INVALID_SOCKET ) NetworkUDPReceive(true); |
|
1427 |
if ( _udp_server_socket != INVALID_SOCKET ) NetworkUDPReceive(false); |
|
1428 |
||
1429 |
if (_networking) { |
|
1430 |
NetworkReceive(); |
|
1431 |
NetworkProcessCommands(); // to check if we got any new commands belonging to the current frame before we increase it. |
|
1432 |
} |
|
1433 |
||
1434 |
} else { |
|
1435 |
||
1436 |
// outgoing |
|
1437 |
||
1438 |
if (_networking) { |
|
1439 |
NetworkSend(); |
|
1440 |
} |
|
1441 |
||
1442 |
} |
|
1443 |
||
1444 |
} |
|
1445 |
||
1446 |
void NetworkLobbyInit() { |
|
1447 |
DEBUG(misc,3) ("[NET][Lobby] init()"); |
|
1448 |
NetworkUDPListen(true); |
|
1449 |
} |
|
1450 |
||
1451 |
void NetworkLobbyShutdown() { |
|
1452 |
DEBUG(misc,3) ("[NET][Lobby] shutdown()"); |
|
1453 |
NetworkUDPClose(true); |
|
1454 |
} |
|
1455 |
||
1456 |
||
1457 |
// ******************************** // |
|
1458 |
// * Network Game List Extensions * // |
|
1459 |
// ******************************** // |
|
1460 |
||
1461 |
void NetworkGameListClear() { |
|
1462 |
NetworkGameList * item; |
|
1463 |
NetworkGameList * next; |
|
1464 |
||
1465 |
DEBUG(misc,4) ("[NET][G-List] cleared server list"); |
|
1466 |
||
1467 |
item = _network_game_list; |
|
1468 |
while (item != NULL) { |
|
1469 |
next = (NetworkGameList *) item -> _next; |
|
1470 |
free (item); |
|
1471 |
item = next; |
|
1472 |
} |
|
1473 |
_network_game_list=NULL; |
|
1474 |
_network_game_count=0; |
|
1475 |
} |
|
1476 |
||
1477 |
char * NetworkGameListAdd() { |
|
1478 |
NetworkGameList * item; |
|
1479 |
NetworkGameList * before; |
|
1480 |
||
1481 |
DEBUG(misc,4) ("[NET][G-List] added server to list"); |
|
1482 |
||
1483 |
item = _network_game_list; |
|
1484 |
before = item; |
|
1485 |
while (item != NULL) { |
|
1486 |
before = item; |
|
1487 |
item = (NetworkGameList *) item -> _next; |
|
1488 |
} |
|
1489 |
item = malloc(sizeof(NetworkGameList)); |
|
1490 |
item -> _next = NULL; |
|
1491 |
if (before == NULL) { |
|
1492 |
_network_game_list = item; |
|
1493 |
} else { |
|
1494 |
before -> _next = (char *) item; |
|
1495 |
} |
|
1496 |
_network_game_count++; |
|
1497 |
return (char *) item; |
|
1498 |
} |
|
1499 |
||
1500 |
void NetworkGameListFromLAN() { |
|
1501 |
struct UDPPacket packet; |
|
1502 |
DEBUG(misc,2) ("[NET][G-List] searching server over lan"); |
|
1503 |
NetworkGameListClear(); |
|
1504 |
packet.command_check=packet.command_code=NET_UDPCMD_SERVERSEARCH; |
|
1505 |
packet.data_len=0; |
|
1506 |
NetworkUDPBroadCast(true,packet); |
|
1507 |
} |
|
1508 |
||
1509 |
void NetworkGameListFromInternet() { |
|
1510 |
DEBUG(misc,2) ("[NET][G-List] searching servers over internet"); |
|
1511 |
NetworkGameListClear(); |
|
1512 |
||
1513 |
// **TODO** masterserver communication [internet protocol list] |
|
1514 |
||
1515 |
} |
|
1516 |
||
1517 |
char * NetworkGameListItem(uint16 index) { |
|
1518 |
NetworkGameList * item; |
|
1519 |
NetworkGameList * next; |
|
1520 |
uint16 cnt = 0; |
|
1521 |
||
1522 |
item = _network_game_list; |
|
1523 |
||
1524 |
while ((item != NULL) && (cnt != index)) { |
|
1525 |
next = (NetworkGameList *) item -> _next; |
|
1526 |
item = next; |
|
1527 |
cnt++; |
|
1528 |
} |
|
1529 |
||
1530 |
return (char *) item; |
|
1531 |
} |
|
1532 |
||
1533 |
// *************************** // |
|
1534 |
// * Network Game Extensions * // |
|
1535 |
// *************************** // |
|
1536 |
||
1537 |
void NetworkGameFillDefaults() { |
|
1538 |
NetworkGameInfo * game = &_network_game; |
|
1539 |
#if defined(WITH_REV) |
|
1540 |
extern char _openttd_revision[]; |
|
1541 |
#endif |
|
1542 |
||
1543 |
DEBUG(misc,4) ("[NET][G-Info] setting defaults"); |
|
1544 |
||
1545 |
ttd_strlcpy(game->server_name,"OpenTTD Game",13); |
|
1546 |
game->game_password[0]='\0'; |
|
1547 |
game->map_name[0]='\0'; |
|
1548 |
#if defined(WITH_REV) |
|
1549 |
ttd_strlcpy(game->server_revision,_openttd_revision,strlen(_openttd_revision)); |
|
1550 |
#else |
|
1551 |
ttd_strlcpy(game->server_revision,"norev000",strlen("norev000")); |
|
1552 |
#endif |
|
1553 |
game->game_date=0; |
|
1554 |
||
1555 |
game->map_height=0; |
|
1556 |
game->map_width=0; |
|
1557 |
game->map_set=0; |
|
1558 |
||
1559 |
game->players_max=8; |
|
1560 |
game->players_on=0; |
|
1561 |
||
1562 |
game->server_lang=_dynlang.curr; |
|
1563 |
} |
|
1564 |
||
1565 |
void NetworkGameChangeDate(uint16 newdate) { |
|
1566 |
if (_networking_server) { |
|
1567 |
_network_game.game_date = newdate; |
|
1568 |
} |
|
1569 |
} |
|
1570 |
||
8
9ab81ef450f2
(svn r9) Fixed a couple of warnings and minor coding issues
dominik
parents:
1
diff
changeset
|
1571 |
#else // not ENABLE_NETWORK |
0 | 1572 |
|
1573 |
// stubs |
|
105 | 1574 |
void NetworkInitialize() {} |
0 | 1575 |
void NetworkShutdown() {} |
105 | 1576 |
void NetworkListen() {} |
0 | 1577 |
void NetworkConnect(const char *hostname, int port) {} |
1578 |
void NetworkReceive() {} |
|
1579 |
void NetworkSend() {} |
|
1580 |
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) {} |
|
1581 |
void NetworkProcessCommands() {} |
|
1582 |
void NetworkStartSync() {} |
|
105 | 1583 |
void NetworkCoreInit() { _network_available=false; }; |
1584 |
void NetworkCoreShutdown() {}; |
|
1585 |
void NetworkCoreDisconnect() {}; |
|
1586 |
void NetworkCoreLoop(bool incomming) {}; |
|
1587 |
bool NetworkCoreConnectGame(byte* b, unsigned short port) {}; |
|
1588 |
bool NetworkCoreStartGame() {}; |
|
1589 |
void NetworkLobbyShutdown() {}; |
|
1590 |
void NetworkLobbyInit() {}; |
|
1591 |
void NetworkGameListClear() {}; |
|
1592 |
char * NetworkGameListAdd() {return NULL;}; |
|
1593 |
void NetworkGameListFromLAN() {}; |
|
1594 |
void NetworkGameListFromInternet() {}; |
|
1595 |
void NetworkGameFillDefaults() {}; |
|
1596 |
char * NetworkGameListItem(uint16 index) {return NULL;}; |
|
1597 |
void NetworkGameChangeDate(uint16 newdate) {}; |
|
1598 |
} |
|
1599 |
||
1600 |
#endif |