… | |
… | |
9 | use Event; |
9 | use Event; |
10 | use Opcode; |
10 | use Opcode; |
11 | use Safe; |
11 | use Safe; |
12 | use Safe::Hole; |
12 | use Safe::Hole; |
13 | |
13 | |
14 | use Coro 3.52 (); |
14 | use Coro 3.61 (); |
15 | use Coro::State; |
15 | use Coro::State; |
16 | use Coro::Event; |
16 | use Coro::Event; |
17 | use Coro::Timer; |
17 | use Coro::Timer; |
18 | use Coro::Signal; |
18 | use Coro::Signal; |
19 | use Coro::Semaphore; |
19 | use Coro::Semaphore; |
… | |
… | |
54 | |
54 | |
55 | our $RELOAD; # number of reloads so far |
55 | our $RELOAD; # number of reloads so far |
56 | our @EVENT; |
56 | our @EVENT; |
57 | our $LIBDIR = datadir . "/ext"; |
57 | our $LIBDIR = datadir . "/ext"; |
58 | |
58 | |
59 | our $TICK = MAX_TIME * 1e-6; |
59 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
60 | our $TICK_WATCHER; |
60 | our $TICK_WATCHER; |
61 | our $AIO_POLL_WATCHER; |
61 | our $AIO_POLL_WATCHER; |
62 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
62 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
63 | our $NEXT_TICK; |
63 | our $NEXT_TICK; |
64 | our $NOW; |
64 | our $NOW; |
… | |
… | |
2332 | } |
2332 | } |
2333 | |
2333 | |
2334 | ############################################################################# |
2334 | ############################################################################# |
2335 | # the server's init and main functions |
2335 | # the server's init and main functions |
2336 | |
2336 | |
2337 | sub load_facedata { |
2337 | sub load_facedata($) { |
2338 | my $path = sprintf "%s/facedata", cf::datadir; |
2338 | my ($path) = @_; |
2339 | |
2339 | |
2340 | warn "loading facedata from $path\n"; |
2340 | warn "loading facedata from $path\n"; |
2341 | |
2341 | |
2342 | my $facedata; |
2342 | my $facedata; |
2343 | 0 < aio_load $path, $facedata |
2343 | 0 < aio_load $path, $facedata |
… | |
… | |
2384 | } |
2384 | } |
2385 | |
2385 | |
2386 | 1 |
2386 | 1 |
2387 | } |
2387 | } |
2388 | |
2388 | |
2389 | our $archetypes_loaded; |
2389 | sub reload_facedata { |
2390 | |
|
|
2391 | sub load_archetypes { |
|
|
2392 | return 1 if $archetypes_loaded++; # current can only load them once |
|
|
2393 | load_archetype_file sprintf "%s/archetypes", cf::datadir |
2390 | load_facedata sprintf "%s/facedata", cf::datadir |
|
|
2391 | or die "unable to load facedata\n"; |
2394 | } |
2392 | } |
2395 | |
2393 | |
2396 | sub load_treasures { |
|
|
2397 | load_treasure_file sprintf "%s/treasures", cf::datadir |
|
|
2398 | } |
|
|
2399 | |
|
|
2400 | sub reload_resources { |
2394 | sub reload_regions { |
2401 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2395 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2402 | or die "unable to load regions file\n"; |
2396 | or die "unable to load regions file\n"; |
2403 | load_facedata |
2397 | } |
2404 | or die "unable to load facedata\n"; |
2398 | |
2405 | load_archetypes |
2399 | sub reload_archetypes { |
|
|
2400 | load_archetype_file sprintf "%s/archetypes", cf::datadir |
2406 | or die "unable to load archetypes\n"; |
2401 | or die "unable to load archetypes\n"; |
2407 | load_treasures |
2402 | } |
|
|
2403 | |
|
|
2404 | sub reload_treasures { |
|
|
2405 | load_treasure_file sprintf "%s/treasures", cf::datadir |
2408 | or die "unable to load treasurelists\n"; |
2406 | or die "unable to load treasurelists\n"; |
|
|
2407 | } |
|
|
2408 | |
|
|
2409 | sub reload_resources { |
|
|
2410 | warn "reloading resource files...\n"; |
|
|
2411 | |
|
|
2412 | reload_regions; |
|
|
2413 | reload_facedata; |
|
|
2414 | reload_archetypes; |
|
|
2415 | reload_treasures; |
|
|
2416 | |
|
|
2417 | warn "finished reloading resource files\n"; |
2409 | } |
2418 | } |
2410 | |
2419 | |
2411 | sub init { |
2420 | sub init { |
2412 | reload_resources; |
2421 | reload_resources; |
2413 | } |
2422 | } |
… | |
… | |
2506 | |
2515 | |
2507 | warn Carp::longmess "post_cleanup backtrace" |
2516 | warn Carp::longmess "post_cleanup backtrace" |
2508 | if $make_core; |
2517 | if $make_core; |
2509 | } |
2518 | } |
2510 | |
2519 | |
2511 | sub reload() { |
2520 | sub do_reload_perl() { |
2512 | # can/must only be called in main |
2521 | # can/must only be called in main |
2513 | if ($Coro::current != $Coro::main) { |
2522 | if ($Coro::current != $Coro::main) { |
2514 | warn "can only reload from main coroutine"; |
2523 | warn "can only reload from main coroutine"; |
2515 | return; |
2524 | return; |
2516 | } |
2525 | } |
… | |
… | |
2615 | warn "reattaching attachments to maps"; |
2624 | warn "reattaching attachments to maps"; |
2616 | reattach $_ for values %MAP; |
2625 | reattach $_ for values %MAP; |
2617 | warn "reattaching attachments to players"; |
2626 | warn "reattaching attachments to players"; |
2618 | reattach $_ for values %PLAYER; |
2627 | reattach $_ for values %PLAYER; |
2619 | |
2628 | |
2620 | warn "loading reloadable resources"; |
|
|
2621 | reload_resources; |
|
|
2622 | |
|
|
2623 | warn "leaving sync_job"; |
2629 | warn "leaving sync_job"; |
2624 | |
2630 | |
2625 | 1 |
2631 | 1 |
2626 | } or do { |
2632 | } or do { |
2627 | warn $@; |
2633 | warn $@; |
… | |
… | |
2632 | warn "reloaded"; |
2638 | warn "reloaded"; |
2633 | }; |
2639 | }; |
2634 | |
2640 | |
2635 | our $RELOAD_WATCHER; # used only during reload |
2641 | our $RELOAD_WATCHER; # used only during reload |
2636 | |
2642 | |
|
|
2643 | sub reload_perl() { |
|
|
2644 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2645 | # coro crashes during coro_state_free->destroy here. |
|
|
2646 | |
|
|
2647 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2648 | reentrant => 0, |
|
|
2649 | after => 0, |
|
|
2650 | data => WF_AUTOCANCEL, |
|
|
2651 | cb => sub { |
|
|
2652 | do_reload_perl; |
|
|
2653 | undef $RELOAD_WATCHER; |
|
|
2654 | }, |
|
|
2655 | ); |
|
|
2656 | } |
|
|
2657 | |
2637 | register_command "reload" => sub { |
2658 | register_command "reload" => sub { |
2638 | my ($who, $arg) = @_; |
2659 | my ($who, $arg) = @_; |
2639 | |
2660 | |
2640 | if ($who->flag (FLAG_WIZ)) { |
2661 | if ($who->flag (FLAG_WIZ)) { |
2641 | $who->message ("reloading server."); |
2662 | $who->message ("reloading server."); |
2642 | |
2663 | async { reload_perl }; |
2643 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2644 | # coro crashes during coro_state_free->destroy here. |
|
|
2645 | |
|
|
2646 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2647 | reentrant => 0, |
|
|
2648 | after => 0, |
|
|
2649 | data => WF_AUTOCANCEL, |
|
|
2650 | cb => sub { |
|
|
2651 | reload; |
|
|
2652 | undef $RELOAD_WATCHER; |
|
|
2653 | }, |
|
|
2654 | ); |
|
|
2655 | } |
2664 | } |
2656 | }; |
2665 | }; |
2657 | |
2666 | |
2658 | unshift @INC, $LIBDIR; |
2667 | unshift @INC, $LIBDIR; |
2659 | |
2668 | |
… | |
… | |
2694 | } |
2703 | } |
2695 | |
2704 | |
2696 | $NOW = Event::time; |
2705 | $NOW = Event::time; |
2697 | |
2706 | |
2698 | cf::server_tick; # one server iteration |
2707 | cf::server_tick; # one server iteration |
|
|
2708 | |
2699 | $RUNTIME += $TICK; |
2709 | $RUNTIME += $TICK; |
2700 | $NEXT_TICK += $TICK; |
2710 | $NEXT_TICK += $TICK; |
2701 | |
2711 | |
2702 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2712 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2703 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
2713 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
… | |
… | |
2705 | write_runtime |
2715 | write_runtime |
2706 | or warn "ERROR: unable to write runtime file: $!"; |
2716 | or warn "ERROR: unable to write runtime file: $!"; |
2707 | }; |
2717 | }; |
2708 | } |
2718 | } |
2709 | |
2719 | |
|
|
2720 | # my $AFTER = Event::time; |
|
|
2721 | # warn $AFTER - $NOW;#d# |
|
|
2722 | |
|
|
2723 | # if we are delayed by four ticks or more, skip them all |
|
|
2724 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
|
|
2725 | |
|
|
2726 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
2727 | $TICK_WATCHER->start; |
|
|
2728 | |
2710 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
2729 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
2711 | $sig->send; |
2730 | $sig->send; |
2712 | } |
2731 | } |
2713 | while (my $sig = shift @WAIT_FOR_TICK) { |
2732 | while (my $sig = shift @WAIT_FOR_TICK) { |
2714 | $sig->send; |
2733 | $sig->send; |
2715 | } |
2734 | } |
2716 | |
2735 | |
2717 | # my $AFTER = Event::time; |
2736 | _post_tick; |
2718 | # warn $AFTER - $NOW;#d# |
|
|
2719 | |
|
|
2720 | # if we are delayed by four ticks or more, skip them all |
|
|
2721 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
|
|
2722 | |
|
|
2723 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
2724 | $TICK_WATCHER->start; |
|
|
2725 | }, |
2737 | }, |
2726 | ); |
2738 | ); |
2727 | |
2739 | |
2728 | { |
2740 | { |
2729 | BDB::max_poll_time $TICK * 0.1; |
2741 | BDB::max_poll_time $TICK * 0.1; |