… | |
… | |
87 | our %COMMAND = (); |
87 | our %COMMAND = (); |
88 | our %COMMAND_TIME = (); |
88 | our %COMMAND_TIME = (); |
89 | |
89 | |
90 | our @EXTS = (); # list of extension package names |
90 | our @EXTS = (); # list of extension package names |
91 | our %EXTCMD = (); |
91 | our %EXTCMD = (); |
|
|
92 | our %EXTACMD = (); |
92 | our %EXTICMD = (); |
93 | our %EXTICMD = (); |
|
|
94 | our %EXTIACMD = (); |
93 | our %EXT_CORO = (); # coroutines bound to extensions |
95 | our %EXT_CORO = (); # coroutines bound to extensions |
94 | our %EXT_MAP = (); # pluggable maps |
96 | our %EXT_MAP = (); # pluggable maps |
95 | |
97 | |
96 | our $RELOAD; # number of reloads so far, non-zero while in reload |
98 | our $RELOAD; # number of reloads so far, non-zero while in reload |
97 | our @EVENT; |
99 | our @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 | |
1454 | Register a callback for execution when the client sends an (synchronous) |
1456 | Register a callback for execution when the client sends an (synchronous) |
1455 | extcmd packet. Ext commands will be processed in the order they are |
1457 | extcmd packet. Ext commands will be processed in the order they are |
1456 | received by the server, like other user commands. The first argument is |
1458 | received by the server, like other user commands. The first argument is |
1457 | the logged-in player. Ext commands can only be processed after a player |
1459 | the logged-in player. Ext commands can only be processed after a player |
1458 | has logged in successfully. |
1460 | has logged in successfully. |
1459 | |
1461 | |
1460 | If the callback returns something, it is sent back as if reply was being |
1462 | The values will be sent back to the client. |
1461 | called. |
|
|
1462 | |
1463 | |
|
|
1464 | =item cf::register_async_extcmd $name => \&callback($pl,$reply->(...),@args) |
|
|
1465 | |
|
|
1466 | Same as C<cf::register_extcmd>, but instead of returning values, the |
|
|
1467 | callback 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 | |
1465 | Register a callback for execution when the client sends an (asynchronous) |
1471 | Register a callback for execution when the client sends an (asynchronous) |
1466 | exticmd packet. Exti commands are processed by the server as soon as they |
1472 | exticmd packet. Exti commands are processed by the server as soon as they |
1467 | are received, i.e. out of order w.r.t. other commands. The first argument |
1473 | are received, i.e. out of order w.r.t. other commands. The first argument |
1468 | is a client socket. Exti commands can be received anytime, even before |
1474 | is a client socket. Exti commands can be received anytime, even before |
1469 | log-in. |
1475 | log-in. |
1470 | |
1476 | |
1471 | If the callback returns something, it is sent back as if reply was being |
1477 | The values will be sent back to the client. |
1472 | called. |
|
|
1473 | |
1478 | |
1474 | =cut |
1479 | =item cf::register_async_exticmd $name => \&callback($ns,$reply->(...),@args) |
1475 | |
1480 | |
|
|
1481 | Same as C<cf::register_extcmd>, but instead of returning values, the |
|
|
1482 | callback needs to clal the C<$reply> function. |
|
|
1483 | |
|
|
1484 | =cut |
|
|
1485 | |
1476 | sub register_extcmd { |
1486 | sub register_extcmd($$) { |
1477 | my ($name, $cb) = @_; |
1487 | my ($name, $cb) = @_; |
1478 | |
1488 | |
1479 | $EXTCMD{$name} = $cb; |
1489 | $EXTCMD{$name} = $cb; |
1480 | } |
1490 | } |
1481 | |
1491 | |
1482 | sub register_exticmd { |
1492 | sub register_async_extcmd($$) { |
1483 | my ($name, $cb) = @_; |
1493 | my ($name, $cb) = @_; |
1484 | |
1494 | |
|
|
1495 | $EXTACMD{$name} = $cb; |
|
|
1496 | } |
|
|
1497 | |
|
|
1498 | sub register_exticmd($$) { |
|
|
1499 | my ($name, $cb) = @_; |
|
|
1500 | |
1485 | $EXTICMD{$name} = $cb; |
1501 | $EXTICMD{$name} = $cb; |
|
|
1502 | } |
|
|
1503 | |
|
|
1504 | sub register_async_exticmd($$) { |
|
|
1505 | my ($name, $cb) = @_; |
|
|
1506 | |
|
|
1507 | $EXTIACMD{$name} = $cb; |
1486 | } |
1508 | } |
1487 | |
1509 | |
1488 | use File::Glob (); |
1510 | use File::Glob (); |
1489 | |
1511 | |
1490 | cf::player->attach ( |
1512 | cf::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; |