… | |
… | |
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; |
… | |
… | |
76 | our %MAP; # all maps |
76 | our %MAP; # all maps |
77 | our $LINK_MAP; # the special {link} map, which is always available |
77 | our $LINK_MAP; # the special {link} map, which is always available |
78 | our $RANDOM_MAPS = cf::localdir . "/random"; |
78 | our $RANDOM_MAPS = cf::localdir . "/random"; |
79 | our $BDB_ENV_DIR = cf::localdir . "/db"; |
79 | our $BDB_ENV_DIR = cf::localdir . "/db"; |
80 | |
80 | |
81 | our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal; |
|
|
82 | our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal; |
|
|
83 | |
|
|
84 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
81 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
85 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
82 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
86 | |
83 | |
87 | binmode STDOUT; |
84 | binmode STDOUT; |
88 | binmode STDERR; |
85 | binmode STDERR; |
… | |
… | |
136 | =item %cf::CFG |
133 | =item %cf::CFG |
137 | |
134 | |
138 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
135 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
139 | from wherever your confdir points to. |
136 | from wherever your confdir points to. |
140 | |
137 | |
141 | =item $cf::WAIT_FOR_TICK, $cf::WAIT_FOR_TICK_ONE |
138 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
142 | |
139 | |
143 | These are Coro::Signal objects that are C<< ->broadcast >> (WAIT_FOR_TICK) |
140 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
144 | or C<< ->send >> (WAIT_FOR_TICK_ONE) on after normal server tick |
141 | returns directly I<after> the tick processing (and consequently, can only wake one process |
145 | processing has been done. Call C<< ->wait >> on them to maximise the |
142 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
146 | window of cpu time available, or simply to synchronise to the server tick. |
|
|
147 | |
143 | |
148 | =back |
144 | =back |
149 | |
145 | |
150 | =cut |
146 | =cut |
151 | |
147 | |
… | |
… | |
1483 | $self->in_memory (cf::MAP_LOADING); |
1479 | $self->in_memory (cf::MAP_LOADING); |
1484 | |
1480 | |
1485 | $self->alloc; |
1481 | $self->alloc; |
1486 | |
1482 | |
1487 | $self->pre_load; |
1483 | $self->pre_load; |
|
|
1484 | Coro::cede; |
1488 | |
1485 | |
1489 | $self->_load_objects ($self->{load_path}, 1) |
1486 | $self->_load_objects ($self->{load_path}, 1) |
1490 | or return; |
1487 | or return; |
1491 | |
1488 | |
1492 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
1489 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
… | |
… | |
1499 | $self->_load_objects ($uniq, 0); |
1496 | $self->_load_objects ($uniq, 0); |
1500 | } |
1497 | } |
1501 | } |
1498 | } |
1502 | |
1499 | |
1503 | Coro::cede; |
1500 | Coro::cede; |
1504 | |
|
|
1505 | # now do the right thing for maps |
1501 | # now do the right thing for maps |
1506 | $self->link_multipart_objects; |
1502 | $self->link_multipart_objects; |
|
|
1503 | Coro::cede; |
1507 | |
1504 | |
1508 | unless ($self->{deny_activate}) { |
1505 | unless ($self->{deny_activate}) { |
1509 | $self->decay_objects; |
1506 | $self->decay_objects; |
1510 | $self->fix_auto_apply; |
1507 | $self->fix_auto_apply; |
1511 | $self->update_buttons; |
1508 | $self->update_buttons; |
… | |
… | |
1513 | $self->set_darkness_map; |
1510 | $self->set_darkness_map; |
1514 | $self->difficulty ($self->estimate_difficulty) |
1511 | $self->difficulty ($self->estimate_difficulty) |
1515 | unless $self->difficulty; |
1512 | unless $self->difficulty; |
1516 | Coro::cede; |
1513 | Coro::cede; |
1517 | $self->activate; |
1514 | $self->activate; |
|
|
1515 | Coro::cede; |
1518 | } |
1516 | } |
1519 | |
1517 | |
1520 | $self->post_load; |
1518 | $self->post_load; |
|
|
1519 | Coro::cede; |
1521 | |
1520 | |
1522 | $self->in_memory (cf::MAP_IN_MEMORY); |
1521 | $self->in_memory (cf::MAP_IN_MEMORY); |
1523 | } |
1522 | } |
1524 | |
1523 | |
1525 | sub customise_for { |
1524 | sub customise_for { |
… | |
… | |
2333 | } |
2332 | } |
2334 | |
2333 | |
2335 | ############################################################################# |
2334 | ############################################################################# |
2336 | # the server's init and main functions |
2335 | # the server's init and main functions |
2337 | |
2336 | |
2338 | sub load_facedata { |
2337 | sub load_facedata($) { |
2339 | my $path = sprintf "%s/facedata", cf::datadir; |
2338 | my ($path) = @_; |
2340 | |
2339 | |
2341 | warn "loading facedata from $path\n"; |
2340 | warn "loading facedata from $path\n"; |
2342 | |
2341 | |
2343 | my $facedata; |
2342 | my $facedata; |
2344 | 0 < aio_load $path, $facedata |
2343 | 0 < aio_load $path, $facedata |
… | |
… | |
2385 | } |
2384 | } |
2386 | |
2385 | |
2387 | 1 |
2386 | 1 |
2388 | } |
2387 | } |
2389 | |
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_resource_file sprintf "%s/treasures", cf::datadir |
|
|
2406 | or die "unable to load treasurelists\n"; |
|
|
2407 | } |
|
|
2408 | |
2390 | sub reload_resources { |
2409 | sub reload_resources { |
2391 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2410 | warn "reloading resource files...\n"; |
2392 | or die "unable to load regions file\n";#d# |
2411 | |
|
|
2412 | reload_regions; |
2393 | load_facedata |
2413 | reload_facedata; |
2394 | or die "unable to load facedata\n";#d# |
2414 | reload_archetypes; |
|
|
2415 | reload_treasures; |
|
|
2416 | |
|
|
2417 | warn "finished reloading resource files\n"; |
2395 | } |
2418 | } |
2396 | |
2419 | |
2397 | sub init { |
2420 | sub init { |
2398 | reload_resources; |
2421 | reload_resources; |
2399 | } |
2422 | } |
… | |
… | |
2492 | |
2515 | |
2493 | warn Carp::longmess "post_cleanup backtrace" |
2516 | warn Carp::longmess "post_cleanup backtrace" |
2494 | if $make_core; |
2517 | if $make_core; |
2495 | } |
2518 | } |
2496 | |
2519 | |
2497 | sub reload() { |
2520 | sub do_reload_perl() { |
2498 | # can/must only be called in main |
2521 | # can/must only be called in main |
2499 | if ($Coro::current != $Coro::main) { |
2522 | if ($Coro::current != $Coro::main) { |
2500 | warn "can only reload from main coroutine"; |
2523 | warn "can only reload from main coroutine"; |
2501 | return; |
2524 | return; |
2502 | } |
2525 | } |
… | |
… | |
2601 | warn "reattaching attachments to maps"; |
2624 | warn "reattaching attachments to maps"; |
2602 | reattach $_ for values %MAP; |
2625 | reattach $_ for values %MAP; |
2603 | warn "reattaching attachments to players"; |
2626 | warn "reattaching attachments to players"; |
2604 | reattach $_ for values %PLAYER; |
2627 | reattach $_ for values %PLAYER; |
2605 | |
2628 | |
2606 | warn "loading reloadable resources"; |
|
|
2607 | reload_resources; |
|
|
2608 | |
|
|
2609 | warn "leaving sync_job"; |
2629 | warn "leaving sync_job"; |
2610 | |
2630 | |
2611 | 1 |
2631 | 1 |
2612 | } or do { |
2632 | } or do { |
2613 | warn $@; |
2633 | warn $@; |
… | |
… | |
2618 | warn "reloaded"; |
2638 | warn "reloaded"; |
2619 | }; |
2639 | }; |
2620 | |
2640 | |
2621 | our $RELOAD_WATCHER; # used only during reload |
2641 | our $RELOAD_WATCHER; # used only during reload |
2622 | |
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 | |
2623 | register_command "reload" => sub { |
2658 | register_command "reload" => sub { |
2624 | my ($who, $arg) = @_; |
2659 | my ($who, $arg) = @_; |
2625 | |
2660 | |
2626 | if ($who->flag (FLAG_WIZ)) { |
2661 | if ($who->flag (FLAG_WIZ)) { |
2627 | $who->message ("reloading server."); |
2662 | $who->message ("reloading server."); |
2628 | |
2663 | async { reload_perl }; |
2629 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2630 | # coro crashes during coro_state_free->destroy here. |
|
|
2631 | |
|
|
2632 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2633 | reentrant => 0, |
|
|
2634 | after => 0, |
|
|
2635 | data => WF_AUTOCANCEL, |
|
|
2636 | cb => sub { |
|
|
2637 | reload; |
|
|
2638 | undef $RELOAD_WATCHER; |
|
|
2639 | }, |
|
|
2640 | ); |
|
|
2641 | } |
2664 | } |
2642 | }; |
2665 | }; |
2643 | |
2666 | |
2644 | unshift @INC, $LIBDIR; |
2667 | unshift @INC, $LIBDIR; |
2645 | |
2668 | |
2646 | my $bug_warning = 0; |
2669 | my $bug_warning = 0; |
|
|
2670 | |
|
|
2671 | our @WAIT_FOR_TICK; |
|
|
2672 | our @WAIT_FOR_TICK_BEGIN; |
|
|
2673 | |
|
|
2674 | sub wait_for_tick { |
|
|
2675 | return unless $TICK_WATCHER->is_active; |
|
|
2676 | return if $Coro::current == $Coro::main; |
|
|
2677 | |
|
|
2678 | my $signal = new Coro::Signal; |
|
|
2679 | push @WAIT_FOR_TICK, $signal; |
|
|
2680 | $signal->wait; |
|
|
2681 | } |
|
|
2682 | |
|
|
2683 | sub wait_for_tick_begin { |
|
|
2684 | return unless $TICK_WATCHER->is_active; |
|
|
2685 | return if $Coro::current == $Coro::main; |
|
|
2686 | |
|
|
2687 | my $signal = new Coro::Signal; |
|
|
2688 | push @WAIT_FOR_TICK_BEGIN, $signal; |
|
|
2689 | $signal->wait; |
|
|
2690 | } |
2647 | |
2691 | |
2648 | $TICK_WATCHER = Event->timer ( |
2692 | $TICK_WATCHER = Event->timer ( |
2649 | reentrant => 0, |
2693 | reentrant => 0, |
2650 | parked => 1, |
2694 | parked => 1, |
2651 | prio => 0, |
2695 | prio => 0, |
… | |
… | |
2659 | } |
2703 | } |
2660 | |
2704 | |
2661 | $NOW = Event::time; |
2705 | $NOW = Event::time; |
2662 | |
2706 | |
2663 | cf::server_tick; # one server iteration |
2707 | cf::server_tick; # one server iteration |
|
|
2708 | |
2664 | $RUNTIME += $TICK; |
2709 | $RUNTIME += $TICK; |
2665 | $NEXT_TICK += $TICK; |
2710 | $NEXT_TICK += $TICK; |
2666 | |
2711 | |
2667 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2712 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2668 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
2713 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
… | |
… | |
2670 | write_runtime |
2715 | write_runtime |
2671 | or warn "ERROR: unable to write runtime file: $!"; |
2716 | or warn "ERROR: unable to write runtime file: $!"; |
2672 | }; |
2717 | }; |
2673 | } |
2718 | } |
2674 | |
2719 | |
2675 | $WAIT_FOR_TICK->broadcast; |
|
|
2676 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
|
|
2677 | |
|
|
2678 | # my $AFTER = Event::time; |
2720 | # my $AFTER = Event::time; |
2679 | # warn $AFTER - $NOW;#d# |
2721 | # warn $AFTER - $NOW;#d# |
2680 | |
2722 | |
2681 | # if we are delayed by four ticks or more, skip them all |
2723 | # if we are delayed by four ticks or more, skip them all |
2682 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2724 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2683 | |
2725 | |
2684 | $TICK_WATCHER->at ($NEXT_TICK); |
2726 | $TICK_WATCHER->at ($NEXT_TICK); |
2685 | $TICK_WATCHER->start; |
2727 | $TICK_WATCHER->start; |
|
|
2728 | |
|
|
2729 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
|
|
2730 | $sig->send; |
|
|
2731 | } |
|
|
2732 | while (my $sig = shift @WAIT_FOR_TICK) { |
|
|
2733 | $sig->send; |
|
|
2734 | } |
|
|
2735 | |
|
|
2736 | _post_tick; |
2686 | }, |
2737 | }, |
2687 | ); |
2738 | ); |
2688 | |
2739 | |
2689 | { |
2740 | { |
2690 | BDB::max_poll_time $TICK * 0.1; |
2741 | BDB::max_poll_time $TICK * 0.1; |