… | |
… | |
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. |
… | |
… | |
48 | void |
43 | void |
49 | client::flush () |
44 | client::flush () |
50 | { |
45 | { |
51 | if (destroyed ()) |
46 | if (destroyed ()) |
52 | return; |
47 | return; |
53 | |
|
|
54 | # if HAVE_SIOCOUTQ |
|
|
55 | { |
|
|
56 | int value; |
|
|
57 | |
|
|
58 | if (!ioctl (fd, SIOCOUTQ, &value)) |
|
|
59 | outq = value; |
|
|
60 | } |
|
|
61 | # endif |
|
|
62 | |
48 | |
63 | #if HAVE_TCP_INFO |
49 | #if HAVE_TCP_INFO |
64 | // check about once per second, spread evenly over all clients |
50 | // check about once per second, spread evenly over all clients |
65 | // do this only when player is active |
51 | // do this only when player is active |
66 | if (!((pticks + fd) & 7) && pl && pl->active) |
52 | if (!((pticks + fd) & 7) && pl && pl->active) |
67 | { |
53 | { |
68 | // check time of last ack, and, if too old, kill connection |
54 | // check time of last ack, and, if too old, kill connection |
69 | struct tcp_info tcpi; |
|
|
70 | socklen_t len = sizeof (tcpi); |
55 | socklen_t len = sizeof (tcpi); |
71 | |
56 | |
72 | if (!getsockopt (fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi)) |
57 | if (!getsockopt (fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi)) |
73 | { |
58 | { |
74 | if (tcpi.tcpi_snd_mss) |
59 | if (tcpi.tcpi_snd_mss) |
75 | mss = tcpi.tcpi_snd_mss; |
60 | mss = tcpi.tcpi_snd_mss; |
76 | |
61 | |
77 | rtt = tcpi.tcpi_rtt; |
62 | //fprintf (stderr, "uack %d sst %d cwnd %d mss %d pmtu %d\n", |
78 | rttvar = tcpi.tcpi_rttvar; |
63 | // tcpi.tcpi_unacked, tcpi.tcpi_snd_ssthresh, tcpi.tcpi_snd_cwnd, tcpi.tcpi_advmss, tcpi.tcpi_pmtu); |
79 | |
64 | |
80 | if (tcpi.tcpi_last_ack_recv > int (SOCKET_TIMEOUT * 1000)) |
65 | if (tcpi.tcpi_last_ack_recv > int (SOCKET_TIMEOUT * 1000)) |
81 | { |
66 | { |
82 | send_msg (NDI_RED, "connection-timeout", "safety disconnect due to tcp/ip timeout (no packets received)"); |
67 | send_msg (NDI_RED | NDI_REPLY, "connection-timeout", "safety disconnect due to tcp/ip timeout (no packets received)"); |
83 | write_outputbuffer (); |
68 | write_outputbuffer (); |
84 | |
69 | |
85 | LOG (llevDebug, "connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, |
70 | 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); |
71 | (unsigned)tcpi.tcpi_last_ack_recv, (unsigned)tcpi.tcpi_last_data_sent, (unsigned)tcpi.tcpi_unacked); |
87 | destroy (); |
72 | destroy (); |
… | |
… | |
179 | if (inbuf_len >= 2 + pkt_len) |
164 | if (inbuf_len >= 2 + pkt_len) |
180 | return 2 + pkt_len; |
165 | return 2 + pkt_len; |
181 | |
166 | |
182 | if (inbuf_len == sizeof (inbuf)) |
167 | if (inbuf_len == sizeof (inbuf)) |
183 | { |
168 | { |
184 | send_packet_printf ("drawinfo %d input buffer overflow - closing connection.", NDI_RED); |
169 | send_packet_printf ("drawinfo %d input buffer overflow - closing connection.", NDI_RED | NDI_REPLY); |
185 | destroy (); |
170 | destroy (); |
186 | return -1; |
171 | return -1; |
187 | } |
172 | } |
188 | } |
173 | } |
189 | |
174 | |
… | |
… | |
283 | ((void (*)(char *, int, player *))pkt->cb)((char *)data, datalen, pl); |
268 | ((void (*)(char *, int, player *))pkt->cb)((char *)data, datalen, pl); |
284 | else |
269 | else |
285 | ((void (*)(char *, int, client *))pkt->cb)((char *)data, datalen, this); |
270 | ((void (*)(char *, int, client *))pkt->cb)((char *)data, datalen, this); |
286 | } |
271 | } |
287 | else |
272 | else |
288 | send_packet_printf ("drawinfo %d ERROR: you cannot execute '%s' now.", NDI_RED, pkt->name); |
273 | send_packet_printf ("drawinfo %d ERROR: you cannot execute '%s' now.", NDI_RED | NDI_REPLY, pkt->name); |
289 | } |
274 | } |
290 | |
275 | |
291 | bool |
276 | bool |
292 | client::handle_packet () |
277 | client::handle_packet () |
293 | { |
278 | { |
… | |
… | |
334 | |
319 | |
335 | goto next_packet; |
320 | goto next_packet; |
336 | } |
321 | } |
337 | |
322 | |
338 | // If we get here, we didn't find a valid command. |
323 | // 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); |
324 | send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED | NDI_REPLY, (char *)inbuf + 2); |
340 | next_packet: |
325 | next_packet: |
341 | skip_packet (pkt_len); |
326 | skip_packet (pkt_len); |
342 | |
327 | |
343 | // input buffer has space again |
328 | // input buffer has space again |
344 | socket_ev.poll (socket_ev.poll () | PE_R); |
329 | socket_ev.poll (socket_ev.poll () | PE_R); |
… | |
… | |
537 | client::send_msg (int color, const char *type, const char *msg) |
522 | client::send_msg (int color, const char *type, const char *msg) |
538 | { |
523 | { |
539 | if (msg_is_special (msg)) |
524 | if (msg_is_special (msg)) |
540 | cfperl_send_msg (this, color, type, msg); |
525 | cfperl_send_msg (this, color, type, msg); |
541 | else if (can_msg) |
526 | else if (can_msg) |
542 | send_packet_printf ("msg %d %s %s", color, type, msg); |
527 | send_packet_printf ("msg %d %s %s", color & NDI_CLIENT_MASK, type, msg); |
543 | else if (color < 0) |
528 | else if (color < 0) |
544 | return; // client cannot handle this |
529 | return; // client cannot handle this |
545 | else |
530 | else |
546 | send_packet_printf ("drawinfo %d %s", color, msg); |
531 | send_packet_printf ("drawinfo %d %s", color & NDI_COLOR_MASK, msg); |
547 | } |
532 | } |
548 | |
533 | |
549 | void |
534 | void |
550 | client::send_drawinfo (const char *msg, int flags) |
535 | client::send_drawinfo (const char *msg, int flags) |
551 | { |
536 | { |