… | |
… | |
31 | # include <sys/socket.h> |
31 | # include <sys/socket.h> |
32 | # include <netinet/in.h> |
32 | # include <netinet/in.h> |
33 | # include <netinet/tcp.h> |
33 | # include <netinet/tcp.h> |
34 | #endif |
34 | #endif |
35 | |
35 | |
36 | #define HAVE_SIOCOUTQ 1 //TODO configure.ac support |
|
|
37 | #if HAVE_SIOCOUTQ |
|
|
38 | # include <linux/sockios.h> |
|
|
39 | #endif |
|
|
40 | |
|
|
41 | // disconnect a socket after this many seconds without an ack |
36 | // disconnect a socket after this many seconds without an ack |
42 | #define SOCKET_TIMEOUT 8. |
37 | #define SOCKET_TIMEOUT 8. |
43 | |
38 | |
44 | // force a packet when idle for more than this many seconds, |
39 | // force a packet when idle for more than this many seconds, |
45 | // forcing an ack regularly. |
40 | // forcing an ack regularly. |
… | |
… | |
49 | client::flush () |
44 | client::flush () |
50 | { |
45 | { |
51 | if (destroyed ()) |
46 | if (destroyed ()) |
52 | return; |
47 | return; |
53 | |
48 | |
54 | # if HAVE_SIOCOUTQ |
|
|
55 | { |
|
|
56 | int value; |
|
|
57 | |
|
|
58 | if (!ioctl (fd, SIOCOUTQ, &value)) |
|
|
59 | outq = value; |
|
|
60 | } |
|
|
61 | # endif |
|
|
62 | |
|
|
63 | #if HAVE_TCP_INFO |
49 | #if HAVE_TCP_INFO |
64 | // check about once per second, spread evenly over all clients |
|
|
65 | // do this only when player is active |
|
|
66 | if (!((pticks + fd) & 7) && pl && pl->active) |
|
|
67 | { |
|
|
68 | // check time of last ack, and, if too old, kill connection |
50 | // check time of last ack, and, if too old, kill connection |
69 | struct tcp_info tcpi; |
|
|
70 | socklen_t len = sizeof (tcpi); |
51 | socklen_t len = sizeof (tcpi); |
71 | |
52 | |
72 | if (!getsockopt (fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi)) |
53 | if (!getsockopt (fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi)) |
73 | { |
54 | { |
74 | if (tcpi.tcpi_snd_mss) |
55 | if (tcpi.tcpi_snd_mss) |
75 | mss = tcpi.tcpi_snd_mss; |
56 | mss = tcpi.tcpi_snd_mss; |
76 | |
57 | |
77 | rtt = tcpi.tcpi_rtt; |
58 | #if 0 |
78 | rttvar = tcpi.tcpi_rttvar; |
59 | fprintf (stderr, "uack %d ack %d lost %d ret %d fack %d sst %d cwnd %d mss %d pmtu %d advmss %d EXC %d\n", |
|
|
60 | tcpi.tcpi_unacked, |
|
|
61 | tcpi.tcpi_sacked, |
|
|
62 | tcpi.tcpi_lost, |
|
|
63 | tcpi.tcpi_retrans, |
|
|
64 | tcpi.tcpi_fackets, |
|
|
65 | tcpi.tcpi_snd_ssthresh, tcpi.tcpi_snd_cwnd, tcpi.tcpi_advmss, tcpi.tcpi_pmtu, tcpi.tcpi_advmss, |
|
|
66 | |
|
|
67 | tcpi.tcpi_snd_cwnd - (tcpi.tcpi_unacked - tcpi.tcpi_sacked)); |
|
|
68 | #endif |
79 | |
69 | |
|
|
70 | // do this only when player is active |
|
|
71 | if (pl && pl->active |
80 | if (tcpi.tcpi_last_ack_recv > int (SOCKET_TIMEOUT * 1000)) |
72 | && tcpi.tcpi_last_ack_recv > int (SOCKET_TIMEOUT * 1000)) |
81 | { |
73 | { |
82 | send_msg (NDI_RED, "connection-timeout", "safety disconnect due to tcp/ip timeout (no packets received)"); |
74 | send_msg (NDI_RED | NDI_REPLY, "connection-timeout", "safety disconnect due to tcp/ip timeout (no packets received)"); |
83 | write_outputbuffer (); |
75 | write_outputbuffer (); |
84 | |
76 | |
85 | LOG (llevDebug, "connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, |
77 | LOG (llevDebug, "connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, |
86 | (unsigned)tcpi.tcpi_last_ack_recv, (unsigned)tcpi.tcpi_last_data_sent, (unsigned)tcpi.tcpi_unacked); |
78 | (unsigned)tcpi.tcpi_last_ack_recv, (unsigned)tcpi.tcpi_last_data_sent, (unsigned)tcpi.tcpi_unacked); |
87 | destroy (); |
79 | destroy (); |
88 | } |
|
|
89 | } |
80 | } |
90 | } |
81 | } |
91 | #endif |
82 | #endif |
92 | |
83 | |
93 | /** |
84 | /** |
… | |
… | |
179 | if (inbuf_len >= 2 + pkt_len) |
170 | if (inbuf_len >= 2 + pkt_len) |
180 | return 2 + pkt_len; |
171 | return 2 + pkt_len; |
181 | |
172 | |
182 | if (inbuf_len == sizeof (inbuf)) |
173 | if (inbuf_len == sizeof (inbuf)) |
183 | { |
174 | { |
184 | send_packet_printf ("drawinfo %d input buffer overflow - closing connection.", NDI_RED); |
175 | send_packet_printf ("drawinfo %d input buffer overflow - closing connection.", NDI_RED | NDI_REPLY); |
185 | destroy (); |
176 | destroy (); |
186 | return -1; |
177 | return -1; |
187 | } |
178 | } |
188 | } |
179 | } |
189 | |
180 | |
… | |
… | |
283 | ((void (*)(char *, int, player *))pkt->cb)((char *)data, datalen, pl); |
274 | ((void (*)(char *, int, player *))pkt->cb)((char *)data, datalen, pl); |
284 | else |
275 | else |
285 | ((void (*)(char *, int, client *))pkt->cb)((char *)data, datalen, this); |
276 | ((void (*)(char *, int, client *))pkt->cb)((char *)data, datalen, this); |
286 | } |
277 | } |
287 | else |
278 | else |
288 | send_packet_printf ("drawinfo %d ERROR: you cannot execute '%s' now.", NDI_RED, pkt->name); |
279 | send_packet_printf ("drawinfo %d ERROR: you cannot execute '%s' now.", NDI_RED | NDI_REPLY, pkt->name); |
289 | } |
280 | } |
290 | |
281 | |
291 | bool |
282 | bool |
292 | client::handle_packet () |
283 | client::handle_packet () |
293 | { |
284 | { |
… | |
… | |
334 | |
325 | |
335 | goto next_packet; |
326 | goto next_packet; |
336 | } |
327 | } |
337 | |
328 | |
338 | // If we get here, we didn't find a valid command. |
329 | // If we get here, we didn't find a valid command. |
339 | send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED, (char *)inbuf + 2); |
330 | send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED | NDI_REPLY, (char *)inbuf + 2); |
340 | next_packet: |
331 | next_packet: |
341 | skip_packet (pkt_len); |
332 | skip_packet (pkt_len); |
342 | |
333 | |
343 | // input buffer has space again |
334 | // input buffer has space again |
344 | socket_ev.poll (socket_ev.poll () | PE_R); |
335 | socket_ev.poll (socket_ev.poll () | PE_R); |
… | |
… | |
476 | client::send_packet (packet &sl) |
467 | client::send_packet (packet &sl) |
477 | { |
468 | { |
478 | if (destroyed ()) |
469 | if (destroyed ()) |
479 | return; |
470 | return; |
480 | |
471 | |
481 | if (sl.length () >= MAXSOCKBUF) |
472 | if (sl.length () > MAXSOCKBUF) |
482 | { |
473 | { |
483 | LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ()); |
474 | LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ()); |
484 | /* Almost certainly we've overflowed a buffer, so quit now to make |
475 | /* Almost certainly we've overflowed a buffer, so quit now to make |
485 | * it easier to debug. |
476 | * it easier to debug. |
486 | */ |
477 | */ |
… | |
… | |
537 | client::send_msg (int color, const char *type, const char *msg) |
528 | client::send_msg (int color, const char *type, const char *msg) |
538 | { |
529 | { |
539 | if (msg_is_special (msg)) |
530 | if (msg_is_special (msg)) |
540 | cfperl_send_msg (this, color, type, msg); |
531 | cfperl_send_msg (this, color, type, msg); |
541 | else if (can_msg) |
532 | else if (can_msg) |
542 | send_packet_printf ("msg %d %s %s", color, type, msg); |
533 | send_packet_printf ("msg %d %s %s", color & NDI_CLIENT_MASK, type, msg); |
543 | else if (color < 0) |
534 | else if (color < 0) |
544 | return; // client cannot handle this |
535 | return; // client cannot handle this |
545 | else |
536 | else |
546 | send_packet_printf ("drawinfo %d %s", color, msg); |
537 | send_packet_printf ("drawinfo %d %s", color & NDI_COLOR_MASK, msg); |
547 | } |
538 | } |
548 | |
539 | |
549 | void |
540 | void |
550 | client::send_drawinfo (const char *msg, int flags) |
541 | client::send_drawinfo (const char *msg, int flags) |
551 | { |
542 | { |