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.489 by root, Fri Oct 23 03:08:35 2009 UTC vs.
Revision 1.494 by root, Mon Oct 26 05:18:00 2009 UTC

32use EV; 32use EV;
33use Opcode; 33use Opcode;
34use Safe; 34use Safe;
35use Safe::Hole; 35use Safe::Hole;
36use Storable (); 36use Storable ();
37use Carp ();
37 38
38use Guard (); 39use Guard ();
39use Coro (); 40use Coro ();
40use Coro::State; 41use Coro::State;
41use Coro::Handle; 42use Coro::Handle;
289)) { 290)) {
290 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 291 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
291} 292}
292 293
293$EV::DIED = sub { 294$EV::DIED = sub {
294 warn "error in event callback: @_"; 295 Carp::cluck "error in event callback: @_";
295}; 296};
296 297
297############################################################################# 298#############################################################################
298 299
299=head2 UTILITY FUNCTIONS 300=head2 UTILITY FUNCTIONS
2344} 2345}
2345 2346
2346=item cf::map::static_maps 2347=item cf::map::static_maps
2347 2348
2348Returns an arrayref if paths of all static maps (all preinstalled F<.map> 2349Returns an arrayref if paths of all static maps (all preinstalled F<.map>
2349file in the shared directory). May block. 2350file in the shared directory excluding F</styles> and F</editor>). May
2351block.
2350 2352
2351=cut 2353=cut
2352 2354
2353sub static_maps() { 2355sub static_maps() {
2354 my @dirs = ""; 2356 my @dirs = "";
2355 my @maps; 2357 my @maps;
2356 2358
2357 while (@dirs) { 2359 while (@dirs) {
2358 my $dir = shift @dirs; 2360 my $dir = shift @dirs;
2361
2362 next if $dir eq "/styles" || $dir eq "/editor";
2359 2363
2360 my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2 2364 my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2
2361 or return; 2365 or return;
2362 2366
2363 for (@$files) { 2367 for (@$files) {
2574 ($x, $y) = (-1, -1) 2578 ($x, $y) = (-1, -1)
2575 unless (defined $x) && (defined $y); 2579 unless (defined $x) && (defined $y);
2576 2580
2577 # use -1 or undef as default coordinates, not 0, 0 2581 # use -1 or undef as default coordinates, not 0, 0
2578 ($x, $y) = ($map->enter_x, $map->enter_y) 2582 ($x, $y) = ($map->enter_x, $map->enter_y)
2579 if $x <=0 && $y <= 0; 2583 if $x <= 0 && $y <= 0;
2580 2584
2581 $map->load; 2585 $map->load;
2582 $map->load_neighbours; 2586 $map->load_neighbours;
2583 2587
2584 return unless $self->contr->active; 2588 return unless $self->contr->active;
2781sub cf::client::send_drawinfo { 2785sub cf::client::send_drawinfo {
2782 my ($self, $text, $flags) = @_; 2786 my ($self, $text, $flags) = @_;
2783 2787
2784 utf8::encode $text; 2788 utf8::encode $text;
2785 $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); 2789 $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text);
2790}
2791
2792=item $client->send_big_packet ($pkt)
2793
2794Like C<send_packet>, but tries to compress large packets, and fragments
2795them as required.
2796
2797=cut
2798
2799our $MAXFRAGSIZE = cf::MAXSOCKBUF - 64;
2800
2801sub cf::client::send_big_packet {
2802 my ($self, $pkt) = @_;
2803
2804 # try lzf for large packets
2805 $pkt = "lzf " . Compress::LZF::compress $pkt
2806 if 1024 <= length $pkt and $self->{can_lzf};
2807
2808 # split very large packets
2809 if ($MAXFRAGSIZE < length $pkt and $self->{can_lzf}) {
2810 $self->send_packet ("frag $_") for unpack "(a$MAXFRAGSIZE)*", $pkt;
2811 $pkt = "frag";
2812 }
2813
2814 $self->send_packet ($pkt);
2786} 2815}
2787 2816
2788=item $client->send_msg ($channel, $msg, $color, [extra...]) 2817=item $client->send_msg ($channel, $msg, $color, [extra...])
2789 2818
2790Send a drawinfo or msg packet to the client, formatting the msg for the 2819Send a drawinfo or msg packet to the client, formatting the msg for the
2942 my $pkt = "msg " 2971 my $pkt = "msg "
2943 . $self->{json_coder}->encode ( 2972 . $self->{json_coder}->encode (
2944 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] 2973 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra]
2945 ); 2974 );
2946 2975
2947 # try lzf for large packets
2948 $pkt = "lzf " . Compress::LZF::compress $pkt
2949 if 1024 <= length $pkt and $self->{can_lzf};
2950
2951 # split very large packets
2952 if (8192 < length $pkt and $self->{can_lzf}) {
2953 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt;
2954 $pkt = "frag";
2955 }
2956
2957 $self->send_packet ($pkt); 2976 $self->send_big_packet ($pkt);
2958} 2977}
2959 2978
2960=item $client->ext_msg ($type, @msg) 2979=item $client->ext_msg ($type, @msg)
2961 2980
2962Sends an ext event to the client. 2981Sends an ext event to the client.
2965 2984
2966sub cf::client::ext_msg($$@) { 2985sub cf::client::ext_msg($$@) {
2967 my ($self, $type, @msg) = @_; 2986 my ($self, $type, @msg) = @_;
2968 2987
2969 if ($self->extcmd == 2) { 2988 if ($self->extcmd == 2) {
2970 $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); 2989 $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg]));
2971 } elsif ($self->extcmd == 1) { # TODO: remove 2990 } elsif ($self->extcmd == 1) { # TODO: remove
2972 push @msg, msgtype => "event_$type"; 2991 push @msg, msgtype => "event_$type";
2973 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); 2992 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2974 } 2993 }
2975} 2994}
2976 2995
2977=item $client->ext_reply ($msgid, @msg) 2996=item $client->ext_reply ($msgid, @msg)
2978 2997
2982 3001
2983sub cf::client::ext_reply($$@) { 3002sub cf::client::ext_reply($$@) {
2984 my ($self, $id, @msg) = @_; 3003 my ($self, $id, @msg) = @_;
2985 3004
2986 if ($self->extcmd == 2) { 3005 if ($self->extcmd == 2) {
2987 $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); 3006 $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg]));
2988 } elsif ($self->extcmd == 1) { 3007 } elsif ($self->extcmd == 1) {
2989 #TODO: version 1, remove 3008 #TODO: version 1, remove
2990 unshift @msg, msgtype => "reply", msgid => $id; 3009 unshift @msg, msgtype => "reply", msgid => $id;
2991 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); 3010 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2992 } 3011 }
2993} 3012}
2994 3013
2995=item $success = $client->query ($flags, "text", \&cb) 3014=item $success = $client->query ($flags, "text", \&cb)
2996 3015

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines