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.18 by root, Thu Dec 14 05:09:32 2006 UTC vs.
Revision 1.21 by root, Fri Dec 15 00:14:13 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
67 rtt = tcpi.tcpi_rtt;
68 rttvar = tcpi.tcpi_rttvar;
66 69
67 if (tcpi.tcpi_unacked && SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s 70 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 71 && SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s
69 { 72 {
70 LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", ns->fd, 73 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); 74 (unsigned) tcpi.tcpi_last_ack_recv, (unsigned) tcpi.tcpi_last_data_sent, (unsigned) tcpi.tcpi_unacked);
72 ns->status = Ns_Dead; 75 status = Ns_Dead;
73 } 76 }
74 } 77 }
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 78#endif
79
80 /**
81 * Writes data to socket.
82 *
83 * When the socket is clear to write, and we have backlogged data, this
84 * is called to write it out.
85 */
86
87 if (!outputbuffer.len || socket_ev.poll () & PE_W)
88 return;
89
90 write_outputbuffer ();
91}
92
93void
94client_socket::write_outputbuffer ()
95{
96 while (outputbuffer.len)
97 {
98 int res = write (fd, outputbuffer.data + outputbuffer.start,
99 min (outputbuffer.len, SOCKETBUFSIZE - outputbuffer.start));
100
101 if (res > 0)
102 {
103 outputbuffer.start += res;
104 /* wrap back to start of buffer */
105 if (outputbuffer.start == SOCKETBUFSIZE)
106 outputbuffer.start = 0;
107
108 outputbuffer.len -= res;
109#ifdef CS_LOGSTATS
110 cst_tot.obytes += res;
111 cst_lst.obytes += res;
112#endif
113 }
114 else if (res == 0)
115 {
116 LOG (llevError, "socket write failed, connection closed.\n");
117 status = Ns_Dead;
118 return;
119 }
120 else if (errno == EINTR)
121 {
122 // just retry
123 }
124 else if (errno == EAGAIN)
125 {
126 // delay till ready
127 socket_ev.poll (socket_ev.poll () | PE_W);
128 socket_ev.start ();
129 return;
130 }
131 else
132 {
133 LOG (llevError, "socket write failed: %s\n", strerror (errno));
134 status = Ns_Dead;
135 return;
136 }
137 }
138
139 socket_ev.poll (socket_ev.poll () & ~PE_W);
81} 140}
82 141
83/*********************************************************************** 142/***********************************************************************
84 * 143 *
85 * packet functions/utilities 144 * packet functions/utilities
171 } 230 }
172 else if (amount < 0) 231 else if (amount < 0)
173 { 232 {
174 if (errno != EAGAIN && errno != EINTR) 233 if (errno != EAGAIN && errno != EINTR)
175 { 234 {
176 LOG (llevError, "read error: %s", strerror (errno)); 235 LOG (llevError, "read error: %s\n", strerror (errno));
177 return -1; 236 return -1;
178 } 237 }
179 238
180 return 0; 239 return 0;
181 } 240 }
204 * Adds data to a socket buffer for whatever reason. 263 * Adds data to a socket buffer for whatever reason.
205 * 264 *
206 * ns is the socket we are adding the data to, buf is the start of the 265 * 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. 266 * data, and len is the number of bytes to add.
208 */ 267 */
209 268void
210static void 269client_socket::send (void *buf_, int len)
211add_to_buffer (client_socket *ns, char *buf, int len)
212{ 270{
271 char *buf = (char *)buf_;
272 char *pos = buf;
273 int amt = 0;
274
275 if (status == Ns_Dead || !buf)
276 {
277 LOG (llevDebug, "Write_To_Socket called with dead socket\n");
278 return;
279 }
280
281 if ((len + outputbuffer.len) > SOCKETBUFSIZE)
282 {
283 LOG (llevDebug, "Socket on fd %d has overrun internal buffer - marking as dead\n", fd);
284 status = Ns_Dead;
285 return;
286 }
287
213 int avail, end; 288 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 289
222 /* data + end is where we start putting the new data. The last byte 290 /* data + end is where we start putting the new data. The last byte
223 * currently in use is actually data + end -1 291 * currently in use is actually data + end -1
224 */ 292 */
225
226 end = ns->outputbuffer.start + ns->outputbuffer.len; 293 end = outputbuffer.start + outputbuffer.len;
227 /* The buffer is already in a wrapped state, so adjust end */ 294 /* The buffer is already in a wrapped state, so adjust end */
228 if (end >= SOCKETBUFSIZE) 295 if (end >= SOCKETBUFSIZE)
229 end -= SOCKETBUFSIZE; 296 end -= SOCKETBUFSIZE;
230 297
231 avail = SOCKETBUFSIZE - end; 298 avail = SOCKETBUFSIZE - end;
232 299
233 /* We can all fit it behind the current data without wrapping */ 300 /* We can all fit it behind the current data without wrapping */
234 if (avail >= len) 301 if (avail >= len)
235 memcpy (ns->outputbuffer.data + end, buf, len); 302 memcpy (outputbuffer.data + end, buf, len);
236 else 303 else
237 { 304 {
238 memcpy (ns->outputbuffer.data + end, buf, avail); 305 memcpy (outputbuffer.data + end, buf, avail);
239 memcpy (ns->outputbuffer.data, buf + avail, len - avail); 306 memcpy (outputbuffer.data, buf + avail, len - avail);
240 } 307 }
241 308
242 ns->outputbuffer.len += len; 309 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} 310}
247 311
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 312void
255write_socket_buffer (client_socket * ns) 313client_socket::socket_cb (iow &w, int got)
256{ 314{
257 int amt, max; 315 write_outputbuffer ();
258 316
259 if (ns->outputbuffer.len == 0) 317 if (!outputbuffer.len)
260 { 318 socket_ev.poll (socket_ev.poll () & ~PE_W);
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} 319}
374 320
375/** 321/**
376 * Takes a string of data, and writes it out to the socket. A very handy 322 * Takes a string of data, and writes it out to the socket. A very handy
377 * shortcut function. 323 * shortcut function.
378 */ 324 */
379
380void 325void
381client_socket::send_packet (packet &sl) 326client_socket::send_packet (packet &sl)
382{ 327{
383 Send_With_Handling (this, &sl);
384}
385
386void
387client_socket::send_packet (const char *buf, int len)
388{
389 packet sl;
390
391 sl << data (buf, len);
392 send_packet (sl);
393}
394
395void
396client_socket::send_packet (const char *buf)
397{
398 send_packet (buf, strlen (buf));
399}
400
401/**
402 * Calls Write_To_Socket to send data to the client.
403 *
404 * The only difference in this function is that we take a packet
405 *, and we prepend the length information.
406 */
407void
408Send_With_Handling (client_socket *ns, packet *msg)
409{
410 unsigned char sbuf[4];
411
412 if (ns->status == Ns_Dead || !msg) 328 if (status == Ns_Dead)
413 return; 329 return;
414 330
415 if (msg->length () >= MAXSOCKBUF) 331 if (sl.length () >= MAXSOCKBUF)
416 { 332 {
417 LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", msg->length ()); 333 LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ());
418 /* Almost certainly we've overflowed a buffer, so quite now to make 334 /* Almost certainly we've overflowed a buffer, so quit now to make
419 * it easier to debug. 335 * it easier to debug.
420 */ 336 */
421 abort (); 337 abort ();
422 } 338 }
423 339
424 sbuf[0] = ((uint32) (msg->length ()) >> 8); 340 if (!sl.length ())
425 sbuf[1] = ((uint32) (msg->length ()) ); 341 return;
426 342
427 //TODO: single write, or writev 343 assert (sl.hdrlen == 2);
428 ns->send (sbuf, 2); 344
429 ns->send (msg->buf, msg->length ()); 345 sl.buf_ [0] = sl.length () >> 8;
346 sl.buf_ [1] = sl.length () ;
347
348 send (sl.buf_, sl.length () + sl.hdrlen);
349}
350
351void
352client_socket::send_packet (const char *buf, int len)
353{
354 packet sl;
355
356 sl << data (buf, len);
357 send_packet (sl);
358}
359
360void
361client_socket::send_packet (const char *buf)
362{
363 send_packet (buf, strlen (buf));
430} 364}
431 365
432/****************************************************************************** 366/******************************************************************************
433 * 367 *
434 * statistics logging functions. 368 * statistics logging functions.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines