… | |
… | |
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; |
… | |
… | |
2329 | sub db_put($$$) { |
2329 | sub db_put($$$) { |
2330 | BDB::dbreq_pri 4; |
2330 | BDB::dbreq_pri 4; |
2331 | BDB::db_put $DB, undef, "$_[0]/$_[1]", Compress::LZF::sfreeze_cr $_[2], 0, sub { }; |
2331 | BDB::db_put $DB, undef, "$_[0]/$_[1]", Compress::LZF::sfreeze_cr $_[2], 0, sub { }; |
2332 | } |
2332 | } |
2333 | |
2333 | |
|
|
2334 | =item cf::cache $id => ... |
|
|
2335 | |
|
|
2336 | Generic caching function that returns the value of the resource $id, |
|
|
2337 | caching and regenerating as required. |
|
|
2338 | |
|
|
2339 | This function can block. |
|
|
2340 | |
|
|
2341 | source => filename returning the data (must be a scalar) |
|
|
2342 | expensive => true == try to cache harder |
|
|
2343 | filter => sub that processes the data into a scalar |
|
|
2344 | |
|
|
2345 | =cut |
|
|
2346 | |
|
|
2347 | sub cache { |
|
|
2348 | my ($id, %arg) = @_; |
|
|
2349 | |
|
|
2350 | aio_stat $arg{source} |
|
|
2351 | and Carp::croak "$arg{source}: $!"; |
|
|
2352 | |
|
|
2353 | my $meta = join ":", (stat _)[7,9]; |
|
|
2354 | my $md5; |
|
|
2355 | |
|
|
2356 | if ($arg{expensive}) { |
|
|
2357 | 0 <= aio_load $arg{source}, my $buf |
|
|
2358 | or Carp::croak "$arg{source}: $!"; |
|
|
2359 | |
|
|
2360 | $md5 = Digest::MD5::md5_hex $buf; |
|
|
2361 | } |
|
|
2362 | |
|
|
2363 | my $dbmeta = db_get "$id/meta"; |
|
|
2364 | if ($dbmeta ne $meta) { |
|
|
2365 | # changed, we need to process |
|
|
2366 | } else { |
|
|
2367 | # just fetch |
|
|
2368 | } |
|
|
2369 | |
|
|
2370 | |
|
|
2371 | } |
|
|
2372 | |
2334 | ############################################################################# |
2373 | ############################################################################# |
2335 | # the server's init and main functions |
2374 | # the server's init and main functions |
2336 | |
2375 | |
2337 | sub load_facedata { |
2376 | sub load_facedata($) { |
2338 | my $path = sprintf "%s/facedata", cf::datadir; |
2377 | my ($path) = @_; |
2339 | |
2378 | |
2340 | warn "loading facedata from $path\n"; |
2379 | warn "loading facedata from $path\n"; |
2341 | |
2380 | |
2342 | my $facedata; |
2381 | my $facedata; |
2343 | 0 < aio_load $path, $facedata |
2382 | 0 < aio_load $path, $facedata |
… | |
… | |
2384 | } |
2423 | } |
2385 | |
2424 | |
2386 | 1 |
2425 | 1 |
2387 | } |
2426 | } |
2388 | |
2427 | |
2389 | our $archetypes_loaded; |
2428 | 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 |
2429 | load_facedata sprintf "%s/facedata", cf::datadir |
|
|
2430 | or die "unable to load facedata\n"; |
2394 | } |
2431 | } |
2395 | |
2432 | |
2396 | sub load_treasures { |
|
|
2397 | load_treasure_file sprintf "%s/treasures", cf::datadir |
|
|
2398 | } |
|
|
2399 | |
|
|
2400 | sub reload_resources { |
2433 | sub reload_regions { |
2401 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2434 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2402 | or die "unable to load regions file\n"; |
2435 | or die "unable to load regions file\n"; |
2403 | load_facedata |
2436 | } |
2404 | or die "unable to load facedata\n"; |
2437 | |
2405 | load_archetypes |
2438 | sub reload_archetypes { |
|
|
2439 | load_resource_file sprintf "%s/archetypes", cf::datadir |
2406 | or die "unable to load archetypes\n"; |
2440 | or die "unable to load archetypes\n"; |
2407 | load_treasures |
2441 | } |
|
|
2442 | |
|
|
2443 | sub reload_treasures { |
|
|
2444 | load_resource_file sprintf "%s/treasures", cf::datadir |
2408 | or die "unable to load treasurelists\n"; |
2445 | or die "unable to load treasurelists\n"; |
|
|
2446 | } |
|
|
2447 | |
|
|
2448 | sub reload_resources { |
|
|
2449 | warn "reloading resource files...\n"; |
|
|
2450 | |
|
|
2451 | reload_regions; |
|
|
2452 | reload_facedata; |
|
|
2453 | reload_archetypes; |
|
|
2454 | reload_treasures; |
|
|
2455 | |
|
|
2456 | warn "finished reloading resource files\n"; |
2409 | } |
2457 | } |
2410 | |
2458 | |
2411 | sub init { |
2459 | sub init { |
2412 | reload_resources; |
2460 | reload_resources; |
2413 | } |
2461 | } |
… | |
… | |
2506 | |
2554 | |
2507 | warn Carp::longmess "post_cleanup backtrace" |
2555 | warn Carp::longmess "post_cleanup backtrace" |
2508 | if $make_core; |
2556 | if $make_core; |
2509 | } |
2557 | } |
2510 | |
2558 | |
2511 | sub reload() { |
2559 | sub do_reload_perl() { |
2512 | # can/must only be called in main |
2560 | # can/must only be called in main |
2513 | if ($Coro::current != $Coro::main) { |
2561 | if ($Coro::current != $Coro::main) { |
2514 | warn "can only reload from main coroutine"; |
2562 | warn "can only reload from main coroutine"; |
2515 | return; |
2563 | return; |
2516 | } |
2564 | } |
… | |
… | |
2615 | warn "reattaching attachments to maps"; |
2663 | warn "reattaching attachments to maps"; |
2616 | reattach $_ for values %MAP; |
2664 | reattach $_ for values %MAP; |
2617 | warn "reattaching attachments to players"; |
2665 | warn "reattaching attachments to players"; |
2618 | reattach $_ for values %PLAYER; |
2666 | reattach $_ for values %PLAYER; |
2619 | |
2667 | |
2620 | warn "loading reloadable resources"; |
|
|
2621 | reload_resources; |
|
|
2622 | |
|
|
2623 | warn "leaving sync_job"; |
2668 | warn "leaving sync_job"; |
2624 | |
2669 | |
2625 | 1 |
2670 | 1 |
2626 | } or do { |
2671 | } or do { |
2627 | warn $@; |
2672 | warn $@; |
… | |
… | |
2632 | warn "reloaded"; |
2677 | warn "reloaded"; |
2633 | }; |
2678 | }; |
2634 | |
2679 | |
2635 | our $RELOAD_WATCHER; # used only during reload |
2680 | our $RELOAD_WATCHER; # used only during reload |
2636 | |
2681 | |
|
|
2682 | sub reload_perl() { |
|
|
2683 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2684 | # coro crashes during coro_state_free->destroy here. |
|
|
2685 | |
|
|
2686 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2687 | reentrant => 0, |
|
|
2688 | after => 0, |
|
|
2689 | data => WF_AUTOCANCEL, |
|
|
2690 | cb => sub { |
|
|
2691 | do_reload_perl; |
|
|
2692 | undef $RELOAD_WATCHER; |
|
|
2693 | }, |
|
|
2694 | ); |
|
|
2695 | } |
|
|
2696 | |
2637 | register_command "reload" => sub { |
2697 | register_command "reload" => sub { |
2638 | my ($who, $arg) = @_; |
2698 | my ($who, $arg) = @_; |
2639 | |
2699 | |
2640 | if ($who->flag (FLAG_WIZ)) { |
2700 | if ($who->flag (FLAG_WIZ)) { |
2641 | $who->message ("reloading server."); |
2701 | $who->message ("reloading server."); |
2642 | |
2702 | 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 | } |
2703 | } |
2656 | }; |
2704 | }; |
2657 | |
2705 | |
2658 | unshift @INC, $LIBDIR; |
2706 | unshift @INC, $LIBDIR; |
2659 | |
2707 | |
… | |
… | |
2694 | } |
2742 | } |
2695 | |
2743 | |
2696 | $NOW = Event::time; |
2744 | $NOW = Event::time; |
2697 | |
2745 | |
2698 | cf::server_tick; # one server iteration |
2746 | cf::server_tick; # one server iteration |
|
|
2747 | |
2699 | $RUNTIME += $TICK; |
2748 | $RUNTIME += $TICK; |
2700 | $NEXT_TICK += $TICK; |
2749 | $NEXT_TICK += $TICK; |
2701 | |
2750 | |
2702 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2751 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2703 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
2752 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
… | |
… | |
2705 | write_runtime |
2754 | write_runtime |
2706 | or warn "ERROR: unable to write runtime file: $!"; |
2755 | or warn "ERROR: unable to write runtime file: $!"; |
2707 | }; |
2756 | }; |
2708 | } |
2757 | } |
2709 | |
2758 | |
|
|
2759 | # my $AFTER = Event::time; |
|
|
2760 | # warn $AFTER - $NOW;#d# |
|
|
2761 | |
|
|
2762 | # if we are delayed by four ticks or more, skip them all |
|
|
2763 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
|
|
2764 | |
|
|
2765 | $TICK_WATCHER->at ($NEXT_TICK); |
|
|
2766 | $TICK_WATCHER->start; |
|
|
2767 | |
2710 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
2768 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
2711 | $sig->send; |
2769 | $sig->send; |
2712 | } |
2770 | } |
2713 | while (my $sig = shift @WAIT_FOR_TICK) { |
2771 | while (my $sig = shift @WAIT_FOR_TICK) { |
2714 | $sig->send; |
2772 | $sig->send; |
2715 | } |
2773 | } |
2716 | |
2774 | |
2717 | # my $AFTER = Event::time; |
2775 | _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 | }, |
2776 | }, |
2726 | ); |
2777 | ); |
2727 | |
2778 | |
2728 | { |
2779 | { |
2729 | BDB::max_poll_time $TICK * 0.1; |
2780 | BDB::max_poll_time $TICK * 0.1; |
… | |
… | |
2788 | prio => 6, |
2839 | prio => 6, |
2789 | cb => \&IO::AIO::poll_cb, |
2840 | cb => \&IO::AIO::poll_cb, |
2790 | ); |
2841 | ); |
2791 | } |
2842 | } |
2792 | |
2843 | |
|
|
2844 | # load additional modules |
|
|
2845 | use cf::pod; |
|
|
2846 | |
2793 | END { cf::emergency_save } |
2847 | END { cf::emergency_save } |
2794 | |
2848 | |
2795 | 1 |
2849 | 1 |
2796 | |
2850 | |