--- deliantra/server/lib/cf.pm 2007/07/16 15:43:49 1.310 +++ deliantra/server/lib/cf.pm 2007/07/23 16:53:15 1.314 @@ -324,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 @@ -920,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 @@ -2427,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 @@ -2437,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) {