… | |
… | |
49 | our %COMMAND = (); |
49 | our %COMMAND = (); |
50 | our %COMMAND_TIME = (); |
50 | our %COMMAND_TIME = (); |
51 | |
51 | |
52 | our @EXTS = (); # list of extension package names |
52 | our @EXTS = (); # list of extension package names |
53 | our %EXTCMD = (); |
53 | our %EXTCMD = (); |
|
|
54 | our %EXTICMD = (); |
54 | our %EXT_CORO = (); # coroutines bound to extensions |
55 | our %EXT_CORO = (); # coroutines bound to extensions |
55 | our %EXT_MAP = (); # pluggable maps |
56 | our %EXT_MAP = (); # pluggable maps |
56 | |
57 | |
57 | our $RELOAD; # number of reloads so far |
58 | our $RELOAD; # number of reloads so far |
58 | our @EVENT; |
59 | our @EVENT; |
… | |
… | |
236 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
237 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
237 | $d |
238 | $d |
238 | } || "[unable to dump $_[0]: '$@']"; |
239 | } || "[unable to dump $_[0]: '$@']"; |
239 | } |
240 | } |
240 | |
241 | |
241 | use JSON::XS qw(to_json from_json); # TODO# replace by JSON::PC once working |
242 | use JSON::XS (); |
242 | |
243 | |
243 | =item $ref = cf::from_json $json |
244 | =item $ref = cf::from_json $json |
244 | |
245 | |
245 | Converts a JSON string into the corresponding perl data structure. |
246 | Converts a JSON string into the corresponding perl data structure. |
246 | |
247 | |
247 | =item $json = cf::to_json $ref |
248 | =item $json = cf::to_json $ref |
248 | |
249 | |
249 | Converts a perl data structure into its JSON representation. |
250 | Converts a perl data structure into its JSON representation. |
|
|
251 | |
|
|
252 | =cut |
|
|
253 | |
|
|
254 | our $json_coder = JSON::XS->new->convert_blessed->utf8; |
|
|
255 | |
|
|
256 | sub to_json ($) { $json_coder->encode ($_[0]) } |
|
|
257 | sub from_json ($) { $json_coder->decode ($_[0]) } |
250 | |
258 | |
251 | =item cf::lock_wait $string |
259 | =item cf::lock_wait $string |
252 | |
260 | |
253 | Wait until the given lock is available. See cf::lock_acquire. |
261 | Wait until the given lock is available. See cf::lock_acquire. |
254 | |
262 | |
… | |
… | |
1029 | push @{ $COMMAND{$name} }, [$caller, $cb]; |
1037 | push @{ $COMMAND{$name} }, [$caller, $cb]; |
1030 | } |
1038 | } |
1031 | |
1039 | |
1032 | =item cf::register_extcmd $name => \&callback($pl,$packet); |
1040 | =item cf::register_extcmd $name => \&callback($pl,$packet); |
1033 | |
1041 | |
1034 | Register a callback fro execution when the client sends an extcmd packet. |
1042 | Register a callback for execution when the client sends an (synchronous) |
|
|
1043 | extcmd packet. Ext commands will be processed in the order they are |
|
|
1044 | received by the server, like other user commands. The first argument is |
|
|
1045 | the logged-in player. Ext commands can only be processed after a player |
|
|
1046 | has logged in successfully. |
1035 | |
1047 | |
1036 | If the callback returns something, it is sent back as if reply was being |
1048 | If the callback returns something, it is sent back as if reply was being |
1037 | called. |
1049 | called. |
1038 | |
1050 | |
|
|
1051 | =item cf::register_exticmd $name => \&callback($ns,$packet); |
|
|
1052 | |
|
|
1053 | Register a callback for execution when the client sends an (asynchronous) |
|
|
1054 | exticmd packet. Exti commands are processed by the server as soon as they |
|
|
1055 | are received, i.e. out of order w.r.t. other commands. The first argument |
|
|
1056 | is a client socket. Exti commands can be received anytime, even before |
|
|
1057 | log-in. |
|
|
1058 | |
|
|
1059 | If the callback returns something, it is sent back as if reply was being |
|
|
1060 | called. |
|
|
1061 | |
1039 | =cut |
1062 | =cut |
1040 | |
1063 | |
1041 | sub register_extcmd { |
1064 | sub register_extcmd { |
1042 | my ($name, $cb) = @_; |
1065 | my ($name, $cb) = @_; |
1043 | |
1066 | |
1044 | $EXTCMD{$name} = $cb; |
1067 | $EXTCMD{$name} = $cb; |
|
|
1068 | } |
|
|
1069 | |
|
|
1070 | sub register_exticmd { |
|
|
1071 | my ($name, $cb) = @_; |
|
|
1072 | |
|
|
1073 | $EXTICMD{$name} = $cb; |
1045 | } |
1074 | } |
1046 | |
1075 | |
1047 | cf::player->attach ( |
1076 | cf::player->attach ( |
1048 | on_command => sub { |
1077 | on_command => sub { |
1049 | my ($pl, $name, $params) = @_; |
1078 | my ($pl, $name, $params) = @_; |
… | |
… | |
1390 | |
1419 | |
1391 | sub ext_reply($$%) { |
1420 | sub ext_reply($$%) { |
1392 | my ($self, $id, %msg) = @_; |
1421 | my ($self, $id, %msg) = @_; |
1393 | |
1422 | |
1394 | $msg{msgid} = $id; |
1423 | $msg{msgid} = $id; |
1395 | |
|
|
1396 | $self->send ("ext " . cf::to_json \%msg); |
1424 | $self->send ("ext " . cf::to_json \%msg); |
1397 | } |
1425 | } |
1398 | |
1426 | |
1399 | =item $player->ext_event ($type, %msg) |
1427 | =item $player->ext_event ($type, %msg) |
1400 | |
1428 | |
… | |
… | |
2389 | } |
2417 | } |
2390 | } |
2418 | } |
2391 | |
2419 | |
2392 | =item $client->ext_event ($type, %msg) |
2420 | =item $client->ext_event ($type, %msg) |
2393 | |
2421 | |
2394 | Sends an exti event to the client. |
2422 | Sends an ext event to the client. |
2395 | |
2423 | |
2396 | =cut |
2424 | =cut |
2397 | |
2425 | |
2398 | sub cf::client::ext_event($$%) { |
2426 | sub cf::client::ext_event($$%) { |
2399 | my ($self, $type, %msg) = @_; |
2427 | my ($self, $type, %msg) = @_; |
… | |
… | |
2406 | |
2434 | |
2407 | Queues a query to the client, calling the given callback with |
2435 | Queues a query to the client, calling the given callback with |
2408 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
2436 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
2409 | C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>. |
2437 | C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>. |
2410 | |
2438 | |
2411 | Queries can fail, so check the return code. Or don't, as queries will become |
2439 | Queries can fail, so check the return code. Or don't, as queries will |
2412 | reliable at some point in the future. |
2440 | become reliable at some point in the future. |
2413 | |
2441 | |
2414 | =cut |
2442 | =cut |
2415 | |
2443 | |
2416 | sub cf::client::query { |
2444 | sub cf::client::query { |
2417 | my ($self, $flags, $text, $cb) = @_; |
2445 | my ($self, $flags, $text, $cb) = @_; |
… | |
… | |
2425 | utf8::encode $text; |
2453 | utf8::encode $text; |
2426 | push @{ $self->{query_queue} }, [(sprintf "query %d %s", $flags, $text), $cb]; |
2454 | push @{ $self->{query_queue} }, [(sprintf "query %d %s", $flags, $text), $cb]; |
2427 | |
2455 | |
2428 | $self->send_packet ($self->{query_queue}[0][0]) |
2456 | $self->send_packet ($self->{query_queue}[0][0]) |
2429 | if @{ $self->{query_queue} } == 1; |
2457 | if @{ $self->{query_queue} } == 1; |
|
|
2458 | |
|
|
2459 | 1 |
2430 | } |
2460 | } |
2431 | |
2461 | |
2432 | cf::client->attach ( |
2462 | cf::client->attach ( |
2433 | on_reply => sub { |
2463 | on_reply => sub { |
2434 | my ($ns, $msg) = @_; |
2464 | my ($ns, $msg) = @_; |
… | |
… | |
2448 | $ns->send_packet ($ns->{query_queue}[0][0]); |
2478 | $ns->send_packet ($ns->{query_queue}[0][0]); |
2449 | } else { |
2479 | } else { |
2450 | $ns->state (ST_PLAYING) if $ns->state == ST_CUSTOM; |
2480 | $ns->state (ST_PLAYING) if $ns->state == ST_CUSTOM; |
2451 | } |
2481 | } |
2452 | } |
2482 | } |
|
|
2483 | }, |
|
|
2484 | on_exticmd => sub { |
|
|
2485 | my ($ns, $buf) = @_; |
|
|
2486 | |
|
|
2487 | my $msg = eval { from_json $buf }; |
|
|
2488 | |
|
|
2489 | if (ref $msg) { |
|
|
2490 | if (my $cb = $EXTICMD{$msg->{msgtype}}) { |
|
|
2491 | if (my %reply = $cb->($ns, $msg)) { |
|
|
2492 | $reply{msgid} = $msg->{msgid}; |
|
|
2493 | $ns->send ("ext " . cf::to_json \%reply); |
|
|
2494 | } |
|
|
2495 | } |
|
|
2496 | } else { |
|
|
2497 | warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
|
|
2498 | } |
|
|
2499 | |
|
|
2500 | cf::override; |
2453 | }, |
2501 | }, |
2454 | ); |
2502 | ); |
2455 | |
2503 | |
2456 | =item $client->async (\&cb) |
2504 | =item $client->async (\&cb) |
2457 | |
2505 | |
… | |
… | |
2864 | %EXT_CORO = (); |
2912 | %EXT_CORO = (); |
2865 | |
2913 | |
2866 | warn "removing commands"; |
2914 | warn "removing commands"; |
2867 | %COMMAND = (); |
2915 | %COMMAND = (); |
2868 | |
2916 | |
2869 | warn "removing ext commands"; |
2917 | warn "removing ext/exti commands"; |
2870 | %EXTCMD = (); |
2918 | %EXTCMD = (); |
|
|
2919 | %EXTICMD = (); |
2871 | |
2920 | |
2872 | warn "unloading/nuking all extensions"; |
2921 | warn "unloading/nuking all extensions"; |
2873 | for my $pkg (@EXTS) { |
2922 | for my $pkg (@EXTS) { |
2874 | warn "... unloading $pkg"; |
2923 | warn "... unloading $pkg"; |
2875 | |
2924 | |