ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/lowlevel.C
(Generate patch)

Comparing deliantra/server/socket/lowlevel.C (file contents):
Revision 1.19 by root, Thu Dec 14 20:39:54 2006 UTC vs.
Revision 1.20 by root, Thu Dec 14 21:46:34 2006 UTC

51// easily die in 20 seconds... 51// easily die in 20 seconds...
52#define SOCKET_TIMEOUT1 10 52#define SOCKET_TIMEOUT1 10
53#define SOCKET_TIMEOUT2 20 53#define SOCKET_TIMEOUT2 20
54 54
55void 55void
56Socket_Flush (client_socket * ns) 56client_socket::flush ()
57{ 57{
58#ifdef __linux__ 58#ifdef __linux__
59 // check time of last ack, and, if too old, kill connection 59 // check time of last ack, and, if too old, kill connection
60 struct tcp_info tcpi; 60 struct tcp_info tcpi;
61 socklen_t len = sizeof (tcpi); 61 socklen_t len = sizeof (tcpi);
62 62
63 if (!getsockopt (ns->fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi)) 63 if (!getsockopt (fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi))
64 { 64 {
65 unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent; 65 unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent;
66 66
67 if (tcpi.tcpi_unacked && SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s 67 if (tcpi.tcpi_unacked && SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s
68 && SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s 68 && SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s
69 { 69 {
70 LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", ns->fd, 70 LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd,
71 (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);
72 ns->status = Ns_Dead; 72 status = Ns_Dead;
73 } 73 }
74 } 74 }
75
76 int val;
77
78 val = 0; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val));
79 val = 1; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val));
80#endif 75#endif
76
77 /**
78 * Writes data to socket.
79 *
80 * When the socket is clear to write, and we have backlogged data, this
81 * is called to write it out.
82 */
83
84 if (!outputbuffer.len || socket_ev.poll () & PE_W)
85 return;
86
87 write_outputbuffer ();
88}
89
90void
91client_socket::write_outputbuffer ()
92{
93 while (outputbuffer.len)
94 {
95 int res = write (fd, outputbuffer.data + outputbuffer.start,
96 min (outputbuffer.len, SOCKETBUFSIZE - outputbuffer.start));
97
98 if (res > 0)
99 {
100 outputbuffer.start += res;
101 /* wrap back to start of buffer */
102 if (outputbuffer.start == SOCKETBUFSIZE)
103 outputbuffer.start = 0;
104
105 outputbuffer.len -= res;
106#ifdef CS_LOGSTATS
107 cst_tot.obytes += res;
108 cst_lst.obytes += res;
109#endif
110 }
111 else if (res == 0)
112 {
113 LOG (llevError, "socket write failed, connection closed.\n");
114 status = Ns_Dead;
115 return;
116 }
117 else if (errno == EINTR)
118 {
119 // just retry
120 }
121 else if (errno == EAGAIN)
122 {
123 // delay till ready
124 socket_ev.poll (socket_ev.poll () | PE_W);
125 socket_ev.start ();
126 return;
127 }
128 else
129 {
130 LOG (llevError, "socket write failed: %s\n", strerror (errno));
131 status = Ns_Dead;
132 return;
133 }
134 }
135
136 socket_ev.poll (socket_ev.poll () & ~PE_W);
81} 137}
82 138
83/*********************************************************************** 139/***********************************************************************
84 * 140 *
85 * packet functions/utilities 141 * packet functions/utilities
171 } 227 }
172 else if (amount < 0) 228 else if (amount < 0)
173 { 229 {
174 if (errno != EAGAIN && errno != EINTR) 230 if (errno != EAGAIN && errno != EINTR)
175 { 231 {
176 LOG (llevError, "read error: %s", strerror (errno)); 232 LOG (llevError, "read error: %s\n", strerror (errno));
177 return -1; 233 return -1;
178 } 234 }
179 235
180 return 0; 236 return 0;
181 } 237 }
204 * Adds data to a socket buffer for whatever reason. 260 * Adds data to a socket buffer for whatever reason.
205 * 261 *
206 * ns is the socket we are adding the data to, buf is the start of the 262 * ns is the socket we are adding the data to, buf is the start of the
207 * data, and len is the number of bytes to add. 263 * data, and len is the number of bytes to add.
208 */ 264 */
209 265void
210static void 266client_socket::send (void *buf_, int len)
211add_to_buffer (client_socket *ns, char *buf, int len)
212{ 267{
268 char *buf = (char *)buf_;
269 char *pos = buf;
270 int amt = 0;
271
272 if (status == Ns_Dead || !buf)
273 {
274 LOG (llevDebug, "Write_To_Socket called with dead socket\n");
275 return;
276 }
277
278 if ((len + outputbuffer.len) > SOCKETBUFSIZE)
279 {
280 LOG (llevDebug, "Socket on fd %d has overrun internal buffer - marking as dead\n", fd);
281 status = Ns_Dead;
282 return;
283 }
284
213 int avail, end; 285 int avail, end;
214
215 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE)
216 {
217 LOG (llevDebug, "Socket on fd %d has overrun internal buffer - marking as dead\n", ns->fd);
218 ns->status = Ns_Dead;
219 return;
220 }
221 286
222 /* data + end is where we start putting the new data. The last byte 287 /* data + end is where we start putting the new data. The last byte
223 * currently in use is actually data + end -1 288 * currently in use is actually data + end -1
224 */ 289 */
225
226 end = ns->outputbuffer.start + ns->outputbuffer.len; 290 end = outputbuffer.start + outputbuffer.len;
227 /* The buffer is already in a wrapped state, so adjust end */ 291 /* The buffer is already in a wrapped state, so adjust end */
228 if (end >= SOCKETBUFSIZE) 292 if (end >= SOCKETBUFSIZE)
229 end -= SOCKETBUFSIZE; 293 end -= SOCKETBUFSIZE;
230 294
231 avail = SOCKETBUFSIZE - end; 295 avail = SOCKETBUFSIZE - end;
232 296
233 /* We can all fit it behind the current data without wrapping */ 297 /* We can all fit it behind the current data without wrapping */
234 if (avail >= len) 298 if (avail >= len)
235 memcpy (ns->outputbuffer.data + end, buf, len); 299 memcpy (outputbuffer.data + end, buf, len);
236 else 300 else
237 { 301 {
238 memcpy (ns->outputbuffer.data + end, buf, avail); 302 memcpy (outputbuffer.data + end, buf, avail);
239 memcpy (ns->outputbuffer.data, buf + avail, len - avail); 303 memcpy (outputbuffer.data, buf + avail, len - avail);
240 } 304 }
241 305
242 ns->outputbuffer.len += len; 306 outputbuffer.len += len;
243#if 0
244 LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start);
245#endif
246}
247
248/**
249 * Writes data to socket.
250 *
251 * When the socket is clear to write, and we have backlogged data, this
252 * is called to write it out.
253 */
254void
255write_socket_buffer (client_socket * ns)
256{
257 int amt, max;
258
259 if (ns->outputbuffer.len == 0)
260 {
261 LOG (llevDebug, "write_socket_buffer called when there is no data, fd=%d\n", ns->fd);
262 return;
263 }
264
265 do
266 {
267 max = SOCKETBUFSIZE - ns->outputbuffer.start;
268 if (ns->outputbuffer.len < max)
269 max = ns->outputbuffer.len;
270
271 do
272 {
273 amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max);
274 }
275 while ((amt < 0) && (errno == EINTR));
276
277 if (amt < 0)
278 { /* We got an error */
279
280 if (errno != EWOULDBLOCK)
281 {
282 LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno));
283 ns->status = Ns_Dead;
284 return;
285 }
286 else
287 { /* EWOULDBLOCK */
288 /* can't write it, so store it away. */
289 ns->can_write = 0;
290 return;
291 }
292 }
293 ns->outputbuffer.start += amt;
294 /* wrap back to start of buffer */
295 if (ns->outputbuffer.start == SOCKETBUFSIZE)
296 ns->outputbuffer.start = 0;
297 ns->outputbuffer.len -= amt;
298#ifdef CS_LOGSTATS
299 cst_tot.obytes += amt;
300 cst_lst.obytes += amt;
301#endif
302 }
303 while (ns->outputbuffer.len > 0);
304}
305
306/**
307 * This writes data to the socket. - It is very low level -
308 * all we try and do is write out the data to the socket
309 * provided (ns). buf is the data to write, len is the number
310 * of bytes to write. IT doesn't return anything - rather, it
311 * updates the ns structure if we get an error.
312 */
313void
314client_socket::send (void *buf_, int len)
315{
316 char *buf = (char *)buf_;
317 char *pos = buf;
318 int amt = 0;
319
320 if (status == Ns_Dead || !buf)
321 {
322 LOG (llevDebug, "Write_To_Socket called with dead socket\n");
323 return;
324 }
325
326#ifndef __GNU__ /* This caused problems on Hurd */
327 if (!can_write)
328 {
329 add_to_buffer (this, buf, len);
330 return;
331 }
332#endif
333
334 /* If we manage to write more than we wanted, take it as a bonus */
335 while (len > 0)
336 {
337 do
338 {
339 amt = write (fd, pos, len);
340 }
341 while ((amt < 0) && (errno == EINTR));
342
343 if (amt < 0)
344 { /* We got an error */
345 if (errno != EWOULDBLOCK)
346 {
347 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */
348 errno, strerror (errno));
349 status = Ns_Dead;
350 return;
351 }
352 else
353 { /* EWOULDBLOCK */
354 /* can't write it, so store it away. */
355 add_to_buffer (this, pos, len);
356 can_write = 0;
357 return;
358 }
359 }
360 /* amt gets set to 0 above in blocking code, so we do this as
361 * an else if to make sure we don't reprocess it.
362 */
363 else if (amt == 0)
364 LOG (llevError, "Write_To_Socket: No data written out.\n");
365
366 len -= amt;
367 pos += amt;
368#ifdef CS_LOGSTATS
369 cst_tot.obytes += amt;
370 cst_lst.obytes += amt;
371#endif
372 }
373} 307}
374 308
375void 309void
376client_socket::socket_cb (iow &w, int got) 310client_socket::socket_cb (iow &w, int got)
377{ 311{
378 //printf ("iow got %x\n", got); 312 write_outputbuffer ();
379 w.stop (); 313
314 if (!outputbuffer.len)
315 socket_ev.poll (socket_ev.poll () & ~PE_W);
380} 316}
381 317
382/** 318/**
383 * Takes a string of data, and writes it out to the socket. A very handy 319 * Takes a string of data, and writes it out to the socket. A very handy
384 * shortcut function. 320 * shortcut function.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines