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.14 by root, Thu Dec 14 01:21:58 2006 UTC vs.
Revision 1.19 by root, Thu Dec 14 20:39:54 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);
86 * 86 *
87 **********************************************************************/ 87 **********************************************************************/
88 88
89packet &packet::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
115} 120}
116 121
117void 122void
118packet::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 129 int len = vsnprintf ((char *)cur, size, format, ap);
123 len += vsnprintf ((char *)buf + len, MAXSOCKBUF, format, ap);
124
125 va_end (ap); 130 va_end (ap);
131
132 if (len >= size)
133 return reset ();
134
135 cur += len;
126} 136}
127 137
128/****************************************************************************** 138/******************************************************************************
129 * 139 *
130 * Start of read routines. 140 * Start of read routines.
131 * 141 *
132 ******************************************************************************/ 142 ******************************************************************************/
133 143
134int 144int
135NewSocket::read_packet () 145client_socket::read_packet ()
136{ 146{
137 for (;;) 147 for (;;)
138 { 148 {
139 if (inbuf_len >= 2) 149 if (inbuf_len >= 2)
140 { 150 {
176 cst_lst.ibytes += amount; 186 cst_lst.ibytes += amount;
177 } 187 }
178} 188}
179 189
180void 190void
181NewSocket::skip_packet (int len) 191client_socket::skip_packet (int len)
182{ 192{
183 inbuf_len -= len; 193 inbuf_len -= len;
184 memmove (inbuf, inbuf + len, inbuf_len); 194 memmove (inbuf, inbuf + len, inbuf_len);
185} 195}
186 196
196 * 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
197 * data, and len is the number of bytes to add. 207 * data, and len is the number of bytes to add.
198 */ 208 */
199 209
200static void 210static void
201add_to_buffer (NewSocket *ns, char *buf, int len) 211add_to_buffer (client_socket *ns, char *buf, int len)
202{ 212{
203 int avail, end; 213 int avail, end;
204 214
205 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) 215 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE)
206 { 216 {
240 * 250 *
241 * 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
242 * is called to write it out. 252 * is called to write it out.
243 */ 253 */
244void 254void
245write_socket_buffer (NewSocket * ns) 255write_socket_buffer (client_socket * ns)
246{ 256{
247 int amt, max; 257 int amt, max;
248 258
249 if (ns->outputbuffer.len == 0) 259 if (ns->outputbuffer.len == 0)
250 { 260 {
299 * 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
300 * of bytes to write. IT doesn't return anything - rather, it 310 * of bytes to write. IT doesn't return anything - rather, it
301 * updates the ns structure if we get an error. 311 * updates the ns structure if we get an error.
302 */ 312 */
303void 313void
304Write_To_Socket (NewSocket * ns, char *buf, int len) 314client_socket::send (void *buf_, int len)
305{ 315{
316 char *buf = (char *)buf_;
317 char *pos = buf;
306 int amt = 0; 318 int amt = 0;
307 char *pos = buf;
308 319
309 if (ns->status == Ns_Dead || !buf) 320 if (status == Ns_Dead || !buf)
310 { 321 {
311 LOG (llevDebug, "Write_To_Socket called with dead socket\n"); 322 LOG (llevDebug, "Write_To_Socket called with dead socket\n");
312 return; 323 return;
313 } 324 }
314 325
315#ifndef __GNU__ /* This caused problems on Hurd */ 326#ifndef __GNU__ /* This caused problems on Hurd */
316 if (!ns->can_write) 327 if (!can_write)
317 { 328 {
318 add_to_buffer (ns, buf, len); 329 add_to_buffer (this, buf, len);
319 return; 330 return;
320 } 331 }
321#endif 332#endif
333
322 /* 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 */
323 while (len > 0) 335 while (len > 0)
324 { 336 {
325
326 do 337 do
327 { 338 {
328 amt = write (ns->fd, pos, len); 339 amt = write (fd, pos, len);
329 } 340 }
330 while ((amt < 0) && (errno == EINTR)); 341 while ((amt < 0) && (errno == EINTR));
331 342
332 if (amt < 0) 343 if (amt < 0)
333 { /* We got an error */ 344 { /* We got an error */
334 if (errno != EWOULDBLOCK) 345 if (errno != EWOULDBLOCK)
335 { 346 {
336 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ 347 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */
337 errno, strerror (errno)); 348 errno, strerror (errno));
338 ns->status = Ns_Dead; 349 status = Ns_Dead;
339 return; 350 return;
340 } 351 }
341 else 352 else
342 { /* EWOULDBLOCK */ 353 { /* EWOULDBLOCK */
343 /* can't write it, so store it away. */ 354 /* can't write it, so store it away. */
344 add_to_buffer (ns, pos, len); 355 add_to_buffer (this, pos, len);
345 ns->can_write = 0; 356 can_write = 0;
346 return; 357 return;
347 } 358 }
348 } 359 }
349 /* 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
350 * an else if to make sure we don't reprocess it. 361 * an else if to make sure we don't reprocess it.
351 */ 362 */
352 else if (amt == 0) 363 else if (amt == 0)
353 {
354 LOG (llevError, "Write_To_Socket: No data written out.\n"); 364 LOG (llevError, "Write_To_Socket: No data written out.\n");
355 } 365
356 len -= amt; 366 len -= amt;
357 pos += amt; 367 pos += amt;
358#ifdef CS_LOGSTATS 368#ifdef CS_LOGSTATS
359 cst_tot.obytes += amt; 369 cst_tot.obytes += amt;
360 cst_lst.obytes += amt; 370 cst_lst.obytes += amt;
361#endif 371#endif
362 } 372 }
363} 373}
364 374
375void
376client_socket::socket_cb (iow &w, int got)
377{
378 //printf ("iow got %x\n", got);
379 w.stop ();
380}
381
365/** 382/**
366 * Takes a string of data, and writes it out to the socket. A very handy 383 * Takes a string of data, and writes it out to the socket. A very handy
367 * shortcut function. 384 * shortcut function.
368 */ 385 */
369
370void 386void
371NewSocket::send_packet (packet &sl) 387client_socket::send_packet (packet &sl)
372{ 388{
373 Send_With_Handling (this, &sl);
374}
375
376void
377NewSocket::send_packet (const char *buf, int len)
378{
379 packet sl;
380
381 sl << data (buf, len);
382 send_packet (sl);
383}
384
385void
386NewSocket::send_packet (const char *buf)
387{
388 send_packet (buf, strlen (buf));
389}
390
391/**
392 * Calls Write_To_Socket to send data to the client.
393 *
394 * The only difference in this function is that we take a packet
395 *, and we prepend the length information.
396 */
397void
398Send_With_Handling (NewSocket *ns, packet *msg)
399{
400 unsigned char sbuf[4];
401
402 if (ns->status == Ns_Dead || !msg) 389 if (status == Ns_Dead)
403 return; 390 return;
404 391
405 if (msg->len >= MAXSOCKBUF) 392 if (sl.length () >= MAXSOCKBUF)
406 { 393 {
407 LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", msg->len); 394 LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ());
408 /* Almost certainly we've overflowed a buffer, so quite now to make 395 /* Almost certainly we've overflowed a buffer, so quit now to make
409 * it easier to debug. 396 * it easier to debug.
410 */ 397 */
411 abort (); 398 abort ();
412 } 399 }
413 sbuf[0] = ((uint32) (msg->len) >> 8) & 0xFF; 400
414 sbuf[1] = ((uint32) (msg->len)) & 0xFF; 401 if (!sl.length ())
415 if (ns->status != Ns_Old) 402 return;
416 Write_To_Socket (ns, (char *) sbuf, 2); 403
417 Write_To_Socket (ns, (char *) msg->buf, msg->len); 404 assert (sl.hdrlen == 2);
405
406 sl.buf_ [0] = sl.length () >> 8;
407 sl.buf_ [1] = sl.length () ;
408
409 send (sl.buf_, sl.length () + sl.hdrlen);
410}
411
412void
413client_socket::send_packet (const char *buf, int len)
414{
415 packet sl;
416
417 sl << data (buf, len);
418 send_packet (sl);
419}
420
421void
422client_socket::send_packet (const char *buf)
423{
424 send_packet (buf, strlen (buf));
418} 425}
419 426
420/****************************************************************************** 427/******************************************************************************
421 * 428 *
422 * statistics logging functions. 429 * statistics logging functions.
446 LOG (llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n", 453 LOG (llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n",
447 ctime (&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn, 454 ctime (&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn,
448 now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes, cst_lst.max_conn, now - cst_lst.time_start); 455 now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes, cst_lst.max_conn, now - cst_lst.time_start);
449 cst_lst.ibytes = 0; 456 cst_lst.ibytes = 0;
450 cst_lst.obytes = 0; 457 cst_lst.obytes = 0;
451 cst_lst.max_conn = socket_info.nconns;
452 cst_lst.time_start = now; 458 cst_lst.time_start = now;
453} 459}
454#endif 460#endif
455 461

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines