--- deliantra/server/lib/cf.pm 2007/01/04 20:29:46 1.135 +++ deliantra/server/lib/cf.pm 2007/01/09 15:36:19 1.154 @@ -17,9 +17,10 @@ use Coro::Semaphore; use Coro::AIO; +use Data::Dumper; use Digest::MD5; use Fcntl; -use IO::AIO 2.31 (); +use IO::AIO 2.32 (); use YAML::Syck (); use Time::HiRes; @@ -28,7 +29,7 @@ # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? $YAML::Syck::ImplicitUnicode = 1; -$Coro::main->prio (2); # run main coroutine ("the server") with very high priority +$Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload @@ -49,10 +50,11 @@ our $UPTIME; $UPTIME ||= time; our $RUNTIME; -our %MAP; # all maps +our %PLAYER; # all users +our %MAP; # all maps our $LINK_MAP; # the special {link} map our $RANDOM_MAPS = cf::localdir . "/random"; -our %EXT_CORO; +our %EXT_CORO; # coroutines bound to extensions binmode STDOUT; binmode STDERR; @@ -119,7 +121,7 @@ $msg .= "\n" unless $msg =~ /\n$/; - LOG llevError, "cfperl: $msg"; + LOG llevError, $msg; }; } @@ -155,8 +157,29 @@ =over 4 +=item dumpval $ref + =cut +sub dumpval { + eval { + local $SIG{__DIE__}; + my $d; + if (1) { + $d = new Data::Dumper([$_[0]], ["*var"]); + $d->Terse(1); + $d->Indent(2); + $d->Quotekeys(0); + $d->Useqq(1); + #$d->Bless(...); + $d->Seen($_[1]) if @_ > 1; + $d = $d->Dump(); + } + $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; + $d + } || "[unable to dump $_[0]: '$@']"; +} + use JSON::Syck (); # TODO# replace by JSON::PC once working =item $ref = cf::from_json $json @@ -223,22 +246,6 @@ } } -=item cf::async { BLOCK } - -Like C, but runs the given BLOCK in an eval and only logs the -error instead of exiting the server in case of a problem. - -=cut - -sub async(&) { - my ($cb) = @_; - - Coro::async { - eval { $cb->() }; - warn $@ if $@; - } -} - sub freeze_mainloop { return unless $TICK_WATCHER->is_active; @@ -247,6 +254,16 @@ $guard } +=item cf::async { BLOCK } + +Currently the same as Coro::async_pool, meaning you cannot use +C, C or other gimmicks on these coroutines. The only +thing you are allowed to do is call C on it. + +=cut + +BEGIN { *async = \&Coro::async_pool } + =item cf::sync_job { BLOCK } The design of crossfire+ requires that the main coro ($Coro::main) is @@ -274,15 +291,14 @@ my $busy = 1; my @res; - (Coro::async { + (async { @res = eval { $job->() }; warn $@ if $@; undef $busy; })->prio (Coro::PRIO_MAX); while ($busy) { - Coro::cede_notself; - Event::one_event unless Coro::nready; + Coro::cede or Event::one_event; } wantarray ? @res : $res[0] @@ -293,17 +309,17 @@ } } -=item $coro = cf::coro { BLOCK } +=item $coro = cf::async_ext { BLOCK } -Creates and returns a new coro. This coro is automcatially being canceled -when the extension calling this is being unloaded. +Like async, but this coro is automcatially being canceled when the +extension calling this is being unloaded. =cut -sub coro(&) { +sub async_ext(&) { my $cb = shift; - my $coro = &cf::async ($cb); + my $coro = &Coro::async ($cb); $coro->on_destroy (sub { delete $EXT_CORO{$coro+0}; @@ -343,6 +359,9 @@ package cf::path; +# used to convert map paths into valid unix filenames by repalcing / by ∕ +our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons + sub new { my ($class, $path, $base) = @_; @@ -415,8 +434,7 @@ # escape the /'s in the path sub _escaped_path { - # ∕ is U+2215 - (my $path = $_[0]{path}) =~ s/\//∕/g; + (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; $path } @@ -1080,19 +1098,169 @@ Functions and methods that extend core crossfire objects. +=cut + +package cf::player; + +use Coro::AIO; + =head3 cf::player =over 4 -=item cf::player::exists $login +=item cf::player::find $login + +Returns the given player object, loading it if necessary (might block). + +=cut + +sub playerdir($) { + cf::localdir + . "/" + . cf::playerdir + . "/" + . (ref $_[0] ? $_[0]->ob->name : $_[0]) +} + +sub path($) { + my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; + + (playerdir $login) . "/$login.pl" +} + +sub find_active($) { + $cf::PLAYER{$_[0]} + and $cf::PLAYER{$_[0]}->active + and $cf::PLAYER{$_[0]} +} + +sub exists($) { + my ($login) = @_; + + $cf::PLAYER{$login} + or cf::sync_job { !aio_stat $login } +} + +sub find($) { + return $cf::PLAYER{$_[0]} || do { + my $login = $_[0]; + + my $guard = cf::lock_acquire "user_find:$login"; + + $cf::PLAYER{$_[0]} || do { + my $pl = load_pl path $login + or return; + $cf::PLAYER{$login} = $pl + } + } +} + +sub save($) { + my ($pl) = @_; + + return if $pl->{deny_save}; + + my $path = path $pl; + my $guard = cf::lock_acquire "user_save:$path"; + + return if $pl->{deny_save}; + + aio_mkdir playerdir $pl, 0770; + $pl->{last_save} = $cf::RUNTIME; -Returns true when the given account exists. + $pl->save_pl ($path); + Coro::cede; +} + +sub new($) { + my ($login) = @_; + + my $self = create; + + $self->ob->name ($login); + $self->{deny_save} = 1; + + $cf::PLAYER{$login} = $self; + + $self +} + +=item $pl->quit_character + +Nukes the player without looking back. If logged in, the connection will +be destroyed. May block for a long time. + +=cut + +sub quit_character { + my ($pl) = @_; + + $pl->{deny_save} = 1; + $pl->password ("*"); # this should lock out the player until we nuked the dir + + $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; + $pl->deactivate; + $pl->invoke (cf::EVENT_PLAYER_QUIT); + $pl->ns->destroy if $pl->ns; + + my $path = playerdir $pl; + my $temp = "$path~$cf::RUNTIME~deleting~"; + aio_rename $path, $temp; + delete $cf::PLAYER{$pl->ob->name}; + $pl->destroy; + IO::AIO::aio_rmtree $temp; +} + +=item cf::player::list_logins + +Returns am arrayref of all valid playernames in the system, can take a +while and may block, so not sync_job-capable, ever. =cut -sub cf::player::exists($) { - cf::player::find $_[0] - or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; +sub list_logins { + my $dirs = aio_readdir cf::localdir . "/" . cf::playerdir + or return []; + + my @logins; + + for my $login (@$dirs) { + my $fh = aio_open path $login, Fcntl::O_RDONLY, 0 or next; + aio_read $fh, 0, 512, my $buf, 0 or next; + $buf !~ /^password -------------$/ or next; # official not-valid tag + + utf8::decode $login; + push @logins, $login; + } + + \@logins +} + +=item $player->maps + +Returns an arrayref of cf::path's of all maps that are private for this +player. May block. + +=cut + +sub maps($) { + my ($pl) = @_; + + my $files = aio_readdir playerdir $pl + or return; + + my @paths; + + for (@$files) { + utf8::decode $_; + next if /\.(?:pl|pst)$/; + next unless /^$PATH_SEP/; + + s/$PATH_SEP/\//g; + push @paths, new cf::path "~" . $pl->ob->name . "/" . $_; + } + + \@paths } =item $player->ext_reply ($msgid, $msgtype, %msg) @@ -1101,14 +1269,16 @@ =cut -sub cf::player::ext_reply($$$%) { +sub ext_reply($$$%) { my ($self, $id, %msg) = @_; $msg{msgid} = $id; - $self->send ("ext " . to_json \%msg); + $self->send ("ext " . cf::to_json \%msg); } +package cf; + =back @@ -1298,6 +1468,8 @@ sub save { my ($self) = @_; + my $lock = cf::lock_acquire "map_data:" . $self->path; + $self->{last_save} = $cf::RUNTIME; return unless $self->dirty; @@ -1311,6 +1483,10 @@ local $self->{last_access} = $self->last_access;#d# + cf::async { + $_->contr->save for $self->players; + }; + if ($uniq) { $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); $self->save_objects ($uniq, cf::IO_UNIQUES); @@ -1325,6 +1501,8 @@ # save first because save cedes $self->save; + my $lock = cf::lock_acquire "map_data:" . $self->path; + return if $self->players; return if $self->in_memory != cf::MAP_IN_MEMORY; return if $self->{deny_save}; @@ -1376,6 +1554,8 @@ sub reset { my ($self) = @_; + my $lock = cf::lock_acquire "map_data:" . $self->path; + return if $self->players; return if $self->{path}{user_rel};#d# @@ -1413,14 +1593,19 @@ sub emergency_save { my $freeze_guard = cf::freeze_mainloop; - warn "enter emergency map save\n"; + warn "enter emergency perl save\n"; cf::sync_job { + warn "begin emergency player save\n"; + $_->save for values %cf::PLAYER; + warn "end emergency player save\n"; + warn "begin emergency map save\n"; $_->save for values %cf::MAP; + warn "end emergency map save\n"; }; - warn "end emergency map save\n"; + warn "leave emergency perl save\n"; } package cf; @@ -1529,6 +1714,7 @@ $map->load; + return unless $self->contr->active; $self->activate_recursive; $self->enter_map ($map, $x, $y); } @@ -1551,35 +1737,40 @@ # should happen only on crashes if ($pl->ob->{_link_pos}) { $pl->ob->enter_link; - cf::async { + (async { # we need this sleep as the login has a concurrent enter_exit running # and this sleep increases chances of the player not ending up in scorn + $pl->ob->reply (undef, + "There was an internal problem at your last logout, " + . "the server will try to bring you to your intended destination in a second.", + cf::NDI_RED); Coro::Timer::sleep 1; $pl->ob->leave_link; - }; + })->prio (2); } }, ); -=item $player_object->goto_map ($path, $x, $y) +=item $player_object->goto ($path, $x, $y) =cut -sub cf::object::player::goto_map { +sub cf::object::player::goto { my ($self, $path, $x, $y) = @_; - $self->enter_link; + $path = new cf::path $path; + $path ne "/" or Carp::cluck ("oy");#d# - (cf::async { - $path = new cf::path $path; + $self->enter_link; + (async { my $map = cf::map::find $path->as_string; $map = $map->customise_for ($self) if $map; # warn "entering ", $map->path, " at ($x, $y)\n" # if $map; - $map or $self->message ("The exit is closed", cf::NDI_UNIQUE | cf::NDI_RED); + $map or $self->message ("The exit to '" . ($path->visible_name) . "' is closed", cf::NDI_UNIQUE | cf::NDI_RED); $self->leave_link ($map, $x, $y); })->prio (1); @@ -1642,14 +1833,14 @@ $self->enter_link; - (cf::async { + (async { $self->deactivate_recursive; # just to be sure unless (eval { prepare_random_map $exit if $exit->slaying eq "/!"; my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; - $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); + $self->goto ($path, $exit->stats->hp, $exit->stats->sp); 1; }) { @@ -1733,7 +1924,7 @@ }, ); -=item $client->coro (\&cb) +=item $client->async (\&cb) Create a new coroutine, running the specified callback. The coroutine will be automatically cancelled when the client gets destroyed (e.g. on logout, @@ -1741,10 +1932,10 @@ =cut -sub cf::client::coro { +sub cf::client::async { my ($self, $cb) = @_; - my $coro = &cf::async ($cb); + my $coro = &Coro::async ($cb); $coro->on_destroy (sub { delete $self->{_coro}{$coro+0}; @@ -1992,9 +2183,12 @@ $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; + $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}; + if (exists $CFG{mlockall}) { eval { - $CFG{mlockall} ? &mlockall : &munlockall + $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" and die "WARNING: m(un)lockall failed: $!\n"; }; warn $@ if $@; @@ -2005,9 +2199,7 @@ # we must not ever block the main coroutine local $Coro::idle = sub { Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# - (Coro::unblock_sub { - Event::one_event; - })->(); + async { Event::one_event }; }; cfg_load; @@ -2100,6 +2292,7 @@ # reattach attachments to objects warn "reattach"; _global_reattach; + reattach $_ for values %MAP; }; if ($@) { @@ -2125,7 +2318,7 @@ # dirty hack because... archetypes are not yet loaded Event->timer ( - after => 2, + after => 10, cb => sub { $_[0]->w->cancel;