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.242 by root, Mon Apr 16 12:37:08 2007 UTC vs.
Revision 1.246 by root, Tue Apr 17 10:38:28 2007 UTC

9use Event; 9use Event;
10use Opcode; 10use Opcode;
11use Safe; 11use Safe;
12use Safe::Hole; 12use Safe::Hole;
13 13
14use Coro 3.52 (); 14use Coro 3.61 ();
15use Coro::State; 15use Coro::State;
16use Coro::Event; 16use Coro::Event;
17use Coro::Timer; 17use Coro::Timer;
18use Coro::Signal; 18use Coro::Signal;
19use Coro::Semaphore; 19use Coro::Semaphore;
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;
2332} 2332}
2333 2333
2334############################################################################# 2334#############################################################################
2335# the server's init and main functions 2335# the server's init and main functions
2336 2336
2337sub load_facedata { 2337sub 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
2389our $archetypes_loaded; 2389sub reload_facedata {
2390
2391sub load_archetypes {
2392 return 1 if $archetypes_loaded++; # current can only load them once
2393# load_archetype_file sprintf "%s/archetypes", cf::datadir;
2394 load_archetype_file sprintf "%s/archetypes", cf::datadir 2390 load_facedata sprintf "%s/facedata", cf::datadir
2391 or die "unable to load facedata\n";
2395} 2392}
2396 2393
2397sub load_treasures {
2398 load_treasure_file sprintf "%s/treasures", cf::datadir
2399}
2400
2401sub reload_resources { 2394sub reload_regions {
2402 load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir 2395 load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir
2403 or die "unable to load regions file\n"; 2396 or die "unable to load regions file\n";
2404 load_facedata 2397}
2405 or die "unable to load facedata\n"; 2398
2406 load_archetypes 2399sub reload_archetypes {
2400 load_archetype_file sprintf "%s/archetypes", cf::datadir
2407 or die "unable to load archetypes\n"; 2401 or die "unable to load archetypes\n";
2408 load_treasures 2402}
2403
2404sub reload_treasures {
2405 load_treasure_file sprintf "%s/treasures", cf::datadir
2409 or die "unable to load treasurelists\n"; 2406 or die "unable to load treasurelists\n";
2407}
2408
2409sub 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";
2410} 2418}
2411 2419
2412sub init { 2420sub init {
2413 reload_resources; 2421 reload_resources;
2414} 2422}
2507 2515
2508 warn Carp::longmess "post_cleanup backtrace" 2516 warn Carp::longmess "post_cleanup backtrace"
2509 if $make_core; 2517 if $make_core;
2510} 2518}
2511 2519
2512sub reload() { 2520sub do_reload_perl() {
2513 # can/must only be called in main 2521 # can/must only be called in main
2514 if ($Coro::current != $Coro::main) { 2522 if ($Coro::current != $Coro::main) {
2515 warn "can only reload from main coroutine"; 2523 warn "can only reload from main coroutine";
2516 return; 2524 return;
2517 } 2525 }
2616 warn "reattaching attachments to maps"; 2624 warn "reattaching attachments to maps";
2617 reattach $_ for values %MAP; 2625 reattach $_ for values %MAP;
2618 warn "reattaching attachments to players"; 2626 warn "reattaching attachments to players";
2619 reattach $_ for values %PLAYER; 2627 reattach $_ for values %PLAYER;
2620 2628
2621 warn "loading reloadable resources";
2622 reload_resources;
2623
2624 warn "leaving sync_job"; 2629 warn "leaving sync_job";
2625 2630
2626 1 2631 1
2627 } or do { 2632 } or do {
2628 warn $@; 2633 warn $@;
2633 warn "reloaded"; 2638 warn "reloaded";
2634}; 2639};
2635 2640
2636our $RELOAD_WATCHER; # used only during reload 2641our $RELOAD_WATCHER; # used only during reload
2637 2642
2643sub 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
2638register_command "reload" => sub { 2658register_command "reload" => sub {
2639 my ($who, $arg) = @_; 2659 my ($who, $arg) = @_;
2640 2660
2641 if ($who->flag (FLAG_WIZ)) { 2661 if ($who->flag (FLAG_WIZ)) {
2642 $who->message ("reloading server."); 2662 $who->message ("reloading server.");
2643 2663 async { 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 reload;
2653 undef $RELOAD_WATCHER;
2654 },
2655 );
2656 } 2664 }
2657}; 2665};
2658 2666
2659unshift @INC, $LIBDIR; 2667unshift @INC, $LIBDIR;
2660 2668
2695 } 2703 }
2696 2704
2697 $NOW = Event::time; 2705 $NOW = Event::time;
2698 2706
2699 cf::server_tick; # one server iteration 2707 cf::server_tick; # one server iteration
2708
2700 $RUNTIME += $TICK; 2709 $RUNTIME += $TICK;
2701 $NEXT_TICK += $TICK; 2710 $NEXT_TICK += $TICK;
2702 2711
2703 if ($NOW >= $NEXT_RUNTIME_WRITE) { 2712 if ($NOW >= $NEXT_RUNTIME_WRITE) {
2704 $NEXT_RUNTIME_WRITE = $NOW + 10; 2713 $NEXT_RUNTIME_WRITE = $NOW + 10;
2706 write_runtime 2715 write_runtime
2707 or warn "ERROR: unable to write runtime file: $!"; 2716 or warn "ERROR: unable to write runtime file: $!";
2708 }; 2717 };
2709 } 2718 }
2710 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
2711 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { 2729 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
2712 $sig->send; 2730 $sig->send;
2713 } 2731 }
2714 while (my $sig = shift @WAIT_FOR_TICK) { 2732 while (my $sig = shift @WAIT_FOR_TICK) {
2715 $sig->send; 2733 $sig->send;
2716 } 2734 }
2717 2735
2718# my $AFTER = Event::time; 2736 _post_tick;
2719# warn $AFTER - $NOW;#d#
2720
2721 # if we are delayed by four ticks or more, skip them all
2722 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4;
2723
2724 $TICK_WATCHER->at ($NEXT_TICK);
2725 $TICK_WATCHER->start;
2726 }, 2737 },
2727); 2738);
2728 2739
2729{ 2740{
2730 BDB::max_poll_time $TICK * 0.1; 2741 BDB::max_poll_time $TICK * 0.1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines