ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.244 by root, Mon Apr 16 13:28:00 2007 UTC vs.
Revision 1.249 by root, Wed Apr 18 12:43:54 2007 UTC

54 54
55our $RELOAD; # number of reloads so far 55our $RELOAD; # number of reloads so far
56our @EVENT; 56our @EVENT;
57our $LIBDIR = datadir . "/ext"; 57our $LIBDIR = datadir . "/ext";
58 58
59our $TICK = MAX_TIME * 1e-6; 59our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!)
60our $TICK_WATCHER; 60our $TICK_WATCHER;
61our $AIO_POLL_WATCHER; 61our $AIO_POLL_WATCHER;
62our $NEXT_RUNTIME_WRITE; # when should the runtime file be written 62our $NEXT_RUNTIME_WRITE; # when should the runtime file be written
63our $NEXT_TICK; 63our $NEXT_TICK;
64our $NOW; 64our $NOW;
2329sub db_put($$$) { 2329sub 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
2336Generic caching function that returns the value of the resource $id,
2337caching and regenerating as required.
2338
2339This 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
2347sub 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
2337sub load_facedata { 2376sub 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
2389sub load_archetypes { 2428sub reload_facedata {
2390 load_archetype_file sprintf "%s/archetypes", cf::datadir; # remove when stable
2391 load_archetype_file sprintf "%s/archetypes", cf::datadir 2429 load_facedata sprintf "%s/facedata", cf::datadir
2430 or die "unable to load facedata\n";
2392} 2431}
2393 2432
2394sub load_treasures {
2395 load_treasure_file sprintf "%s/treasures", cf::datadir
2396}
2397
2398sub reload_resources { 2433sub reload_regions {
2399 load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir 2434 load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir
2400 or die "unable to load regions file\n"; 2435 or die "unable to load regions file\n";
2401 load_facedata 2436}
2402 or die "unable to load facedata\n"; 2437
2403 load_archetypes 2438sub reload_archetypes {
2439 load_resource_file sprintf "%s/archetypes", cf::datadir
2404 or die "unable to load archetypes\n"; 2440 or die "unable to load archetypes\n";
2405 load_treasures 2441}
2442
2443sub reload_treasures {
2444 load_resource_file sprintf "%s/treasures", cf::datadir
2406 or die "unable to load treasurelists\n"; 2445 or die "unable to load treasurelists\n";
2446}
2447
2448sub 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";
2407} 2457}
2408 2458
2409sub init { 2459sub init {
2410 reload_resources; 2460 reload_resources;
2411} 2461}
2504 2554
2505 warn Carp::longmess "post_cleanup backtrace" 2555 warn Carp::longmess "post_cleanup backtrace"
2506 if $make_core; 2556 if $make_core;
2507} 2557}
2508 2558
2509sub reload() { 2559sub do_reload_perl() {
2510 # can/must only be called in main 2560 # can/must only be called in main
2511 if ($Coro::current != $Coro::main) { 2561 if ($Coro::current != $Coro::main) {
2512 warn "can only reload from main coroutine"; 2562 warn "can only reload from main coroutine";
2513 return; 2563 return;
2514 } 2564 }
2613 warn "reattaching attachments to maps"; 2663 warn "reattaching attachments to maps";
2614 reattach $_ for values %MAP; 2664 reattach $_ for values %MAP;
2615 warn "reattaching attachments to players"; 2665 warn "reattaching attachments to players";
2616 reattach $_ for values %PLAYER; 2666 reattach $_ for values %PLAYER;
2617 2667
2618 warn "loading reloadable resources";
2619 reload_resources;
2620
2621 warn "leaving sync_job"; 2668 warn "leaving sync_job";
2622 2669
2623 1 2670 1
2624 } or do { 2671 } or do {
2625 warn $@; 2672 warn $@;
2630 warn "reloaded"; 2677 warn "reloaded";
2631}; 2678};
2632 2679
2633our $RELOAD_WATCHER; # used only during reload 2680our $RELOAD_WATCHER; # used only during reload
2634 2681
2682sub 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
2635register_command "reload" => sub { 2697register_command "reload" => sub {
2636 my ($who, $arg) = @_; 2698 my ($who, $arg) = @_;
2637 2699
2638 if ($who->flag (FLAG_WIZ)) { 2700 if ($who->flag (FLAG_WIZ)) {
2639 $who->message ("reloading server."); 2701 $who->message ("reloading server.");
2640 2702 async { reload_perl };
2641 # doing reload synchronously and two reloads happen back-to-back,
2642 # coro crashes during coro_state_free->destroy here.
2643
2644 $RELOAD_WATCHER ||= Event->timer (
2645 reentrant => 0,
2646 after => 0,
2647 data => WF_AUTOCANCEL,
2648 cb => sub {
2649 reload;
2650 undef $RELOAD_WATCHER;
2651 },
2652 );
2653 } 2703 }
2654}; 2704};
2655 2705
2656unshift @INC, $LIBDIR; 2706unshift @INC, $LIBDIR;
2657 2707
2692 } 2742 }
2693 2743
2694 $NOW = Event::time; 2744 $NOW = Event::time;
2695 2745
2696 cf::server_tick; # one server iteration 2746 cf::server_tick; # one server iteration
2747
2697 $RUNTIME += $TICK; 2748 $RUNTIME += $TICK;
2698 $NEXT_TICK += $TICK; 2749 $NEXT_TICK += $TICK;
2699 2750
2700 if ($NOW >= $NEXT_RUNTIME_WRITE) { 2751 if ($NOW >= $NEXT_RUNTIME_WRITE) {
2701 $NEXT_RUNTIME_WRITE = $NOW + 10; 2752 $NEXT_RUNTIME_WRITE = $NOW + 10;
2703 write_runtime 2754 write_runtime
2704 or warn "ERROR: unable to write runtime file: $!"; 2755 or warn "ERROR: unable to write runtime file: $!";
2705 }; 2756 };
2706 } 2757 }
2707 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
2708 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { 2768 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
2709 $sig->send; 2769 $sig->send;
2710 } 2770 }
2711 while (my $sig = shift @WAIT_FOR_TICK) { 2771 while (my $sig = shift @WAIT_FOR_TICK) {
2712 $sig->send; 2772 $sig->send;
2713 } 2773 }
2714 2774
2715# my $AFTER = Event::time; 2775 _post_tick;
2716# warn $AFTER - $NOW;#d#
2717
2718 # if we are delayed by four ticks or more, skip them all
2719 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4;
2720
2721 $TICK_WATCHER->at ($NEXT_TICK);
2722 $TICK_WATCHER->start;
2723 }, 2776 },
2724); 2777);
2725 2778
2726{ 2779{
2727 BDB::max_poll_time $TICK * 0.1; 2780 BDB::max_poll_time $TICK * 0.1;
2786 prio => 6, 2839 prio => 6,
2787 cb => \&IO::AIO::poll_cb, 2840 cb => \&IO::AIO::poll_cb,
2788 ); 2841 );
2789} 2842}
2790 2843
2844# load additional modules
2845use cf::pod;
2846
2791END { cf::emergency_save } 2847END { cf::emergency_save }
2792 2848
27931 28491
2794 2850

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines