ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.95 by root, Fri Dec 22 02:04:20 2006 UTC vs.
Revision 1.96 by root, Fri Dec 22 06:02:29 2006 UTC

1package cf; 1package cf;
2
3use utf8;
4use strict;
2 5
3use Symbol; 6use Symbol;
4use List::Util; 7use List::Util;
5use Storable; 8use Storable;
6use Opcode; 9use Opcode;
7use Safe; 10use Safe;
8use Safe::Hole; 11use Safe::Hole;
9 12
13use Coro;
14use Coro::Event;
15use Coro::Timer;
16use Coro::Signal;
17use Coro::Semaphore;
18
10use IO::AIO (); 19use IO::AIO;
11use YAML::Syck (); 20use YAML::Syck ();
12use Time::HiRes; 21use Time::HiRes;
13use Event; 22
14$Event::Eval = 1; # no idea why this is required, but it is 23use Event; $Event::Eval = 1; # no idea why this is required, but it is
15 24
16# work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? 25# work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode?
17$YAML::Syck::ImplicitUnicode = 1; 26$YAML::Syck::ImplicitUnicode = 1;
18 27
19use strict; 28$Coro::main->prio (Coro::PRIO_MIN);
20 29
21sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload 30sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
22 31
23our %COMMAND = (); 32our %COMMAND = ();
24our %COMMAND_TIME = (); 33our %COMMAND_TIME = ();
249 my ($self, $victim) = @_; 258 my ($self, $victim) = @_;
250 ... 259 ...
251 } 260 }
252 } 261 }
253 262
263=item $attachable->valid
264
265Just because you have a perl object does not mean that the corresponding
266C-level object still exists. If you try to access an object that has no
267valid C counterpart anymore you get an exception at runtime. This method
268can be used to test for existence of the C object part without causing an
269exception.
270
254=cut 271=cut
255 272
256# the following variables are defined in .xs and must not be re-created 273# the following variables are defined in .xs and must not be re-created
257our @CB_GLOBAL = (); # registry for all global events 274our @CB_GLOBAL = (); # registry for all global events
258our @CB_OBJECT = (); # all objects (should not be used except in emergency) 275our @CB_OBJECT = (); # all objects (should not be used except in emergency)
413 } 430 }
414 431
415 0 432 0
416} 433}
417 434
418=item $bool = cf::invoke EVENT_GLOBAL_XXX, ... 435=item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...)
419 436
420=item $bool = $object->invoke (EVENT_OBJECT_XXX, ...)
421
422=item $bool = $player->invoke (EVENT_PLAYER_XXX, ...)
423
424=item $bool = $client->invoke (EVENT_CLIENT_XXX, ...) 437=item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...)
425 438
426=item $bool = $map->invoke (EVENT_MAP_XXX, ...)
427
428Generate a global/object/player/map-specific event with the given arguments. 439Generate an object-specific event with the given arguments.
429 440
430This API is preliminary (most likely, the EVENT_KLASS_xxx prefix will be 441This API is preliminary (most likely, the EVENT_CLASS_xxx prefix will be
431removed in future versions), and there is no public API to access override 442removed in future versions), and there is no public API to access override
432results (if you must, access C<@cf::invoke_results> directly). 443results (if you must, access C<@cf::invoke_results> directly).
433
434=back
435
436=cut
437
438#############################################################################
439
440=head2 METHODS VALID FOR ALL ATTACHABLE OBJECTS
441
442Attachable objects includes objects, players, clients and maps.
443
444=over 4
445
446=item $object->valid
447
448Just because you have a perl object does not mean that the corresponding
449C-level object still exists. If you try to access an object that has no
450valid C counterpart anymore you get an exception at runtime. This method
451can be used to test for existence of the C object part without causing an
452exception.
453 444
454=back 445=back
455 446
456=cut 447=cut
457 448
898 $ns->send_packet ($ns->{query_queue}[0][0]); 889 $ns->send_packet ($ns->{query_queue}[0][0]);
899 } else { 890 } else {
900 $ns->state (ST_PLAYING); 891 $ns->state (ST_PLAYING);
901 } 892 }
902 } 893 }
894 },
895);
896
897=item $client->coro (\&cb)
898
899Create a new coroutine, running the specified callback. The coroutine will
900be automatically cancelled when the client gets destroyed (e.g. on logout,
901or loss of connection).
902
903=cut
904
905sub cf::client::coro {
906 my ($self, $cb) = @_;
907
908 my $coro; $coro = async {
909 eval {
910 $cb->();
911 };
912 warn $@ if $@;
913 warn "cancel myself\n";#d#
914 delete $self->{_coro}{$coro+0};
915 };
916
917 $self->{_coro}{$coro+0} = $coro;
918}
919
920cf::client->attach (
921 on_destroy => sub {
922 my ($ns) = @_;
923
924 warn "cancel $_" for values %{ $ns->{_coro} || {} };#d#
925 $_->cancel for values %{ $ns->{_coro} || {} };
903 }, 926 },
904); 927);
905 928
906=back 929=back
907 930

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines