… | |
… | |
166 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
166 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
167 | |
167 | |
168 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
168 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
169 | returns directly I<after> the tick processing (and consequently, can only wake one process |
169 | returns directly I<after> the tick processing (and consequently, can only wake one process |
170 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
170 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
|
|
171 | |
|
|
172 | =item @cf::INVOKE_RESULTS |
|
|
173 | |
|
|
174 | This array contains the results of the last C<invoke ()> call. When |
|
|
175 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
|
|
176 | that call. |
171 | |
177 | |
172 | =back |
178 | =back |
173 | |
179 | |
174 | =cut |
180 | =cut |
175 | |
181 | |
… | |
… | |
316 | }; |
322 | }; |
317 | $TICK_WATCHER->stop; |
323 | $TICK_WATCHER->stop; |
318 | $guard |
324 | $guard |
319 | } |
325 | } |
320 | |
326 | |
|
|
327 | =item cf::get_slot $time[, $priority] |
|
|
328 | |
|
|
329 | Allocate $time seconds of blocking CPU time at priority C<$priority>: |
|
|
330 | This call blocks and returns only when you have at least C<$time> seconds |
|
|
331 | of cpu time till the next tick. The slot is only valid till the next cede. |
|
|
332 | |
|
|
333 | Useful for short background jobs. |
|
|
334 | |
|
|
335 | =cut |
|
|
336 | |
|
|
337 | our @SLOT_QUEUE; |
|
|
338 | our $SLOT_QUEUE; |
|
|
339 | |
|
|
340 | $SLOT_QUEUE->cancel if $SLOT_QUEUE; |
|
|
341 | $SLOT_QUEUE = Coro::async { |
|
|
342 | my $signal = new Coro::Signal; |
|
|
343 | |
|
|
344 | while () { |
|
|
345 | next_job: |
|
|
346 | my $avail = cf::till_tick; |
|
|
347 | if ($avail > 0.01) { |
|
|
348 | for (0 .. $#SLOT_QUEUE) { |
|
|
349 | if ($SLOT_QUEUE[$_][0] < $avail) { |
|
|
350 | my $job = splice @SLOT_QUEUE, $_, 1, (); |
|
|
351 | $job->[2]->send; |
|
|
352 | Coro::cede; |
|
|
353 | goto next_job; |
|
|
354 | } |
|
|
355 | } |
|
|
356 | } |
|
|
357 | |
|
|
358 | if (@SLOT_QUEUE) { |
|
|
359 | # we do not use wait_For_tick() as it returns immediately when tick is inactive |
|
|
360 | push @cf::WAIT_FOR_TICK, $signal; |
|
|
361 | $signal->wait; |
|
|
362 | } else { |
|
|
363 | Coro::schedule; |
|
|
364 | } |
|
|
365 | } |
|
|
366 | }; |
|
|
367 | |
|
|
368 | sub get_slot($;$) { |
|
|
369 | my ($time, $pri) = @_; |
|
|
370 | |
|
|
371 | push @SLOT_QUEUE, [$time, $pri, my $sig = new Coro::Signal]; |
|
|
372 | @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE; |
|
|
373 | $SLOT_QUEUE->ready; |
|
|
374 | $sig->wait; |
|
|
375 | } |
|
|
376 | |
321 | =item cf::async { BLOCK } |
377 | =item cf::async { BLOCK } |
322 | |
378 | |
323 | Currently the same as Coro::async_pool, meaning you cannot use |
379 | Currently the same as Coro::async_pool, meaning you cannot use |
324 | C<on_destroy>, C<join> or other gimmicks on these coroutines. The only |
380 | C<on_destroy>, C<join> or other gimmicks on these coroutines. The only |
325 | thing you are allowed to do is call C<prio> on it. |
381 | thing you are allowed to do is call C<prio> on it. |
… | |
… | |
866 | "; |
922 | "; |
867 | die if $@; |
923 | die if $@; |
868 | } |
924 | } |
869 | |
925 | |
870 | our $override; |
926 | our $override; |
871 | our @invoke_results = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
927 | our @INVOKE_RESULTS = (); # referenced from .xs code. TODO: play tricks with reify and mortals? |
872 | |
928 | |
873 | sub override { |
929 | sub override { |
874 | $override = 1; |
930 | $override = 1; |
875 | @invoke_results = (@_); |
931 | @INVOKE_RESULTS = (@_); |
876 | } |
932 | } |
877 | |
933 | |
878 | sub do_invoke { |
934 | sub do_invoke { |
879 | my $event = shift; |
935 | my $event = shift; |
880 | my $callbacks = shift; |
936 | my $callbacks = shift; |
881 | |
937 | |
882 | @invoke_results = (); |
938 | @INVOKE_RESULTS = (); |
883 | |
939 | |
884 | local $override; |
940 | local $override; |
885 | |
941 | |
886 | for (@$callbacks) { |
942 | for (@$callbacks) { |
887 | eval { &{$_->[1]} }; |
943 | eval { &{$_->[1]} }; |
… | |
… | |
904 | |
960 | |
905 | Generate an object-specific event with the given arguments. |
961 | Generate an object-specific event with the given arguments. |
906 | |
962 | |
907 | This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be |
963 | This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be |
908 | removed in future versions), and there is no public API to access override |
964 | removed in future versions), and there is no public API to access override |
909 | results (if you must, access C<@cf::invoke_results> directly). |
965 | results (if you must, access C<@cf::INVOKE_RESULTS> directly). |
910 | |
966 | |
911 | =back |
967 | =back |
912 | |
968 | |
913 | =cut |
969 | =cut |
914 | |
970 | |
915 | ############################################################################# |
971 | ############################################################################# |
916 | # object support |
972 | # object support |
|
|
973 | # |
|
|
974 | |
|
|
975 | sub _can_merge { |
|
|
976 | my ($ob1, $ob2) = @_; |
|
|
977 | |
|
|
978 | local $Storable::canonical = 1; |
|
|
979 | my $fob1 = Storable::freeze $ob1; |
|
|
980 | my $fob2 = Storable::freeze $ob2; |
|
|
981 | |
|
|
982 | $fob1 eq $fob2 |
|
|
983 | } |
917 | |
984 | |
918 | sub reattach { |
985 | sub reattach { |
919 | # basically do the same as instantiate, without calling instantiate |
986 | # basically do the same as instantiate, without calling instantiate |
920 | my ($obj) = @_; |
987 | my ($obj) = @_; |
921 | |
988 | |
… | |
… | |
2419 | |
2486 | |
2420 | utf8::encode $text; |
2487 | utf8::encode $text; |
2421 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2488 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
2422 | } |
2489 | } |
2423 | |
2490 | |
2424 | =item $client->send_msg ($color, $type, $msg, [extra...]) |
2491 | =item $client->send_msg ($channel, $msg, $color, [extra...]) |
2425 | |
2492 | |
2426 | Send a drawinfo or msg packet to the client, formatting the msg for the |
2493 | Send a drawinfo or msg packet to the client, formatting the msg for the |
2427 | client if neccessary. C<$type> should be a string identifying the type of |
2494 | client if neccessary. C<$type> should be a string identifying the type of |
2428 | the message, with C<log> being the default. If C<$color> is negative, suppress |
2495 | the message, with C<log> being the default. If C<$color> is negative, suppress |
2429 | the message unless the client supports the msg packet. |
2496 | the message unless the client supports the msg packet. |
2430 | |
2497 | |
2431 | =cut |
2498 | =cut |
2432 | |
2499 | |
2433 | sub cf::client::send_msg { |
2500 | sub cf::client::send_msg { |
2434 | my ($self, $color, $type, $msg, @extra) = @_; |
2501 | my ($self, $channel, $msg, $color, @extra) = @_; |
2435 | |
2502 | |
2436 | $msg = $self->pl->expand_cfpod ($msg); |
2503 | $msg = $self->pl->expand_cfpod ($msg); |
2437 | |
2504 | |
|
|
2505 | $color &= ~cf::NDI_UNIQUE; # just in case... |
|
|
2506 | |
|
|
2507 | if (ref $channel) { |
|
|
2508 | # send meta info to client, if not yet sent |
|
|
2509 | unless (exists $self->{channel}{$channel->{id}}) { |
|
|
2510 | $self->{channel}{$channel->{id}} = $channel; |
|
|
2511 | $self->ext_event (channel_info => %$channel); |
|
|
2512 | } |
|
|
2513 | |
|
|
2514 | $channel = $channel->{id}; |
|
|
2515 | } |
|
|
2516 | |
2438 | return unless @extra || length $msg; |
2517 | return unless @extra || length $msg; |
2439 | |
2518 | |
2440 | if ($self->can_msg) { |
2519 | if ($self->can_msg) { |
2441 | $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $type, $msg, @extra])); |
2520 | $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $channel, $msg, @extra])); |
2442 | } else { |
2521 | } else { |
2443 | # replace some tags by gcfclient-compatible ones |
2522 | # replace some tags by gcfclient-compatible ones |
2444 | for ($msg) { |
2523 | for ($msg) { |
2445 | 1 while |
2524 | 1 while |
2446 | s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ |
2525 | s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ |