--- deliantra/server/lib/cf.pm 2010/04/06 22:59:28 1.510 +++ deliantra/server/lib/cf.pm 2010/04/23 04:42:12 1.528 @@ -110,6 +110,12 @@ our %RESOURCE; +our $OUTPUT_RATE_MIN = 3000; +our $OUTPUT_RATE_MAX = 1000000; + +our $MAX_LINKS = 32; # how many chained exits to follow +our $VERBOSE_IO = 1; + our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) our $NEXT_RUNTIME_WRITE; # when should the runtime file be written our $NEXT_TICK; @@ -164,6 +170,10 @@ sub cf::map::normalise; +sub in_main() { + $Coro::current == $Coro::main +} + ############################################################################# %REFLECT = (); @@ -260,7 +270,7 @@ warn Carp::longmess $_[0]; - if ($Coro::current == $Coro::main) {#d# + if (in_main) {#d# warn "DIEHOOK called in main context, Coro bug?\n";#d# return;#d# }#d# @@ -398,7 +408,7 @@ =item cf::periodic $interval, $cb Like EV::periodic, but randomly selects a starting point so that the actions -get spread over timer. +get spread over time. =cut @@ -505,7 +515,7 @@ my ($job) = @_; if ($Coro::current == $Coro::main) { - my $time = EV::time; + my $time = AE::time; # this is the main coro, too bad, we have to block # till the operation succeeds, freezing the server :/ @@ -532,7 +542,7 @@ } } - my $time = EV::time - $time; + my $time = AE::time - $time; $TICK_START += $time; # do not account sync jobs to server load @@ -588,6 +598,17 @@ wantarray ? @res : $res[-1] } +sub objinfo { + ( + "counter value" => cf::object::object_count, + "objects created" => cf::object::create_count, + "objects destroyed" => cf::object::destroy_count, + "freelist size" => cf::object::free_count, + "allocated objects" => cf::object::objects_size, + "active objects" => cf::object::actives_size, + ) +} + =item $coin = coin_from_name $name =cut @@ -1176,7 +1197,8 @@ if (length $$rdata) { utf8::decode (my $decname = $filename); warn sprintf "saving %s (%d,%d)\n", - $decname, length $$rdata, scalar @$objs; + $decname, length $$rdata, scalar @$objs + if $VERBOSE_IO; if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { aio_chmod $fh, SAVE_MODE; @@ -1243,7 +1265,8 @@ utf8::decode (my $decname = $filename); warn sprintf "loading %s (%d,%d)\n", - $decname, length $data, scalar @{$av || []}; + $decname, length $data, scalar @{$av || []} + if $VERBOSE_IO; ($data, $av) } @@ -1424,6 +1447,9 @@ cf::cleanup "mandatory extension '$k' failed to load, exiting." if exists $v->{meta}{mandatory}; + + warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; + delete $todo{$k}; } else { $done{$k} = delete $todo{$k}; push @EXTS, $v->{pkg}; @@ -1535,6 +1561,19 @@ } } +cf::player->attach ( + on_load => sub { + my ($pl, $path) = @_; + + # restore slots saved in save, below + my $slots = delete $pl->{_slots}; + + $pl->ob->current_weapon ($slots->[0]); + $pl->combat_ob ($slots->[1]); + $pl->ranged_ob ($slots->[2]); + }, +); + sub save($) { my ($pl) = @_; @@ -1550,6 +1589,9 @@ cf::get_slot 0.01; + # save slots, to be restored later + local $pl->{_slots} = [$pl->ob->current_weapon, $pl->combat_ob, $pl->ranged_ob]; + $pl->save_pl ($path); cf::cede_to_tick; } @@ -1660,6 +1702,8 @@ =item $player->maps +=item cf::player::maps $login + Returns an arrayref of map paths that are private for this player. May block. @@ -1731,6 +1775,8 @@ sub find_by_path($) { my ($path) = @_; + $path =~ s/^~[^\/]*//; # skip ~login + my ($match, $specificity); for my $region (list) { @@ -1803,7 +1849,7 @@ } # also paths starting with '/' -$EXT_MAP{"cf::map"} = [0, qr{^(?=/)}]; +$EXT_MAP{"cf::map::wrap"} = [0, qr{^(?=/)}]; sub thawer_merge { my ($self, $merge) = @_; @@ -2123,6 +2169,7 @@ or next; $neigh->load; + # now find the diagonal neighbours push @neigh, [$neigh->tile_path (($_ + 3) % 4), $neigh], [$neigh->tile_path (($_ + 1) % 4), $neigh]; @@ -2184,11 +2231,10 @@ () } -sub save { +# common code, used by both ->save and ->swapout +sub _save { my ($self) = @_; - my $lock = cf::lock_acquire "map_data:$self->{path}"; - $self->{last_save} = $cf::RUNTIME; return unless $self->dirty; @@ -2217,22 +2263,32 @@ } } -sub swap_out { +sub save { my ($self) = @_; - # save first because save cedes - $self->save; + my $lock = cf::lock_acquire "map_data:$self->{path}"; + + $self->_save; +} + +sub swap_out { + my ($self) = @_; my $lock = cf::lock_acquire "map_data:$self->{path}"; - return if $self->players; return if $self->in_memory != cf::MAP_ACTIVE; return if $self->{deny_save}; + return if $self->players; - $self->in_memory (cf::MAP_SWAPPED); - + # first deactivate the map and "unlink" it from the core $self->deactivate; $_->clear_links_to ($self) for values %cf::MAP; + $self->in_memory (cf::MAP_SWAPPED); + + # then atomically save + $self->_save; + + # then free the map $self->clear; } @@ -2295,7 +2351,7 @@ $self->unlink_save; - bless $self, "cf::map"; + bless $self, "cf::map::wrap"; delete $self->{deny_reset}; $self->{deny_save} = 1; $self->reset_timeout (1); @@ -2535,7 +2591,7 @@ The player should be reasonably safe there for short amounts of time (e.g. for loading a map). You I call C as soon as possible, -though, as the palyer cannot control the character while it is on the link +though, as the player cannot control the character while it is on the link map. Will never block. @@ -2566,12 +2622,14 @@ $self->deactivate_recursive; + ++$self->{_link_recursion}; + return if UNIVERSAL::isa $self->map, "ext::map_link"; $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] if $self->map && $self->map->{path} ne "{link}"; - $self->enter_map ($LINK_MAP || link_map, 10, 10); + $self->enter_map ($LINK_MAP || link_map, 3, 3); } sub cf::object::player::leave_link { @@ -2606,13 +2664,16 @@ return unless $self->contr->active; local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext - $self->enter_map ($map, $x, $y); + if ($self->enter_map ($map, $x, $y)) { + # entering was successful + delete $self->{_link_recursion}; + # only activate afterwards, to support waiting in hooks + $self->activate_recursive; + } - # only activate afterwards, to support waiting in hooks - $self->activate_recursive; } -=item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) +=item $player_object->goto ($path, $x, $y[, $check->($map, $x, $y, $player)[, $done->($player)]]) Moves the player to the given map-path and coordinates by first freezing her, loading and preparing them map, calling the provided $check callback @@ -2630,6 +2691,12 @@ sub cf::object::player::goto { my ($self, $path, $x, $y, $check, $done) = @_; + if ($self->{_link_recursion} >= $MAX_LINKS) { + warn "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting."; + $self->failmsg ("Something went wrong inside the server - please contact an administrator!"); + ($path, $x, $y) = @$EMERGENCY_POSITION; + } + # do generation counting so two concurrent goto's will be executed in-order my $gen = $self->{_goto_generation} = ++$GOTOGEN; @@ -2662,7 +2729,7 @@ if ($map) { $map = $map->customise_for ($self); - $map = $check->($map) if $check && $map; + $map = $check->($map, $x, $y, $self) if $check && $map; } else { $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED); } @@ -2680,7 +2747,7 @@ $self->leave_link ($map, $x, $y); } - $done->() if $done; + $done->($self) if $done; })->prio (1); } @@ -3463,7 +3530,7 @@ local $/; *CFG = YAML::XS::Load scalar <$fh>; - $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; + $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; @@ -3515,11 +3582,6 @@ LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; - cf::init_experience; - cf::init_anim; - cf::init_attackmess; - cf::init_dynamic; - $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority # we must not ever block the main coroutine @@ -3534,6 +3596,11 @@ evthread_start IO::AIO::poll_fileno; cf::sync_job { + cf::init_experience; + cf::init_anim; + cf::init_attackmess; + cf::init_dynamic; + cf::load_settings; cf::load_materials; @@ -3560,6 +3627,9 @@ (pop @POST_INIT)->(0) while @POST_INIT; }; + cf::object::thawer::errors_are_fatal 0; + warn "parse errors in files are no longer fatal from this point on.\n"; + main_loop; } @@ -3570,14 +3640,14 @@ BEGIN { our %SIGWATCHER = (); for my $signal (qw(INT HUP TERM)) { - $SIGWATCHER{$signal} = EV::signal $signal, sub { + $SIGWATCHER{$signal} = AE::signal $signal, sub { cf::cleanup "SIG$signal"; }; } } sub write_runtime_sync { - my $t0 = EV::time; + my $t0 = AE::time; # first touch the runtime file to show we are still running: # the fsync below can take a very very long time. @@ -3609,7 +3679,7 @@ aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE and return; - warn sprintf "runtime file written (%gs).\n", EV::time - $t0; + warn sprintf "runtime file written (%gs).\n", AE::time - $t0; 1 } @@ -3745,19 +3815,18 @@ _gv_clear *{"$pkg$name"}; # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; } - warn "cleared package $pkg\n";#d# } sub do_reload_perl() { # can/must only be called in main - if ($Coro::current != $Coro::main) { + if (in_main) { warn "can only reload from main coroutine"; return; } return if $RELOAD++; - my $t1 = EV::time; + my $t1 = AE::time; while ($RELOAD) { warn "reloading..."; @@ -3870,7 +3939,7 @@ --$RELOAD; } - $t1 = EV::time - $t1; + $t1 = AE::time - $t1; warn "reload completed in ${t1}s\n"; }; @@ -3883,7 +3952,7 @@ $RELOAD_WATCHER ||= cf::async { Coro::AIO::aio_wait cache_extensions; - $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { + $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub { do_reload_perl; undef $RELOAD_WATCHER; }; @@ -3934,7 +4003,7 @@ cf::server_tick; # one server iteration - #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d# + #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# if ($NOW >= $NEXT_RUNTIME_WRITE) { $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;