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.490 by root, Sat Oct 24 06:16:40 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 excluding F</styles>). 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;
2359 2361
2360 next if $dir eq "/styles"; 2362 next if $dir eq "/styles" || $dir eq "/editor";
2361 2363
2362 my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2 2364 my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2
2363 or return; 2365 or return;
2364 2366
2365 for (@$files) { 2367 for (@$files) {
2576 ($x, $y) = (-1, -1) 2578 ($x, $y) = (-1, -1)
2577 unless (defined $x) && (defined $y); 2579 unless (defined $x) && (defined $y);
2578 2580
2579 # use -1 or undef as default coordinates, not 0, 0 2581 # use -1 or undef as default coordinates, not 0, 0
2580 ($x, $y) = ($map->enter_x, $map->enter_y) 2582 ($x, $y) = ($map->enter_x, $map->enter_y)
2581 if $x <=0 && $y <= 0; 2583 if $x <= 0 && $y <= 0;
2582 2584
2583 $map->load; 2585 $map->load;
2584 $map->load_neighbours; 2586 $map->load_neighbours;
2585 2587
2586 return unless $self->contr->active; 2588 return unless $self->contr->active;
2783sub cf::client::send_drawinfo { 2785sub cf::client::send_drawinfo {
2784 my ($self, $text, $flags) = @_; 2786 my ($self, $text, $flags) = @_;
2785 2787
2786 utf8::encode $text; 2788 utf8::encode $text;
2787 $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);
2788} 2815}
2789 2816
2790=item $client->send_msg ($channel, $msg, $color, [extra...]) 2817=item $client->send_msg ($channel, $msg, $color, [extra...])
2791 2818
2792Send 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
2944 my $pkt = "msg " 2971 my $pkt = "msg "
2945 . $self->{json_coder}->encode ( 2972 . $self->{json_coder}->encode (
2946 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] 2973 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra]
2947 ); 2974 );
2948 2975
2949 # try lzf for large packets
2950 $pkt = "lzf " . Compress::LZF::compress $pkt
2951 if 1024 <= length $pkt and $self->{can_lzf};
2952
2953 # split very large packets
2954 if (8192 < length $pkt and $self->{can_lzf}) {
2955 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt;
2956 $pkt = "frag";
2957 }
2958
2959 $self->send_packet ($pkt); 2976 $self->send_big_packet ($pkt);
2960} 2977}
2961 2978
2962=item $client->ext_msg ($type, @msg) 2979=item $client->ext_msg ($type, @msg)
2963 2980
2964Sends an ext event to the client. 2981Sends an ext event to the client.
2967 2984
2968sub cf::client::ext_msg($$@) { 2985sub cf::client::ext_msg($$@) {
2969 my ($self, $type, @msg) = @_; 2986 my ($self, $type, @msg) = @_;
2970 2987
2971 if ($self->extcmd == 2) { 2988 if ($self->extcmd == 2) {
2972 $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); 2989 $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg]));
2973 } elsif ($self->extcmd == 1) { # TODO: remove 2990 } elsif ($self->extcmd == 1) { # TODO: remove
2974 push @msg, msgtype => "event_$type"; 2991 push @msg, msgtype => "event_$type";
2975 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); 2992 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2976 } 2993 }
2977} 2994}
2978 2995
2979=item $client->ext_reply ($msgid, @msg) 2996=item $client->ext_reply ($msgid, @msg)
2980 2997
2984 3001
2985sub cf::client::ext_reply($$@) { 3002sub cf::client::ext_reply($$@) {
2986 my ($self, $id, @msg) = @_; 3003 my ($self, $id, @msg) = @_;
2987 3004
2988 if ($self->extcmd == 2) { 3005 if ($self->extcmd == 2) {
2989 $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); 3006 $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg]));
2990 } elsif ($self->extcmd == 1) { 3007 } elsif ($self->extcmd == 1) {
2991 #TODO: version 1, remove 3008 #TODO: version 1, remove
2992 unshift @msg, msgtype => "reply", msgid => $id; 3009 unshift @msg, msgtype => "reply", msgid => $id;
2993 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); 3010 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2994 } 3011 }
2995} 3012}
2996 3013
2997=item $success = $client->query ($flags, "text", \&cb) 3014=item $success = $client->query ($flags, "text", \&cb)
2998 3015

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines