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.6 by root, Thu Sep 14 22:34:06 2006 UTC vs.
Revision 1.18 by root, Thu Dec 14 05:09:32 2006 UTC

35using namespace std; 35using namespace std;
36 36
37#include <global.h> 37#include <global.h>
38#include <newclient.h> 38#include <newclient.h>
39#include <sproto.h> 39#include <sproto.h>
40#include <cstdarg>
40 41
41#ifdef __linux__ 42#ifdef __linux__
42# include <sys/types.h> 43# include <sys/types.h>
43# include <sys/socket.h> 44# include <sys/socket.h>
44# include <netinet/in.h> 45# include <netinet/in.h>
50// easily die in 20 seconds... 51// easily die in 20 seconds...
51#define SOCKET_TIMEOUT1 10 52#define SOCKET_TIMEOUT1 10
52#define SOCKET_TIMEOUT2 20 53#define SOCKET_TIMEOUT2 20
53 54
54void 55void
55Socket_Flush (NewSocket * ns) 56Socket_Flush (client_socket * ns)
56{ 57{
57#ifdef __linux__ 58#ifdef __linux__
58 // check time of last ack, and, if too old, kill connection 59 // check time of last ack, and, if too old, kill connection
59 struct tcp_info tcpi; 60 struct tcp_info tcpi;
60 socklen_t len = sizeof (tcpi); 61 socklen_t len = sizeof (tcpi);
72 } 73 }
73 } 74 }
74 75
75 int val; 76 int val;
76 77
77 val = 0;
78 setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); 78 val = 0; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val));
79 val = 1;
80 setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); 79 val = 1; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val));
81#endif 80#endif
82} 81}
83 82
84/*********************************************************************** 83/***********************************************************************
85 * 84 *
86 * SockList functions/utilities 85 * packet functions/utilities
87 * 86 *
88 **********************************************************************/ 87 **********************************************************************/
89 88
90void 89packet &packet::operator <<(const data &v)
91SockList_Init (SockList * sl)
92{ 90{
93 sl->len = 0; 91 if (room () < v.len)
94 sl->buf = NULL; 92 reset ();
95} 93 else
94 {
95 if (v.len)
96 {
97 memcpy (cur, v.ptr, v.len);
98 cur += v.len;
99 }
100 }
96 101
97void 102 return *this;
98SockList_AddInt (SockList * sl, uint32 data)
99{
100 sl->buf[sl->len++] = (data >> 24) & 0xff;
101 sl->buf[sl->len++] = (data >> 16) & 0xff;
102 sl->buf[sl->len++] = (data >> 8) & 0xff;
103 sl->buf[sl->len++] = data & 0xff;
104} 103}
105 104
106void 105packet &packet::operator <<(const data8 &v)
107SockList_AddInt64 (SockList * sl, uint64 data)
108{ 106{
109 sl->buf[sl->len++] = (char) ((data >> 56) & 0xff); 107 unsigned int len = min (v.len, 0x00FF);
110 sl->buf[sl->len++] = (char) ((data >> 48) & 0xff); 108 return *this << uint8 (len) << data (v.ptr, len);
111 sl->buf[sl->len++] = (char) ((data >> 40) & 0xff);
112 sl->buf[sl->len++] = (char) ((data >> 32) & 0xff);
113
114 sl->buf[sl->len++] = (char) ((data >> 24) & 0xff);
115 sl->buf[sl->len++] = (char) ((data >> 16) & 0xff);
116 sl->buf[sl->len++] = (char) ((data >> 8) & 0xff);
117 sl->buf[sl->len++] = (char) (data & 0xff);
118} 109}
119 110
120/* Basically does the reverse of SockList_AddInt, but on 111packet &packet::operator <<(const data16 &v)
121 * strings instead. Same for the GetShort, but for 16 bits. 112{
113 unsigned int len = min (v.len, 0xFFFF);
114 return *this << uint16 (len) << data (v.ptr, len);
115}
116
117packet &packet::operator <<(const char *v)
118{
119 return *this << data (v, strlen (v ? v : 0));
120}
121
122void
123packet::printf (const char *format, ...)
124{
125 int size = room ();
126
127 va_list ap;
128 va_start (ap, format);
129 int len = vsnprintf ((char *)cur, size, format, ap);
130 va_end (ap);
131
132 if (len >= size)
133 return reset ();
134
135 cur += len;
136}
137
138/******************************************************************************
122 */ 139 *
140 * Start of read routines.
141 *
142 ******************************************************************************/
143
123int 144int
124GetInt_String (unsigned char *data) 145client_socket::read_packet ()
125{ 146{
126 return ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]); 147 for (;;)
127}
128
129short
130GetShort_String (unsigned char *data)
131{
132 return ((data[0] << 8) + data[1]);
133}
134
135/******************************************************************************
136 *
137 * Start of read routines.
138 *
139 ******************************************************************************/
140
141/**
142 * This reads from fd and puts the data in sl. We return true if we think
143 * we have a full packet, 0 if we have a partial packet. The only processing
144 * we do is remove the intial size value. len (As passed) is the size of the
145 * buffer allocated in the socklist. We make the assumption the buffer is
146 * at least 2 bytes long.
147 */
148
149int
150SockList_ReadPacket (int fd, SockList * sl, int len)
151{
152 int stat, toread;
153
154 /* Sanity check - shouldn't happen */
155 if (sl->len < 0)
156 {
157 abort ();
158 } 148 {
159 /* We already have a partial packet */ 149 if (inbuf_len >= 2)
160 if (sl->len < 2)
161 {
162#ifdef WIN32 /* ***WIN32 SockList_ReadPacket: change read() to recv() */
163
164 stat = recv (fd, sl->buf + sl->len, 2 - sl->len, 0);
165
166#else
167 do
168 {
169 stat = read (fd, sl->buf + sl->len, 2 - sl->len);
170 } 150 {
171 while ((stat == -1) && (errno == EINTR)); 151 unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1];
172#endif 152
173 if (stat < 0) 153 if (inbuf_len >= 2 + pkt_len)
154 return pkt_len + 2;
174 { 155 }
175 /* In non blocking mode, EAGAIN is set when there is no 156
176 * data available. 157 int amount = sizeof (inbuf) - inbuf_len;
177 */ 158
178#ifdef WIN32 /* ***WIN32 SockList_ReadPacket: error handling for win32 */ 159 if (amount <= 0)
179 if ((stat == -1) && WSAGetLastError () != WSAEWOULDBLOCK) 160 {
161 LOG (llevError, "packet too large");//TODO
162 return -1;
163 }
164
165 amount = read (fd, inbuf + inbuf_len, amount);
166
167 if (!amount)
168 {
169 status = Ns_Dead;
170 return -1;
171 }
172 else if (amount < 0)
173 {
174 if (errno != EAGAIN && errno != EINTR)
180 { 175 {
181 if (WSAGetLastError () == WSAECONNRESET) 176 LOG (llevError, "read error: %s", strerror (errno));
182 LOG (llevDebug, "Connection closed by client\n"); 177 return -1;
183 else
184 {
185 LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ());
186 }
187 return -1; /* kick this user! */
188 } 178 }
189#else
190 if (errno != EAGAIN && errno != EWOULDBLOCK)
191 {
192 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno));
193 }
194#endif
195 return 0; /*Error */
196 }
197 if (stat == 0)
198 return -1;
199 sl->len += stat;
200#ifdef CS_LOGSTATS
201 cst_tot.ibytes += stat;
202 cst_lst.ibytes += stat;
203#endif
204 if (stat < 2)
205 return 0; /* Still don't have a full packet */
206 }
207 /* Figure out how much more data we need to read. Add 2 from the
208 * end of this - size header information is not included.
209 */
210 toread = 2 + (sl->buf[0] << 8) + sl->buf[1] - sl->len;
211 if ((toread + sl->len) >= len)
212 {
213 LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len);
214 /* Quick hack in case for 'oldsocketmode' input. If we are
215 * closing the socket anyways, then reading this extra 100 bytes
216 * shouldn't hurt.
217 */
218#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */
219 recv (fd, sl->buf + 2, 100, 0);
220#else
221 read (fd, sl->buf + 2, 100);
222#endif /* end win32 */
223 179
224 /* return error so the socket is closed */
225 return -1;
226 }
227 do
228 {
229#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */
230 stat = recv (fd, sl->buf + sl->len, toread, 0);
231#else
232 do
233 {
234 stat = read (fd, sl->buf + sl->len, toread);
235 }
236 while ((stat < 0) && (errno == EINTR));
237#endif
238 if (stat < 0)
239 {
240
241#ifdef WIN32 /* ***win32 SockList_ReadPacket: change error handling for win32 */
242 if ((stat == -1) && WSAGetLastError () != WSAEWOULDBLOCK)
243 {
244 if (WSAGetLastError () == WSAECONNRESET)
245 LOG (llevDebug, "Connection closed by client\n");
246 else
247 {
248 LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ());
249 }
250 return -1; /* kick this user! */
251 }
252#else
253 if (errno != EAGAIN && errno != EWOULDBLOCK)
254 {
255 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno));
256 }
257#endif
258 return 0; /*Error */
259 }
260 if (stat == 0)
261 return -1;
262 sl->len += stat;
263#ifdef CS_LOGSTATS
264 cst_tot.ibytes += stat;
265 cst_lst.ibytes += stat;
266#endif
267 toread -= stat;
268 if (toread == 0)
269 return 1;
270 if (toread < 0)
271 {
272 LOG (llevError, "SockList_ReadPacket: Read more bytes than desired.\n");
273 return 1; 180 return 0;
274 } 181 }
182
183 inbuf_len += amount;
184
185 cst_tot.ibytes += amount;
186 cst_lst.ibytes += amount;
275 } 187 }
276 while (toread > 0); 188}
277 return 0; 189
190void
191client_socket::skip_packet (int len)
192{
193 inbuf_len -= len;
194 memmove (inbuf, inbuf + len, inbuf_len);
278} 195}
279 196
280/******************************************************************************* 197/*******************************************************************************
281 * 198 *
282 * Start of write related routines. 199 * Start of write related routines.
289 * ns is the socket we are adding the data to, buf is the start of the 206 * ns is the socket we are adding the data to, buf is the start of the
290 * data, and len is the number of bytes to add. 207 * data, and len is the number of bytes to add.
291 */ 208 */
292 209
293static void 210static void
294add_to_buffer (NewSocket * ns, char *buf, int len) 211add_to_buffer (client_socket *ns, char *buf, int len)
295{ 212{
296 int avail, end; 213 int avail, end;
297 214
298 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) 215 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE)
299 { 216 {
308 225
309 end = ns->outputbuffer.start + ns->outputbuffer.len; 226 end = ns->outputbuffer.start + ns->outputbuffer.len;
310 /* The buffer is already in a wrapped state, so adjust end */ 227 /* The buffer is already in a wrapped state, so adjust end */
311 if (end >= SOCKETBUFSIZE) 228 if (end >= SOCKETBUFSIZE)
312 end -= SOCKETBUFSIZE; 229 end -= SOCKETBUFSIZE;
230
313 avail = SOCKETBUFSIZE - end; 231 avail = SOCKETBUFSIZE - end;
314 232
315 /* We can all fit it behind the current data without wrapping */ 233 /* We can all fit it behind the current data without wrapping */
316 if (avail >= len) 234 if (avail >= len)
317 {
318 memcpy (ns->outputbuffer.data + end, buf, len); 235 memcpy (ns->outputbuffer.data + end, buf, len);
319 }
320 else 236 else
321 { 237 {
322 memcpy (ns->outputbuffer.data + end, buf, avail); 238 memcpy (ns->outputbuffer.data + end, buf, avail);
323 memcpy (ns->outputbuffer.data, buf + avail, len - avail); 239 memcpy (ns->outputbuffer.data, buf + avail, len - avail);
324 } 240 }
241
325 ns->outputbuffer.len += len; 242 ns->outputbuffer.len += len;
326#if 0 243#if 0
327 LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start); 244 LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start);
328#endif 245#endif
329} 246}
333 * 250 *
334 * When the socket is clear to write, and we have backlogged data, this 251 * When the socket is clear to write, and we have backlogged data, this
335 * is called to write it out. 252 * is called to write it out.
336 */ 253 */
337void 254void
338write_socket_buffer (NewSocket * ns) 255write_socket_buffer (client_socket * ns)
339{ 256{
340 int amt, max; 257 int amt, max;
341 258
342 if (ns->outputbuffer.len == 0) 259 if (ns->outputbuffer.len == 0)
343 { 260 {
349 { 266 {
350 max = SOCKETBUFSIZE - ns->outputbuffer.start; 267 max = SOCKETBUFSIZE - ns->outputbuffer.start;
351 if (ns->outputbuffer.len < max) 268 if (ns->outputbuffer.len < max)
352 max = ns->outputbuffer.len; 269 max = ns->outputbuffer.len;
353 270
354#ifdef WIN32 /* ***win32 write_socket_buffer: change write() to send() */
355 amt = send (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max, 0);
356#else
357 do 271 do
358 { 272 {
359 amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); 273 amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max);
360 } 274 }
361 while ((amt < 0) && (errno == EINTR)); 275 while ((amt < 0) && (errno == EINTR));
362#endif
363 276
364 if (amt < 0) 277 if (amt < 0)
365 { /* We got an error */ 278 { /* We got an error */
366 279
367#ifdef WIN32 /* ***win32 write_socket_buffer: change error handling */
368 if (amt == -1 && WSAGetLastError () != WSAEWOULDBLOCK)
369 {
370 LOG (llevError, "New socket write failed (wsb) (%d).\n", WSAGetLastError ());
371#else
372 if (errno != EWOULDBLOCK) 280 if (errno != EWOULDBLOCK)
373 { 281 {
374 LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); 282 LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno));
375#endif
376 ns->status = Ns_Dead; 283 ns->status = Ns_Dead;
377 return; 284 return;
378 } 285 }
379 else 286 else
380 { /* EWOULDBLOCK */ 287 { /* EWOULDBLOCK */
402 * provided (ns). buf is the data to write, len is the number 309 * provided (ns). buf is the data to write, len is the number
403 * of bytes to write. IT doesn't return anything - rather, it 310 * of bytes to write. IT doesn't return anything - rather, it
404 * updates the ns structure if we get an error. 311 * updates the ns structure if we get an error.
405 */ 312 */
406void 313void
407Write_To_Socket (NewSocket * ns, char *buf, int len) 314client_socket::send (void *buf_, int len)
408{ 315{
316 char *buf = (char *)buf_;
317 char *pos = buf;
409 int amt = 0; 318 int amt = 0;
410 char *pos = buf;
411 319
412 if (ns->status == Ns_Dead || !buf) 320 if (status == Ns_Dead || !buf)
413 { 321 {
414 LOG (llevDebug, "Write_To_Socket called with dead socket\n"); 322 LOG (llevDebug, "Write_To_Socket called with dead socket\n");
415 return; 323 return;
416 } 324 }
417 325
418#ifndef __GNU__ /* This caused problems on Hurd */ 326#ifndef __GNU__ /* This caused problems on Hurd */
419 if (!ns->can_write) 327 if (!can_write)
420 { 328 {
421 add_to_buffer (ns, buf, len); 329 add_to_buffer (this, buf, len);
422 return; 330 return;
423 } 331 }
424#endif 332#endif
333
425 /* If we manage to write more than we wanted, take it as a bonus */ 334 /* If we manage to write more than we wanted, take it as a bonus */
426 while (len > 0) 335 while (len > 0)
427 { 336 {
428
429#ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */
430 amt = send (ns->fd, pos, len, 0);
431#else
432 do 337 do
433 { 338 {
434 amt = write (ns->fd, pos, len); 339 amt = write (fd, pos, len);
435 } 340 }
436 while ((amt < 0) && (errno == EINTR)); 341 while ((amt < 0) && (errno == EINTR));
437#endif
438 342
439 if (amt < 0) 343 if (amt < 0)
440 { /* We got an error */ 344 { /* We got an error */
441#ifdef WIN32 /* ***win32 Write_To_Socket: change error handling */
442 if (amt == -1 && WSAGetLastError () != WSAEWOULDBLOCK)
443 {
444 LOG (llevError, "New socket write failed WTS (%d).\n", WSAGetLastError ());
445#else
446 if (errno != EWOULDBLOCK) 345 if (errno != EWOULDBLOCK)
447 { 346 {
448 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ 347 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */
449 errno, strerror (errno)); 348 errno, strerror (errno));
450#endif
451 ns->status = Ns_Dead; 349 status = Ns_Dead;
452 return; 350 return;
453 } 351 }
454 else 352 else
455 { /* EWOULDBLOCK */ 353 { /* EWOULDBLOCK */
456 /* can't write it, so store it away. */ 354 /* can't write it, so store it away. */
457 add_to_buffer (ns, pos, len); 355 add_to_buffer (this, pos, len);
458 ns->can_write = 0; 356 can_write = 0;
459 return; 357 return;
460 } 358 }
461 } 359 }
462 /* amt gets set to 0 above in blocking code, so we do this as 360 /* amt gets set to 0 above in blocking code, so we do this as
463 * an else if to make sure we don't reprocess it. 361 * an else if to make sure we don't reprocess it.
464 */ 362 */
465 else if (amt == 0) 363 else if (amt == 0)
466 {
467 LOG (llevError, "Write_To_Socket: No data written out.\n"); 364 LOG (llevError, "Write_To_Socket: No data written out.\n");
468 } 365
469 len -= amt; 366 len -= amt;
470 pos += amt; 367 pos += amt;
471#ifdef CS_LOGSTATS 368#ifdef CS_LOGSTATS
472 cst_tot.obytes += amt; 369 cst_tot.obytes += amt;
473 cst_lst.obytes += amt; 370 cst_lst.obytes += amt;
474#endif 371#endif
475 } 372 }
476} 373}
477 374
478
479/** 375/**
480 * Takes a string of data, and writes it out to the socket. A very handy 376 * Takes a string of data, and writes it out to the socket. A very handy
481 * shortcut function. 377 * shortcut function.
482 */ 378 */
483void
484cs_write_string (NewSocket * ns, const char *buf, int len)
485{
486 SockList sl;
487 379
488 sl.len = len; 380void
489 sl.buf = (unsigned char *) buf; 381client_socket::send_packet (packet &sl)
382{
490 Send_With_Handling (ns, &sl); 383 Send_With_Handling (this, &sl);
491} 384}
492 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}
493 400
494/** 401/**
495 * Calls Write_To_Socket to send data to the client. 402 * Calls Write_To_Socket to send data to the client.
496 * 403 *
497 * The only difference in this function is that we take a SockList 404 * The only difference in this function is that we take a packet
498 *, and we prepend the length information. 405 *, and we prepend the length information.
499 */ 406 */
500void 407void
501Send_With_Handling (NewSocket * ns, SockList * msg) 408Send_With_Handling (client_socket *ns, packet *msg)
502{ 409{
503 unsigned char sbuf[4]; 410 unsigned char sbuf[4];
504 411
505 if (ns->status == Ns_Dead || !msg) 412 if (ns->status == Ns_Dead || !msg)
506 return; 413 return;
507 414
508 if (msg->len >= MAXSOCKBUF) 415 if (msg->length () >= MAXSOCKBUF)
509 { 416 {
510 LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", msg->len); 417 LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", msg->length ());
511 /* Almost certainly we've overflowed a buffer, so quite now to make 418 /* Almost certainly we've overflowed a buffer, so quite now to make
512 * it easier to debug. 419 * it easier to debug.
513 */ 420 */
514 abort (); 421 abort ();
515 } 422 }
423
516 sbuf[0] = ((uint32) (msg->len) >> 8) & 0xFF; 424 sbuf[0] = ((uint32) (msg->length ()) >> 8);
517 sbuf[1] = ((uint32) (msg->len)) & 0xFF; 425 sbuf[1] = ((uint32) (msg->length ()) );
518 if (ns->status != Ns_Old)
519 Write_To_Socket (ns, (char *) sbuf, 2);
520 Write_To_Socket (ns, (char *) msg->buf, msg->len);
521}
522 426
523/** 427 //TODO: single write, or writev
524 * Takes a string of data, and writes it out to the socket. A very handy 428 ns->send (sbuf, 2);
525 * shortcut function. 429 ns->send (msg->buf, msg->length ());
526 */
527void
528Write_String_To_Socket (NewSocket * ns, char *buf, int len)
529{
530 SockList sl;
531
532 sl.len = len;
533 sl.buf = (unsigned char *) buf;
534 Send_With_Handling (ns, &sl);
535} 430}
536
537 431
538/****************************************************************************** 432/******************************************************************************
539 * 433 *
540 * statistics logging functions. 434 * statistics logging functions.
541 * 435 *
564 LOG (llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n", 458 LOG (llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n",
565 ctime (&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn, 459 ctime (&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn,
566 now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes, cst_lst.max_conn, now - cst_lst.time_start); 460 now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes, cst_lst.max_conn, now - cst_lst.time_start);
567 cst_lst.ibytes = 0; 461 cst_lst.ibytes = 0;
568 cst_lst.obytes = 0; 462 cst_lst.obytes = 0;
569 cst_lst.max_conn = socket_info.nconns;
570 cst_lst.time_start = now; 463 cst_lst.time_start = now;
571} 464}
572#endif 465#endif
466

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines