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.9 by root, Tue Dec 12 20:53:03 2006 UTC vs.
Revision 1.10 by root, Wed Dec 13 02:55:51 2006 UTC

32 * The reading of data is handled in ericserver.c 32 * The reading of data is handled in ericserver.c
33 */ 33 */
34 34
35 35
36#include <global.h> 36#include <global.h>
37#ifndef __CEXTRACT__
38# include <sproto.h> 37#include <sproto.h>
39# include <sockproto.h> 38#include <sockproto.h>
40#endif
41 39
42#include <sys/types.h> 40#include <sys/types.h>
43#include <sys/time.h> 41#include <sys/time.h>
44#include <sys/socket.h> 42#include <sys/socket.h>
45#include <netinet/in.h> 43#include <netinet/in.h>
158 { 156 {
159 *cp = '\0'; 157 *cp = '\0';
160 params = cp + 1; 158 params = cp + 1;
161 break; 159 break;
162 } 160 }
161
163 if (!strcmp (buf, "image_info")) 162 if (!strcmp (buf, "image_info"))
164 send_image_info (ns, params); 163 send_image_info (ns, params);
165 else if (!strcmp (buf, "image_sums")) 164 else if (!strcmp (buf, "image_sums"))
166 send_image_sums (ns, params); 165 send_image_sums (ns, params);
167 else if (!strcmp (buf, "skill_info")) 166 else if (!strcmp (buf, "skill_info"))
324 if (pl->socket.status != Ns_Dead) 323 if (pl->socket.status != Ns_Dead)
325 Socket_Flush (&pl->socket); 324 Socket_Flush (&pl->socket);
326} 325}
327 326
328/** 327/**
329 * This checks the sockets for input and exceptions, does the right thing. 328 * This checks the sockets for input, does the right thing.
330 * 329 *
331 * A bit of this code is grabbed out of socket.c 330 * A bit of this code is grabbed out of socket.c
332 * There are 2 lists we need to look through - init_sockets is a list 331 * There are 2 lists we need to look through - init_sockets is a list
333 * 332 *
334 */ 333 */
335void 334void
336doeric_server (void) 335doeric_server (void)
337{ 336{
338 int i, pollret; 337 int i, pollret;
339 fd_set tmp_read, tmp_exceptions, tmp_write; 338 fd_set tmp_read, tmp_write;
340 struct sockaddr_in addr; 339 struct sockaddr_in addr;
341 socklen_t addrlen = sizeof (struct sockaddr); 340 socklen_t addrlen = sizeof (struct sockaddr);
342 player *pl, *next; 341 player *pl, *next;
343 342
344#ifdef CS_LOGSTATS 343#ifdef CS_LOGSTATS
346 write_cs_stats (); 345 write_cs_stats ();
347#endif 346#endif
348 347
349 FD_ZERO (&tmp_read); 348 FD_ZERO (&tmp_read);
350 FD_ZERO (&tmp_write); 349 FD_ZERO (&tmp_write);
351 FD_ZERO (&tmp_exceptions);
352 350
353 for (i = 0; i < socket_info.allocated_sockets; i++) 351 for (i = 0; i < socket_info.allocated_sockets; i++)
354 { 352 {
355 if (init_sockets[i].status == Ns_Dead) 353 if (init_sockets[i].status == Ns_Dead)
356 { 354 {
358 init_sockets[i].status = Ns_Avail; 356 init_sockets[i].status = Ns_Avail;
359 socket_info.nconns--; 357 socket_info.nconns--;
360 } 358 }
361 else if (init_sockets[i].status != Ns_Avail) 359 else if (init_sockets[i].status != Ns_Avail)
362 { 360 {
363 FD_SET ((uint32) init_sockets[i].fd, &tmp_read); 361 FD_SET (init_sockets[i].fd, &tmp_read);
364 FD_SET ((uint32) init_sockets[i].fd, &tmp_write); 362 FD_SET (init_sockets[i].fd, &tmp_write);
365 FD_SET ((uint32) init_sockets[i].fd, &tmp_exceptions);
366 } 363 }
367 } 364 }
368 365
369 /* Go through the players. Let the loop set the next pl value, 366 /* Go through the players. Let the loop set the next pl value,
370 * since we may remove some 367 * since we may remove some
374 if (pl->socket.status == Ns_Dead) 371 if (pl->socket.status == Ns_Dead)
375 { 372 {
376 player *npl = pl->next; 373 player *npl = pl->next;
377 374
378 save_player (pl->ob, 0); 375 save_player (pl->ob, 0);
376
379 if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) 377 if (!QUERY_FLAG (pl->ob, FLAG_REMOVED))
380 { 378 {
381 terminate_all_pets (pl->ob); 379 terminate_all_pets (pl->ob);
382 pl->ob->remove (); 380 pl->ob->remove ();
383 } 381 }
382
384 leave (pl, 1); 383 leave (pl, 1);
385 final_free_player (pl); 384 final_free_player (pl);
386 pl = npl; 385 pl = npl;
387 } 386 }
388 else 387 else
389 { 388 {
390 FD_SET ((uint32) pl->socket.fd, &tmp_read); 389 FD_SET ((uint32) pl->socket.fd, &tmp_read);
391 FD_SET ((uint32) pl->socket.fd, &tmp_write); 390 FD_SET ((uint32) pl->socket.fd, &tmp_write);
392 FD_SET ((uint32) pl->socket.fd, &tmp_exceptions);
393 pl = pl->next; 391 pl = pl->next;
394 } 392 }
395 } 393 }
396 394
397 /* Reset timeout each time, since some OS's will change the values on 395 /* Reset timeout each time, since some OS's will change the values on
398 * the return from select. 396 * the return from select.
399 */ 397 */
400 socket_info.timeout.tv_sec = 0; 398 socket_info.timeout.tv_sec = 0;
401 socket_info.timeout.tv_usec = 0; 399 socket_info.timeout.tv_usec = 0;
402 400
403 pollret = select (socket_info.max_filedescriptor, &tmp_read, &tmp_write, &tmp_exceptions, &socket_info.timeout); 401 pollret = select (socket_info.max_filedescriptor,
402 &tmp_read, &tmp_write, 0,
403 &socket_info.timeout);
404 404
405 if (pollret == -1) 405 if (pollret == -1)
406 { 406 {
407 LOG (llevError, "select failed: %s\n", strerror (errno)); 407 LOG (llevError, "select failed: %s\n", strerror (errno));
408 return; 408 return;
443 { 443 {
444 newsocknum = j; 444 newsocknum = j;
445 break; 445 break;
446 } 446 }
447 } 447 }
448
448 init_sockets[newsocknum].fd = accept (init_sockets[0].fd, (struct sockaddr *) &addr, &addrlen); 449 init_sockets[newsocknum].fd = accept (init_sockets[0].fd, (struct sockaddr *) &addr, &addrlen);
450
449 if (init_sockets[newsocknum].fd == -1) 451 if (init_sockets[newsocknum].fd == -1)
450 {
451 LOG (llevError, "accept failed: %s\n", strerror (errno)); 452 LOG (llevError, "accept failed: %s\n", strerror (errno));
452 }
453 else 453 else
454 { 454 {
455 char buf[MAX_BUF]; 455 char buf[MAX_BUF];
456 long ip; 456 long ip;
457 NewSocket *ns; 457 NewSocket *ns;
473 socket_info.nconns++; 473 socket_info.nconns++;
474 } 474 }
475 } 475 }
476 } 476 }
477 477
478 /* Check for any exceptions/input on the sockets */ 478 /* Check for any input on the sockets */
479 if (pollret) 479 if (pollret)
480 for (i = 1; i < socket_info.allocated_sockets; i++) 480 for (i = 1; i < socket_info.allocated_sockets; i++)
481 { 481 {
482 if (init_sockets[i].status == Ns_Avail) 482 if (init_sockets[i].status == Ns_Avail)
483 continue; 483 continue;
484 if (FD_ISSET (init_sockets[i].fd, &tmp_exceptions)) 484
485 {
486 free_newsocket (&init_sockets[i]);
487 init_sockets[i].status = Ns_Avail;
488 socket_info.nconns--;
489 continue;
490 }
491 if (FD_ISSET (init_sockets[i].fd, &tmp_read)) 485 if (FD_ISSET (init_sockets[i].fd, &tmp_read))
492 {
493 HandleClient (&init_sockets[i], NULL); 486 HandleClient (&init_sockets[i], NULL);
494 } 487
495 if (FD_ISSET (init_sockets[i].fd, &tmp_write)) 488 if (FD_ISSET (init_sockets[i].fd, &tmp_write))
496 {
497 init_sockets[i].can_write = 1; 489 init_sockets[i].can_write = 1;
498 }
499 } 490 }
500 491
501 /* This does roughly the same thing, but for the players now */ 492 /* This does roughly the same thing, but for the players now */
502 for (pl = first_player; pl != NULL; pl = next) 493 for (pl = first_player; pl != NULL; pl = next)
503 { 494 {
514 LOG (llevDebug, "Player %s socket now write enabled\n", pl->ob->name); 505 LOG (llevDebug, "Player %s socket now write enabled\n", pl->ob->name);
515#endif 506#endif
516 pl->socket.can_write = 1; 507 pl->socket.can_write = 1;
517 write_socket_buffer (&pl->socket); 508 write_socket_buffer (&pl->socket);
518 } 509 }
510
519 /* if we get an error on the write_socket buffer, no reason to 511 /* if we get an error on the write_socket buffer, no reason to
520 * continue on this socket. 512 * continue on this socket.
521 */ 513 */
522 if (pl->socket.status == Ns_Dead) 514 if (pl->socket.status == Ns_Dead)
523 continue; 515 continue;
524 } 516 }
525 else 517 else
526 pl->socket.can_write = 0; 518 pl->socket.can_write = 0;
527 519
528 if (FD_ISSET (pl->socket.fd, &tmp_exceptions)) 520 HandleClient (&pl->socket, pl);
521 /* If the player has left the game, then the socket status
522 * will be set to this be the leave function. We don't
523 * need to call leave again, as it has already been called
524 * once.
525 */
526 if (pl->socket.status == Ns_Dead)
529 { 527 {
530 save_player (pl->ob, 0); 528 save_player (pl->ob, 0);
529
531 if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) 530 if (!QUERY_FLAG (pl->ob, FLAG_REMOVED))
532 { 531 {
533 terminate_all_pets (pl->ob); 532 terminate_all_pets (pl->ob);
534 pl->ob->remove (); 533 pl->ob->remove ();
535 } 534 }
535
536 leave (pl, 1); 536 leave (pl, 1);
537 final_free_player (pl); 537 final_free_player (pl);
538 } 538 }
539 else 539 else
540 { 540 {
541 HandleClient (&pl->socket, pl); 541 /* Update the players stats once per tick. More efficient than
542 /* If the player has left the game, then the socket status 542 * sending them whenever they change, and probably just as useful
543 * will be set to this be the leave function. We don't
544 * need to call leave again, as it has already been called
545 * once.
546 */ 543 */
547 if (pl->socket.status == Ns_Dead)
548 {
549 save_player (pl->ob, 0);
550 if (!QUERY_FLAG (pl->ob, FLAG_REMOVED))
551 {
552 terminate_all_pets (pl->ob);
553 pl->ob->remove ();
554 }
555 leave (pl, 1);
556 final_free_player (pl);
557 }
558 else
559 {
560
561 /* Update the players stats once per tick. More efficient than
562 * sending them whenever they change, and probably just as useful
563 */
564 esrv_update_stats (pl); 544 esrv_update_stats (pl);
565 if (pl->last_weight != -1 && pl->last_weight != WEIGHT (pl->ob)) 545 if (pl->last_weight != -1 && pl->last_weight != WEIGHT (pl->ob))
566 { 546 {
567 esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob); 547 esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob);
568 if (pl->last_weight != WEIGHT (pl->ob)) 548 if (pl->last_weight != WEIGHT (pl->ob))
569 LOG (llevError, "esrv_update_item(UPD_WEIGHT) did not set player weight: is %lu, should be %lu\n", 549 LOG (llevError, "esrv_update_item(UPD_WEIGHT) did not set player weight: is %lu, should be %lu\n",
570 (unsigned long) pl->last_weight, WEIGHT (pl->ob)); 550 (unsigned long) pl->last_weight, WEIGHT (pl->ob));
571 } 551 }
552
572 /* draw_client_map does sanity checking that map is 553 /* draw_client_map does sanity checking that map is
573 * valid, so don't do it here. 554 * valid, so don't do it here.
574 */ 555 */
575 draw_client_map (pl->ob); 556 draw_client_map (pl->ob);
576 if (pl->socket.update_look) 557 if (pl->socket.update_look)
577 esrv_draw_look (pl->ob); 558 esrv_draw_look (pl->ob);
578 }
579 } 559 }
580 } 560 }
581} 561}
562

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines