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