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.20 by root, Thu Dec 14 21:46:34 2006 UTC vs.
Revision 1.22 by root, Fri Dec 15 19:59:20 2006 UTC

33 */ 33 */
34 34
35using namespace std; 35using namespace std;
36 36
37#include <global.h> 37#include <global.h>
38#include <newclient.h>
39#include <sproto.h> 38#include <sproto.h>
40#include <cstdarg> 39#include <cstdarg>
41 40
42#ifdef __linux__ 41#ifdef __linux__
43# include <sys/types.h> 42# include <sys/types.h>
51// easily die in 20 seconds... 50// easily die in 20 seconds...
52#define SOCKET_TIMEOUT1 10 51#define SOCKET_TIMEOUT1 10
53#define SOCKET_TIMEOUT2 20 52#define SOCKET_TIMEOUT2 20
54 53
55void 54void
56client_socket::flush () 55client::flush ()
57{ 56{
58#ifdef __linux__ 57#ifdef __linux__
59 // check time of last ack, and, if too old, kill connection 58 // check time of last ack, and, if too old, kill connection
60 struct tcp_info tcpi; 59 struct tcp_info tcpi;
61 socklen_t len = sizeof (tcpi); 60 socklen_t len = sizeof (tcpi);
62 61
63 if (!getsockopt (fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi)) 62 if (!getsockopt (fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi))
64 { 63 {
65 unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent; 64 unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent;
65
66 rtt = tcpi.tcpi_rtt;
67 rttvar = tcpi.tcpi_rttvar;
66 68
67 if (tcpi.tcpi_unacked && SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s 69 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 70 && SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s
69 { 71 {
70 LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, 72 LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd,
86 88
87 write_outputbuffer (); 89 write_outputbuffer ();
88} 90}
89 91
90void 92void
91client_socket::write_outputbuffer () 93client::write_outputbuffer ()
92{ 94{
93 while (outputbuffer.len) 95 while (outputbuffer.len)
94 { 96 {
95 int res = write (fd, outputbuffer.data + outputbuffer.start, 97 int res = write (fd, outputbuffer.data + outputbuffer.start,
96 min (outputbuffer.len, SOCKETBUFSIZE - outputbuffer.start)); 98 min (outputbuffer.len, SOCKETBUFSIZE - outputbuffer.start));
196 * Start of read routines. 198 * Start of read routines.
197 * 199 *
198 ******************************************************************************/ 200 ******************************************************************************/
199 201
200int 202int
201client_socket::read_packet () 203client::read_packet ()
202{ 204{
203 for (;;) 205 for (;;)
204 { 206 {
205 if (inbuf_len >= 2) 207 if (inbuf_len >= 2)
206 { 208 {
242 cst_lst.ibytes += amount; 244 cst_lst.ibytes += amount;
243 } 245 }
244} 246}
245 247
246void 248void
247client_socket::skip_packet (int len) 249client::skip_packet (int len)
248{ 250{
249 inbuf_len -= len; 251 inbuf_len -= len;
250 memmove (inbuf, inbuf + len, inbuf_len); 252 memmove (inbuf, inbuf + len, inbuf_len);
251} 253}
252 254
261 * 263 *
262 * ns is the socket we are adding the data to, buf is the start of the 264 * ns is the socket we are adding the data to, buf is the start of the
263 * data, and len is the number of bytes to add. 265 * data, and len is the number of bytes to add.
264 */ 266 */
265void 267void
266client_socket::send (void *buf_, int len) 268client::send (void *buf_, int len)
267{ 269{
268 char *buf = (char *)buf_; 270 char *buf = (char *)buf_;
269 char *pos = buf; 271 char *pos = buf;
270 int amt = 0; 272 int amt = 0;
271 273
305 307
306 outputbuffer.len += len; 308 outputbuffer.len += len;
307} 309}
308 310
309void 311void
310client_socket::socket_cb (iow &w, int got) 312client::socket_cb (iow &w, int got)
311{ 313{
312 write_outputbuffer (); 314 write_outputbuffer ();
313 315
314 if (!outputbuffer.len) 316 if (!outputbuffer.len)
315 socket_ev.poll (socket_ev.poll () & ~PE_W); 317 socket_ev.poll (socket_ev.poll () & ~PE_W);
318/** 320/**
319 * Takes a string of data, and writes it out to the socket. A very handy 321 * Takes a string of data, and writes it out to the socket. A very handy
320 * shortcut function. 322 * shortcut function.
321 */ 323 */
322void 324void
323client_socket::send_packet (packet &sl) 325client::send_packet (packet &sl)
324{ 326{
325 if (status == Ns_Dead) 327 if (status == Ns_Dead)
326 return; 328 return;
327 329
328 if (sl.length () >= MAXSOCKBUF) 330 if (sl.length () >= MAXSOCKBUF)
344 346
345 send (sl.buf_, sl.length () + sl.hdrlen); 347 send (sl.buf_, sl.length () + sl.hdrlen);
346} 348}
347 349
348void 350void
349client_socket::send_packet (const char *buf, int len) 351client::send_packet (const char *buf, int len)
350{ 352{
351 packet sl; 353 packet sl;
352 354
353 sl << data (buf, len); 355 sl << data (buf, len);
354 send_packet (sl); 356 send_packet (sl);
355} 357}
356 358
357void 359void
358client_socket::send_packet (const char *buf) 360client::send_packet (const char *buf)
359{ 361{
360 send_packet (buf, strlen (buf)); 362 send_packet (buf, strlen (buf));
361} 363}
362 364
363/****************************************************************************** 365/******************************************************************************

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines