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.593 by root, Fri Nov 9 01:59:33 2012 UTC vs.
Revision 1.594 by root, Fri Nov 9 02:50:50 2012 UTC

87our %COMMAND = (); 87our %COMMAND = ();
88our %COMMAND_TIME = (); 88our %COMMAND_TIME = ();
89 89
90our @EXTS = (); # list of extension package names 90our @EXTS = (); # list of extension package names
91our %EXTCMD = (); 91our %EXTCMD = ();
92our %EXTACMD = ();
92our %EXTICMD = (); 93our %EXTICMD = ();
94our %EXTIACMD = ();
93our %EXT_CORO = (); # coroutines bound to extensions 95our %EXT_CORO = (); # coroutines bound to extensions
94our %EXT_MAP = (); # pluggable maps 96our %EXT_MAP = (); # pluggable maps
95 97
96our $RELOAD; # number of reloads so far, non-zero while in reload 98our $RELOAD; # number of reloads so far, non-zero while in reload
97our @EVENT; 99our @EVENT;
1447 #warn "registering command '$name/$time' to '$caller'"; 1449 #warn "registering command '$name/$time' to '$caller'";
1448 1450
1449 push @{ $COMMAND{$name} }, [$caller, $cb]; 1451 push @{ $COMMAND{$name} }, [$caller, $cb];
1450} 1452}
1451 1453
1452=item cf::register_extcmd $name => \&callback($pl,$packet) 1454=item cf::register_extcmd $name => \&callback($pl,@args)
1453 1455
1454Register a callback for execution when the client sends an (synchronous) 1456Register a callback for execution when the client sends an (synchronous)
1455extcmd packet. Ext commands will be processed in the order they are 1457extcmd packet. Ext commands will be processed in the order they are
1456received by the server, like other user commands. The first argument is 1458received by the server, like other user commands. The first argument is
1457the logged-in player. Ext commands can only be processed after a player 1459the logged-in player. Ext commands can only be processed after a player
1458has logged in successfully. 1460has logged in successfully.
1459 1461
1460If the callback returns something, it is sent back as if reply was being 1462The values will be sent back to the client.
1461called.
1462 1463
1464=item cf::register_async_extcmd $name => \&callback($pl,$reply->(...),@args)
1465
1466Same as C<cf::register_extcmd>, but instead of returning values, the
1467callback needs to clal the C<$reply> function.
1468
1463=item cf::register_exticmd $name => \&callback($ns,$packet) 1469=item cf::register_exticmd $name => \&callback($ns,@args)
1464 1470
1465Register a callback for execution when the client sends an (asynchronous) 1471Register a callback for execution when the client sends an (asynchronous)
1466exticmd packet. Exti commands are processed by the server as soon as they 1472exticmd packet. Exti commands are processed by the server as soon as they
1467are received, i.e. out of order w.r.t. other commands. The first argument 1473are received, i.e. out of order w.r.t. other commands. The first argument
1468is a client socket. Exti commands can be received anytime, even before 1474is a client socket. Exti commands can be received anytime, even before
1469log-in. 1475log-in.
1470 1476
1471If the callback returns something, it is sent back as if reply was being 1477The values will be sent back to the client.
1472called.
1473 1478
1474=cut 1479=item cf::register_async_exticmd $name => \&callback($ns,$reply->(...),@args)
1475 1480
1481Same as C<cf::register_extcmd>, but instead of returning values, the
1482callback needs to clal the C<$reply> function.
1483
1484=cut
1485
1476sub register_extcmd { 1486sub register_extcmd($$) {
1477 my ($name, $cb) = @_; 1487 my ($name, $cb) = @_;
1478 1488
1479 $EXTCMD{$name} = $cb; 1489 $EXTCMD{$name} = $cb;
1480} 1490}
1481 1491
1482sub register_exticmd { 1492sub register_async_extcmd($$) {
1483 my ($name, $cb) = @_; 1493 my ($name, $cb) = @_;
1484 1494
1495 $EXTACMD{$name} = $cb;
1496}
1497
1498sub register_exticmd($$) {
1499 my ($name, $cb) = @_;
1500
1485 $EXTICMD{$name} = $cb; 1501 $EXTICMD{$name} = $cb;
1502}
1503
1504sub register_async_exticmd($$) {
1505 my ($name, $cb) = @_;
1506
1507 $EXTIACMD{$name} = $cb;
1486} 1508}
1487 1509
1488use File::Glob (); 1510use File::Glob ();
1489 1511
1490cf::player->attach ( 1512cf::player->attach (
1506 my $msg = eval { $pl->ns->{json_coder}->decode ($buf) }; 1528 my $msg = eval { $pl->ns->{json_coder}->decode ($buf) };
1507 1529
1508 if (ref $msg) { 1530 if (ref $msg) {
1509 my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash 1531 my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash
1510 1532
1511 my @reply;
1512
1513 if (my $cb = $EXTCMD{$type}) { 1533 if (my $cb = $EXTACMD{$type}) {
1534 $cb->(
1535 $pl,
1536 sub {
1537 $pl->ext_msg ("reply-$reply", @_)
1538 if $reply;
1539 },
1540 @payload
1541 );
1542 } else {
1543 my @reply;
1544
1545 if (my $cb = $EXTCMD{$type}) {
1514 @reply = $cb->($pl, @payload); 1546 @reply = $cb->($pl, @payload);
1547 }
1548
1549 $pl->ext_msg ("reply-$reply", @reply)
1550 if $reply;
1515 } 1551 }
1516
1517 $pl->ext_msg ("reply-$reply", @reply)
1518 if $reply;
1519 1552
1520 } else { 1553 } else {
1521 error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 1554 error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
1522 } 1555 }
1523 1556
3290 my $msg = eval { $ns->{json_coder}->decode ($buf) }; 3323 my $msg = eval { $ns->{json_coder}->decode ($buf) };
3291 3324
3292 if (ref $msg) { 3325 if (ref $msg) {
3293 my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash 3326 my ($type, $reply, @payload) = @$msg; # version 1 used %type, $id, %$hash
3294 3327
3295 my @reply;
3296
3297 if (my $cb = $EXTICMD{$type}) { 3328 if (my $cb = $EXTIACMD{$type}) {
3329 $cb->(
3330 $ns,
3331 sub {
3332 $ns->ext_msg ("reply-$reply", @_)
3333 if $reply;
3334 },
3335 @payload
3336 );
3337 } else {
3338 my @reply;
3339
3340 if (my $cb = $EXTICMD{$type}) {
3298 @reply = $cb->($ns, @payload); 3341 @reply = $cb->($ns, @payload);
3342 }
3343
3344 $ns->ext_msg ("reply-$reply", @reply)
3345 if $reply;
3299 } 3346 }
3300
3301 $ns->ext_msg ("reply-$reply", @reply)
3302 if $reply;
3303
3304 } else { 3347 } else {
3305 error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 3348 error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
3306 } 3349 }
3307 3350
3308 cf::override; 3351 cf::override;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines