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