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.11 by root, Wed Dec 13 21:27:09 2006 UTC vs.
Revision 1.18 by root, Thu Dec 14 05:09:32 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 (NewSocket * ns) 56Socket_Flush (client_socket * ns)
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);
80#endif 80#endif
81} 81}
82 82
83/*********************************************************************** 83/***********************************************************************
84 * 84 *
85 * SockList functions/utilities 85 * packet functions/utilities
86 * 86 *
87 **********************************************************************/ 87 **********************************************************************/
88 88
89SockList &SockList::operator <<(const data &v) 89packet &packet::operator <<(const data &v)
90{ 90{
91 if (room () < v.len)
92 reset ();
93 else
94 {
91 if (v.len) 95 if (v.len)
92 { 96 {
93 memcpy (buf + len, v.ptr, v.len); 97 memcpy (cur, v.ptr, v.len);
94 len += v.len; 98 cur += v.len;
99 }
95 } 100 }
96 101
97 return *this; 102 return *this;
98} 103}
99 104
100SockList &SockList::operator <<(const data8 &v) 105packet &packet::operator <<(const data8 &v)
101{ 106{
102 unsigned int len = min (v.len, 0x00FF); 107 unsigned int len = min (v.len, 0x00FF);
103 return *this << uint8 (len) << data (v.ptr, len); 108 return *this << uint8 (len) << data (v.ptr, len);
104} 109}
105 110
106SockList &SockList::operator <<(const data16 &v) 111packet &packet::operator <<(const data16 &v)
107{ 112{
108 unsigned int len = min (v.len, 0xFFFF); 113 unsigned int len = min (v.len, 0xFFFF);
109 return *this << uint16 (len) << data (v.ptr, len); 114 return *this << uint16 (len) << data (v.ptr, len);
110} 115}
111 116
112SockList &SockList::operator <<(const char *v) 117packet &packet::operator <<(const char *v)
113{ 118{
114 return *this << data (v, strlen (v ? v : 0)); 119 return *this << data (v, strlen (v ? v : 0));
115} 120}
116 121
117void 122void
118SockList::printf (const char *format, ...) 123packet::printf (const char *format, ...)
119{ 124{
125 int size = room ();
126
120 va_list ap; 127 va_list ap;
121 va_start (ap, format); 128 va_start (ap, format);
122
123 len += vsprintf ((char *)buf + len, format, ap); 129 int len = vsnprintf ((char *)cur, size, format, ap);
124
125 va_end (ap); 130 va_end (ap);
126}
127 131
128/* Basically does the reverse of SockList_AddInt, but on 132 if (len >= size)
129 * strings instead. Same for the GetShort, but for 16 bits. 133 return reset ();
134
135 cur += len;
136}
137
138/******************************************************************************
130 */ 139 *
140 * Start of read routines.
141 *
142 ******************************************************************************/
143
131int 144int
132GetInt_String (unsigned char *data) 145client_socket::read_packet ()
133{ 146{
134 return ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]); 147 for (;;)
135}
136
137short
138GetShort_String (unsigned char *data)
139{
140 return ((data[0] << 8) + data[1]);
141}
142
143/******************************************************************************
144 *
145 * Start of read routines.
146 *
147 ******************************************************************************/
148
149/**
150 * This reads from fd and puts the data in sl. We return true if we think
151 * we have a full packet, 0 if we have a partial packet. The only processing
152 * we do is remove the intial size value. len (As passed) is the size of the
153 * buffer allocated in the socklist. We make the assumption the buffer is
154 * at least 2 bytes long.
155 */
156
157int
158SockList_ReadPacket (int fd, SockList *sl, int len)
159{
160 int stat, toread;
161
162 /* Sanity check - shouldn't happen */
163 if (sl->len < 0)
164 {
165 abort ();
166 } 148 {
167 /* We already have a partial packet */ 149 if (inbuf_len >= 2)
168 if (sl->len < 2)
169 {
170 do
171 {
172 stat = read (fd, sl->buf + sl->len, 2 - sl->len);
173 } 150 {
174 while ((stat == -1) && (errno == EINTR)); 151 unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1];
175 152
176 if (stat < 0) 153 if (inbuf_len >= 2 + pkt_len)
154 return pkt_len + 2;
177 { 155 }
178 /* In non blocking mode, EAGAIN is set when there is no 156
179 * data available. 157 int amount = sizeof (inbuf) - inbuf_len;
180 */ 158
159 if (amount <= 0)
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 {
181 if (errno != EAGAIN && errno != EWOULDBLOCK) 174 if (errno != EAGAIN && errno != EINTR)
182 { 175 {
183 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); 176 LOG (llevError, "read error: %s", strerror (errno));
177 return -1;
184 } 178 }
185 return 0; /*Error */
186 }
187 if (stat == 0)
188 return -1;
189 sl->len += stat;
190#ifdef CS_LOGSTATS
191 cst_tot.ibytes += stat;
192 cst_lst.ibytes += stat;
193#endif
194 if (stat < 2)
195 return 0; /* Still don't have a full packet */
196 }
197 /* Figure out how much more data we need to read. Add 2 from the
198 * end of this - size header information is not included.
199 */
200 toread = 2 + (sl->buf[0] << 8) + sl->buf[1] - sl->len;
201 if ((toread + sl->len) >= len)
202 {
203 LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len);
204 /* Quick hack in case for 'oldsocketmode' input. If we are
205 * closing the socket anyways, then reading this extra 100 bytes
206 * shouldn't hurt.
207 */
208 read (fd, sl->buf + 2, 100);
209 179
210 /* return error so the socket is closed */
211 return -1;
212 }
213 do
214 {
215 do
216 {
217 stat = read (fd, sl->buf + sl->len, toread);
218 }
219 while ((stat < 0) && (errno == EINTR));
220 if (stat < 0)
221 {
222
223 if (errno != EAGAIN && errno != EWOULDBLOCK)
224 {
225 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno));
226 }
227 return 0; /*Error */
228 }
229 if (stat == 0)
230 return -1;
231 sl->len += stat;
232#ifdef CS_LOGSTATS
233 cst_tot.ibytes += stat;
234 cst_lst.ibytes += stat;
235#endif
236 toread -= stat;
237 if (toread == 0)
238 return 1;
239 if (toread < 0)
240 {
241 LOG (llevError, "SockList_ReadPacket: Read more bytes than desired.\n");
242 return 1; 180 return 0;
243 } 181 }
182
183 inbuf_len += amount;
184
185 cst_tot.ibytes += amount;
186 cst_lst.ibytes += amount;
244 } 187 }
245 while (toread > 0); 188}
246 return 0; 189
190void
191client_socket::skip_packet (int len)
192{
193 inbuf_len -= len;
194 memmove (inbuf, inbuf + len, inbuf_len);
247} 195}
248 196
249/******************************************************************************* 197/*******************************************************************************
250 * 198 *
251 * Start of write related routines. 199 * Start of write related routines.
258 * 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
259 * data, and len is the number of bytes to add. 207 * data, and len is the number of bytes to add.
260 */ 208 */
261 209
262static void 210static void
263add_to_buffer (NewSocket * ns, char *buf, int len) 211add_to_buffer (client_socket *ns, char *buf, int len)
264{ 212{
265 int avail, end; 213 int avail, end;
266 214
267 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) 215 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE)
268 { 216 {
302 * 250 *
303 * 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
304 * is called to write it out. 252 * is called to write it out.
305 */ 253 */
306void 254void
307write_socket_buffer (NewSocket * ns) 255write_socket_buffer (client_socket * ns)
308{ 256{
309 int amt, max; 257 int amt, max;
310 258
311 if (ns->outputbuffer.len == 0) 259 if (ns->outputbuffer.len == 0)
312 { 260 {
361 * 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
362 * of bytes to write. IT doesn't return anything - rather, it 310 * of bytes to write. IT doesn't return anything - rather, it
363 * updates the ns structure if we get an error. 311 * updates the ns structure if we get an error.
364 */ 312 */
365void 313void
366Write_To_Socket (NewSocket * ns, char *buf, int len) 314client_socket::send (void *buf_, int len)
367{ 315{
316 char *buf = (char *)buf_;
317 char *pos = buf;
368 int amt = 0; 318 int amt = 0;
369 char *pos = buf;
370 319
371 if (ns->status == Ns_Dead || !buf) 320 if (status == Ns_Dead || !buf)
372 { 321 {
373 LOG (llevDebug, "Write_To_Socket called with dead socket\n"); 322 LOG (llevDebug, "Write_To_Socket called with dead socket\n");
374 return; 323 return;
375 } 324 }
376 325
377#ifndef __GNU__ /* This caused problems on Hurd */ 326#ifndef __GNU__ /* This caused problems on Hurd */
378 if (!ns->can_write) 327 if (!can_write)
379 { 328 {
380 add_to_buffer (ns, buf, len); 329 add_to_buffer (this, buf, len);
381 return; 330 return;
382 } 331 }
383#endif 332#endif
333
384 /* 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 */
385 while (len > 0) 335 while (len > 0)
386 { 336 {
387
388 do 337 do
389 { 338 {
390 amt = write (ns->fd, pos, len); 339 amt = write (fd, pos, len);
391 } 340 }
392 while ((amt < 0) && (errno == EINTR)); 341 while ((amt < 0) && (errno == EINTR));
393 342
394 if (amt < 0) 343 if (amt < 0)
395 { /* We got an error */ 344 { /* We got an error */
396 if (errno != EWOULDBLOCK) 345 if (errno != EWOULDBLOCK)
397 { 346 {
398 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ 347 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */
399 errno, strerror (errno)); 348 errno, strerror (errno));
400 ns->status = Ns_Dead; 349 status = Ns_Dead;
401 return; 350 return;
402 } 351 }
403 else 352 else
404 { /* EWOULDBLOCK */ 353 { /* EWOULDBLOCK */
405 /* can't write it, so store it away. */ 354 /* can't write it, so store it away. */
406 add_to_buffer (ns, pos, len); 355 add_to_buffer (this, pos, len);
407 ns->can_write = 0; 356 can_write = 0;
408 return; 357 return;
409 } 358 }
410 } 359 }
411 /* 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
412 * an else if to make sure we don't reprocess it. 361 * an else if to make sure we don't reprocess it.
413 */ 362 */
414 else if (amt == 0) 363 else if (amt == 0)
415 {
416 LOG (llevError, "Write_To_Socket: No data written out.\n"); 364 LOG (llevError, "Write_To_Socket: No data written out.\n");
417 } 365
418 len -= amt; 366 len -= amt;
419 pos += amt; 367 pos += amt;
420#ifdef CS_LOGSTATS 368#ifdef CS_LOGSTATS
421 cst_tot.obytes += amt; 369 cst_tot.obytes += amt;
422 cst_lst.obytes += amt; 370 cst_lst.obytes += amt;
423#endif 371#endif
424 } 372 }
425} 373}
426 374
427
428/** 375/**
429 * 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
430 * shortcut function. 377 * shortcut function.
431 */ 378 */
432void
433cs_write_string (NewSocket *ns, const char *buf, int len)
434{
435 SockList sl;
436 379
437 sl.len = len; 380void
438 sl.buf = (unsigned char *) buf; 381client_socket::send_packet (packet &sl)
382{
439 Send_With_Handling (ns, &sl); 383 Send_With_Handling (this, &sl);
440} 384}
441 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}
442 400
443/** 401/**
444 * Calls Write_To_Socket to send data to the client. 402 * Calls Write_To_Socket to send data to the client.
445 * 403 *
446 * 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
447 *, and we prepend the length information. 405 *, and we prepend the length information.
448 */ 406 */
449void 407void
450Send_With_Handling (NewSocket *ns, SockList *msg) 408Send_With_Handling (client_socket *ns, packet *msg)
451{ 409{
452 unsigned char sbuf[4]; 410 unsigned char sbuf[4];
453 411
454 if (ns->status == Ns_Dead || !msg) 412 if (ns->status == Ns_Dead || !msg)
455 return; 413 return;
456 414
457 if (msg->len >= MAXSOCKBUF) 415 if (msg->length () >= MAXSOCKBUF)
458 { 416 {
459 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 ());
460 /* 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
461 * it easier to debug. 419 * it easier to debug.
462 */ 420 */
463 abort (); 421 abort ();
464 } 422 }
423
465 sbuf[0] = ((uint32) (msg->len) >> 8) & 0xFF; 424 sbuf[0] = ((uint32) (msg->length ()) >> 8);
466 sbuf[1] = ((uint32) (msg->len)) & 0xFF; 425 sbuf[1] = ((uint32) (msg->length ()) );
467 if (ns->status != Ns_Old)
468 Write_To_Socket (ns, (char *) sbuf, 2);
469 Write_To_Socket (ns, (char *) msg->buf, msg->len);
470}
471 426
472/** 427 //TODO: single write, or writev
473 * Takes a string of data, and writes it out to the socket. A very handy 428 ns->send (sbuf, 2);
474 * shortcut function. 429 ns->send (msg->buf, msg->length ());
475 */
476void
477Write_String_To_Socket (NewSocket *ns, char *buf, int len)
478{
479 SockList sl;
480
481 sl.len = len;
482 sl.buf = (unsigned char *) buf;
483 Send_With_Handling (ns, &sl);
484} 430}
485
486 431
487/****************************************************************************** 432/******************************************************************************
488 * 433 *
489 * statistics logging functions. 434 * statistics logging functions.
490 * 435 *
513 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",
514 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,
515 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);
516 cst_lst.ibytes = 0; 461 cst_lst.ibytes = 0;
517 cst_lst.obytes = 0; 462 cst_lst.obytes = 0;
518 cst_lst.max_conn = socket_info.nconns;
519 cst_lst.time_start = now; 463 cst_lst.time_start = now;
520} 464}
521#endif 465#endif
522 466

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines