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.286 by root, Sun Jun 24 04:27:20 2007 UTC vs.
Revision 1.287 by root, Mon Jun 25 05:43:45 2007 UTC

49our %COMMAND = (); 49our %COMMAND = ();
50our %COMMAND_TIME = (); 50our %COMMAND_TIME = ();
51 51
52our @EXTS = (); # list of extension package names 52our @EXTS = (); # list of extension package names
53our %EXTCMD = (); 53our %EXTCMD = ();
54our %EXTICMD = ();
54our %EXT_CORO = (); # coroutines bound to extensions 55our %EXT_CORO = (); # coroutines bound to extensions
55our %EXT_MAP = (); # pluggable maps 56our %EXT_MAP = (); # pluggable maps
56 57
57our $RELOAD; # number of reloads so far 58our $RELOAD; # number of reloads so far
58our @EVENT; 59our @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
241use JSON::XS qw(to_json from_json); # TODO# replace by JSON::PC once working 242use JSON::XS ();
242 243
243=item $ref = cf::from_json $json 244=item $ref = cf::from_json $json
244 245
245Converts a JSON string into the corresponding perl data structure. 246Converts 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
249Converts a perl data structure into its JSON representation. 250Converts a perl data structure into its JSON representation.
251
252=cut
253
254our $json_coder = JSON::XS->new->convert_blessed->utf8;
255
256sub to_json ($) { $json_coder->encode ($_[0]) }
257sub from_json ($) { $json_coder->decode ($_[0]) }
250 258
251=item cf::lock_wait $string 259=item cf::lock_wait $string
252 260
253Wait until the given lock is available. See cf::lock_acquire. 261Wait 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
1034Register a callback fro execution when the client sends an extcmd packet. 1042Register a callback for execution when the client sends an (synchronous)
1043extcmd packet. Ext commands will be processed in the order they are
1044received by the server, like other user commands. The first argument is
1045the logged-in player. Ext commands can only be processed after a player
1046has logged in successfully.
1035 1047
1036If the callback returns something, it is sent back as if reply was being 1048If the callback returns something, it is sent back as if reply was being
1037called. 1049called.
1038 1050
1051=item cf::register_exticmd $name => \&callback($ns,$packet);
1052
1053Register a callback for execution when the client sends an (asynchronous)
1054exticmd packet. Exti commands are processed by the server as soon as they
1055are received, i.e. out of order w.r.t. other commands. The first argument
1056is a client socket. Exti commands can be received anytime, even before
1057log-in.
1058
1059If the callback returns something, it is sent back as if reply was being
1060called.
1061
1039=cut 1062=cut
1040 1063
1041sub register_extcmd { 1064sub register_extcmd {
1042 my ($name, $cb) = @_; 1065 my ($name, $cb) = @_;
1043 1066
1044 $EXTCMD{$name} = $cb; 1067 $EXTCMD{$name} = $cb;
1068}
1069
1070sub register_exticmd {
1071 my ($name, $cb) = @_;
1072
1073 $EXTICMD{$name} = $cb;
1045} 1074}
1046 1075
1047cf::player->attach ( 1076cf::player->attach (
1048 on_command => sub { 1077 on_command => sub {
1049 my ($pl, $name, $params) = @_; 1078 my ($pl, $name, $params) = @_;
1390 1419
1391sub ext_reply($$%) { 1420sub 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
2394Sends an exti event to the client. 2422Sends an ext event to the client.
2395 2423
2396=cut 2424=cut
2397 2425
2398sub cf::client::ext_event($$%) { 2426sub cf::client::ext_event($$%) {
2399 my ($self, $type, %msg) = @_; 2427 my ($self, $type, %msg) = @_;
2406 2434
2407Queues a query to the client, calling the given callback with 2435Queues a query to the client, calling the given callback with
2408the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, 2436the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>,
2409C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>. 2437C<cf::CS_QUERY_SINGLECHAR> or C<cf::CS_QUERY_HIDEINPUT> or C<0>.
2410 2438
2411Queries can fail, so check the return code. Or don't, as queries will become 2439Queries can fail, so check the return code. Or don't, as queries will
2412reliable at some point in the future. 2440become reliable at some point in the future.
2413 2441
2414=cut 2442=cut
2415 2443
2416sub cf::client::query { 2444sub 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
2432cf::client->attach ( 2462cf::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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines