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.289 by root, Sat Jun 30 03:00:54 2007 UTC vs.
Revision 1.290 by root, Mon Jul 2 03:15:30 2007 UTC

248 248
249Converts a perl data structure into its JSON representation. 249Converts a perl data structure into its JSON representation.
250 250
251=cut 251=cut
252 252
253our $json_coder = JSON::XS->new->convert_blessed->utf8->max_size (1e6); # accept ~1mb max 253our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max
254 254
255sub to_json ($) { $json_coder->encode ($_[0]) } 255sub to_json ($) { $json_coder->encode ($_[0]) }
256sub from_json ($) { $json_coder->decode ($_[0]) } 256sub from_json ($) { $json_coder->decode ($_[0]) }
257 257
258=item cf::lock_wait $string 258=item cf::lock_wait $string
1086 cf::override; 1086 cf::override;
1087 }, 1087 },
1088 on_extcmd => sub { 1088 on_extcmd => sub {
1089 my ($pl, $buf) = @_; 1089 my ($pl, $buf) = @_;
1090 1090
1091 my $msg = eval { from_json $buf }; 1091 my $msg = eval { $pl->ns->{json_coder}->decode ($buf) };
1092 1092
1093 if (ref $msg) { 1093 if (ref $msg) {
1094 if (my $cb = $EXTCMD{$msg->{msgtype}}) { 1094 if (my $cb = $EXTCMD{$msg->{msgtype}}) {
1095 if (my %reply = $cb->($pl, $msg)) { 1095 if (my %reply = $cb->($pl, $msg)) {
1096 $pl->ext_reply ($msg->{msgid}, %reply); 1096 $pl->ext_reply ($msg->{msgid}, %reply);
1418 1418
1419sub ext_reply($$%) { 1419sub ext_reply($$%) {
1420 my ($self, $id, %msg) = @_; 1420 my ($self, $id, %msg) = @_;
1421 1421
1422 $msg{msgid} = $id; 1422 $msg{msgid} = $id;
1423 $self->send ("ext " . cf::to_json \%msg); 1423 $self->send ("ext " . $self->ns->{json_coder}->encode (\%msg));
1424} 1424}
1425 1425
1426=item $player->ext_event ($type, %msg) 1426=item $player->ext_event ($type, %msg)
1427 1427
1428Sends an ext event to the client. 1428Sends an ext event to the client.
2391 my ($self, $color, $type, $msg, @extra) = @_; 2391 my ($self, $color, $type, $msg, @extra) = @_;
2392 2392
2393 $msg = $self->pl->expand_cfpod ($msg); 2393 $msg = $self->pl->expand_cfpod ($msg);
2394 2394
2395 if ($self->can_msg) { 2395 if ($self->can_msg) {
2396 $self->send_packet ("msg " . cf::to_json [$color, $type, $msg, @extra]); 2396 $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $type, $msg, @extra]));
2397 } else { 2397 } else {
2398 # replace some tags by gcfclient-compatible ones 2398 # replace some tags by gcfclient-compatible ones
2399 for ($msg) { 2399 for ($msg) {
2400 1 while 2400 1 while
2401 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ 2401 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/
2424 2424
2425sub cf::client::ext_event($$%) { 2425sub cf::client::ext_event($$%) {
2426 my ($self, $type, %msg) = @_; 2426 my ($self, $type, %msg) = @_;
2427 2427
2428 $msg{msgtype} = "event_$type"; 2428 $msg{msgtype} = "event_$type";
2429 $self->send_packet ("ext " . cf::to_json \%msg); 2429 $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg));
2430} 2430}
2431 2431
2432=item $success = $client->query ($flags, "text", \&cb) 2432=item $success = $client->query ($flags, "text", \&cb)
2433 2433
2434Queues a query to the client, calling the given callback with 2434Queues a query to the client, calling the given callback with
2457 2457
2458 1 2458 1
2459} 2459}
2460 2460
2461cf::client->attach ( 2461cf::client->attach (
2462 on_connect => sub {
2463 my ($ns) = @_;
2464
2465 $ns->{json_coder} = JSON::XS->new->utf8->max_size (1e6)->convert_blessed;
2466 },
2462 on_reply => sub { 2467 on_reply => sub {
2463 my ($ns, $msg) = @_; 2468 my ($ns, $msg) = @_;
2464 2469
2465 # this weird shuffling is so that direct followup queries 2470 # this weird shuffling is so that direct followup queries
2466 # get handled first 2471 # get handled first
2481 } 2486 }
2482 }, 2487 },
2483 on_exticmd => sub { 2488 on_exticmd => sub {
2484 my ($ns, $buf) = @_; 2489 my ($ns, $buf) = @_;
2485 2490
2486 my $msg = eval { from_json $buf }; 2491 my $msg = eval { $ns->{json_coder}->decode ($buf) };
2487 2492
2488 if (ref $msg) { 2493 if (ref $msg) {
2489 if (my $cb = $EXTICMD{$msg->{msgtype}}) { 2494 if (my $cb = $EXTICMD{$msg->{msgtype}}) {
2490 if (my %reply = $cb->($ns, $msg)) { 2495 if (my %reply = $cb->($ns, $msg)) {
2491 $reply{msgid} = $msg->{msgid}; 2496 $reply{msgid} = $msg->{msgid};
2492 $ns->send ("ext " . cf::to_json \%reply); 2497 $ns->send ("ext " . $ns->{json_coder}->encode (\%reply));
2493 } 2498 }
2494 } 2499 }
2495 } else { 2500 } else {
2496 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 2501 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
2497 } 2502 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines