ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/loop.C
(Generate patch)

Comparing deliantra/server/socket/loop.C (file contents):
Revision 1.12 by root, Thu Dec 14 01:12:35 2006 UTC vs.
Revision 1.16 by root, Thu Dec 14 22:45:41 2006 UTC

41#include <sys/time.h> 41#include <sys/time.h>
42#include <sys/socket.h> 42#include <sys/socket.h>
43#include <netinet/in.h> 43#include <netinet/in.h>
44#include <netdb.h> 44#include <netdb.h>
45 45
46#ifdef HAVE_UNISTD_H
47# include <unistd.h> 46#include <unistd.h>
48#endif
49
50#ifdef HAVE_ARPA_INET_H
51# include <arpa/inet.h> 47#include <arpa/inet.h>
52#endif
53 48
54#include <loader.h> 49#include <loader.h>
55#include <newserver.h> 50#include <newserver.h>
56 51
57/***************************************************************************** 52/*****************************************************************************
65 * before player joins, and those happen after the player has joined. 60 * before player joins, and those happen after the player has joined.
66 * As such, we have function types that might be called, so 61 * As such, we have function types that might be called, so
67 * we end up having 2 tables. 62 * we end up having 2 tables.
68 */ 63 */
69 64
70typedef void (*func_uint8_int_ns) (char *, int, NewSocket *); 65typedef void (*func_uint8_int_ns) (char *, int, client_socket *);
71 66
72struct NsCmdMapping 67struct NsCmdMapping
73{ 68{
74 const char *cmdname; 69 const char *cmdname;
75 func_uint8_int_ns cmdproc; 70 func_uint8_int_ns cmdproc;
132 * RequestInfo is sort of a meta command. There is some specific 127 * RequestInfo is sort of a meta command. There is some specific
133 * request of information, but we call other functions to provide 128 * request of information, but we call other functions to provide
134 * that information. 129 * that information.
135 */ 130 */
136void 131void
137RequestInfo (char *buf, int len, NewSocket * ns) 132RequestInfo (char *buf, int len, client_socket * ns)
138{ 133{
139 char *params = NULL, *cp; 134 char *params = NULL, *cp;
140 135
141 /* No match */ 136 /* No match */
142 char bigbuf[MAX_BUF]; 137 char bigbuf[MAX_BUF];
181 * with this socket, null if no player (one of the init_sockets for just 176 * with this socket, null if no player (one of the init_sockets for just
182 * starting a connection) 177 * starting a connection)
183 */ 178 */
184 179
185void 180void
186HandleClient (NewSocket *ns, player *pl) 181HandleClient (client_socket *ns, player *pl)
187{ 182{
188 /* Loop through this - maybe we have several complete packets here. */ 183 /* Loop through this - maybe we have several complete packets here. */
189 // limit to a few commands only, though, as to not monopolise the server 184 // limit to a few commands only, though, as to not monopolise the server
190 for (int repeat = 16; repeat--;) 185 for (int repeat = 16; repeat--;)
191 { 186 {
264 259
265 return; 260 return;
266 } 261 }
267 } 262 }
268 263
269 printf ("BP34n");//D
270 /* If we get here, we didn't find a valid command. Logging 264 /* If we get here, we didn't find a valid command. Logging
271 * this might be questionable, because a broken client/malicious 265 * this might be questionable, because a broken client/malicious
272 * user could certainly send a whole bunch of invalid commands. 266 * user could certainly send a whole bunch of invalid commands.
273 */ 267 */
274 LOG (llevDebug, "Bad command from client (%s)\n", ns->inbuf + 2); 268 LOG (llevDebug, "Bad command from client (%s)\n", ns->inbuf + 2);
276 next_packet: 270 next_packet:
277 ; 271 ;
278 } 272 }
279} 273}
280 274
281
282/*****************************************************************************
283 *
284 * Low level socket looping - select calls and watchdog udp packet
285 * sending.
286 *
287 ******************************************************************************/
288
289#ifdef WATCHDOG
290
291/**
292 * Tell watchdog that we are still alive
293 *
294 * I put the function here since we should hopefully already be getting
295 * all the needed include files for socket support
296 */
297
298void
299watchdog (void)
300{
301 static int fd = -1;
302 static struct sockaddr_in insock;
303
304 if (fd == -1)
305 {
306 struct protoent *protoent;
307
308 if ((protoent = getprotobyname ("udp")) == NULL || (fd = socket (PF_INET, SOCK_DGRAM, protoent->p_proto)) == -1)
309 {
310 return;
311 }
312 insock.sin_family = AF_INET;
313 insock.sin_port = htons ((unsigned short) 13325);
314 insock.sin_addr.s_addr = inet_addr ("127.0.0.1");
315 }
316 sendto (fd, (void *) &fd, 1, 0, (struct sockaddr *) &insock, sizeof (insock));
317}
318#endif
319
320void 275void
321flush_sockets (void) 276flush_sockets (void)
322{ 277{
323 player *pl; 278 for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ++i)
324
325 for (pl = first_player; pl != NULL; pl = pl->next)
326 if (pl->socket.status != Ns_Dead) 279 if ((*i)->status != Ns_Dead)
327 Socket_Flush (&pl->socket); 280 (*i)->flush ();
328} 281}
329 282
330/** 283/**
331 * This checks the sockets for input, does the right thing. 284 * This checks the sockets for input, does the right thing.
332 * 285 *
336 */ 289 */
337void 290void
338doeric_server (void) 291doeric_server (void)
339{ 292{
340 int i, pollret; 293 int i, pollret;
341 fd_set tmp_read, tmp_write; 294 fd_set tmp_read;
342 struct sockaddr_in addr; 295 struct sockaddr_in addr;
343 socklen_t addrlen = sizeof (struct sockaddr); 296 socklen_t addrlen = sizeof (struct sockaddr);
344 player *pl, *next; 297 player *pl, *next;
298 int maxfd = 0;
345 299
346#ifdef CS_LOGSTATS 300#ifdef CS_LOGSTATS
347 if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME) 301 if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME)
348 write_cs_stats (); 302 write_cs_stats ();
349#endif 303#endif
350 304
351 FD_ZERO (&tmp_read);
352 FD_ZERO (&tmp_write);
353
354 for (i = 0; i < socket_info.allocated_sockets; i++)
355 {
356 if (init_sockets[i].status == Ns_Dead)
357 {
358 free_newsocket (&init_sockets[i]);
359 init_sockets[i].status = Ns_Avail;
360 socket_info.nconns--;
361 }
362 else if (init_sockets[i].status != Ns_Avail)
363 {
364 FD_SET (init_sockets[i].fd, &tmp_read);
365 FD_SET (init_sockets[i].fd, &tmp_write);
366 }
367 }
368
369 /* Go through the players. Let the loop set the next pl value, 305 /* Go through the players. Let the loop set the next pl value,
370 * since we may remove some 306 * since we may remove some
371 */ 307 */
308 //TODO: must be handled cleanly elsewhere
372 for (pl = first_player; pl != NULL;) 309 for (pl = first_player; pl; )
373 { 310 {
311 player *npl = pl->next;
312
313 //TODO: must be handled cleanly elsewhere
374 if (pl->socket.status == Ns_Dead) 314 if (pl->socket->status == Ns_Dead)
375 { 315 {
376 player *npl = pl->next;
377
378 save_player (pl->ob, 0); 316 save_player (pl->ob, 0);
379 317
380 if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) 318 if (!QUERY_FLAG (pl->ob, FLAG_REMOVED))
381 { 319 {
382 terminate_all_pets (pl->ob); 320 terminate_all_pets (pl->ob);
383 pl->ob->remove (); 321 pl->ob->remove ();
384 } 322 }
385 323
386 leave (pl, 1); 324 leave (pl, 1);
387 final_free_player (pl); 325 final_free_player (pl);
326 }
327
388 pl = npl; 328 pl = npl;
329 }
330
331 FD_ZERO (&tmp_read);
332
333 for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); )
334 {
335 client_socket *s = *i;
336
337 if (s->status == Ns_Dead)
338 {
339 client_sockets.erase (i);
340 delete s;
389 } 341 }
390 else 342 else
391 { 343 {
344 if (s->fd > maxfd) maxfd = s->fd;
345
392 FD_SET ((uint32) pl->socket.fd, &tmp_read); 346 FD_SET (s->fd, &tmp_read);
393 FD_SET ((uint32) pl->socket.fd, &tmp_write); 347
394 pl = pl->next; 348 ++i;
395 } 349 }
396 } 350 }
397 351
398 /* Reset timeout each time, since some OS's will change the values on 352 struct timeval timeout;
399 * the return from select. 353
400 */
401 socket_info.timeout.tv_sec = 0; 354 timeout.tv_sec = 0;
402 socket_info.timeout.tv_usec = 0; 355 timeout.tv_usec = 0;
403 356
404 pollret = select (socket_info.max_filedescriptor, 357 pollret = select (maxfd + 1,
405 &tmp_read, &tmp_write, 0, 358 &tmp_read, 0, 0,
406 &socket_info.timeout); 359 &timeout);
407 360
408 if (pollret == -1) 361 if (pollret == -1)
409 { 362 {
410 LOG (llevError, "select failed: %s\n", strerror (errno)); 363 LOG (llevError, "select failed: %s\n", strerror (errno));
411 return; 364 return;
412 } 365 }
413 366
414 /* We need to do some of the processing below regardless */ 367 /* We need to do some of the processing below regardless */
415 368
416/* if (!pollret) return;*/ 369 /* Check for any input on the sockets */
417 370 for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ++i)
418 /* Following adds a new connection */
419 if (pollret && FD_ISSET (init_sockets[0].fd, &tmp_read))
420 { 371 {
421 int newsocknum = 0; 372 client_socket *s = *i;
373 player *pl = s->pl;
422 374
423#ifdef ESRV_DEBUG
424 LOG (llevDebug, "doeric_server: New Connection\n");
425#endif
426 /* If this is the case, all sockets currently in used */
427 if (socket_info.allocated_sockets <= socket_info.nconns)
428 {
429 init_sockets = (NewSocket *) realloc (init_sockets, sizeof (NewSocket) * (socket_info.nconns + 1));
430 if (!init_sockets)
431 fatal (OUT_OF_MEMORY);
432 newsocknum = socket_info.allocated_sockets;
433 socket_info.allocated_sockets++;
434 init_sockets[newsocknum].faces_sent_len = nrofpixmaps;
435 init_sockets[newsocknum].faces_sent = (uint8 *) malloc (nrofpixmaps * sizeof (*init_sockets[newsocknum].faces_sent));
436 if (!init_sockets[newsocknum].faces_sent)
437 fatal (OUT_OF_MEMORY);
438 init_sockets[newsocknum].status = Ns_Avail;
439 }
440 else
441 {
442 int j;
443
444 for (j = 1; j < socket_info.allocated_sockets; j++)
445 if (init_sockets[j].status == Ns_Avail)
446 {
447 newsocknum = j;
448 break;
449 }
450 }
451
452 init_sockets[newsocknum].fd = accept (init_sockets[0].fd, (struct sockaddr *) &addr, &addrlen);
453
454 if (init_sockets[newsocknum].fd == -1)
455 LOG (llevError, "accept failed: %s\n", strerror (errno));
456 else
457 {
458 char buf[MAX_BUF];
459 long ip;
460 NewSocket *ns;
461
462 ns = &init_sockets[newsocknum];
463
464 ip = ntohl (addr.sin_addr.s_addr);
465 sprintf (buf, "%ld.%ld.%ld.%ld", (ip >> 24) & 255, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255);
466
467 if (checkbanned (NULL, buf))
468 {
469 LOG (llevInfo, "Banned host tried to connect: [%s]\n", buf);
470 close (init_sockets[newsocknum].fd);
471 init_sockets[newsocknum].fd = -1;
472 }
473 else
474 {
475 InitConnection (ns, buf);
476 socket_info.nconns++;
477 }
478 }
479 }
480
481 /* Check for any input on the sockets */
482 if (pollret)
483 for (i = 1; i < socket_info.allocated_sockets; i++)
484 {
485 if (init_sockets[i].status == Ns_Avail)
486 continue;
487
488 //TODO: disassociate handleclient from socket readin 375 //TODO: disassociate handleclient from socket readin
489 if (init_sockets[i].inbuf_len || FD_ISSET (init_sockets[i].fd, &tmp_read)) 376 if (s->inbuf_len || FD_ISSET (s->fd, &tmp_read))
490 HandleClient (&init_sockets[i], NULL);
491
492 if (FD_ISSET (init_sockets[i].fd, &tmp_write))
493 init_sockets[i].can_write = 1;
494 }
495
496 /* This does roughly the same thing, but for the players now */
497 for (pl = first_player; pl != NULL; pl = next)
498 {
499
500 next = pl->next;
501 if (pl->socket.status == Ns_Dead)
502 continue;
503
504 if (FD_ISSET (pl->socket.fd, &tmp_write))
505 {
506 if (!pl->socket.can_write)
507 {
508#if 0
509 LOG (llevDebug, "Player %s socket now write enabled\n", pl->ob->name);
510#endif
511 pl->socket.can_write = 1;
512 write_socket_buffer (&pl->socket);
513 }
514
515 /* if we get an error on the write_socket buffer, no reason to
516 * continue on this socket.
517 */
518 if (pl->socket.status == Ns_Dead)
519 continue;
520 }
521 else
522 pl->socket.can_write = 0;
523
524 HandleClient (&pl->socket, pl); 377 HandleClient (s, pl);
525 /* If the player has left the game, then the socket status 378
526 * will be set to this be the leave function. We don't 379 //TODO: should not be done here, either
527 * need to call leave again, as it has already been called
528 * once.
529 */
530 if (pl->socket.status == Ns_Dead) 380 if (s->status != Ns_Dead && pl)
531 {
532 save_player (pl->ob, 0);
533
534 if (!QUERY_FLAG (pl->ob, FLAG_REMOVED))
535 {
536 terminate_all_pets (pl->ob);
537 pl->ob->remove ();
538 }
539
540 leave (pl, 1);
541 final_free_player (pl);
542 }
543 else
544 { 381 {
545 /* Update the players stats once per tick. More efficient than 382 /* Update the players stats once per tick. More efficient than
546 * sending them whenever they change, and probably just as useful 383 * sending them whenever they change, and probably just as useful
547 */ 384 */
548 esrv_update_stats (pl); 385 esrv_update_stats (pl);
556 393
557 /* draw_client_map does sanity checking that map is 394 /* draw_client_map does sanity checking that map is
558 * valid, so don't do it here. 395 * valid, so don't do it here.
559 */ 396 */
560 draw_client_map (pl->ob); 397 draw_client_map (pl->ob);
561 if (pl->socket.update_look) 398 if (s->update_look)
562 esrv_draw_look (pl->ob); 399 esrv_draw_look (pl->ob);
563 } 400 }
564 } 401 }
565} 402}
566 403

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines