--- deliantra/server/lib/cf.pm 2007/07/11 16:55:18 1.304 +++ deliantra/server/lib/cf.pm 2007/07/23 16:53:15 1.314 @@ -169,6 +169,12 @@ returns directly I the tick processing (and consequently, can only wake one process per tick), while cf::wait_for_tick wakes up all waiters after tick processing. +=item @cf::INVOKE_RESULTS + +This array contains the results of the last C call. When +C is called C<@cf::INVOKE_RESULTS> is set to the parameters of +that call. + =back =cut @@ -318,6 +324,56 @@ $guard } +=item cf::get_slot $time[, $priority] + +Allocate $time seconds of blocking CPU time at priority C<$priority>: +This call blocks and returns only when you have at least C<$time> seconds +of cpu time till the next tick. The slot is only valid till the next cede. + +Useful for short background jobs. + +=cut + +our @SLOT_QUEUE; +our $SLOT_QUEUE; + +$SLOT_QUEUE->cancel if $SLOT_QUEUE; +$SLOT_QUEUE = Coro::async { + my $signal = new Coro::Signal; + + while () { + next_job: + my $avail = cf::till_tick; + if ($avail > 0.01) { + for (0 .. $#SLOT_QUEUE) { + if ($SLOT_QUEUE[$_][0] < $avail) { + my $job = splice @SLOT_QUEUE, $_, 1, (); + $job->[2]->send; + Coro::cede; + goto next_job; + } + } + } + + if (@SLOT_QUEUE) { + # we do not use wait_For_tick() as it returns immediately when tick is inactive + push @cf::WAIT_FOR_TICK, $signal; + $signal->wait; + } else { + Coro::schedule; + } + } +}; + +sub get_slot($;$) { + my ($time, $pri) = @_; + + push @SLOT_QUEUE, [$time, $pri, my $sig = new Coro::Signal]; + @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE; + $SLOT_QUEUE->ready; + $sig->wait; +} + =item cf::async { BLOCK } Currently the same as Coro::async_pool, meaning you cannot use @@ -868,18 +924,18 @@ } our $override; -our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? +our @INVOKE_RESULTS = (); # referenced from .xs code. TODO: play tricks with reify and mortals? sub override { $override = 1; - @invoke_results = (); + @INVOKE_RESULTS = (@_); } sub do_invoke { my $event = shift; my $callbacks = shift; - @invoke_results = (); + @INVOKE_RESULTS = (); local $override; @@ -906,7 +962,7 @@ This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be removed in future versions), and there is no public API to access override -results (if you must, access C<@cf::invoke_results> directly). +results (if you must, access C<@cf::INVOKE_RESULTS> directly). =back @@ -914,6 +970,17 @@ ############################################################################# # object support +# + +sub _can_merge { + my ($ob1, $ob2) = @_; + + local $Storable::canonical = 1; + my $fob1 = Storable::freeze $ob1; + my $fob2 = Storable::freeze $ob2; + + $fob1 eq $fob2 +} sub reattach { # basically do the same as instantiate, without calling instantiate @@ -1621,7 +1688,7 @@ } } - Carp::carp "unable to resolve path '$path' (base '$base')."; + Carp::cluck "unable to resolve path '$path' (base '$base')."; () } @@ -1909,9 +1976,9 @@ $MAP_PREFETCHER ||= cf::async { while (%MAP_PREFETCH) { for my $path (keys %MAP_PREFETCH) { - my $map = find $path - or next; - $map->load; + if (my $map = find $path) { + $map->load; + } delete $MAP_PREFETCH{$path}; } @@ -2258,19 +2325,20 @@ $self->enter_map ($map, $x, $y); } -=item $player_object->goto ($path, $x, $y[, $check->($map)]) +=item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) Moves the player to the given map-path and coordinates by first freezing her, loading and preparing them map, calling the provided $check callback that has to return the map if sucecssful, and then unfreezes the player on -the new (success) or old (failed) map position. +the new (success) or old (failed) map position. In either case, $done will +be called at the end of this process. =cut our $GOTOGEN; sub cf::object::player::goto { - my ($self, $path, $x, $y, $check) = @_; + my ($self, $path, $x, $y, $check, $done) = @_; # do generation counting so two concurrent goto's will be executed in-order my $gen = $self->{_goto_generation} = ++$GOTOGEN; @@ -2300,6 +2368,8 @@ delete $self->{_goto_generation}; $self->leave_link ($map, $x, $y); } + + $done->() if $done; })->prio (1); } @@ -2418,7 +2488,7 @@ $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); } -=item $client->send_msg ($color, $type, $msg, [extra...]) +=item $client->send_msg ($channel, $msg, $color, [extra...]) Send a drawinfo or msg packet to the client, formatting the msg for the client if neccessary. C<$type> should be a string identifying the type of @@ -2428,14 +2498,26 @@ =cut sub cf::client::send_msg { - my ($self, $color, $type, $msg, @extra) = @_; + my ($self, $channel, $msg, $color, @extra) = @_; $msg = $self->pl->expand_cfpod ($msg); + $color &= ~cf::NDI_UNIQUE; # just in case... + + if (ref $channel) { + # send meta info to client, if not yet sent + unless (exists $self->{channel}{$channel->{id}}) { + $self->{channel}{$channel->{id}} = $channel; + $self->ext_event (channel_info => %$channel); + } + + $channel = $channel->{id}; + } + return unless @extra || length $msg; if ($self->can_msg) { - $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $type, $msg, @extra])); + $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $channel, $msg, @extra])); } else { # replace some tags by gcfclient-compatible ones for ($msg) { @@ -2467,6 +2549,8 @@ sub cf::client::ext_event($$%) { my ($self, $type, %msg) = @_; + return unless $self->extcmd; + $msg{msgtype} = "event_$type"; $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg)); } @@ -2768,12 +2852,19 @@ type => $info->{type}, copyright => $info->{copyright}, #TODO# }); - my $data = pack "(w/a*)*", $meta, $info->{data}; - my $chk = Digest::MD5::md5 "$info->{chksum},$meta"; # mangle data checksum and metadata my $idx = (cf::face::find $name) || cf::face::alloc $name; - cf::face::set_type $idx, 1; - cf::face::set_data $idx, 0, $data, $chk; + + if ($name =~ /\.jpg$/) { + cf::face::set_data $idx, 0, $info->{data}, $info->{chksum};#d# temp hack + cf::face::set_data $idx, 1, $info->{data}, $info->{chksum};#d# temp hack + } else { + my $data = pack "(w/a*)*", $meta, $info->{data}; + my $chk = Digest::MD5::md5 "$info->{chksum},$meta"; # mangle data checksum and metadata + + cf::face::set_type $idx, 1; + cf::face::set_data $idx, 0, $data, $chk; + } cf::cede_to_tick; }