… | |
… | |
37 | #include <sproto.h> |
37 | #include <sproto.h> |
38 | #include <sys/types.h> |
38 | #include <sys/types.h> |
39 | #include <sys/time.h> |
39 | #include <sys/time.h> |
40 | #include <sys/socket.h> |
40 | #include <sys/socket.h> |
41 | #include <netinet/in.h> |
41 | #include <netinet/in.h> |
|
|
42 | #include <netinet/tcp.h> |
|
|
43 | #include <netinet/ip.h> |
42 | #include <netdb.h> |
44 | #include <netdb.h> |
43 | |
45 | |
44 | #include <unistd.h> |
46 | #include <unistd.h> |
45 | #include <arpa/inet.h> |
47 | #include <arpa/inet.h> |
46 | |
48 | |
… | |
… | |
52 | * client. |
54 | * client. |
53 | */ |
55 | */ |
54 | client::client (int fd, const char *peername) |
56 | client::client (int fd, const char *peername) |
55 | : fd (fd), host (strdup (peername)), |
57 | : fd (fd), host (strdup (peername)), |
56 | socket_ev (this, &client::socket_cb), |
58 | socket_ev (this, &client::socket_cb), |
57 | cmd_ev (this, &client::cmd_cb) |
59 | cmd_ev (this, &client::cmd_cb), |
|
|
60 | cc_inv (this), cc_other (this) |
58 | { |
61 | { |
59 | { |
62 | { |
60 | struct linger linger_opt; |
63 | struct linger linger_opt; |
61 | |
64 | |
62 | linger_opt.l_onoff = 0; |
65 | linger_opt.l_onoff = 0; |
63 | linger_opt.l_linger = 0; |
66 | linger_opt.l_linger = 0; |
64 | |
67 | |
65 | if (setsockopt (fd, SOL_SOCKET, SO_LINGER, &linger_opt, sizeof (struct linger))) |
68 | if (setsockopt (fd, SOL_SOCKET, SO_LINGER, &linger_opt, sizeof (struct linger))) |
66 | LOG (llevError, "Cannot setsockopt(SO_LINGER): %s\n", strerror (errno)); |
69 | LOG (llevError, "SO_LINGER: %s\n", strerror (errno)); |
67 | } |
70 | } |
|
|
71 | |
|
|
72 | { |
|
|
73 | int val = IPTOS_LOWDELAY; |
|
|
74 | |
|
|
75 | if (setsockopt (fd, IPPROTO_IP, IP_TOS, &val, sizeof (val))) |
|
|
76 | LOG (llevError, "IP_TOS: %s\n", strerror (errno)); |
|
|
77 | } |
|
|
78 | |
|
|
79 | { |
|
|
80 | int val = 1; |
|
|
81 | |
|
|
82 | if (setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val))) |
|
|
83 | LOG (llevError, "TCP_NODELAY: %s\n", strerror (errno)); |
|
|
84 | } |
|
|
85 | |
|
|
86 | #ifdef __linux__ |
|
|
87 | { |
|
|
88 | int val; |
|
|
89 | |
|
|
90 | // set some very aggressive keepalive parameters |
|
|
91 | val = 10; |
|
|
92 | if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof (val))) |
|
|
93 | LOG (llevError, "TCP_KEEPIDLE: %s\n", strerror (errno)); |
|
|
94 | |
|
|
95 | val = 5; |
|
|
96 | if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof (val))) |
|
|
97 | LOG (llevError, "TCP_KEEPCNT: %s\n", strerror (errno)); |
|
|
98 | |
|
|
99 | val = 1; |
|
|
100 | if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof (val))) |
|
|
101 | LOG (llevError, "TCP_KEEPINTVL: %s\n", strerror (errno)); |
|
|
102 | } |
|
|
103 | #endif |
68 | |
104 | |
69 | { |
105 | { |
70 | int bufsize = 65535; /*Supposed absolute upper limit */ |
106 | int bufsize = 65535; /*Supposed absolute upper limit */ |
71 | int oldbufsize; |
107 | int oldbufsize; |
72 | int buflen = sizeof (int); |
108 | int buflen = sizeof (int); |
… | |
… | |
90 | status = Ns_Add; |
126 | status = Ns_Add; |
91 | mapx = 11; |
127 | mapx = 11; |
92 | mapy = 11; |
128 | mapy = 11; |
93 | itemcmd = 1; /* Default is version item1 command */ |
129 | itemcmd = 1; /* Default is version item1 command */ |
94 | |
130 | |
95 | faces_sent_len = nrofpixmaps; |
131 | faces_sent = salloc<uint8> (nrofpixmaps, 0); |
96 | faces_sent = (uint8 *)calloc (nrofpixmaps, sizeof (*faces_sent)); |
|
|
97 | if (!faces_sent) |
|
|
98 | fatal (OUT_OF_MEMORY); |
|
|
99 | |
132 | |
100 | /* Do this so we don't send a face command for the client for |
133 | /* Do this so we don't send a face command for the client for |
101 | * this face. Face 0 is sent to the client to say clear |
134 | * this face. Face 0 is sent to the client to say clear |
102 | * face information. |
135 | * face information. |
103 | */ |
136 | */ |
… | |
… | |
112 | // command handling is even lower priority |
145 | // command handling is even lower priority |
113 | // that way no client can monopolise the server |
146 | // that way no client can monopolise the server |
114 | cmd_ev.prio (PE_PRIO_NORMAL + 2); |
147 | cmd_ev.prio (PE_PRIO_NORMAL + 2); |
115 | |
148 | |
116 | // initialisation done, kick it! |
149 | // initialisation done, kick it! |
117 | |
|
|
118 | send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); |
150 | send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); |
119 | flush (); |
151 | flush (); |
120 | |
152 | |
121 | #if 0//TODO |
153 | #if 0//TODO |
122 | socket_info.nconns++; |
154 | socket_info.nconns++; |
… | |
… | |
129 | { |
161 | { |
130 | if (pl) |
162 | if (pl) |
131 | pl->socket = 0; |
163 | pl->socket = 0; |
132 | |
164 | |
133 | if (fd >= 0) |
165 | if (fd >= 0) |
134 | close (fd), fd = -1; |
166 | close (fd); |
135 | |
167 | |
136 | free (stats.range), stats.range = 0; |
168 | sfree<uint8> (faces_sent, nrofpixmaps); |
137 | free (stats.title), stats.title = 0; |
169 | free (stats.range); |
138 | free (host), host = 0; |
170 | free (stats.title); |
|
|
171 | free (host); |
139 | } |
172 | } |
140 | |
173 | |
141 | void |
174 | void |
142 | client::destroy () |
175 | client::destroy () |
143 | { |
176 | { |