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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines