… | |
… | |
40 | |
40 | |
41 | #ifdef __linux__ |
41 | #ifdef __linux__ |
42 | # include <sys/types.h> |
42 | # include <sys/types.h> |
43 | # include <sys/socket.h> |
43 | # include <sys/socket.h> |
44 | # include <netinet/in.h> |
44 | # include <netinet/in.h> |
45 | # define TCP_HZ 1000 // sorry... |
|
|
46 | # include <netinet/tcp.h> |
45 | # include <netinet/tcp.h> |
47 | #endif |
46 | #endif |
48 | |
47 | |
49 | // use a really low timeout, as it doesn't cost any bandwidth, and you can |
48 | // use a really low timeout, as it doesn't cost any bandwidth, and you can |
50 | // easily die in 20 seconds... |
49 | // easily die in 20 seconds... |
51 | #define SOCKET_TIMEOUT1 10 |
50 | #define SOCKET_TIMEOUT1 10 * 1000 |
52 | #define SOCKET_TIMEOUT2 20 |
51 | #define SOCKET_TIMEOUT2 20 * 1000 |
53 | |
52 | |
54 | void |
53 | void |
55 | client::flush () |
54 | client::flush () |
56 | { |
55 | { |
|
|
56 | if (destroyed ()) |
|
|
57 | return; |
|
|
58 | |
57 | #ifdef __linux__ |
59 | #ifdef __linux__ |
58 | // check time of last ack, and, if too old, kill connection |
60 | // check time of last ack, and, if too old, kill connection |
59 | struct tcp_info tcpi; |
61 | struct tcp_info tcpi; |
60 | socklen_t len = sizeof (tcpi); |
62 | socklen_t len = sizeof (tcpi); |
61 | |
63 | |
… | |
… | |
64 | unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent; |
66 | unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent; |
65 | |
67 | |
66 | rtt = tcpi.tcpi_rtt; |
68 | rtt = tcpi.tcpi_rtt; |
67 | rttvar = tcpi.tcpi_rttvar; |
69 | rttvar = tcpi.tcpi_rttvar; |
68 | |
70 | |
69 | if (tcpi.tcpi_unacked && SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s |
71 | if (tcpi.tcpi_unacked && SOCKET_TIMEOUT1 < diff && diff < 0x80000000UL // ack delayed for 20s |
70 | && SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s |
72 | && SOCKET_TIMEOUT2 < tcpi.tcpi_last_data_sent) // no data sent for 10s |
71 | { |
73 | { |
72 | LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, |
74 | LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, |
73 | (unsigned) tcpi.tcpi_last_ack_recv, (unsigned) tcpi.tcpi_last_data_sent, (unsigned) tcpi.tcpi_unacked); |
75 | (unsigned) tcpi.tcpi_last_ack_recv, (unsigned) tcpi.tcpi_last_data_sent, (unsigned) tcpi.tcpi_unacked); |
74 | destroy (); |
76 | destroy (); |
75 | } |
77 | } |
… | |
… | |
188 | {"ncom", PC(NewPlayerCmd) PF_PLAYING }, |
190 | {"ncom", PC(NewPlayerCmd) PF_PLAYING }, |
189 | {"command", PC(PlayerCmd) PF_PLAYING }, |
191 | {"command", PC(PlayerCmd) PF_PLAYING }, |
190 | |
192 | |
191 | {"examine", PC(ExamineCmd) PF_PLAYING }, |
193 | {"examine", PC(ExamineCmd) PF_PLAYING }, |
192 | {"apply", PC(ApplyCmd) PF_PLAYING }, |
194 | {"apply", PC(ApplyCmd) PF_PLAYING }, |
193 | {"reply", PC(ReplyCmd) PF_IMMEDIATE }, |
|
|
194 | {"lookat", PC(LookAt) PF_PLAYING }, |
195 | {"lookat", PC(LookAt) PF_PLAYING }, |
195 | {"lock", PC(LockItem) PF_PLAYING }, |
196 | {"lock", PC(LockItem) PF_PLAYING }, |
196 | {"mark", PC(MarkItem) PF_PLAYING }, |
197 | {"mark", PC(MarkItem) PF_PLAYING }, |
197 | {"move", PC(MoveCmd) PF_PLAYING }, |
198 | {"move", PC(MoveCmd) PF_PLAYING }, |
198 | {"exti", PC(ExtCmd) PF_IMMEDIATE }, /* CF+ */ |
|
|
199 | {"ext", PC(ExtCmd) 0 }, /* CF+ */ |
199 | {"ext", PC(ExtCmd) 0 }, /* CF+ */ |
200 | {"mapredraw", PC(MapRedrawCmd) PF_IMMEDIATE }, /* Added: phil */ |
200 | {"mapredraw", PC(MapRedrawCmd) 0 }, /* Added: phil */ |
201 | {"mapinfo", PC(MapInfoCmd) 0 }, /* CF+ */ |
201 | {"mapinfo", PC(MapInfoCmd) 0 }, /* CF+ */ |
202 | |
202 | |
|
|
203 | {"reply", SC(ReplyCmd) 0 }, |
|
|
204 | {"exti", SC(ExtiCmd) 0 }, /* CF+ */ |
203 | {"addme", SC(AddMeCmd) PF_IMMEDIATE }, |
205 | {"addme", SC(AddMeCmd) 0 }, |
204 | {"askface", SC(SendFaceCmd) PF_IMMEDIATE }, /* Added: phil */ |
206 | {"askface", SC(SendFaceCmd) 0 }, /* Added: phil */ |
205 | {"requestinfo", SC(RequestInfo) PF_IMMEDIATE }, |
207 | {"requestinfo", SC(RequestInfo) 0 }, |
206 | {"setfacemode", SC(SetFaceMode) PF_IMMEDIATE }, |
208 | {"setfacemode", SC(SetFaceMode) 0 }, |
207 | {"setsound", SC(SetSound) PF_IMMEDIATE }, |
209 | {"setsound", SC(SetSound) 0 }, |
208 | {"setup", SC(SetUp) PF_IMMEDIATE }, |
210 | {"setup", SC(SetUp) 0 }, |
209 | {"version", SC(VersionCmd) PF_IMMEDIATE }, |
211 | {"version", SC(VersionCmd) 0 }, |
210 | {"toggleextendedinfos", SC(ToggleExtendedInfos) PF_IMMEDIATE }, /*Added: tchize */ |
212 | {"toggleextendedinfos", SC(ToggleExtendedInfos) 0 }, /*Added: tchize */ |
211 | {"toggleextendedtext", SC(ToggleExtendedText) PF_IMMEDIATE }, /*Added: tchize */ |
213 | {"toggleextendedtext", SC(ToggleExtendedText) 0 }, /*Added: tchize */ |
212 | {"asksmooth", SC(AskSmooth) PF_IMMEDIATE }, /*Added: tchize (smoothing technologies) */ |
214 | {"asksmooth", SC(AskSmooth) 0 }, /*Added: tchize (smoothing technologies) */ |
213 | }; |
215 | }; |
214 | |
216 | |
215 | bool |
217 | bool |
216 | client::may_execute (const packet_type *pkt) const |
218 | client::may_execute (const packet_type *pkt) const |
217 | { |
219 | { |
218 | return (!(pkt->flags & PF_PLAYER) || pl) |
220 | return (!(pkt->flags & PF_PLAYER) || pl) |
219 | && (!(pkt->flags & PF_PLAYING) || (pl && pl->state == ST_PLAYING)); |
221 | && (!(pkt->flags & PF_PLAYING) || state == ST_PLAYING); |
220 | } |
222 | } |
221 | |
223 | |
222 | void |
224 | void |
223 | client::execute (const packet_type *pkt, char *data, int datalen) |
225 | client::execute (const packet_type *pkt, char *data, int datalen) |
224 | { |
226 | { |
… | |
… | |
271 | } |
273 | } |
272 | |
274 | |
273 | for (packet_type *pkt = packets; pkt < packets + (sizeof (packets) / sizeof (packets[0])); ++pkt) |
275 | for (packet_type *pkt = packets; pkt < packets + (sizeof (packets) / sizeof (packets[0])); ++pkt) |
274 | if (!strcmp ((char *)inbuf + 2, pkt->name)) |
276 | if (!strcmp ((char *)inbuf + 2, pkt->name)) |
275 | { |
277 | { |
276 | if (pkt->flags & PF_IMMEDIATE) |
278 | if (pkt->flags & PF_PLAYER) |
|
|
279 | queue_command (pkt, data, datalen); |
|
|
280 | else |
277 | execute (pkt, data, datalen); |
281 | execute (pkt, data, datalen); |
278 | else |
|
|
279 | queue_command (pkt, data, datalen); |
|
|
280 | |
282 | |
281 | goto next_packet; |
283 | goto next_packet; |
282 | } |
284 | } |
283 | |
285 | |
284 | /* If we get here, we didn't find a valid command. Logging |
286 | // If we get here, we didn't find a valid command. |
285 | * this might be questionable, because a broken client/malicious |
|
|
286 | * user could certainly send a whole bunch of invalid commands. |
|
|
287 | */ |
|
|
288 | send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED, (char *)inbuf + 2); |
287 | send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED, (char *)inbuf + 2); |
289 | next_packet: |
288 | next_packet: |
290 | skip_packet (pkt_len); |
289 | skip_packet (pkt_len); |
291 | |
290 | |
292 | // input buffer has space again |
291 | // input buffer has space again |
… | |
… | |
298 | // callback called when socket is either readable or writable |
297 | // callback called when socket is either readable or writable |
299 | void |
298 | void |
300 | client::socket_cb (iow &w, int got) |
299 | client::socket_cb (iow &w, int got) |
301 | { |
300 | { |
302 | //TODO remove when we have better socket cleanup logic |
301 | //TODO remove when we have better socket cleanup logic |
303 | if (status == Ns_Dead) |
302 | if (destroyed ()) |
304 | { |
303 | { |
305 | socket_ev.poll (0); |
304 | socket_ev.poll (0); |
306 | return; |
305 | return; |
307 | } |
306 | } |
308 | |
307 | |
… | |
… | |
359 | |
358 | |
360 | // called whenever we have additional commands to process |
359 | // called whenever we have additional commands to process |
361 | void |
360 | void |
362 | client::cmd_cb (iw &w) |
361 | client::cmd_cb (iw &w) |
363 | { |
362 | { |
364 | if (handle_packet () || handle_command ()) |
363 | if (handle_packet ()) |
365 | w.start (); |
364 | w.start (); |
366 | else |
365 | else |
367 | flush (); |
366 | flush (); |
368 | } |
367 | } |
369 | |
368 | |
… | |
… | |
384 | { |
383 | { |
385 | char *buf = (char *)buf_; |
384 | char *buf = (char *)buf_; |
386 | char *pos = buf; |
385 | char *pos = buf; |
387 | int amt = 0; |
386 | int amt = 0; |
388 | |
387 | |
389 | if (status == Ns_Dead || !buf) |
388 | if (destroyed () || !buf) |
390 | return; |
389 | return; |
391 | |
390 | |
392 | if ((len + outputbuffer.len) > SOCKETBUFSIZE) |
391 | if ((len + outputbuffer.len) > SOCKETBUFSIZE) |
393 | { |
392 | { |
394 | LOG (llevDebug, "socket on fd %d has overrun internal buffer - marking as dead\n", fd); |
393 | LOG (llevDebug, "socket on fd %d has overrun internal buffer - marking as dead\n", fd); |
… | |
… | |
425 | * shortcut function. |
424 | * shortcut function. |
426 | */ |
425 | */ |
427 | void |
426 | void |
428 | client::send_packet (packet &sl) |
427 | client::send_packet (packet &sl) |
429 | { |
428 | { |
430 | if (status == Ns_Dead) |
429 | if (destroyed ()) |
431 | return; |
430 | return; |
432 | |
431 | |
433 | if (sl.length () >= MAXSOCKBUF) |
432 | if (sl.length () >= MAXSOCKBUF) |
434 | { |
433 | { |
435 | LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ()); |
434 | LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ()); |
… | |
… | |
481 | /*********************************************************************** |
480 | /*********************************************************************** |
482 | * |
481 | * |
483 | * packet functions/utilities |
482 | * packet functions/utilities |
484 | * |
483 | * |
485 | **********************************************************************/ |
484 | **********************************************************************/ |
|
|
485 | |
|
|
486 | packet::packet (const char *name) |
|
|
487 | { |
|
|
488 | reset (); |
|
|
489 | |
|
|
490 | int len = strlen (name); |
|
|
491 | memcpy (cur, name, len); cur += len; |
|
|
492 | *cur++ = ' '; |
|
|
493 | } |
486 | |
494 | |
487 | packet &packet::operator <<(const data &v) |
495 | packet &packet::operator <<(const data &v) |
488 | { |
496 | { |
489 | if (room () < v.len) |
497 | if (room () < v.len) |
490 | reset (); |
498 | reset (); |