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.237 by root, Fri Apr 13 05:08:51 2007 UTC vs.
Revision 1.249 by root, Wed Apr 18 12:43:54 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;
76our %MAP; # all maps 76our %MAP; # all maps
77our $LINK_MAP; # the special {link} map, which is always available 77our $LINK_MAP; # the special {link} map, which is always available
78our $RANDOM_MAPS = cf::localdir . "/random"; 78our $RANDOM_MAPS = cf::localdir . "/random";
79our $BDB_ENV_DIR = cf::localdir . "/db"; 79our $BDB_ENV_DIR = cf::localdir . "/db";
80 80
81our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal;
82our $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 ∕
85our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons 82our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons
86 83
87binmode STDOUT; 84binmode STDOUT;
88binmode STDERR; 85binmode STDERR;
136=item %cf::CFG 133=item %cf::CFG
137 134
138Configuration for the server, loaded from C</etc/crossfire/config>, or 135Configuration for the server, loaded from C</etc/crossfire/config>, or
139from wherever your confdir points to. 136from 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
143These are Coro::Signal objects that are C<< ->broadcast >> (WAIT_FOR_TICK) 140These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only
144or C<< ->send >> (WAIT_FOR_TICK_ONE) on after normal server tick 141returns directly I<after> the tick processing (and consequently, can only wake one process
145processing has been done. Call C<< ->wait >> on them to maximise the 142per tick), while cf::wait_for_tick wakes up all waiters after tick processing.
146window 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
1159 my ($self, $type, %msg) = @_; 1155 my ($self, $type, %msg) = @_;
1160 1156
1161 $self->ns->ext_event ($type, %msg); 1157 $self->ns->ext_event ($type, %msg);
1162} 1158}
1163 1159
1164package cf; 1160=head3 cf::region
1161
1162=over 4
1163
1164=cut
1165
1166package cf::region;
1167
1168=item cf::region::find_by_path $path
1169
1170Tries to decuce the probable region for a map knowing only its path.
1171
1172=cut
1173
1174sub find_by_path($) {
1175 my ($path) = @_;
1176
1177 my ($match, $specificity);
1178
1179 for my $region (list) {
1180 if ($region->match && $path =~ $region->match) {
1181 ($match, $specificity) = ($region, $region->specificity)
1182 if $region->specificity > $specificity;
1183 }
1184 }
1185
1186 $match
1187}
1165 1188
1166=back 1189=back
1167
1168 1190
1169=head3 cf::map 1191=head3 cf::map
1170 1192
1171=over 4 1193=over 4
1172 1194
1403 $self->load_header_orig 1425 $self->load_header_orig
1404 or return; 1426 or return;
1405 $self->prepare_orig; 1427 $self->prepare_orig;
1406 } 1428 }
1407 1429
1430 $self->default_region (cf::region::find_by_path $self->{path})
1431 unless $self->default_region;
1432
1408 1 1433 1
1409} 1434}
1410 1435
1411sub find; 1436sub find;
1412sub find { 1437sub find {
1454 $self->in_memory (cf::MAP_LOADING); 1479 $self->in_memory (cf::MAP_LOADING);
1455 1480
1456 $self->alloc; 1481 $self->alloc;
1457 1482
1458 $self->pre_load; 1483 $self->pre_load;
1484 Coro::cede;
1459 1485
1460 $self->_load_objects ($self->{load_path}, 1) 1486 $self->_load_objects ($self->{load_path}, 1)
1461 or return; 1487 or return;
1462 1488
1463 $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) 1489 $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1)
1470 $self->_load_objects ($uniq, 0); 1496 $self->_load_objects ($uniq, 0);
1471 } 1497 }
1472 } 1498 }
1473 1499
1474 Coro::cede; 1500 Coro::cede;
1475
1476 # now do the right thing for maps 1501 # now do the right thing for maps
1477 $self->link_multipart_objects; 1502 $self->link_multipart_objects;
1503 Coro::cede;
1478 1504
1479 unless ($self->{deny_activate}) { 1505 unless ($self->{deny_activate}) {
1480 $self->decay_objects; 1506 $self->decay_objects;
1481 $self->fix_auto_apply; 1507 $self->fix_auto_apply;
1482 $self->update_buttons; 1508 $self->update_buttons;
1484 $self->set_darkness_map; 1510 $self->set_darkness_map;
1485 $self->difficulty ($self->estimate_difficulty) 1511 $self->difficulty ($self->estimate_difficulty)
1486 unless $self->difficulty; 1512 unless $self->difficulty;
1487 Coro::cede; 1513 Coro::cede;
1488 $self->activate; 1514 $self->activate;
1515 Coro::cede;
1489 } 1516 }
1490 1517
1491 $self->post_load; 1518 $self->post_load;
1519 Coro::cede;
1492 1520
1493 $self->in_memory (cf::MAP_IN_MEMORY); 1521 $self->in_memory (cf::MAP_IN_MEMORY);
1494} 1522}
1495 1523
1496sub customise_for { 1524sub customise_for {
2301sub db_put($$$) { 2329sub db_put($$$) {
2302 BDB::dbreq_pri 4; 2330 BDB::dbreq_pri 4;
2303 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 { };
2304} 2332}
2305 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
2306############################################################################# 2373#############################################################################
2307# the server's init and main functions 2374# the server's init and main functions
2308 2375
2309sub load_facedata { 2376sub load_facedata($) {
2310 my $path = sprintf "%s/facedata", cf::datadir; 2377 my ($path) = @_;
2311 2378
2312 warn "loading facedata from $path\n"; 2379 warn "loading facedata from $path\n";
2313 2380
2314 my $facedata; 2381 my $facedata;
2315 0 < aio_load $path, $facedata 2382 0 < aio_load $path, $facedata
2356 } 2423 }
2357 2424
2358 1 2425 1
2359} 2426}
2360 2427
2428sub reload_facedata {
2429 load_facedata sprintf "%s/facedata", cf::datadir
2430 or die "unable to load facedata\n";
2431}
2432
2433sub reload_regions {
2434 load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir
2435 or die "unable to load regions file\n";
2436}
2437
2438sub reload_archetypes {
2439 load_resource_file sprintf "%s/archetypes", cf::datadir
2440 or die "unable to load archetypes\n";
2441}
2442
2443sub reload_treasures {
2444 load_resource_file sprintf "%s/treasures", cf::datadir
2445 or die "unable to load treasurelists\n";
2446}
2447
2361sub reload_resources { 2448sub reload_resources {
2362 load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir 2449 warn "reloading resource files...\n";
2363 or die "unable to load regions file\n";#d# 2450
2451 reload_regions;
2364 load_facedata 2452 reload_facedata;
2365 or die "unable to load facedata\n";#d# 2453 reload_archetypes;
2454 reload_treasures;
2455
2456 warn "finished reloading resource files\n";
2366} 2457}
2367 2458
2368sub init { 2459sub init {
2369 reload_resources; 2460 reload_resources;
2370} 2461}
2463 2554
2464 warn Carp::longmess "post_cleanup backtrace" 2555 warn Carp::longmess "post_cleanup backtrace"
2465 if $make_core; 2556 if $make_core;
2466} 2557}
2467 2558
2468sub reload() { 2559sub do_reload_perl() {
2469 # can/must only be called in main 2560 # can/must only be called in main
2470 if ($Coro::current != $Coro::main) { 2561 if ($Coro::current != $Coro::main) {
2471 warn "can only reload from main coroutine"; 2562 warn "can only reload from main coroutine";
2472 return; 2563 return;
2473 } 2564 }
2572 warn "reattaching attachments to maps"; 2663 warn "reattaching attachments to maps";
2573 reattach $_ for values %MAP; 2664 reattach $_ for values %MAP;
2574 warn "reattaching attachments to players"; 2665 warn "reattaching attachments to players";
2575 reattach $_ for values %PLAYER; 2666 reattach $_ for values %PLAYER;
2576 2667
2577 warn "loading reloadable resources";
2578 reload_resources;
2579
2580 warn "leaving sync_job"; 2668 warn "leaving sync_job";
2581 2669
2582 1 2670 1
2583 } or do { 2671 } or do {
2584 warn $@; 2672 warn $@;
2589 warn "reloaded"; 2677 warn "reloaded";
2590}; 2678};
2591 2679
2592our $RELOAD_WATCHER; # used only during reload 2680our $RELOAD_WATCHER; # used only during reload
2593 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
2594register_command "reload" => sub { 2697register_command "reload" => sub {
2595 my ($who, $arg) = @_; 2698 my ($who, $arg) = @_;
2596 2699
2597 if ($who->flag (FLAG_WIZ)) { 2700 if ($who->flag (FLAG_WIZ)) {
2598 $who->message ("reloading server."); 2701 $who->message ("reloading server.");
2599 2702 async { reload_perl };
2600 # doing reload synchronously and two reloads happen back-to-back,
2601 # coro crashes during coro_state_free->destroy here.
2602
2603 $RELOAD_WATCHER ||= Event->timer (
2604 reentrant => 0,
2605 after => 0,
2606 data => WF_AUTOCANCEL,
2607 cb => sub {
2608 reload;
2609 undef $RELOAD_WATCHER;
2610 },
2611 );
2612 } 2703 }
2613}; 2704};
2614 2705
2615unshift @INC, $LIBDIR; 2706unshift @INC, $LIBDIR;
2616 2707
2617my $bug_warning = 0; 2708my $bug_warning = 0;
2709
2710our @WAIT_FOR_TICK;
2711our @WAIT_FOR_TICK_BEGIN;
2712
2713sub wait_for_tick {
2714 return unless $TICK_WATCHER->is_active;
2715 return if $Coro::current == $Coro::main;
2716
2717 my $signal = new Coro::Signal;
2718 push @WAIT_FOR_TICK, $signal;
2719 $signal->wait;
2720}
2721
2722sub wait_for_tick_begin {
2723 return unless $TICK_WATCHER->is_active;
2724 return if $Coro::current == $Coro::main;
2725
2726 my $signal = new Coro::Signal;
2727 push @WAIT_FOR_TICK_BEGIN, $signal;
2728 $signal->wait;
2729}
2618 2730
2619$TICK_WATCHER = Event->timer ( 2731$TICK_WATCHER = Event->timer (
2620 reentrant => 0, 2732 reentrant => 0,
2621 parked => 1, 2733 parked => 1,
2622 prio => 0, 2734 prio => 0,
2630 } 2742 }
2631 2743
2632 $NOW = Event::time; 2744 $NOW = Event::time;
2633 2745
2634 cf::server_tick; # one server iteration 2746 cf::server_tick; # one server iteration
2747
2635 $RUNTIME += $TICK; 2748 $RUNTIME += $TICK;
2636 $NEXT_TICK += $TICK; 2749 $NEXT_TICK += $TICK;
2637 2750
2638 if ($NOW >= $NEXT_RUNTIME_WRITE) { 2751 if ($NOW >= $NEXT_RUNTIME_WRITE) {
2639 $NEXT_RUNTIME_WRITE = $NOW + 10; 2752 $NEXT_RUNTIME_WRITE = $NOW + 10;
2641 write_runtime 2754 write_runtime
2642 or warn "ERROR: unable to write runtime file: $!"; 2755 or warn "ERROR: unable to write runtime file: $!";
2643 }; 2756 };
2644 } 2757 }
2645 2758
2646 $WAIT_FOR_TICK->broadcast;
2647 $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited;
2648
2649# my $AFTER = Event::time; 2759# my $AFTER = Event::time;
2650# warn $AFTER - $NOW;#d# 2760# warn $AFTER - $NOW;#d#
2651 2761
2652 # if we are delayed by four ticks or more, skip them all 2762 # if we are delayed by four ticks or more, skip them all
2653 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; 2763 $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4;
2654 2764
2655 $TICK_WATCHER->at ($NEXT_TICK); 2765 $TICK_WATCHER->at ($NEXT_TICK);
2656 $TICK_WATCHER->start; 2766 $TICK_WATCHER->start;
2767
2768 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
2769 $sig->send;
2770 }
2771 while (my $sig = shift @WAIT_FOR_TICK) {
2772 $sig->send;
2773 }
2774
2775 _post_tick;
2657 }, 2776 },
2658); 2777);
2659 2778
2660{ 2779{
2661 BDB::max_poll_time $TICK * 0.1; 2780 BDB::max_poll_time $TICK * 0.1;
2720 prio => 6, 2839 prio => 6,
2721 cb => \&IO::AIO::poll_cb, 2840 cb => \&IO::AIO::poll_cb,
2722 ); 2841 );
2723} 2842}
2724 2843
2844# load additional modules
2845use cf::pod;
2846
2725END { cf::emergency_save } 2847END { cf::emergency_save }
2726 2848
27271 28491
2728 2850

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines