… | |
… | |
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 | sub reload_facedata { |
|
|
2390 | load_facedata sprintf "%s/facedata", cf::datadir |
|
|
2391 | or die "unable to load facedata\n"; |
|
|
2392 | } |
|
|
2393 | |
|
|
2394 | sub reload_regions { |
|
|
2395 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
|
|
2396 | or die "unable to load regions file\n"; |
|
|
2397 | } |
|
|
2398 | |
|
|
2399 | sub reload_archetypes { |
|
|
2400 | load_archetype_file sprintf "%s/archetypes", cf::datadir |
|
|
2401 | or die "unable to load archetypes\n"; |
|
|
2402 | } |
|
|
2403 | |
|
|
2404 | sub reload_treasures { |
|
|
2405 | load_treasure_file sprintf "%s/treasures", cf::datadir |
|
|
2406 | or die "unable to load treasurelists\n"; |
|
|
2407 | } |
|
|
2408 | |
2389 | sub reload_resources { |
2409 | sub reload_resources { |
2390 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2410 | warn "reloading resource files...\n"; |
2391 | or die "unable to load regions file\n";#d# |
2411 | |
|
|
2412 | reload_regions; |
2392 | load_facedata |
2413 | reload_facedata; |
2393 | or die "unable to load facedata\n";#d# |
2414 | reload_archetypes; |
|
|
2415 | reload_treasures; |
|
|
2416 | |
|
|
2417 | warn "finished reloading resource files\n"; |
2394 | } |
2418 | } |
2395 | |
2419 | |
2396 | sub init { |
2420 | sub init { |
2397 | reload_resources; |
2421 | reload_resources; |
2398 | } |
2422 | } |
… | |
… | |
2491 | |
2515 | |
2492 | warn Carp::longmess "post_cleanup backtrace" |
2516 | warn Carp::longmess "post_cleanup backtrace" |
2493 | if $make_core; |
2517 | if $make_core; |
2494 | } |
2518 | } |
2495 | |
2519 | |
2496 | sub reload() { |
2520 | sub do_reload_perl() { |
2497 | # can/must only be called in main |
2521 | # can/must only be called in main |
2498 | if ($Coro::current != $Coro::main) { |
2522 | if ($Coro::current != $Coro::main) { |
2499 | warn "can only reload from main coroutine"; |
2523 | warn "can only reload from main coroutine"; |
2500 | return; |
2524 | return; |
2501 | } |
2525 | } |
… | |
… | |
2600 | warn "reattaching attachments to maps"; |
2624 | warn "reattaching attachments to maps"; |
2601 | reattach $_ for values %MAP; |
2625 | reattach $_ for values %MAP; |
2602 | warn "reattaching attachments to players"; |
2626 | warn "reattaching attachments to players"; |
2603 | reattach $_ for values %PLAYER; |
2627 | reattach $_ for values %PLAYER; |
2604 | |
2628 | |
2605 | warn "loading reloadable resources"; |
|
|
2606 | reload_resources; |
|
|
2607 | |
|
|
2608 | warn "leaving sync_job"; |
2629 | warn "leaving sync_job"; |
2609 | |
2630 | |
2610 | 1 |
2631 | 1 |
2611 | } or do { |
2632 | } or do { |
2612 | warn $@; |
2633 | warn $@; |
… | |
… | |
2617 | warn "reloaded"; |
2638 | warn "reloaded"; |
2618 | }; |
2639 | }; |
2619 | |
2640 | |
2620 | our $RELOAD_WATCHER; # used only during reload |
2641 | our $RELOAD_WATCHER; # used only during reload |
2621 | |
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 | |
2622 | register_command "reload" => sub { |
2658 | register_command "reload" => sub { |
2623 | my ($who, $arg) = @_; |
2659 | my ($who, $arg) = @_; |
2624 | |
2660 | |
2625 | if ($who->flag (FLAG_WIZ)) { |
2661 | if ($who->flag (FLAG_WIZ)) { |
2626 | $who->message ("reloading server."); |
2662 | $who->message ("reloading server."); |
2627 | |
2663 | async { reload_perl }; |
2628 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2629 | # coro crashes during coro_state_free->destroy here. |
|
|
2630 | |
|
|
2631 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2632 | reentrant => 0, |
|
|
2633 | after => 0, |
|
|
2634 | data => WF_AUTOCANCEL, |
|
|
2635 | cb => sub { |
|
|
2636 | reload; |
|
|
2637 | undef $RELOAD_WATCHER; |
|
|
2638 | }, |
|
|
2639 | ); |
|
|
2640 | } |
2664 | } |
2641 | }; |
2665 | }; |
2642 | |
2666 | |
2643 | unshift @INC, $LIBDIR; |
2667 | unshift @INC, $LIBDIR; |
2644 | |
2668 | |
… | |
… | |
2646 | |
2670 | |
2647 | our @WAIT_FOR_TICK; |
2671 | our @WAIT_FOR_TICK; |
2648 | our @WAIT_FOR_TICK_BEGIN; |
2672 | our @WAIT_FOR_TICK_BEGIN; |
2649 | |
2673 | |
2650 | sub wait_for_tick { |
2674 | sub wait_for_tick { |
|
|
2675 | return unless $TICK_WATCHER->is_active; |
|
|
2676 | return if $Coro::current == $Coro::main; |
|
|
2677 | |
2651 | my $signal = new Coro::Signal; |
2678 | my $signal = new Coro::Signal; |
2652 | push @WAIT_FOR_TICK, $signal; |
2679 | push @WAIT_FOR_TICK, $signal; |
2653 | $signal->wait; |
2680 | $signal->wait; |
2654 | } |
2681 | } |
2655 | |
2682 | |
2656 | sub wait_for_tick_begin { |
2683 | sub wait_for_tick_begin { |
|
|
2684 | return unless $TICK_WATCHER->is_active; |
|
|
2685 | return if $Coro::current == $Coro::main; |
|
|
2686 | |
2657 | my $signal = new Coro::Signal; |
2687 | my $signal = new Coro::Signal; |
2658 | push @WAIT_FOR_TICK_BEGIN, $signal; |
2688 | push @WAIT_FOR_TICK_BEGIN, $signal; |
2659 | $signal->wait; |
2689 | $signal->wait; |
2660 | } |
2690 | } |
2661 | |
2691 | |
… | |
… | |
2673 | } |
2703 | } |
2674 | |
2704 | |
2675 | $NOW = Event::time; |
2705 | $NOW = Event::time; |
2676 | |
2706 | |
2677 | cf::server_tick; # one server iteration |
2707 | cf::server_tick; # one server iteration |
|
|
2708 | |
2678 | $RUNTIME += $TICK; |
2709 | $RUNTIME += $TICK; |
2679 | $NEXT_TICK += $TICK; |
2710 | $NEXT_TICK += $TICK; |
2680 | |
2711 | |
2681 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2712 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2682 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
2713 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
… | |
… | |
2684 | write_runtime |
2715 | write_runtime |
2685 | or warn "ERROR: unable to write runtime file: $!"; |
2716 | or warn "ERROR: unable to write runtime file: $!"; |
2686 | }; |
2717 | }; |
2687 | } |
2718 | } |
2688 | |
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 | |
2689 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
2729 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
2690 | $sig->send; |
2730 | $sig->send; |
2691 | } |
2731 | } |
2692 | while (my $sig = shift @WAIT_FOR_TICK) { |
2732 | while (my $sig = shift @WAIT_FOR_TICK) { |
2693 | $sig->send; |
2733 | $sig->send; |
2694 | } |
2734 | } |
2695 | |
2735 | |
2696 | # my $AFTER = Event::time; |
2736 | _post_tick; |
2697 | # warn $AFTER - $NOW;#d# |
|
|
2698 | |
|
|
2699 | # if we are delayed by four ticks or more, skip them all |
|
|
2700 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
|
|
2701 | |
|
|
2702 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
2703 | $TICK_WATCHER->start; |
|
|
2704 | }, |
2737 | }, |
2705 | ); |
2738 | ); |
2706 | |
2739 | |
2707 | { |
2740 | { |
2708 | BDB::max_poll_time $TICK * 0.1; |
2741 | BDB::max_poll_time $TICK * 0.1; |