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.22 by root, Fri Dec 15 19:59:20 2006 UTC vs.
Revision 1.29 by root, Thu Dec 21 23:37:06 2006 UTC

40 40
41#ifdef __linux__ 41#ifdef __linux__
42# include <sys/types.h> 42# include <sys/types.h>
43# include <sys/socket.h> 43# include <sys/socket.h>
44# include <netinet/in.h> 44# include <netinet/in.h>
45# define TCP_HZ 1000 // sorry...
46# include <netinet/tcp.h> 45# include <netinet/tcp.h>
47#endif 46#endif
48 47
49// 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
50// easily die in 20 seconds... 49// easily die in 20 seconds...
51#define SOCKET_TIMEOUT1 10 50#define SOCKET_TIMEOUT1 10 * 1000
52#define SOCKET_TIMEOUT2 20 51#define SOCKET_TIMEOUT2 20 * 1000
53 52
54void 53void
55client::flush () 54client::flush ()
56{ 55{
56 if (destroyed ())
57 return;
58
57#ifdef __linux__ 59#ifdef __linux__
58 // check time of last ack, and, if too old, kill connection 60 // check time of last ack, and, if too old, kill connection
59 struct tcp_info tcpi; 61 struct tcp_info tcpi;
60 socklen_t len = sizeof (tcpi); 62 socklen_t len = sizeof (tcpi);
61 63
64 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;
65 67
66 rtt = tcpi.tcpi_rtt; 68 rtt = tcpi.tcpi_rtt;
67 rttvar = tcpi.tcpi_rttvar; 69 rttvar = tcpi.tcpi_rttvar;
68 70
69 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
70 && 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
71 { 73 {
72 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,
73 (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);
74 status = Ns_Dead; 76 destroy ();
75 } 77 }
76 } 78 }
77#endif 79#endif
78 80
79 /** 81 /**
111#endif 113#endif
112 } 114 }
113 else if (res == 0) 115 else if (res == 0)
114 { 116 {
115 LOG (llevError, "socket write failed, connection closed.\n"); 117 LOG (llevError, "socket write failed, connection closed.\n");
116 status = Ns_Dead; 118 destroy ();
117 return; 119 return;
118 } 120 }
119 else if (errno == EINTR) 121 else if (errno == EINTR)
120 { 122 {
121 // just retry 123 // just retry
128 return; 130 return;
129 } 131 }
130 else 132 else
131 { 133 {
132 LOG (llevError, "socket write failed: %s\n", strerror (errno)); 134 LOG (llevError, "socket write failed: %s\n", strerror (errno));
133 status = Ns_Dead; 135 destroy ();
134 return; 136 return;
135 } 137 }
136 } 138 }
137 139
138 socket_ev.poll (socket_ev.poll () & ~PE_W); 140 socket_ev.poll (socket_ev.poll () & ~PE_W);
139} 141}
140 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
141/*********************************************************************** 177/*****************************************************************************
142 * 178 * Start of command dispatch area.
143 * packet functions/utilities 179 * The commands here are protocol commands.
144 *
145 **********************************************************************/ 180 ****************************************************************************/
146 181
147packet &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
148{ 219{
149 if (room () < v.len) 220 return (!(pkt->flags & PF_PLAYER) || pl)
150 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 }
151 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;
152 { 252 }
153 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))
154 { 277 {
155 memcpy (cur, v.ptr, v.len); 278 if (pkt->flags & PF_PLAYER)
156 cur += v.len; 279 queue_command (pkt, data, datalen);
280 else
281 execute (pkt, data, datalen);
282
283 goto next_packet;
157 } 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 ())
158 } 303 {
159 304 socket_ev.poll (0);
160 return *this; 305 return;
161}
162
163packet &packet::operator <<(const data8 &v)
164{
165 unsigned int len = min (v.len, 0x00FF);
166 return *this << uint8 (len) << data (v.ptr, len);
167}
168
169packet &packet::operator <<(const data16 &v)
170{
171 unsigned int len = min (v.len, 0xFFFF);
172 return *this << uint16 (len) << data (v.ptr, len);
173}
174
175packet &packet::operator <<(const char *v)
176{
177 return *this << data (v, strlen (v ? v : 0));
178}
179
180void
181packet::printf (const char *format, ...)
182{
183 int size = room ();
184
185 va_list ap;
186 va_start (ap, format);
187 int len = vsnprintf ((char *)cur, size, format, ap);
188 va_end (ap);
189
190 if (len >= size)
191 return reset ();
192
193 cur += len;
194}
195
196/******************************************************************************
197 *
198 * Start of read routines.
199 *
200 ******************************************************************************/
201
202int
203client::read_packet ()
204{
205 for (;;)
206 { 306 }
207 if (inbuf_len >= 2)
208 {
209 unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1];
210 307
211 if (inbuf_len >= 2 + pkt_len) 308 if (got & PE_W)
212 return pkt_len + 2; 309 {
213 } 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
214 319
215 int amount = sizeof (inbuf) - inbuf_len; 320 int amount = sizeof (inbuf) - inbuf_len;
216 321
217 if (amount <= 0) 322 if (!amount)
218 { 323 {
219 LOG (llevError, "packet too large");//TODO 324 // input buffer full
325 socket_ev.poll (socket_ev.poll () & ~PE_R);
220 return -1; 326 return;
221 } 327 }
222 328
223 amount = read (fd, inbuf + inbuf_len, amount); 329 amount = read (fd, inbuf + inbuf_len, amount);
224 330
225 if (!amount) 331 if (!amount)
226 { 332 {
227 status = Ns_Dead; 333 destroy ();
228 return -1; 334 return;
229 } 335 }
230 else if (amount < 0) 336 else if (amount < 0)
231 { 337 {
232 if (errno != EAGAIN && errno != EINTR) 338 if (errno != EAGAIN && errno != EINTR)
233 { 339 {
234 LOG (llevError, "read error: %s\n", strerror (errno)); 340 LOG (llevError, "read error: %s\n", strerror (errno));
341 destroy ();
235 return -1; 342 return;
236 } 343 }
237 344
238 return 0; 345 // should not be here, normally
346 }
347 else
239 } 348 {
240
241 inbuf_len += amount; 349 inbuf_len += amount;
242 350
243 cst_tot.ibytes += amount; 351 cst_tot.ibytes += amount;
244 cst_lst.ibytes += amount; 352 cst_lst.ibytes += amount;
245 }
246}
247 353
354 cmd_ev.start ();
355 }
356 }
357}
358
359// called whenever we have additional commands to process
248void 360void
249client::skip_packet (int len) 361client::cmd_cb (iw &w)
250{ 362{
251 inbuf_len -= len; 363 if (handle_packet ())
252 memmove (inbuf, inbuf + len, inbuf_len); 364 w.start ();
365 else
366 flush ();
253} 367}
254 368
255/******************************************************************************* 369/*******************************************************************************
256 * 370 *
257 * Start of write related routines. 371 * Start of write related routines.
269{ 383{
270 char *buf = (char *)buf_; 384 char *buf = (char *)buf_;
271 char *pos = buf; 385 char *pos = buf;
272 int amt = 0; 386 int amt = 0;
273 387
274 if (status == Ns_Dead || !buf) 388 if (destroyed () || !buf)
275 {
276 LOG (llevDebug, "Write_To_Socket called with dead socket\n");
277 return; 389 return;
278 }
279 390
280 if ((len + outputbuffer.len) > SOCKETBUFSIZE) 391 if ((len + outputbuffer.len) > SOCKETBUFSIZE)
281 { 392 {
282 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);
283 status = Ns_Dead; 394 destroy ();
284 return; 395 return;
285 } 396 }
286 397
287 int avail, end; 398 int avail, end;
288 399
306 } 417 }
307 418
308 outputbuffer.len += len; 419 outputbuffer.len += len;
309} 420}
310 421
311void
312client::socket_cb (iow &w, int got)
313{
314 write_outputbuffer ();
315
316 if (!outputbuffer.len)
317 socket_ev.poll (socket_ev.poll () & ~PE_W);
318}
319
320/** 422/**
321 * 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
322 * shortcut function. 424 * shortcut function.
323 */ 425 */
324void 426void
325client::send_packet (packet &sl) 427client::send_packet (packet &sl)
326{ 428{
327 if (status == Ns_Dead) 429 if (destroyed ())
328 return; 430 return;
329 431
330 if (sl.length () >= MAXSOCKBUF) 432 if (sl.length () >= MAXSOCKBUF)
331 { 433 {
332 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 ());
358 460
359void 461void
360client::send_packet (const char *buf) 462client::send_packet (const char *buf)
361{ 463{
362 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;
363} 539}
364 540
365/****************************************************************************** 541/******************************************************************************
366 * 542 *
367 * statistics logging functions. 543 * statistics logging functions.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines