--- deliantra/server/lib/cf.pm 2007/08/13 13:10:01 1.331 +++ deliantra/server/lib/cf.pm 2007/08/26 15:31:02 1.342 @@ -12,7 +12,7 @@ use Safe; use Safe::Hole; -use Coro 3.61 (); +use Coro 3.64 (); use Coro::State; use Coro::Handle; use Coro::Event; @@ -21,6 +21,7 @@ use Coro::Semaphore; use Coro::AIO; use Coro::Storable; +use Coro::Util (); use JSON::XS 1.4 (); use BDB (); @@ -474,57 +475,19 @@ =cut -sub _store_scalar { - open my $fh, ">", \my $buf - or die "fork_call: cannot open fh-to-buf in child : $!"; - Storable::store_fd $_[0], $fh; - close $fh; - - $buf -} - sub fork_call(&@) { my ($cb, @args) = @_; -# socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC -# or die "socketpair: $!"; - pipe my $fh1, my $fh2 - or die "pipe: $!"; - - if (my $pid = fork) { - close $fh2; - - my $res = (Coro::Handle::unblock $fh1)->readline (undef); - warn "pst<$res>" unless $res =~ /^pst/; - $res = Coro::Storable::thaw $res; - - waitpid $pid, 0; # should not block anymore, we expect the child to simply behave + # we seemingly have to make a local copy of the whole thing, + # otherwise perl prematurely frees the stuff :/ + # TODO: investigate and fix (liekly this will be rather laborious) - Carp::confess $$res unless "ARRAY" eq ref $res; - - return wantarray ? @$res : $res->[-1]; - } else { + my @res = Coro::Util::fork_eval { reset_signals; - local $SIG{__WARN__}; - local $SIG{__DIE__}; - # just in case, this hack effectively disables event - # in the child. cleaner and slower would be canceling all watchers, - # but this works for the time being. - local $Coro::idle; - $Coro::current->prio (Coro::PRIO_MAX); + &$cb + }, @args; - eval { - close $fh1; - - my @res = eval { $cb->(@args) }; - - syswrite $fh2, _store_scalar $@ ? \"$@" : \@res; - close $fh2; - }; - - warn $@ if $@; - _exit 0; - } + wantarray ? @res : $res[-1] } =item $value = cf::db_get $family => $key @@ -1188,12 +1151,15 @@ ? @$msg : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove - if (my $cb = $EXTCMD{$type}) { - my @reply = $cb->($pl, @payload); + my @reply; - $pl->ext_reply ($reply, @reply) - if $reply; + if (my $cb = $EXTCMD{$type}) { + @reply = $cb->($pl, @payload); } + + $pl->ext_reply ($reply, @reply) + if $reply; + } else { warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; } @@ -1544,13 +1510,7 @@ sub ext_reply($$@) { my ($self, $id, @msg) = @_; - if ($self->ns->extcmd == 2) { - $self->send ("ext " . $self->ns->{json_coder}->encode (["reply-$id", @msg])); - } elsif ($self->ns->extcmd == 1) { - #TODO: version 1, remove - unshift @msg, msgtype => "reply", msgid => $id; - $self->send ("ext " . $self->ns->{json_coder}->encode ({@msg})); - } + $self->ns->ext_reply ($id, @msg) } =item $player->ext_msg ($type, @msg) @@ -1955,10 +1915,10 @@ } # find and load all maps in the 3x3 area around a map -sub load_diag { +sub load_neighbours { my ($map) = @_; - my @diag; # diagonal neighbours + my @neigh; # diagonal neighbours for (0 .. 3) { my $neigh = $map->tile_path ($_) @@ -1967,12 +1927,14 @@ or next; $neigh->load; - push @diag, [$neigh->tile_path (($_ + 3) % 4), $neigh], - [$neigh->tile_path (($_ + 1) % 4), $neigh]; + push @neigh, + [$neigh->tile_path (($_ + 3) % 4), $neigh], + [$neigh->tile_path (($_ + 1) % 4), $neigh]; } - for (@diag) { - my $neigh = find @$_ + for (grep defined $_->[0], @neigh) { + my ($path, $origin) = @$_; + my $neigh = find $path, $origin or next; $neigh->load; } @@ -1987,6 +1949,9 @@ sub do_load_sync { my ($map) = @_; + cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" + if $Coro::current == $Coro::main; + cf::sync_job { $map->load }; } @@ -1994,23 +1959,24 @@ our $MAP_PREFETCHER = undef; sub find_async { - my ($path, $origin) = @_; + my ($path, $origin, $load) = @_; $path = normalise $path, $origin && $origin->{path}; if (my $map = $cf::MAP{$path}) { - return $map if $map->in_memory == cf::MAP_IN_MEMORY; + return $map if !$load || $map->in_memory == cf::MAP_IN_MEMORY; } - undef $MAP_PREFETCH{$path}; + $MAP_PREFETCH{$path} |= $load; + $MAP_PREFETCHER ||= cf::async { while (%MAP_PREFETCH) { - for my $path (keys %MAP_PREFETCH) { - if (my $map = find $path) { - $map->load; + while (my ($k, $v) = each %MAP_PREFETCH) { + if (my $map = find $k) { + $map->load if $v; } - delete $MAP_PREFETCH{$path}; + delete $MAP_PREFETCH{$k}; } } undef $MAP_PREFETCHER; @@ -2351,7 +2317,7 @@ if $x <=0 && $y <= 0; $map->load; - $map->load_diag; + $map->load_neighbours; return unless $self->contr->active; $self->activate_recursive; @@ -2604,6 +2570,24 @@ } } +=item $client->ext_reply ($msgid, @msg) + +Sends an ext reply to the client. + +=cut + +sub cf::client::ext_reply($$@) { + my ($self, $id, @msg) = @_; + + if ($self->extcmd == 2) { + $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); + } elsif ($self->ns->extcmd == 1) { + #TODO: version 1, remove + unshift @msg, msgtype => "reply", msgid => $id; + $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); + } +} + =item $success = $client->query ($flags, "text", \&cb) Queues a query to the client, calling the given callback with @@ -2671,12 +2655,15 @@ ? @$msg : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove - if (my $cb = $EXTICMD{$type}) { - my @reply = $cb->($ns, @payload); + my @reply; - $ns->ext_reply ($reply, @reply) - if $reply; + if (my $cb = $EXTICMD{$type}) { + @reply = $cb->($ns, @payload); } + + $ns->ext_reply ($reply, @reply) + if $reply; + } else { warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; } @@ -2747,7 +2734,7 @@ cf::object contr pay_amount pay_player map x y force_find force_add - insert remove name archname title slaying race + insert remove name archname title slaying race decrease_ob_nr cf::object::player player @@ -2762,7 +2749,8 @@ for ( ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y - insert remove inv name archname title slaying race)], + insert remove inv name archname title slaying race + decrease_ob_nr)], ["cf::object::player" => qw(player)], ["cf::player" => qw(peaceful)], ["cf::map" => qw(trigger)], @@ -2848,6 +2836,8 @@ sub load_facedata($) { my ($path) = @_; + my $enc = JSON::XS->new->utf8->canonical; + warn "loading facedata from $path\n"; my $facedata; @@ -2859,6 +2849,13 @@ $facedata->{version} == 2 or cf::cleanup "$path: version mismatch, cannot proceed."; + # patch in the exptable + $facedata->{resource}{"res/exp_table"} = { + type => FT_RSRC, + data => $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]), + }; + cf::cede_to_tick; + { my $faces = $facedata->{faceinfo}; @@ -2866,8 +2863,8 @@ my $idx = (cf::face::find $face) || cf::face::alloc $face; cf::face::set_visibility $idx, $info->{visibility}; cf::face::set_magicmap $idx, $info->{magicmap}; - cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; - cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; + cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; + cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; cf::cede_to_tick; } @@ -2902,29 +2899,27 @@ # TODO: for gcfclient pleasure, we should give resources # that gcfclient doesn't grok a >10000 face index. my $res = $facedata->{resource}; - my $enc = JSON::XS->new->utf8->canonical; my $soundconf = delete $res->{"res/sound.conf"}; while (my ($name, $info) = each %$res) { - my $meta = $enc->encode ({ - name => $name, - %{ $info->{meta} || {} }, - }); - my $idx = (cf::face::find $name) || cf::face::alloc $name; + my $data; if ($info->{type} & 1) { # prepend meta info - my $data = pack "(w/a*)*", $meta, $info->{data}; - my $chk = Digest::MD5::md5 "$info->{chksum},$meta"; # mangle data checksum and metadata + my $meta = $enc->encode ({ + name => $name, + %{ $info->{meta} || {} }, + }); - cf::face::set_data $idx, 0, $data, $chk; + $data = pack "(w/a*)*", $meta, $info->{data}; } else { - cf::face::set_data $idx, 0, $info->{data}, $info->{chksum}; + $data = $info->{data}; } + cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; cf::face::set_type $idx, $info->{type}; cf::cede_to_tick;