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.412 by root, Wed Apr 2 11:13:55 2008 UTC vs.
Revision 1.424 by root, Sun Apr 20 05:24:55 2008 UTC

25use strict; 25use strict;
26 26
27use Symbol; 27use Symbol;
28use List::Util; 28use List::Util;
29use Socket; 29use Socket;
30use EV 1.86; 30use EV 3.2;
31use Opcode; 31use Opcode;
32use Safe; 32use Safe;
33use Safe::Hole; 33use Safe::Hole;
34use Storable (); 34use Storable ();
35 35
36use Coro 4.32 (); 36use Coro 4.50 ();
37use Coro::State; 37use Coro::State;
38use Coro::Handle; 38use Coro::Handle;
39use Coro::EV; 39use Coro::EV;
40use Coro::Timer; 40use Coro::Timer;
41use Coro::Signal; 41use Coro::Signal;
427 } 427 }
428 } 428 }
429}; 429};
430 430
431sub get_slot($;$$) { 431sub get_slot($;$$) {
432 return if tick_inhibit || $Coro::current == $Coro::main;
433
432 my ($time, $pri, $name) = @_; 434 my ($time, $pri, $name) = @_;
433 435
434 $time = $TICK * .6 if $time > $TICK * .6; 436 $time = $TICK * .6 if $time > $TICK * .6;
435 my $sig = new Coro::Signal; 437 my $sig = new Coro::Signal;
436 438
547 reset_signals; 549 reset_signals;
548 &$cb 550 &$cb
549 }, @args; 551 }, @args;
550 552
551 wantarray ? @res : $res[-1] 553 wantarray ? @res : $res[-1]
554}
555
556=item $coin = coin_from_name $name
557
558=cut
559
560our %coin_alias = (
561 "silver" => "silvercoin",
562 "silvercoin" => "silvercoin",
563 "silvercoins" => "silvercoin",
564 "gold" => "goldcoin",
565 "goldcoin" => "goldcoin",
566 "goldcoins" => "goldcoin",
567 "platinum" => "platinacoin",
568 "platinumcoin" => "platinacoin",
569 "platinumcoins" => "platinacoin",
570 "platina" => "platinacoin",
571 "platinacoin" => "platinacoin",
572 "platinacoins" => "platinacoin",
573 "royalty" => "royalty",
574 "royalties" => "royalty",
575);
576
577sub coin_from_name($) {
578 $coin_alias{$_[0]}
579 ? cf::arch::find $coin_alias{$_[0]}
580 : undef
552} 581}
553 582
554=item $value = cf::db_get $family => $key 583=item $value = cf::db_get $family => $key
555 584
556Returns a single value from the environment database. 585Returns a single value from the environment database.
1446 1475
1447 return if $pl->{deny_save}; 1476 return if $pl->{deny_save};
1448 1477
1449 aio_mkdir playerdir $pl, 0770; 1478 aio_mkdir playerdir $pl, 0770;
1450 $pl->{last_save} = $cf::RUNTIME; 1479 $pl->{last_save} = $cf::RUNTIME;
1480
1481 cf::get_slot 0.01;
1451 1482
1452 $pl->save_pl ($path); 1483 $pl->save_pl ($path);
1453 cf::cede_to_tick; 1484 cf::cede_to_tick;
1454} 1485}
1455 1486
1748our $MAX_RESET = 3600; 1779our $MAX_RESET = 3600;
1749our $DEFAULT_RESET = 3000; 1780our $DEFAULT_RESET = 3000;
1750 1781
1751sub generate_random_map { 1782sub generate_random_map {
1752 my ($self, $rmp) = @_; 1783 my ($self, $rmp) = @_;
1784
1785 my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM
1786
1753 # mit "rum" bekleckern, nicht 1787 # mit "rum" bekleckern, nicht
1754 $self->_create_random_map ( 1788 $self->_create_random_map (
1755 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, 1789 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
1756 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, 1790 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle},
1757 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, 1791 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
1879 1913
1880# the temporary/swap location 1914# the temporary/swap location
1881sub save_path { 1915sub save_path {
1882 my ($self) = @_; 1916 my ($self) = @_;
1883 1917
1884 (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; 1918 (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go;
1885 "$TMPDIR/$path.map" 1919 "$TMPDIR/$path.map"
1886} 1920}
1887 1921
1888# the unique path, undef == no special unique path 1922# the unique path, undef == no special unique path
1889sub uniq_path { 1923sub uniq_path {
1890 my ($self) = @_; 1924 my ($self) = @_;
1891 1925
1892 (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; 1926 (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/go;
1893 "$UNIQUEDIR/$path" 1927 "$UNIQUEDIR/$path"
1894} 1928}
1895 1929
1896# and all this just because we cannot iterate over 1930# and all this just because we cannot iterate over
1897# all maps in C++... 1931# all maps in C++...
2083 } 2117 }
2084 2118
2085 $self->{last_save} = $cf::RUNTIME; 2119 $self->{last_save} = $cf::RUNTIME;
2086 $self->last_access ($cf::RUNTIME); 2120 $self->last_access ($cf::RUNTIME);
2087 2121
2088 $self->in_memory (cf::MAP_IN_MEMORY); 2122 $self->in_memory (cf::MAP_ACTIVE);
2089 } 2123 }
2090 2124
2091 $self->post_load; 2125 $self->post_load;
2092} 2126}
2093 2127
2151 my ($path, $origin, $load) = @_; 2185 my ($path, $origin, $load) = @_;
2152 2186
2153 $path = normalise $path, $origin && $origin->{path}; 2187 $path = normalise $path, $origin && $origin->{path};
2154 2188
2155 if (my $map = $cf::MAP{$path}) { 2189 if (my $map = $cf::MAP{$path}) {
2156 return $map if !$load || $map->in_memory == cf::MAP_IN_MEMORY; 2190 return $map if !$load || $map->in_memory == cf::MAP_ACTIVE;
2157 } 2191 }
2158 2192
2159 $MAP_PREFETCH{$path} |= $load; 2193 $MAP_PREFETCH{$path} |= $load;
2160 2194
2161 $MAP_PREFETCHER ||= cf::async { 2195 $MAP_PREFETCHER ||= cf::async {
2198 cf::async { 2232 cf::async {
2199 $Coro::current->{desc} = "map player save"; 2233 $Coro::current->{desc} = "map player save";
2200 $_->contr->save for $self->players; 2234 $_->contr->save for $self->players;
2201 }; 2235 };
2202 2236
2237 cf::get_slot 0.02;
2238
2203 if ($uniq) { 2239 if ($uniq) {
2204 $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); 2240 $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS);
2205 $self->_save_objects ($uniq, cf::IO_UNIQUES); 2241 $self->_save_objects ($uniq, cf::IO_UNIQUES);
2206 } else { 2242 } else {
2207 $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); 2243 $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES);
2215 $self->save; 2251 $self->save;
2216 2252
2217 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2253 my $lock = cf::lock_acquire "map_data:$self->{path}";
2218 2254
2219 return if $self->players; 2255 return if $self->players;
2220 return if $self->in_memory != cf::MAP_IN_MEMORY; 2256 return if $self->in_memory != cf::MAP_ACTIVE;
2221 return if $self->{deny_save}; 2257 return if $self->{deny_save};
2222 2258
2223 $self->in_memory (cf::MAP_SWAPPED); 2259 $self->in_memory (cf::MAP_SWAPPED);
2224 2260
2225 $self->deactivate; 2261 $self->deactivate;
2343 2379
2344sub unique_maps() { 2380sub unique_maps() {
2345 [ 2381 [
2346 map { 2382 map {
2347 utf8::decode $_; 2383 utf8::decode $_;
2348 /\.map$/ 2384 s/\.map$//; # TODO future compatibility hack
2385 /\.pst$/ || !/^$PATH_SEP/o # TODO unique maps apparebntly lack the .map suffix :/
2386 ? ()
2349 ? normalise $_ 2387 : normalise $_
2350 : ()
2351 } @{ aio_readdir $UNIQUEDIR or [] } 2388 } @{ aio_readdir $UNIQUEDIR or [] }
2352 ] 2389 ]
2353} 2390}
2354 2391
2355package cf;
2356
2357=back 2392=back
2358 2393
2359=head3 cf::object 2394=head3 cf::object
2360 2395
2361=cut 2396=cut
2364 2399
2365=over 4 2400=over 4
2366 2401
2367=item $ob->inv_recursive 2402=item $ob->inv_recursive
2368 2403
2369Returns the inventory of the object _and_ their inventories, recursively. 2404Returns the inventory of the object I<and> their inventories, recursively,
2405but I<not> the object itself.
2370 2406
2371=cut 2407=cut
2372 2408
2373sub inv_recursive_; 2409sub inv_recursive_;
2374sub inv_recursive_ { 2410sub inv_recursive_ {
2379 inv_recursive_ inv $_[0] 2415 inv_recursive_ inv $_[0]
2380} 2416}
2381 2417
2382=item $ref = $ob->ref 2418=item $ref = $ob->ref
2383 2419
2384creates and returns a persistent reference to an objetc that can be stored as a string. 2420Creates and returns a persistent reference to an object that can be stored as a string.
2385 2421
2386=item $ob = cf::object::deref ($refstring) 2422=item $ob = cf::object::deref ($refstring)
2387 2423
2388returns the objetc referenced by refstring. may return undef when it cnanot find the object, 2424returns the objetc referenced by refstring. may return undef when it cnanot find the object,
2389even if the object actually exists. May block. 2425even if the object actually exists. May block.
2663 $rmp->{origin_y} = $exit->y; 2699 $rmp->{origin_y} = $exit->y;
2664 } 2700 }
2665 2701
2666 $rmp->{random_seed} ||= $exit->random_seed; 2702 $rmp->{random_seed} ||= $exit->random_seed;
2667 2703
2668 my $data = cf::encode_json $rmp; 2704 my $data = JSON::XS->new->utf8->pretty->canonical->encode ($rmp);
2669 my $md5 = Digest::MD5::md5_hex $data; 2705 my $md5 = Digest::MD5::md5_hex $data;
2670 my $meta = "$RANDOMDIR/$md5.meta"; 2706 my $meta = "$RANDOMDIR/$md5.meta";
2671 2707
2672 if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { 2708 if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) {
2673 aio_write $fh, 0, (length $data), $data, 0; 2709 aio_write $fh, 0, (length $data), $data, 0;
3276 while (my ($k, $v) = each %$want) { 3312 while (my ($k, $v) = each %$want) {
3277 $ns->fx_want ($k, $v); 3313 $ns->fx_want ($k, $v);
3278 } 3314 }
3279}; 3315};
3280 3316
3317sub load_resource_file($) {
3318 my $guard = lock_acquire "load_resource_file";
3319
3320 my $status = load_resource_file_ $_[0];
3321 get_slot 0.1, 100;
3322 cf::arch::commit_load;
3323
3324 $status
3325}
3326
3281sub reload_regions { 3327sub reload_regions {
3282 # HACK to clear player env face cache, we need some signal framework 3328 # HACK to clear player env face cache, we need some signal framework
3283 # for this (global event?) 3329 # for this (global event?)
3284 %ext::player_env::MUSIC_FACE_CACHE = (); 3330 %ext::player_env::MUSIC_FACE_CACHE = ();
3285 3331
3298} 3344}
3299 3345
3300sub reload_archetypes { 3346sub reload_archetypes {
3301 load_resource_file "$DATADIR/archetypes" 3347 load_resource_file "$DATADIR/archetypes"
3302 or die "unable to load archetypes\n"; 3348 or die "unable to load archetypes\n";
3303 #d# NEED to laod twice to resolve forward references
3304 # this really needs to be done in an extra post-pass
3305 # (which needs to be synchronous, so solve it differently)
3306 load_resource_file "$DATADIR/archetypes"
3307 or die "unable to load archetypes\n";
3308} 3349}
3309 3350
3310sub reload_treasures { 3351sub reload_treasures {
3311 load_resource_file "$DATADIR/treasures" 3352 load_resource_file "$DATADIR/treasures"
3312 or die "unable to load treasurelists\n"; 3353 or die "unable to load treasurelists\n";
3313} 3354}
3314 3355
3315sub reload_resources { 3356sub reload_resources {
3316 warn "reloading resource files...\n"; 3357 warn "reloading resource files...\n";
3317 3358
3359 reload_facedata;
3360 reload_archetypes;
3318 reload_regions; 3361 reload_regions;
3319 reload_facedata;
3320 #reload_archetypes;#d#
3321 reload_archetypes;
3322 reload_treasures; 3362 reload_treasures;
3323 3363
3324 warn "finished reloading resource files\n"; 3364 warn "finished reloading resource files\n";
3325} 3365}
3326 3366
3327sub init { 3367sub init {
3368 my $guard = freeze_mainloop;
3369
3328 reload_resources; 3370 reload_resources;
3329} 3371}
3330 3372
3331sub reload_config { 3373sub reload_config {
3332 open my $fh, "<:utf8", "$CONFDIR/config" 3374 open my $fh, "<:utf8", "$CONFDIR/config"
3357 $Coro::current->{desc} = "IDLE BUG HANDLER"; 3399 $Coro::current->{desc} = "IDLE BUG HANDLER";
3358 EV::loop EV::LOOP_ONESHOT; 3400 EV::loop EV::LOOP_ONESHOT;
3359 })->prio (Coro::PRIO_MAX); 3401 })->prio (Coro::PRIO_MAX);
3360 }; 3402 };
3361 3403
3404 {
3405 my $guard = freeze_mainloop;
3362 reload_config; 3406 reload_config;
3363 db_init; 3407 db_init;
3364 load_extensions; 3408 load_extensions;
3365 3409
3366 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3410 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3367 evthread_start; 3411 evthread_start IO::AIO::poll_fileno;
3412 }
3413
3368 EV::loop; 3414 EV::loop;
3369} 3415}
3370 3416
3371############################################################################# 3417#############################################################################
3372# initialisation and cleanup 3418# initialisation and cleanup
3379 cf::cleanup "SIG$signal"; 3425 cf::cleanup "SIG$signal";
3380 }; 3426 };
3381 } 3427 }
3382} 3428}
3383 3429
3384sub write_runtime { 3430sub write_runtime_sync {
3385 my $runtime = "$LOCALDIR/runtime"; 3431 my $runtime = "$LOCALDIR/runtime";
3386 3432
3387 # first touch the runtime file to show we are still running: 3433 # first touch the runtime file to show we are still running:
3388 # the fsync below can take a very very long time. 3434 # the fsync below can take a very very long time.
3389 3435
3415 and return; 3461 and return;
3416 3462
3417 warn "runtime file written.\n"; 3463 warn "runtime file written.\n";
3418 3464
3419 1 3465 1
3466}
3467
3468our $uuid_lock;
3469our $uuid_skip;
3470
3471sub write_uuid_sync($) {
3472 $uuid_skip ||= $_[0];
3473
3474 return if $uuid_lock;
3475 local $uuid_lock = 1;
3476
3477 my $uuid = "$LOCALDIR/uuid";
3478
3479 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644
3480 or return;
3481
3482 my $value = uuid_str $uuid_skip + uuid_seq uuid_cur;
3483 $uuid_skip = 0;
3484
3485 (aio_write $fh, 0, (length $value), $value, 0) <= 0
3486 and return;
3487
3488 # always fsync - this file is important
3489 aio_fsync $fh
3490 and return;
3491
3492 close $fh
3493 or return;
3494
3495 aio_rename "$uuid~", $uuid
3496 and return;
3497
3498 warn "uuid file written ($value).\n";
3499
3500 1
3501
3502}
3503
3504sub write_uuid($$) {
3505 my ($skip, $sync) = @_;
3506
3507 $sync ? write_uuid_sync $skip
3508 : async { write_uuid_sync $skip };
3420} 3509}
3421 3510
3422sub emergency_save() { 3511sub emergency_save() {
3423 my $freeze_guard = cf::freeze_mainloop; 3512 my $freeze_guard = cf::freeze_mainloop;
3424 3513
3446 warn "end emergency map save\n"; 3535 warn "end emergency map save\n";
3447 3536
3448 warn "begin emergency database checkpoint\n"; 3537 warn "begin emergency database checkpoint\n";
3449 BDB::db_env_txn_checkpoint $DB_ENV; 3538 BDB::db_env_txn_checkpoint $DB_ENV;
3450 warn "end emergency database checkpoint\n"; 3539 warn "end emergency database checkpoint\n";
3540
3541 warn "begin write uuid\n";
3542 write_uuid_sync 1;
3543 warn "end write uuid\n";
3451 }; 3544 };
3452 3545
3453 warn "leave emergency perl save\n"; 3546 warn "leave emergency perl save\n";
3454} 3547}
3455 3548
3470 warn "reloading..."; 3563 warn "reloading...";
3471 3564
3472 warn "entering sync_job"; 3565 warn "entering sync_job";
3473 3566
3474 cf::sync_job { 3567 cf::sync_job {
3475 cf::write_runtime; # external watchdog should not bark 3568 cf::write_runtime_sync; # external watchdog should not bark
3476 cf::emergency_save; 3569 cf::emergency_save;
3477 cf::write_runtime; # external watchdog should not bark 3570 cf::write_runtime_sync; # external watchdog should not bark
3478 3571
3479 warn "syncing database to disk"; 3572 warn "syncing database to disk";
3480 BDB::db_env_txn_checkpoint $DB_ENV; 3573 BDB::db_env_txn_checkpoint $DB_ENV;
3481 3574
3482 # if anything goes wrong in here, we should simply crash as we already saved 3575 # if anything goes wrong in here, we should simply crash as we already saved
3607 3700
3608our @WAIT_FOR_TICK; 3701our @WAIT_FOR_TICK;
3609our @WAIT_FOR_TICK_BEGIN; 3702our @WAIT_FOR_TICK_BEGIN;
3610 3703
3611sub wait_for_tick { 3704sub wait_for_tick {
3612 return if tick_inhibit;
3613 return if $Coro::current == $Coro::main; 3705 return if tick_inhibit || $Coro::current == $Coro::main;
3614 3706
3615 my $signal = new Coro::Signal; 3707 my $signal = new Coro::Signal;
3616 push @WAIT_FOR_TICK, $signal; 3708 push @WAIT_FOR_TICK, $signal;
3617 $signal->wait; 3709 $signal->wait;
3618} 3710}
3619 3711
3620sub wait_for_tick_begin { 3712sub wait_for_tick_begin {
3621 return if tick_inhibit;
3622 return if $Coro::current == $Coro::main; 3713 return if tick_inhibit || $Coro::current == $Coro::main;
3623 3714
3624 my $signal = new Coro::Signal; 3715 my $signal = new Coro::Signal;
3625 push @WAIT_FOR_TICK_BEGIN, $signal; 3716 push @WAIT_FOR_TICK_BEGIN, $signal;
3626 $signal->wait; 3717 $signal->wait;
3627} 3718}
3637 3728
3638 if ($NOW >= $NEXT_RUNTIME_WRITE) { 3729 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3639 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 3730 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
3640 Coro::async_pool { 3731 Coro::async_pool {
3641 $Coro::current->{desc} = "runtime saver"; 3732 $Coro::current->{desc} = "runtime saver";
3642 write_runtime 3733 write_runtime_sync
3643 or warn "ERROR: unable to write runtime file: $!"; 3734 or warn "ERROR: unable to write runtime file: $!";
3644 }; 3735 };
3645 } 3736 }
3646 3737
3647 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { 3738 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines