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.21 by root, Fri Dec 15 00:14:13 2006 UTC vs.
Revision 1.29 by root, Thu Dec 21 23:37:06 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>
44# include <sys/socket.h> 43# include <sys/socket.h>
45# include <netinet/in.h> 44# include <netinet/in.h>
46# define TCP_HZ 1000 // sorry...
47# include <netinet/tcp.h> 45# include <netinet/tcp.h>
48#endif 46#endif
49 47
50// use a really low timeout, as it doesn't cost any bandwidth, and you can 48// use a really low timeout, as it doesn't cost any bandwidth, and you can
51// easily die in 20 seconds... 49// easily die in 20 seconds...
52#define SOCKET_TIMEOUT1 10 50#define SOCKET_TIMEOUT1 10 * 1000
53#define SOCKET_TIMEOUT2 20 51#define SOCKET_TIMEOUT2 20 * 1000
54 52
55void 53void
56client_socket::flush () 54client::flush ()
57{ 55{
56 if (destroyed ())
57 return;
58
58#ifdef __linux__ 59#ifdef __linux__
59 // check time of last ack, and, if too old, kill connection 60 // check time of last ack, and, if too old, kill connection
60 struct tcp_info tcpi; 61 struct tcp_info tcpi;
61 socklen_t len = sizeof (tcpi); 62 socklen_t len = sizeof (tcpi);
62 63
65 unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent; 66 unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent;
66 67
67 rtt = tcpi.tcpi_rtt; 68 rtt = tcpi.tcpi_rtt;
68 rttvar = tcpi.tcpi_rttvar; 69 rttvar = tcpi.tcpi_rttvar;
69 70
70 if (tcpi.tcpi_unacked && SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s 71 if (tcpi.tcpi_unacked && SOCKET_TIMEOUT1 < diff && diff < 0x80000000UL // ack delayed for 20s
71 && SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s 72 && SOCKET_TIMEOUT2 < tcpi.tcpi_last_data_sent) // no data sent for 10s
72 { 73 {
73 LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, 74 LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd,
74 (unsigned) tcpi.tcpi_last_ack_recv, (unsigned) tcpi.tcpi_last_data_sent, (unsigned) tcpi.tcpi_unacked); 75 (unsigned) tcpi.tcpi_last_ack_recv, (unsigned) tcpi.tcpi_last_data_sent, (unsigned) tcpi.tcpi_unacked);
75 status = Ns_Dead; 76 destroy ();
76 } 77 }
77 } 78 }
78#endif 79#endif
79 80
80 /** 81 /**
89 90
90 write_outputbuffer (); 91 write_outputbuffer ();
91} 92}
92 93
93void 94void
94client_socket::write_outputbuffer () 95client::write_outputbuffer ()
95{ 96{
96 while (outputbuffer.len) 97 while (outputbuffer.len)
97 { 98 {
98 int res = write (fd, outputbuffer.data + outputbuffer.start, 99 int res = write (fd, outputbuffer.data + outputbuffer.start,
99 min (outputbuffer.len, SOCKETBUFSIZE - outputbuffer.start)); 100 min (outputbuffer.len, SOCKETBUFSIZE - outputbuffer.start));
112#endif 113#endif
113 } 114 }
114 else if (res == 0) 115 else if (res == 0)
115 { 116 {
116 LOG (llevError, "socket write failed, connection closed.\n"); 117 LOG (llevError, "socket write failed, connection closed.\n");
117 status = Ns_Dead; 118 destroy ();
118 return; 119 return;
119 } 120 }
120 else if (errno == EINTR) 121 else if (errno == EINTR)
121 { 122 {
122 // just retry 123 // just retry
129 return; 130 return;
130 } 131 }
131 else 132 else
132 { 133 {
133 LOG (llevError, "socket write failed: %s\n", strerror (errno)); 134 LOG (llevError, "socket write failed: %s\n", strerror (errno));
134 status = Ns_Dead; 135 destroy ();
135 return; 136 return;
136 } 137 }
137 } 138 }
138 139
139 socket_ev.poll (socket_ev.poll () & ~PE_W); 140 socket_ev.poll (socket_ev.poll () & ~PE_W);
140} 141}
141 142
143/******************************************************************************
144 *
145 * Start of read routines.
146 *
147 ******************************************************************************/
148
149int
150client::next_packet ()
151{
152 if (inbuf_len >= 2)
153 {
154 int pkt_len = (inbuf [0] << 8) | inbuf [1];
155
156 if (inbuf_len >= 2 + pkt_len)
157 return 2 + pkt_len;
158
159 if (inbuf_len == sizeof (inbuf))
160 {
161 send_packet_printf ("drawinfo %d input buffer overflow - closing connection.", NDI_RED);
162 destroy ();
163 return -1;
164 }
165 }
166
167 return 0;
168}
169
170void
171client::skip_packet (int len)
172{
173 inbuf_len -= len;
174 memmove (inbuf, inbuf + len, inbuf_len);
175}
176
142/*********************************************************************** 177/*****************************************************************************
143 * 178 * Start of command dispatch area.
144 * packet functions/utilities 179 * The commands here are protocol commands.
145 *
146 **********************************************************************/ 180 ****************************************************************************/
147 181
148packet &packet::operator <<(const data &v) 182// SocketCommand, PlayingCommand, should not exist with those ugly casts
183#define SC(cb) (void *)static_cast<void (*)(char *, int, client *)>(cb),
184#define PC(cb) (void *)static_cast<void (*)(char *, int, player *)>(cb), PF_PLAYER |
185
186/**
187 * Dispatch table for the server.
188 */
189static struct packet_type packets[] = {
190 {"ncom", PC(NewPlayerCmd) PF_PLAYING },
191 {"command", PC(PlayerCmd) PF_PLAYING },
192
193 {"examine", PC(ExamineCmd) PF_PLAYING },
194 {"apply", PC(ApplyCmd) PF_PLAYING },
195 {"lookat", PC(LookAt) PF_PLAYING },
196 {"lock", PC(LockItem) PF_PLAYING },
197 {"mark", PC(MarkItem) PF_PLAYING },
198 {"move", PC(MoveCmd) PF_PLAYING },
199 {"ext", PC(ExtCmd) 0 }, /* CF+ */
200 {"mapredraw", PC(MapRedrawCmd) 0 }, /* Added: phil */
201 {"mapinfo", PC(MapInfoCmd) 0 }, /* CF+ */
202
203 {"reply", SC(ReplyCmd) 0 },
204 {"exti", SC(ExtiCmd) 0 }, /* CF+ */
205 {"addme", SC(AddMeCmd) 0 },
206 {"askface", SC(SendFaceCmd) 0 }, /* Added: phil */
207 {"requestinfo", SC(RequestInfo) 0 },
208 {"setfacemode", SC(SetFaceMode) 0 },
209 {"setsound", SC(SetSound) 0 },
210 {"setup", SC(SetUp) 0 },
211 {"version", SC(VersionCmd) 0 },
212 {"toggleextendedinfos", SC(ToggleExtendedInfos) 0 }, /*Added: tchize */
213 {"toggleextendedtext", SC(ToggleExtendedText) 0 }, /*Added: tchize */
214 {"asksmooth", SC(AskSmooth) 0 }, /*Added: tchize (smoothing technologies) */
215};
216
217bool
218client::may_execute (const packet_type *pkt) const
149{ 219{
150 if (room () < v.len) 220 return (!(pkt->flags & PF_PLAYER) || pl)
151 reset (); 221 && (!(pkt->flags & PF_PLAYING) || state == ST_PLAYING);
222}
223
224void
225client::execute (const packet_type *pkt, char *data, int datalen)
226{
227 if (may_execute (pkt))
228 {
229 //TODO: only one format
230 if (pkt->flags & PF_PLAYER)
231 ((void (*)(char *, int, player *))pkt->cb)((char *)data, datalen, pl);
232 else
233 ((void (*)(char *, int, client *))pkt->cb)((char *)data, datalen, this);
234 }
152 else 235 else
236 send_packet_printf ("drawinfo %d ERROR: you cannot execute '%s' now.", NDI_RED, pkt->name);
237}
238
239bool
240client::handle_packet ()
241{
242 int pkt_len = next_packet ();
243
244 if (!pkt_len)
245 return false;
246 else if (pkt_len < 0)
247 {
248 LOG (llevError, "read error on player %s\n",
249 pl && pl->ob ? &pl->ob->name : "[anonymous]");
250 destroy ();
251 return false;
153 { 252 }
154 if (v.len) 253
254 inbuf [pkt_len] = 0; /* Terminate buffer - useful for string data */
255
256 /* First, break out beginning word. There are at least
257 * a few commands that do not have any paremeters. If
258 * we get such a command, don't worry about trying
259 * to break it up.
260 */
261 int datalen;
262 char *data = strchr ((char *)inbuf + 2, ' ');
263
264 if (data)
265 {
266 *data++ = 0;
267 datalen = pkt_len - (data - (char *)inbuf);
268 }
269 else
270 {
271 data = (char *)inbuf + 2; // better read garbage than segfault
272 datalen = 0;
273 }
274
275 for (packet_type *pkt = packets; pkt < packets + (sizeof (packets) / sizeof (packets[0])); ++pkt)
276 if (!strcmp ((char *)inbuf + 2, pkt->name))
155 { 277 {
156 memcpy (cur, v.ptr, v.len); 278 if (pkt->flags & PF_PLAYER)
157 cur += v.len; 279 queue_command (pkt, data, datalen);
280 else
281 execute (pkt, data, datalen);
282
283 goto next_packet;
158 } 284 }
285
286 // If we get here, we didn't find a valid command.
287 send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED, (char *)inbuf + 2);
288next_packet:
289 skip_packet (pkt_len);
290
291 // input buffer has space again
292 socket_ev.poll (socket_ev.poll () | PE_R);
293
294 return true;
295}
296
297// callback called when socket is either readable or writable
298void
299client::socket_cb (iow &w, int got)
300{
301 //TODO remove when we have better socket cleanup logic
302 if (destroyed ())
159 } 303 {
160 304 socket_ev.poll (0);
161 return *this; 305 return;
162}
163
164packet &packet::operator <<(const data8 &v)
165{
166 unsigned int len = min (v.len, 0x00FF);
167 return *this << uint8 (len) << data (v.ptr, len);
168}
169
170packet &packet::operator <<(const data16 &v)
171{
172 unsigned int len = min (v.len, 0xFFFF);
173 return *this << uint16 (len) << data (v.ptr, len);
174}
175
176packet &packet::operator <<(const char *v)
177{
178 return *this << data (v, strlen (v ? v : 0));
179}
180
181void
182packet::printf (const char *format, ...)
183{
184 int size = room ();
185
186 va_list ap;
187 va_start (ap, format);
188 int len = vsnprintf ((char *)cur, size, format, ap);
189 va_end (ap);
190
191 if (len >= size)
192 return reset ();
193
194 cur += len;
195}
196
197/******************************************************************************
198 *
199 * Start of read routines.
200 *
201 ******************************************************************************/
202
203int
204client_socket::read_packet ()
205{
206 for (;;)
207 { 306 }
208 if (inbuf_len >= 2)
209 {
210 unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1];
211 307
212 if (inbuf_len >= 2 + pkt_len) 308 if (got & PE_W)
213 return pkt_len + 2; 309 {
214 } 310 write_outputbuffer ();
311
312 if (!outputbuffer.len)
313 socket_ev.poll (socket_ev.poll () & ~PE_W);
314 }
315
316 if (got & PE_R)
317 {
318 //TODO: rate-limit tcp connection in better ways, important
215 319
216 int amount = sizeof (inbuf) - inbuf_len; 320 int amount = sizeof (inbuf) - inbuf_len;
217 321
218 if (amount <= 0) 322 if (!amount)
219 { 323 {
220 LOG (llevError, "packet too large");//TODO 324 // input buffer full
325 socket_ev.poll (socket_ev.poll () & ~PE_R);
221 return -1; 326 return;
222 } 327 }
223 328
224 amount = read (fd, inbuf + inbuf_len, amount); 329 amount = read (fd, inbuf + inbuf_len, amount);
225 330
226 if (!amount) 331 if (!amount)
227 { 332 {
228 status = Ns_Dead; 333 destroy ();
229 return -1; 334 return;
230 } 335 }
231 else if (amount < 0) 336 else if (amount < 0)
232 { 337 {
233 if (errno != EAGAIN && errno != EINTR) 338 if (errno != EAGAIN && errno != EINTR)
234 { 339 {
235 LOG (llevError, "read error: %s\n", strerror (errno)); 340 LOG (llevError, "read error: %s\n", strerror (errno));
341 destroy ();
236 return -1; 342 return;
237 } 343 }
238 344
239 return 0; 345 // should not be here, normally
346 }
347 else
240 } 348 {
241
242 inbuf_len += amount; 349 inbuf_len += amount;
243 350
244 cst_tot.ibytes += amount; 351 cst_tot.ibytes += amount;
245 cst_lst.ibytes += amount; 352 cst_lst.ibytes += amount;
246 }
247}
248 353
354 cmd_ev.start ();
355 }
356 }
357}
358
359// called whenever we have additional commands to process
249void 360void
250client_socket::skip_packet (int len) 361client::cmd_cb (iw &w)
251{ 362{
252 inbuf_len -= len; 363 if (handle_packet ())
253 memmove (inbuf, inbuf + len, inbuf_len); 364 w.start ();
365 else
366 flush ();
254} 367}
255 368
256/******************************************************************************* 369/*******************************************************************************
257 * 370 *
258 * Start of write related routines. 371 * Start of write related routines.
264 * 377 *
265 * ns is the socket we are adding the data to, buf is the start of the 378 * ns is the socket we are adding the data to, buf is the start of the
266 * data, and len is the number of bytes to add. 379 * data, and len is the number of bytes to add.
267 */ 380 */
268void 381void
269client_socket::send (void *buf_, int len) 382client::send (void *buf_, int len)
270{ 383{
271 char *buf = (char *)buf_; 384 char *buf = (char *)buf_;
272 char *pos = buf; 385 char *pos = buf;
273 int amt = 0; 386 int amt = 0;
274 387
275 if (status == Ns_Dead || !buf) 388 if (destroyed () || !buf)
276 {
277 LOG (llevDebug, "Write_To_Socket called with dead socket\n");
278 return; 389 return;
279 }
280 390
281 if ((len + outputbuffer.len) > SOCKETBUFSIZE) 391 if ((len + outputbuffer.len) > SOCKETBUFSIZE)
282 { 392 {
283 LOG (llevDebug, "Socket on fd %d has overrun internal buffer - marking as dead\n", fd); 393 LOG (llevDebug, "socket on fd %d has overrun internal buffer - marking as dead\n", fd);
284 status = Ns_Dead; 394 destroy ();
285 return; 395 return;
286 } 396 }
287 397
288 int avail, end; 398 int avail, end;
289 399
307 } 417 }
308 418
309 outputbuffer.len += len; 419 outputbuffer.len += len;
310} 420}
311 421
312void
313client_socket::socket_cb (iow &w, int got)
314{
315 write_outputbuffer ();
316
317 if (!outputbuffer.len)
318 socket_ev.poll (socket_ev.poll () & ~PE_W);
319}
320
321/** 422/**
322 * Takes a string of data, and writes it out to the socket. A very handy 423 * Takes a string of data, and writes it out to the socket. A very handy
323 * shortcut function. 424 * shortcut function.
324 */ 425 */
325void 426void
326client_socket::send_packet (packet &sl) 427client::send_packet (packet &sl)
327{ 428{
328 if (status == Ns_Dead) 429 if (destroyed ())
329 return; 430 return;
330 431
331 if (sl.length () >= MAXSOCKBUF) 432 if (sl.length () >= MAXSOCKBUF)
332 { 433 {
333 LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ()); 434 LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ());
347 448
348 send (sl.buf_, sl.length () + sl.hdrlen); 449 send (sl.buf_, sl.length () + sl.hdrlen);
349} 450}
350 451
351void 452void
352client_socket::send_packet (const char *buf, int len) 453client::send_packet (const char *buf, int len)
353{ 454{
354 packet sl; 455 packet sl;
355 456
356 sl << data (buf, len); 457 sl << data (buf, len);
357 send_packet (sl); 458 send_packet (sl);
358} 459}
359 460
360void 461void
361client_socket::send_packet (const char *buf) 462client::send_packet (const char *buf)
362{ 463{
363 send_packet (buf, strlen (buf)); 464 send_packet (buf, strlen (buf));
465}
466
467void
468client::send_packet_printf (const char *format, ...)
469{
470 packet sl;
471
472 va_list ap;
473 va_start (ap, format);
474 sl.vprintf (format, ap);
475 va_end (ap);
476
477 send_packet (sl);
478}
479
480/***********************************************************************
481 *
482 * packet functions/utilities
483 *
484 **********************************************************************/
485
486packet::packet (const char *name)
487{
488 reset ();
489
490 int len = strlen (name);
491 memcpy (cur, name, len); cur += len;
492 *cur++ = ' ';
493}
494
495packet &packet::operator <<(const data &v)
496{
497 if (room () < v.len)
498 reset ();
499 else
500 {
501 if (v.len)
502 {
503 memcpy (cur, v.ptr, v.len);
504 cur += v.len;
505 }
506 }
507
508 return *this;
509}
510
511packet &packet::operator <<(const data8 &v)
512{
513 unsigned int len = min (v.len, 0x00FF);
514 return *this << uint8 (len) << data (v.ptr, len);
515}
516
517packet &packet::operator <<(const data16 &v)
518{
519 unsigned int len = min (v.len, 0xFFFF);
520 return *this << uint16 (len) << data (v.ptr, len);
521}
522
523packet &packet::operator <<(const char *v)
524{
525 return *this << data (v, strlen (v ? v : 0));
526}
527
528void
529packet::vprintf (const char *format, va_list ap)
530{
531 int size = room ();
532
533 int len = vsnprintf ((char *)cur, size, format, ap);
534
535 if (len >= size)
536 return reset ();
537
538 cur += len;
364} 539}
365 540
366/****************************************************************************** 541/******************************************************************************
367 * 542 *
368 * statistics logging functions. 543 * statistics logging functions.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines