author | darkvater |
Thu, 19 Aug 2004 09:37:23 +0000 | |
changeset 81 | 1759d41e3d43 |
parent 8 | 9ab81ef450f2 |
child 105 | 44e894da0fef |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
3 |
#include "command.h" |
|
4 |
#include "player.h" |
|
5 |
||
6 |
#if defined(WIN32) |
|
7 |
# include <windows.h> |
|
8 |
# include <winsock.h> |
|
9 |
||
10 |
# pragma comment (lib, "ws2_32.lib") |
|
11 |
# define ENABLE_NETWORK |
|
12 |
# define GET_LAST_ERROR() WSAGetLastError() |
|
13 |
# define EWOULDBLOCK WSAEWOULDBLOCK |
|
14 |
#endif |
|
15 |
||
16 |
#if defined(UNIX) |
|
17 |
// Make compatible with WIN32 names |
|
18 |
# define SOCKET int |
|
19 |
# define INVALID_SOCKET -1 |
|
20 |
// we need different defines for MorphOS and AmigaOS |
|
21 |
#if !defined(__MORPHOS__) && !defined(__AMIGA__) |
|
22 |
# define ioctlsocket ioctl |
|
23 |
# define closesocket close |
|
24 |
# define GET_LAST_ERROR() errno |
|
25 |
#endif |
|
26 |
// Need this for FIONREAD on solaris |
|
27 |
# define BSD_COMP |
|
28 |
# include <unistd.h> |
|
29 |
# include <sys/ioctl.h> |
|
30 |
||
31 |
// Socket stuff |
|
32 |
# include <sys/socket.h> |
|
33 |
# include <netinet/in.h> |
|
34 |
# include <arpa/inet.h> |
|
35 |
# include <errno.h> |
|
36 |
# include <sys/time.h> |
|
37 |
// NetDB |
|
38 |
# include <netdb.h> |
|
39 |
||
40 |
# ifndef TCP_NODELAY |
|
41 |
# define TCP_NODELAY 0x0001 |
|
42 |
# endif |
|
43 |
||
44 |
#endif |
|
45 |
||
46 |
||
47 |
#if defined(__MORPHOS__) || defined(__AMIGA__) |
|
48 |
# include <exec/types.h> |
|
49 |
# include <proto/exec.h> // required for Open/CloseLibrary() |
|
50 |
# if defined(__MORPHOS__) |
|
51 |
# include <sys/filio.h> // FION#? defines |
|
52 |
# else // __AMIGA__ |
|
53 |
# include <proto/socket.h> |
|
54 |
# endif |
|
55 |
||
56 |
// make source compatible with bsdsocket.library functions |
|
57 |
# define closesocket(s) CloseSocket(s) |
|
58 |
# define GET_LAST_ERROR() Errno() |
|
59 |
# define ioctlsocket(s,request,status) IoctlSocket((LONG)s,(ULONG)request,(char*)status) |
|
60 |
||
61 |
struct Library *SocketBase = NULL; |
|
62 |
#endif /* __MORPHOS__ || __AMIGA__ */ |
|
63 |
||
64 |
||
65 |
#define SEND_MTU 1460 |
|
66 |
||
67 |
#if defined(ENABLE_NETWORK) |
|
68 |
||
69 |
// sent from client -> server whenever the client wants to exec a command. |
|
70 |
// send from server -> client when another player execs a command. |
|
71 |
typedef struct CommandPacket { |
|
72 |
byte packet_length; |
|
73 |
byte packet_type; |
|
74 |
uint16 cmd; |
|
75 |
uint32 p1,p2; |
|
76 |
TileIndex tile; |
|
77 |
byte player;// player id, this is checked by the server. |
|
78 |
byte when; // offset from the current max_frame value minus 1. this is set by the server. |
|
79 |
uint32 dp[8]; |
|
80 |
} CommandPacket; |
|
81 |
||
82 |
assert_compile(sizeof(CommandPacket) == 16 + 32); |
|
83 |
||
84 |
#define COMMAND_PACKET_BASE_SIZE (sizeof(CommandPacket) - 8 * sizeof(uint32)) |
|
85 |
||
86 |
// sent from server -> client periodically to tell the client about the current tick in the server |
|
87 |
// and how far the client may progress. |
|
88 |
typedef struct SyncPacket { |
|
89 |
byte packet_length; |
|
90 |
byte packet_type; |
|
91 |
byte frames; // how many more frames may the client execute? this is relative to the old value of max. |
|
92 |
byte server; // where is the server currently executing? this is negatively relative to the old value of max. |
|
93 |
uint32 random_seed_1; // current random state at server. used to detect out of sync. |
|
94 |
uint32 random_seed_2; |
|
95 |
} SyncPacket; |
|
96 |
||
97 |
assert_compile(sizeof(SyncPacket) == 12); |
|
98 |
||
99 |
// sent from server -> client as an acknowledgement that the server received the command. |
|
100 |
// the command will be executed at the current value of "max". |
|
101 |
typedef struct AckPacket { |
|
102 |
byte packet_length; |
|
103 |
byte packet_type; |
|
104 |
} AckPacket; |
|
105 |
||
106 |
typedef struct FilePacketHdr { |
|
107 |
byte packet_length; |
|
108 |
byte packet_type; |
|
109 |
byte unused[2]; |
|
110 |
} FilePacketHdr; |
|
111 |
||
112 |
assert_compile(sizeof(FilePacketHdr) == 4); |
|
113 |
||
114 |
// sent from server to client when the client has joined. |
|
115 |
typedef struct WelcomePacket { |
|
116 |
byte packet_length; |
|
117 |
byte packet_type; |
|
118 |
byte unused[2]; |
|
119 |
} WelcomePacket; |
|
120 |
||
121 |
typedef struct Packet Packet; |
|
122 |
struct Packet { |
|
123 |
Packet *next; // this one has to be the first element. |
|
124 |
uint siz; |
|
125 |
byte buf[SEND_MTU]; // packet payload |
|
126 |
}; |
|
127 |
||
128 |
typedef struct ClientState { |
|
129 |
int socket; |
|
130 |
bool inactive; // disable sending of commands/syncs to client |
|
131 |
bool writable; |
|
132 |
uint xmitpos; |
|
133 |
||
134 |
uint eaten; |
|
135 |
Packet *head, **last; |
|
136 |
||
137 |
uint buflen; // receive buffer len |
|
138 |
byte buf[256]; // receive buffer |
|
139 |
} ClientState; |
|
140 |
||
141 |
||
142 |
static uint _not_packet; |
|
143 |
||
144 |
typedef struct QueuedCommand QueuedCommand; |
|
145 |
struct QueuedCommand { |
|
146 |
QueuedCommand *next; |
|
147 |
CommandPacket cp; |
|
148 |
CommandCallback *callback; |
|
149 |
uint32 cmd; |
|
150 |
int32 frame; |
|
151 |
}; |
|
152 |
||
153 |
typedef struct CommandQueue CommandQueue; |
|
154 |
struct CommandQueue { |
|
155 |
QueuedCommand *head, **last; |
|
156 |
}; |
|
157 |
||
158 |
#define MAX_CLIENTS (MAX_PLAYERS + 1) |
|
159 |
||
160 |
// packets waiting to be executed, for each of the players. |
|
161 |
// this list is sorted in frame order, so the item on the front will be executed first. |
|
162 |
static CommandQueue _command_queue; |
|
163 |
||
164 |
// in the client, this is the list of commands that have not yet been acked. |
|
165 |
// when it is acked, it will be moved to the appropriate position at the end of the player queue. |
|
166 |
static CommandQueue _ack_queue; |
|
167 |
||
168 |
static ClientState _clients[MAX_CLIENTS]; |
|
169 |
static int _num_clients; |
|
170 |
||
171 |
// keep a history of the 16 most recent seeds to be able to capture out of sync errors. |
|
172 |
static uint32 _my_seed_list[16][2]; |
|
173 |
||
174 |
typedef struct FutureSeeds { |
|
175 |
int32 frame; |
|
176 |
uint32 seed[2]; |
|
177 |
} FutureSeeds; |
|
178 |
||
179 |
// remember some future seeds that the server sent to us. |
|
180 |
static FutureSeeds _future_seed[8]; |
|
181 |
static int _num_future_seed; |
|
182 |
||
183 |
static SOCKET _listensocket; |
|
184 |
static SOCKET _udpsocket; |
|
185 |
||
186 |
||
187 |
typedef struct UDPPacket { |
|
188 |
byte command_code; |
|
189 |
byte data_len; |
|
190 |
byte command_check; |
|
191 |
byte data[255]; |
|
192 |
} UDPPacket; |
|
193 |
||
194 |
enum { |
|
195 |
NET_UDPCMD_SERVERSEARCH = 1, |
|
196 |
NET_UDPCMD_SERVERACTIVE, |
|
197 |
}; |
|
198 |
||
199 |
uint32 _network_ip_list[10]; // Network ips |
|
200 |
char * _network_detected_serverip = "255.255.255.255"; // UDP Broadcast detected server-ip |
|
201 |
uint32 _network_detected_serverport = 0; // UDP Broadcast detected server-port |
|
202 |
||
203 |
void NetworkUDPSend(struct sockaddr_in recv,struct UDPPacket packet); |
|
204 |
||
205 |
static bool _network_synced; |
|
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 |
||
213 |
////////////////////////////////////////////////////////////////////// |
|
214 |
||
215 |
static QueuedCommand *AllocQueuedCommand(CommandQueue *nq) |
|
216 |
{ |
|
217 |
QueuedCommand *qp = (QueuedCommand*)calloc(sizeof(QueuedCommand), 1); |
|
218 |
assert(qp); |
|
219 |
*nq->last = qp; |
|
220 |
nq->last = &qp->next; |
|
221 |
return qp; |
|
222 |
} |
|
223 |
||
224 |
// go through the player queues for each player and see if there are any pending commands |
|
225 |
// that should be executed this frame. if there are, execute them. |
|
226 |
void NetworkProcessCommands() |
|
227 |
{ |
|
228 |
CommandQueue *nq; |
|
229 |
QueuedCommand *qp; |
|
230 |
||
231 |
// queue mode ? |
|
232 |
if (_networking_queuing) |
|
233 |
return; |
|
234 |
||
235 |
nq = &_command_queue; |
|
236 |
while ( (qp=nq->head) && (!_networking_sync || qp->frame <= _frame_counter)) { |
|
237 |
// unlink it. |
|
238 |
if (!(nq->head = qp->next)) nq->last = &nq->head; |
|
239 |
||
240 |
if (qp->frame < _frame_counter && _networking_sync) { |
|
241 |
error("!qp->cp.frame < _frame_counter, %d < %d\n", qp->frame, _frame_counter); |
|
242 |
} |
|
243 |
||
244 |
// run the command |
|
245 |
_current_player = qp->cp.player; |
|
246 |
memcpy(_decode_parameters, qp->cp.dp, (qp->cp.packet_length - COMMAND_PACKET_BASE_SIZE)); |
|
247 |
DoCommandP(qp->cp.tile, qp->cp.p1, qp->cp.p2, qp->callback, qp->cmd | CMD_DONT_NETWORK); |
|
248 |
free(qp); |
|
249 |
} |
|
250 |
||
251 |
if (!_networking_server) { |
|
252 |
// remember the random seed so we can check if we're out of sync. |
|
253 |
_my_seed_list[_frame_counter & 15][0] = _sync_seed_1; |
|
254 |
_my_seed_list[_frame_counter & 15][1] = _sync_seed_2; |
|
255 |
||
256 |
while (_num_future_seed) { |
|
257 |
assert(_future_seed[0].frame >= _frame_counter); |
|
258 |
if (_future_seed[0].frame != _frame_counter) break; |
|
259 |
if (_future_seed[0].seed[0] != _sync_seed_1 ||_future_seed[0].seed[1] != _sync_seed_2) |
|
260 |
error("!network sync error"); |
|
261 |
memcpy_overlapping(_future_seed, _future_seed + 1, --_num_future_seed * sizeof(FutureSeeds)); |
|
262 |
} |
|
263 |
} |
|
264 |
} |
|
265 |
||
266 |
// send a packet to a client |
|
267 |
static void SendBytes(ClientState *cs, void *bytes, uint len) |
|
268 |
{ |
|
269 |
byte *b = (byte*)bytes; |
|
270 |
uint n; |
|
271 |
Packet *p; |
|
272 |
||
273 |
assert(len != 0); |
|
274 |
||
275 |
// see if there's space in the last packet? |
|
276 |
if (!cs->head || (p = (Packet*)cs->last, p->siz == sizeof(p->buf))) |
|
277 |
p = NULL; |
|
278 |
||
279 |
do { |
|
280 |
if (!p) { |
|
281 |
// need to allocate a new packet buffer. |
|
282 |
p = (Packet*)malloc(sizeof(Packet)); |
|
283 |
||
284 |
// insert at the end of the linked list. |
|
285 |
*cs->last = p; |
|
286 |
cs->last = &p->next; |
|
287 |
p->next = NULL; |
|
288 |
p->siz = 0; |
|
289 |
} |
|
290 |
||
291 |
// copy bytes to packet. |
|
292 |
n = minu(sizeof(p->buf) - p->siz, len); |
|
293 |
memcpy(p->buf + p->siz, b, n); |
|
294 |
p->siz += n; |
|
295 |
b += n; |
|
296 |
p = NULL; |
|
297 |
} while (len -= n); |
|
298 |
} |
|
299 |
||
300 |
// client: |
|
301 |
// add it to the client's ack queue, and send the command to the server |
|
302 |
// server: |
|
303 |
// add it to the server's player queue, and send it to all clients. |
|
304 |
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) |
|
305 |
{ |
|
306 |
int nump; |
|
307 |
QueuedCommand *qp; |
|
308 |
ClientState *cs; |
|
309 |
||
310 |
qp = AllocQueuedCommand(_networking_server ? &_command_queue : &_ack_queue); |
|
311 |
qp->cp.packet_type = 0; |
|
312 |
qp->cp.tile = tile; |
|
313 |
qp->cp.p1 = p1; |
|
314 |
qp->cp.p2 = p2; |
|
315 |
qp->cp.cmd = (uint16)cmd; |
|
316 |
qp->cp.player = _local_player; |
|
317 |
qp->cp.when = 0; |
|
318 |
qp->cmd = cmd; |
|
319 |
qp->callback = callback; |
|
320 |
||
321 |
// so the server knows when to execute it. |
|
322 |
qp->frame = _frame_counter_max; |
|
323 |
||
324 |
// calculate the amount of extra bytes. |
|
325 |
nump = 8; |
|
326 |
while ( nump != 0 && ((uint32*)_decode_parameters)[nump-1] == 0) nump--; |
|
327 |
qp->cp.packet_length = COMMAND_PACKET_BASE_SIZE + nump * sizeof(uint32); |
|
328 |
if (nump != 0) memcpy(qp->cp.dp, _decode_parameters, nump * sizeof(uint32)); |
|
329 |
||
330 |
#if defined(TTD_BIG_ENDIAN) |
|
331 |
// need to convert the command to little endian before sending it. |
|
332 |
{ |
|
333 |
CommandPacket cp; |
|
334 |
cp = qp->cp; |
|
335 |
cp.cmd = TO_LE16(cp.cmd); |
|
336 |
cp.tile = TO_LE16(cp.tile); |
|
337 |
cp.p1 = TO_LE32(cp.p1); |
|
338 |
cp.p2 = TO_LE32(cp.p2); |
|
339 |
for(cs=_clients; cs->socket != INVALID_SOCKET; cs++) if (!cs->inactive) SendBytes(cs, &cp, cp.packet_length); |
|
340 |
} |
|
341 |
#else |
|
342 |
// send it to the peers |
|
343 |
for(cs=_clients; cs->socket != INVALID_SOCKET; cs++) if (!cs->inactive) SendBytes(cs, &qp->cp, qp->cp.packet_length); |
|
344 |
||
345 |
#endif |
|
346 |
} |
|
347 |
||
348 |
// client: |
|
349 |
// server sends a command from another player that we should execute. |
|
350 |
// put it in the command queue. |
|
351 |
// |
|
352 |
// server: |
|
353 |
// client sends a command that it wants to execute. |
|
354 |
// fill the when field so the client knows when to execute it. |
|
355 |
// put it in the appropriate player queue. |
|
356 |
// send it to all other clients. |
|
357 |
// send an ack packet to the actual client. |
|
358 |
||
359 |
static void HandleCommandPacket(ClientState *cs, CommandPacket *np) |
|
360 |
{ |
|
361 |
QueuedCommand *qp; |
|
362 |
ClientState *c; |
|
363 |
AckPacket ap; |
|
364 |
||
365 |
printf("net: cmd size %d\n", np->packet_length); |
|
366 |
||
367 |
assert(np->packet_length >= COMMAND_PACKET_BASE_SIZE); |
|
368 |
||
369 |
// put it into the command queue |
|
370 |
qp = AllocQueuedCommand(&_command_queue); |
|
371 |
qp->cp = *np; |
|
372 |
||
373 |
qp->frame = _frame_counter_max; |
|
374 |
||
375 |
qp->callback = NULL; |
|
376 |
||
377 |
// extra params |
|
378 |
memcpy(&qp->cp.dp, np->dp, np->packet_length - COMMAND_PACKET_BASE_SIZE); |
|
379 |
||
380 |
ap.packet_type = 2; |
|
381 |
ap.packet_length = 2; |
|
382 |
||
383 |
// send it to the peers |
|
384 |
if (_networking_server) { |
|
385 |
for(c=_clients; c->socket != INVALID_SOCKET; c++) { |
|
386 |
if (c == cs) { |
|
387 |
SendBytes(c, &ap, 2); |
|
388 |
} else { |
|
389 |
if (!cs->inactive) SendBytes(c, &qp->cp, qp->cp.packet_length); |
|
390 |
} |
|
391 |
} |
|
392 |
} |
|
393 |
||
394 |
// convert from little endian to big endian? |
|
395 |
#if defined(TTD_BIG_ENDIAN) |
|
396 |
qp->cp.cmd = TO_LE16(qp->cp.cmd); |
|
397 |
qp->cp.tile = TO_LE16(qp->cp.tile); |
|
398 |
qp->cp.p1 = TO_LE32(qp->cp.p1); |
|
399 |
qp->cp.p2 = TO_LE32(qp->cp.p2); |
|
400 |
#endif |
|
401 |
||
402 |
qp->cmd = qp->cp.cmd; |
|
403 |
} |
|
404 |
||
405 |
// sent from server -> client periodically to tell the client about the current tick in the server |
|
406 |
// and how far the client may progress. |
|
407 |
static void HandleSyncPacket(SyncPacket *sp) |
|
408 |
{ |
|
409 |
uint32 s1,s2; |
|
410 |
||
411 |
_frame_counter_srv = _frame_counter_max - sp->server; |
|
412 |
_frame_counter_max += sp->frames; |
|
413 |
||
414 |
printf("net: sync max=%d cur=%d server=%d\n", _frame_counter_max, _frame_counter, _frame_counter_srv); |
|
415 |
||
416 |
// queueing only? |
|
417 |
if (_networking_queuing || _frame_counter == 0) |
|
418 |
return; |
|
419 |
||
420 |
s1 = TO_LE32(sp->random_seed_1); |
|
421 |
s2 = TO_LE32(sp->random_seed_2); |
|
422 |
||
423 |
if (_frame_counter_srv <= _frame_counter) { |
|
424 |
// we are ahead of the server check if the seed is in our list. |
|
425 |
if (_frame_counter_srv + 16 > _frame_counter) { |
|
426 |
// the random seed exists in our array check it. |
|
427 |
if (s1 != _my_seed_list[_frame_counter_srv & 0xF][0] || s2 != _my_seed_list[_frame_counter_srv & 0xF][1]) |
|
428 |
error("!network is desynched\n"); |
|
429 |
} |
|
430 |
} else { |
|
431 |
// the server's frame has not been executed yet. store the server's seed in a list. |
|
432 |
if (_num_future_seed < lengthof(_future_seed)) { |
|
433 |
_future_seed[_num_future_seed].frame = _frame_counter_srv; |
|
434 |
_future_seed[_num_future_seed].seed[0] = s1; |
|
435 |
_future_seed[_num_future_seed].seed[1] = s2; |
|
436 |
_num_future_seed++; |
|
437 |
} |
|
438 |
} |
|
439 |
} |
|
440 |
||
441 |
// sent from server -> client as an acknowledgement that the server received the command. |
|
442 |
// the command will be executed at the current value of "max". |
|
443 |
static void HandleAckPacket() |
|
444 |
{ |
|
445 |
QueuedCommand *q; |
|
446 |
// move a packet from the ack queue to the end of this player's queue. |
|
447 |
q = _ack_queue.head; |
|
448 |
assert(q); |
|
449 |
if (!(_ack_queue.head = q->next)) _ack_queue.last = &_ack_queue.head; |
|
450 |
q->next = NULL; |
|
451 |
q->frame = _frame_counter_max; |
|
452 |
||
453 |
*_command_queue.last = q; |
|
454 |
_command_queue.last = &q->next; |
|
455 |
||
456 |
printf("net: ack\n"); |
|
457 |
} |
|
458 |
||
459 |
static void HandleFilePacket(FilePacketHdr *fp) |
|
460 |
{ |
|
461 |
int n = fp->packet_length - sizeof(FilePacketHdr); |
|
462 |
if (n == 0) { |
|
463 |
assert(_networking_queuing); |
|
464 |
assert(!_networking_sync); |
|
465 |
// eof |
|
466 |
if (_recv_file) { fclose(_recv_file); _recv_file = NULL; } |
|
467 |
||
468 |
// attempt loading the game. |
|
469 |
_game_mode = GM_NORMAL; |
|
470 |
if (SaveOrLoad("networkc.tmp", SL_LOAD) != SL_OK) error("network load failed"); |
|
471 |
||
472 |
// sync to server. |
|
473 |
_networking_queuing = false; |
|
474 |
||
475 |
_networking_sync = true; |
|
476 |
_frame_counter = 0; // start executing at frame 0. |
|
477 |
_sync_seed_1 = _sync_seed_2 = 0; |
|
478 |
_num_future_seed = 0; |
|
479 |
memset(_my_seed_list, 0, sizeof(_my_seed_list)); |
|
480 |
||
481 |
if (_network_playas == 0) { |
|
482 |
// send a command to make a new player |
|
483 |
_local_player = 0; |
|
484 |
NetworkSendCommand(0, 0, 0, CMD_PLAYER_CTRL, NULL); |
|
1 | 485 |
_local_player = OWNER_SPECTATOR; |
0 | 486 |
} else { |
487 |
// take control over an existing company |
|
488 |
if (DEREF_PLAYER(_network_playas-1)->is_active) |
|
489 |
_local_player = _network_playas-1; |
|
490 |
else |
|
1 | 491 |
_local_player = OWNER_SPECTATOR; |
0 | 492 |
} |
493 |
||
494 |
} else { |
|
495 |
if(!_recv_file) { |
|
496 |
_recv_file = fopen("networkc.tmp", "wb"); |
|
497 |
if (!_recv_file) error("can't open savefile"); |
|
498 |
} |
|
499 |
fwrite( (char*)fp + sizeof(*fp), n, 1, _recv_file); |
|
500 |
} |
|
501 |
} |
|
502 |
||
503 |
static void CloseClient(ClientState *cs) |
|
504 |
{ |
|
505 |
Packet *p, *next; |
|
506 |
||
507 |
printf("CloseClient\n"); |
|
508 |
||
509 |
assert(cs->socket != INVALID_SOCKET); |
|
510 |
||
511 |
closesocket(cs->socket); |
|
512 |
||
513 |
// free buffers |
|
514 |
for(p = cs->head; p; p=next) { |
|
515 |
next = p->next; |
|
516 |
free(p); |
|
517 |
} |
|
518 |
||
519 |
// copy up structs... |
|
520 |
while ((cs+1)->socket != INVALID_SOCKET) { |
|
521 |
*cs = *(cs+1); |
|
522 |
cs++; |
|
523 |
} |
|
524 |
cs->socket = INVALID_SOCKET; |
|
525 |
||
526 |
_num_clients--; |
|
527 |
} |
|
528 |
||
529 |
#define NETWORK_BUFFER_SIZE 4096 |
|
530 |
static bool ReadPackets(ClientState *cs) |
|
531 |
{ |
|
532 |
byte network_buffer[NETWORK_BUFFER_SIZE]; |
|
533 |
uint pos,size; |
|
534 |
unsigned long recv_bytes; |
|
535 |
||
536 |
size = cs->buflen; |
|
537 |
||
538 |
for(;;) { |
|
539 |
if (size != 0) memcpy(network_buffer, cs->buf, size); |
|
540 |
||
541 |
recv_bytes = recv(cs->socket, (char*)network_buffer + size, sizeof(network_buffer) - size, 0); |
|
542 |
if ( recv_bytes == (unsigned long)-1) { |
|
543 |
int err = GET_LAST_ERROR(); |
|
544 |
if (err == EWOULDBLOCK) break; |
|
545 |
printf("net: recv() failed with error %d\n", err); |
|
546 |
CloseClient(cs); |
|
547 |
return false; |
|
548 |
} |
|
549 |
// no more bytes for now? |
|
550 |
if (recv_bytes == 0) |
|
551 |
break; |
|
552 |
||
553 |
size += recv_bytes; // number of bytes read. |
|
554 |
pos = 0; |
|
555 |
while (size >= 2) { |
|
556 |
byte *packet = network_buffer + pos; |
|
557 |
// whole packet not there yet? |
|
558 |
if (size < packet[0]) break; |
|
559 |
size -= packet[0]; |
|
560 |
pos += packet[0]; |
|
561 |
||
562 |
switch(packet[1]) { |
|
563 |
case 0: |
|
564 |
HandleCommandPacket(cs, (CommandPacket*)packet); |
|
565 |
break; |
|
566 |
case 1: |
|
567 |
assert(_networking_sync || _networking_queuing); |
|
568 |
assert(!_networking_server); |
|
569 |
HandleSyncPacket((SyncPacket*)packet); |
|
570 |
break; |
|
571 |
case 2: |
|
572 |
assert(!_networking_server); |
|
573 |
HandleAckPacket(); |
|
574 |
break; |
|
575 |
case 3: |
|
576 |
HandleFilePacket((FilePacketHdr*)packet); |
|
577 |
break; |
|
578 |
default: |
|
579 |
error("unknown packet type"); |
|
580 |
} |
|
581 |
} |
|
582 |
||
583 |
assert(size>=0 && size < sizeof(cs->buf)); |
|
584 |
||
585 |
memcpy(cs->buf, network_buffer + pos, size); |
|
586 |
} |
|
587 |
||
588 |
cs->buflen = size; |
|
589 |
||
590 |
return true; |
|
591 |
} |
|
592 |
||
593 |
||
594 |
static bool SendPackets(ClientState *cs) |
|
595 |
{ |
|
596 |
Packet *p; |
|
597 |
int n; |
|
598 |
uint nskip = cs->eaten, nsent = nskip; |
|
599 |
||
600 |
// try sending as much as possible. |
|
601 |
for(p=cs->head; p ;p = p->next) { |
|
602 |
if (p->siz) { |
|
603 |
assert(nskip < p->siz); |
|
604 |
||
605 |
n = send(cs->socket, p->buf + nskip, p->siz - nskip, 0); |
|
606 |
if (n == -1) { |
|
607 |
int err = GET_LAST_ERROR(); |
|
608 |
if (err == EWOULDBLOCK) break; |
|
609 |
printf("net: send() failed with error %d\n", err); |
|
610 |
CloseClient(cs); |
|
611 |
return false; |
|
612 |
} |
|
613 |
nsent += n; |
|
614 |
// send was not able to send it all? then we assume that the os buffer is full and break. |
|
615 |
if (nskip + n != p->siz) |
|
616 |
break; |
|
617 |
nskip = 0; |
|
618 |
} |
|
619 |
} |
|
620 |
||
621 |
// nsent bytes in the linked list are not invalid. free as many buffers as possible. |
|
622 |
// don't actually free the last buffer. |
|
623 |
while (nsent) { |
|
624 |
p = cs->head; |
|
625 |
assert(p->siz != 0); |
|
626 |
||
627 |
// some bytes of the packet are still unsent. |
|
628 |
if ( (int)(nsent - p->siz) < 0) |
|
629 |
break; |
|
630 |
nsent -= p->siz; |
|
631 |
p->siz = 0; |
|
632 |
if (p->next) { |
|
633 |
cs->head = p->next; |
|
634 |
free(p); |
|
635 |
} |
|
636 |
} |
|
637 |
||
638 |
cs->eaten = nsent; |
|
639 |
||
640 |
return true; |
|
641 |
} |
|
642 |
||
643 |
// transmit the file.. |
|
644 |
static void SendXmit(ClientState *cs) |
|
645 |
{ |
|
646 |
uint pos, n; |
|
647 |
FilePacketHdr hdr; |
|
648 |
int p; |
|
649 |
||
650 |
// if too many unsent bytes left in buffer, don't send more. |
|
651 |
if (cs->head && cs->head->next) |
|
652 |
return; |
|
653 |
||
654 |
pos = cs->xmitpos - 1; |
|
655 |
||
656 |
p = 20; |
|
657 |
do { |
|
658 |
// compute size of data to xmit |
|
659 |
n = minu(_transmit_file_size - pos, 248); |
|
660 |
||
661 |
hdr.packet_length = n + sizeof(hdr); |
|
662 |
hdr.packet_type = 3; |
|
663 |
hdr.unused[0] = hdr.unused[1] = 0; |
|
664 |
SendBytes(cs, &hdr, sizeof(hdr)); |
|
665 |
||
666 |
if (n == 0) { |
|
667 |
pos = -1; // eof |
|
668 |
break; |
|
669 |
} |
|
670 |
SendBytes(cs, _transmit_file + pos, n); |
|
671 |
pos += n; |
|
672 |
} while (--p); |
|
673 |
||
674 |
cs->xmitpos = pos + 1; |
|
675 |
||
676 |
printf("net: client xmit at %d\n", pos + 1); |
|
677 |
} |
|
678 |
||
679 |
static ClientState *AllocClient(SOCKET s) |
|
680 |
{ |
|
681 |
ClientState *cs; |
|
682 |
||
683 |
if (_num_clients == MAX_CLIENTS) |
|
684 |
return NULL; |
|
685 |
||
686 |
cs = &_clients[_num_clients++]; |
|
687 |
memset(cs, 0, sizeof(*cs)); |
|
688 |
cs->last = &cs->head; |
|
689 |
cs->socket = s; |
|
690 |
return cs; |
|
691 |
} |
|
692 |
||
693 |
||
694 |
static void NetworkAcceptClients() |
|
695 |
{ |
|
696 |
struct sockaddr_in sin; |
|
697 |
SOCKET s; |
|
698 |
ClientState *cs; |
|
699 |
#ifndef __MORPHOS__ |
|
700 |
int sin_len; |
|
701 |
#else |
|
702 |
LONG sin_len; // for some reason we need a 'LONG' under MorphOS |
|
703 |
#endif |
|
704 |
||
705 |
assert(_listensocket != INVALID_SOCKET); |
|
706 |
||
707 |
for(;;) { |
|
708 |
sin_len = sizeof(sin); |
|
709 |
s = accept(_listensocket, (struct sockaddr*)&sin, &sin_len); |
|
710 |
if (s == INVALID_SOCKET) return; |
|
711 |
||
712 |
// set nonblocking mode for client socket |
|
713 |
{ unsigned long blocking = 1; ioctlsocket(s, FIONBIO, &blocking); } |
|
714 |
||
715 |
printf("net: got client from %s\n", inet_ntoa(sin.sin_addr)); |
|
716 |
||
717 |
// set nodelay |
|
718 |
{int b = 1; setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b));} |
|
719 |
||
720 |
cs = AllocClient(s); |
|
721 |
if (cs == NULL) { |
|
722 |
// no more clients allowed? |
|
723 |
closesocket(s); |
|
724 |
continue; |
|
725 |
} |
|
726 |
||
727 |
if (_networking_sync) { |
|
728 |
// a new client has connected. it needs a snapshot. |
|
729 |
cs->inactive = true; |
|
730 |
} |
|
731 |
} |
|
732 |
||
733 |
// when a new client has joined. it needs different information depending on if it's at the game menu or in an active game. |
|
734 |
// Game menu: |
|
735 |
// - list of players already in the game (name, company name, face, color) |
|
736 |
// - list of game settings and patch settings |
|
737 |
// Active game: |
|
738 |
// - the state of the world (includes player name, company name, player face, player color) |
|
739 |
// - list of the patch settings |
|
740 |
||
741 |
// Networking can be in several "states". |
|
742 |
// * 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. |
|
743 |
// * sync - games are in sync |
|
744 |
} |
|
745 |
||
746 |
||
747 |
static void SendQueuedCommandsToNewClient(ClientState *cs) |
|
748 |
{ |
|
749 |
// send the commands in the server queue to the new client. |
|
750 |
QueuedCommand *qp; |
|
751 |
SyncPacket sp; |
|
752 |
int32 frame; |
|
753 |
||
754 |
printf("net: sending queued commands to client\n"); |
|
755 |
||
756 |
sp.packet_length = sizeof(sp); |
|
757 |
sp.packet_type = 1; |
|
758 |
sp.random_seed_1 = sp.random_seed_2 = 0; |
|
759 |
sp.server = 0; |
|
760 |
||
761 |
frame = _frame_counter; |
|
762 |
||
763 |
for(qp=_command_queue.head; qp; qp = qp->next) { |
|
764 |
printf("net: sending cmd to be executed at %d (old %d)\n", qp->frame, frame); |
|
765 |
if (qp->frame > frame) { |
|
766 |
assert(qp->frame <= _frame_counter_max); |
|
767 |
sp.frames = qp->frame - frame; |
|
768 |
frame = qp->frame; |
|
769 |
SendBytes(cs, &sp, sizeof(sp)); |
|
770 |
} |
|
771 |
SendBytes(cs, &qp->cp, qp->cp.packet_length); |
|
772 |
} |
|
773 |
||
774 |
if (frame < _frame_counter_max) { |
|
775 |
printf("net: sending queued sync %d (%d)\n", _frame_counter_max, frame); |
|
776 |
sp.frames = _frame_counter_max - frame; |
|
777 |
SendBytes(cs, &sp, sizeof(sp)); |
|
778 |
} |
|
779 |
||
780 |
} |
|
781 |
||
782 |
void NetworkReceive() |
|
783 |
{ |
|
784 |
ClientState *cs; |
|
785 |
int n; |
|
786 |
fd_set read_fd, write_fd; |
|
787 |
struct timeval tv; |
|
788 |
||
789 |
NetworkUDPReceive(); // udp handling |
|
790 |
||
791 |
FD_ZERO(&read_fd); |
|
792 |
FD_ZERO(&write_fd); |
|
793 |
||
794 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
795 |
FD_SET(cs->socket, &read_fd); |
|
796 |
FD_SET(cs->socket, &write_fd); |
|
797 |
} |
|
798 |
||
799 |
// take care of listener port |
|
800 |
if (_networking_server) { |
|
801 |
FD_SET(_listensocket, &read_fd); |
|
802 |
} |
|
803 |
||
804 |
tv.tv_sec = tv.tv_usec = 0; // don't block at all. |
|
805 |
#if !defined(__MORPHOS__) && !defined(__AMIGA__) |
|
806 |
n = select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv); |
|
807 |
#else |
|
808 |
n = WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL); |
|
809 |
#endif |
|
810 |
if (n == -1) error("select failed"); |
|
811 |
||
812 |
// accept clients.. |
|
813 |
if (_networking_server && FD_ISSET(_listensocket, &read_fd)) |
|
814 |
NetworkAcceptClients(); |
|
815 |
||
816 |
// read stuff from clients |
|
817 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
818 |
cs->writable = !!FD_ISSET(cs->socket, &write_fd); |
|
819 |
if (FD_ISSET(cs->socket, &read_fd)) { |
|
820 |
if (!ReadPackets(cs)) |
|
821 |
cs--; |
|
822 |
} |
|
823 |
} |
|
824 |
||
825 |
// 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. |
|
826 |
if (_networking_server && _transmit_file == NULL) { |
|
827 |
bool didsave = false; |
|
828 |
||
829 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
830 |
if (cs->inactive) { |
|
831 |
cs->inactive = false; |
|
832 |
// found a client waiting for a snapshot. make a snapshot. |
|
833 |
if (!didsave) { |
|
834 |
char filename[256]; |
|
835 |
sprintf(filename, "%snetwork.tmp", _path.autosave_dir); |
|
836 |
didsave = true; |
|
837 |
if (SaveOrLoad(filename, SL_SAVE) != SL_OK) error("network savedump failed"); |
|
838 |
_transmit_file = ReadFileToMem(filename, &_transmit_file_size, 500000); |
|
839 |
if (_transmit_file == NULL) error("network savedump failed to load"); |
|
840 |
} |
|
841 |
// and start sending the file.. |
|
842 |
cs->xmitpos = 1; |
|
843 |
||
844 |
// send queue of commands to client. |
|
845 |
SendQueuedCommandsToNewClient(cs); |
|
846 |
} |
|
847 |
} |
|
848 |
} |
|
849 |
} |
|
850 |
||
851 |
void NetworkSend() |
|
852 |
{ |
|
853 |
ClientState *cs; |
|
854 |
void *free_xmit; |
|
855 |
||
856 |
// send sync packets? |
|
857 |
if (_networking_server && _networking_sync && !_pause) { |
|
858 |
if (++_not_packet >= _network_sync_freq) { |
|
859 |
SyncPacket sp; |
|
860 |
uint new_max; |
|
861 |
||
862 |
_not_packet = 0; |
|
863 |
||
864 |
new_max = max(_frame_counter + (int)_network_ahead_frames, _frame_counter_max); |
|
865 |
||
866 |
sp.packet_length = sizeof(sp); |
|
867 |
sp.packet_type = 1; |
|
868 |
sp.frames = new_max - _frame_counter_max; |
|
869 |
sp.server = _frame_counter_max - _frame_counter; |
|
870 |
sp.random_seed_1 = TO_LE32(_sync_seed_1); |
|
871 |
sp.random_seed_2 = TO_LE32(_sync_seed_2); |
|
872 |
_frame_counter_max = new_max; |
|
873 |
||
874 |
// send it to all the clients |
|
875 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) SendBytes(cs, &sp, sizeof(sp)); |
|
876 |
} |
|
877 |
} |
|
878 |
||
879 |
free_xmit = _transmit_file; |
|
880 |
||
881 |
// send stuff to all clients |
|
882 |
for(cs=_clients;cs->socket != INVALID_SOCKET; cs++) { |
|
883 |
if (cs->xmitpos) { |
|
884 |
if (cs->writable) |
|
885 |
SendXmit(cs); |
|
886 |
free_xmit = NULL; |
|
887 |
} |
|
888 |
if (cs->writable) { |
|
889 |
if (!SendPackets(cs)) cs--; |
|
890 |
} |
|
891 |
} |
|
892 |
||
893 |
// no clients left that xmit the file, free it. |
|
894 |
if (free_xmit) { |
|
895 |
_transmit_file = NULL; |
|
896 |
free(free_xmit); |
|
897 |
} |
|
898 |
} |
|
899 |
||
900 |
void NetworkConnect(const char *hostname, int port) |
|
901 |
{ |
|
902 |
SOCKET s; |
|
903 |
struct sockaddr_in sin; |
|
904 |
int b; |
|
905 |
||
906 |
||
907 |
s = socket(AF_INET, SOCK_STREAM, 0); |
|
908 |
if (s == INVALID_SOCKET) error("socket() failed"); |
|
909 |
||
910 |
b = 1; |
|
911 |
setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b)); |
|
912 |
||
913 |
if (strcmp(hostname,"auto")==0) { |
|
914 |
// autodetect server over udp broadcast [works 4 lan] |
|
915 |
if (NetworkUDPSearchServer()) { |
|
916 |
hostname=_network_detected_serverip; |
|
917 |
port=_network_detected_serverport; |
|
918 |
} else { |
|
919 |
error("udp: server not found"); |
|
920 |
} |
|
921 |
} |
|
922 |
||
923 |
sin.sin_family = AF_INET; |
|
924 |
sin.sin_addr.s_addr = inet_addr(hostname); |
|
925 |
sin.sin_port = htons(port); |
|
926 |
||
927 |
if (connect(s, (struct sockaddr*) &sin, sizeof(sin)) != 0) |
|
928 |
error("connect() failed"); |
|
929 |
||
930 |
// set nonblocking mode for socket.. |
|
931 |
{ unsigned long blocking = 1; ioctlsocket(s, FIONBIO, &blocking); } |
|
932 |
||
933 |
// in client mode, only the first client field is used. it's pointing to the server. |
|
934 |
AllocClient(s); |
|
935 |
||
936 |
// queue packets.. because we're waiting for the savegame. |
|
937 |
_networking_queuing = true; |
|
938 |
_frame_counter_max = 0; |
|
939 |
||
940 |
} |
|
941 |
||
942 |
void NetworkListen(int port) |
|
943 |
{ |
|
944 |
||
945 |
SOCKET ls; |
|
946 |
struct sockaddr_in sin; |
|
947 |
||
948 |
ls = socket(AF_INET, SOCK_STREAM, 0); |
|
949 |
if (ls == INVALID_SOCKET) |
|
950 |
error("socket() on listen socket failed"); |
|
951 |
||
952 |
// reuse the socket |
|
953 |
{ |
|
954 |
int reuse = 1; if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) == -1) |
|
955 |
error("setsockopt() on listen socket failed"); |
|
956 |
} |
|
957 |
||
958 |
// set nonblocking mode for socket |
|
959 |
{ unsigned long blocking = 1; ioctlsocket(ls, FIONBIO, &blocking); } |
|
960 |
||
961 |
sin.sin_family = AF_INET; |
|
962 |
sin.sin_addr.s_addr = 0; |
|
963 |
sin.sin_port = htons(port); |
|
964 |
||
965 |
if (bind(ls, (struct sockaddr*)&sin, sizeof(sin)) != 0) |
|
966 |
error("bind() failed"); |
|
967 |
||
968 |
if (listen(ls, 1) != 0) |
|
969 |
error("listen() failed"); |
|
970 |
||
971 |
_listensocket = ls; |
|
972 |
} |
|
973 |
||
1 | 974 |
void NetworkInitialize(const char *hostname) |
0 | 975 |
{ |
976 |
ClientState *cs; |
|
1 | 977 |
|
0 | 978 |
#if defined(WIN32) |
979 |
WSADATA wsa; |
|
980 |
if (WSAStartup(MAKEWORD(2,0), &wsa) != 0) |
|
981 |
error("WSAStartup failed"); |
|
982 |
#endif |
|
983 |
||
984 |
#if defined(__MORPHOS__) || defined(__AMIGA__) |
|
985 |
if (!(SocketBase = OpenLibrary("bsdsocket.library", 4))) { |
|
986 |
error("Couldn't open bsdsocket.library version 4."); |
|
987 |
} |
|
988 |
#endif |
|
989 |
||
990 |
_command_queue.last = &_command_queue.head; |
|
991 |
_ack_queue.last = &_ack_queue.head; |
|
992 |
||
993 |
// invalidate all clients |
|
994 |
for(cs=_clients; cs != &_clients[MAX_CLIENTS]; cs++) |
|
995 |
cs->socket = INVALID_SOCKET; |
|
996 |
||
1 | 997 |
/* startup udp listener |
998 |
* - only if this instance is a server, so clients can find it |
|
999 |
* OR |
|
1000 |
* - a client, wanting to find a server to connect to |
|
1001 |
*/ |
|
1002 |
if (hostname == NULL || strcmp(hostname,"auto") == 0) { |
|
1003 |
printf("Trying to open UDP port...\n"); |
|
1004 |
NetworkUDPListen(_network_port); |
|
1005 |
} |
|
0 | 1006 |
} |
1007 |
||
1008 |
void NetworkShutdown() |
|
1009 |
{ |
|
1010 |
#if defined(__MORPHOS__) || defined(__AMIGA__) |
|
1011 |
if (SocketBase) { |
|
1012 |
CloseLibrary(SocketBase); |
|
1013 |
} |
|
1014 |
#endif |
|
1015 |
} |
|
1016 |
||
1017 |
// switch to synced mode. |
|
1018 |
void NetworkStartSync() |
|
1019 |
{ |
|
1020 |
_networking_sync = true; |
|
1021 |
_frame_counter = 0; |
|
1022 |
_frame_counter_max = 0; |
|
1023 |
_frame_counter_srv = 0; |
|
1024 |
_num_future_seed = 0; |
|
1025 |
_sync_seed_1 = _sync_seed_2 = 0; |
|
1026 |
memset(_my_seed_list, 0, sizeof(_my_seed_list)); |
|
1027 |
} |
|
1028 |
||
1029 |
// ************************** // |
|
1030 |
// * UDP Network Extensions * // |
|
1031 |
// ************************** // |
|
1032 |
||
1033 |
/* multi os compatible sleep function */ |
|
1034 |
void CSleep(int milliseconds) { |
|
1035 |
#if defined(WIN32) |
|
1036 |
Sleep(milliseconds); |
|
1037 |
#endif |
|
1038 |
#if defined(UNIX) |
|
1039 |
#ifndef __BEOS__ |
|
1040 |
usleep(milliseconds*1000); |
|
1041 |
#endif |
|
1042 |
#ifdef __BEOS__ |
|
1043 |
snooze(milliseconds*1000); |
|
1044 |
#endif |
|
1045 |
#endif |
|
1046 |
} |
|
1047 |
||
1048 |
void NetworkUDPListen(int port) |
|
1049 |
{ |
|
1050 |
SOCKET udp; |
|
1051 |
struct sockaddr_in sin; |
|
1052 |
||
1053 |
DEBUG(misc,0) ("udp: listener initiated on port %i", port); |
|
1054 |
NetworkIPListInit(); |
|
1055 |
||
1056 |
udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
|
1057 |
if (udp == INVALID_SOCKET) |
|
1058 |
error("udp: socket() on listen socket failed"); |
|
1059 |
||
1060 |
// set nonblocking mode for socket |
|
1061 |
{ unsigned long blocking = 1; ioctlsocket(udp, FIONBIO, &blocking); } |
|
1062 |
||
1063 |
sin.sin_family = AF_INET; |
|
1064 |
sin.sin_addr.s_addr = 0; |
|
1065 |
sin.sin_port = htons(port); |
|
1066 |
||
1067 |
if (bind(udp, (struct sockaddr*)&sin, sizeof(sin)) != 0) |
|
1068 |
error("udp: bind() failed"); |
|
1069 |
||
1070 |
// enable broadcasting |
|
1071 |
{ unsigned long val=1; setsockopt(udp, SOL_SOCKET, SO_BROADCAST, (char *) &val , sizeof(val)); } |
|
1072 |
||
1073 |
_udpsocket = udp; |
|
1074 |
||
1075 |
} |
|
1076 |
||
1077 |
void NetworkUDPReceive() { |
|
1078 |
struct sockaddr_in client_addr; |
|
1079 |
int client_len; |
|
1080 |
int nbytes; |
|
1081 |
struct UDPPacket packet; |
|
1082 |
int packet_len; |
|
1083 |
||
1084 |
packet_len = sizeof(packet); |
|
1085 |
client_len = sizeof(client_addr); |
|
1086 |
||
1087 |
nbytes = recvfrom(_udpsocket, (char *) &packet, packet_len , 0, (struct sockaddr *) &client_addr, &client_len); |
|
1088 |
if (nbytes>0) { |
|
1089 |
if (packet.command_code==packet.command_check) switch (packet.command_code) { |
|
1090 |
||
1091 |
case NET_UDPCMD_SERVERSEARCH: |
|
1092 |
if (_networking_server) { |
|
1093 |
packet.command_check=packet.command_code=NET_UDPCMD_SERVERACTIVE; |
|
1094 |
NetworkUDPSend(client_addr, packet); |
|
1095 |
} |
|
1096 |
break; |
|
1097 |
case NET_UDPCMD_SERVERACTIVE: |
|
1098 |
if (!_networking_server) { |
|
1099 |
_network_detected_serverip=inet_ntoa(*(struct in_addr *) &client_addr.sin_addr); |
|
1100 |
_network_detected_serverport=ntohs(client_addr.sin_port); |
|
1101 |
} |
|
1102 |
break; |
|
1103 |
} |
|
1104 |
} |
|
1105 |
} |
|
1106 |
||
1107 |
void NetworkIPListInit() { |
|
1108 |
struct hostent* he; |
|
1109 |
char hostname[250]; |
|
1110 |
uint32 bcaddr; |
|
1111 |
int i=0; |
|
1112 |
||
1113 |
_network_detected_serverip=""; |
|
1114 |
||
1115 |
gethostname(hostname,250); |
|
1116 |
DEBUG(misc,0) ("iplist: init for host %s", hostname); |
|
1117 |
he=gethostbyname((char *) hostname); |
|
1118 |
||
81 | 1119 |
if (he == NULL) { |
1120 |
DEBUG(misc, 0) ("iplist: gethostbyname failed for host %s...trying with IP address", hostname); |
|
1121 |
bcaddr = inet_addr(hostname); |
|
1122 |
he = gethostbyaddr(inet_ntoa(*(struct in_addr *)bcaddr), sizeof(bcaddr), AF_INET); |
|
1123 |
} |
|
1124 |
||
1125 |
if (he == NULL) { |
|
1126 |
DEBUG(misc, 0) ("iplist: cannot resolve %s", hostname); |
|
1127 |
} else { |
|
1128 |
while(he->h_addr_list[i]) { |
|
1129 |
bcaddr = inet_addr(inet_ntoa(*(struct in_addr *) he->h_addr_list[i])); |
|
1130 |
_network_ip_list[i]=bcaddr; |
|
1131 |
DEBUG(misc,0) ("iplist: add %s",inet_ntoa(*(struct in_addr *) he->h_addr_list[i])); |
|
1132 |
i++; |
|
1133 |
} |
|
0 | 1134 |
} |
1135 |
_network_ip_list[i]=0; |
|
1136 |
||
1137 |
} |
|
1138 |
||
1139 |
void NetworkUDPBroadCast(struct UDPPacket packet) { |
|
1140 |
int i=0, res; |
|
1141 |
struct sockaddr_in out_addr; |
|
1142 |
uint32 bcaddr; |
|
1143 |
byte * bcptr; |
|
1144 |
while (_network_ip_list[i]!=0) { |
|
1145 |
bcaddr=_network_ip_list[i]; |
|
1146 |
out_addr.sin_family = AF_INET; |
|
1147 |
out_addr.sin_port = htons(_network_port); |
|
1148 |
bcptr = (byte *) &bcaddr; |
|
1149 |
bcptr[3]=255; |
|
1150 |
out_addr.sin_addr.s_addr = bcaddr; |
|
1151 |
res=sendto(_udpsocket,(char *) &packet,sizeof(packet),0,(struct sockaddr *) &out_addr,sizeof(out_addr)); |
|
1152 |
if (res==-1) error("udp: broadcast error: %i",GET_LAST_ERROR()); |
|
1153 |
i++; |
|
1154 |
} |
|
1155 |
||
1156 |
} |
|
1157 |
||
1158 |
void NetworkUDPSend(struct sockaddr_in recv,struct UDPPacket packet) { |
|
1159 |
sendto(_udpsocket,(char *) &packet,sizeof(packet),0,(struct sockaddr *) &recv,sizeof(recv)); |
|
1160 |
} |
|
1161 |
||
1162 |
bool NetworkUDPSearchServer() { |
|
1163 |
struct UDPPacket packet; |
|
1164 |
int timeout=3000; |
|
1165 |
DEBUG(misc,0) ("udp: searching server"); |
|
1166 |
_network_detected_serverip = "255.255.255.255"; |
|
1167 |
_network_detected_serverport = 0; |
|
1168 |
||
1169 |
packet.command_check=packet.command_code=NET_UDPCMD_SERVERSEARCH; |
|
1170 |
packet.data_len=0; |
|
1171 |
NetworkUDPBroadCast(packet); |
|
1172 |
while (timeout>=0) { |
|
1173 |
CSleep(100); |
|
1174 |
timeout-=100; |
|
1175 |
NetworkUDPReceive(); |
|
1176 |
if (_network_detected_serverport>0) { |
|
1177 |
timeout=-1; |
|
1178 |
DEBUG(misc,0) ("udp: server found on %s", _network_detected_serverip); |
|
1179 |
} |
|
1180 |
} |
|
1181 |
return (_network_detected_serverport>0); |
|
1182 |
||
1183 |
} |
|
1184 |
||
1185 |
||
8
9ab81ef450f2
(svn r9) Fixed a couple of warnings and minor coding issues
dominik
parents:
1
diff
changeset
|
1186 |
#else // not ENABLE_NETWORK |
0 | 1187 |
|
1188 |
// stubs |
|
1 | 1189 |
void NetworkInitialize(const char *hostname) {} |
0 | 1190 |
void NetworkShutdown() {} |
1191 |
void NetworkListen(int port) {} |
|
1192 |
void NetworkConnect(const char *hostname, int port) {} |
|
1193 |
void NetworkReceive() {} |
|
1194 |
void NetworkSend() {} |
|
1195 |
void NetworkSendCommand(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback) {} |
|
1196 |
void NetworkProcessCommands() {} |
|
1197 |
void NetworkStartSync() {} |
|
1198 |
void NetworkUDPListen(int port) {} |
|
1199 |
void NetworkUDPReceive() {} |
|
8
9ab81ef450f2
(svn r9) Fixed a couple of warnings and minor coding issues
dominik
parents:
1
diff
changeset
|
1200 |
bool NetworkUDPSearchServer() { return false; } |
0 | 1201 |
#endif // ENABLE_NETWORK |