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.485 by root, Mon Oct 12 14:12:54 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;
228returns directly I<after> the tick processing (and consequently, can only wake one process 229returns directly I<after> the tick processing (and consequently, can only wake one process
229per tick), while cf::wait_for_tick wakes up all waiters after tick processing. 230per tick), while cf::wait_for_tick wakes up all waiters after tick processing.
230 231
231=item @cf::INVOKE_RESULTS 232=item @cf::INVOKE_RESULTS
232 233
233This array contains the results of the last C<invoke ()> call. When 234This array contains the results of the last C<invoke ()> call. When
234C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of 235C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of
235that call. 236that call.
236 237
237=item %cf::REFLECT 238=item %cf::REFLECT
238 239
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
2341 : normalise $_ 2342 : normalise $_
2342 } @{ aio_readdir $UNIQUEDIR or [] } 2343 } @{ aio_readdir $UNIQUEDIR or [] }
2343 ] 2344 ]
2344} 2345}
2345 2346
2347=item cf::map::static_maps
2348
2349Returns an arrayref if paths of all static maps (all preinstalled F<.map>
2350file in the shared directory excluding F</styles> and F</editor>). May
2351block.
2352
2353=cut
2354
2355sub static_maps() {
2356 my @dirs = "";
2357 my @maps;
2358
2359 while (@dirs) {
2360 my $dir = shift @dirs;
2361
2362 next if $dir eq "/styles" || $dir eq "/editor";
2363
2364 my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2
2365 or return;
2366
2367 for (@$files) {
2368 s/\.map$// or next;
2369 utf8::decode $_;
2370 push @maps, "$dir/$_";
2371 }
2372
2373 push @dirs, map "$dir/$_", @$dirs;
2374 }
2375
2376 \@maps
2377}
2378
2346=back 2379=back
2347 2380
2348=head3 cf::object 2381=head3 cf::object
2349 2382
2350=cut 2383=cut
2545 ($x, $y) = (-1, -1) 2578 ($x, $y) = (-1, -1)
2546 unless (defined $x) && (defined $y); 2579 unless (defined $x) && (defined $y);
2547 2580
2548 # use -1 or undef as default coordinates, not 0, 0 2581 # use -1 or undef as default coordinates, not 0, 0
2549 ($x, $y) = ($map->enter_x, $map->enter_y) 2582 ($x, $y) = ($map->enter_x, $map->enter_y)
2550 if $x <=0 && $y <= 0; 2583 if $x <= 0 && $y <= 0;
2551 2584
2552 $map->load; 2585 $map->load;
2553 $map->load_neighbours; 2586 $map->load_neighbours;
2554 2587
2555 return unless $self->contr->active; 2588 return unless $self->contr->active;
2754 2787
2755 utf8::encode $text; 2788 utf8::encode $text;
2756 $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);
2757} 2790}
2758 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);
2815}
2816
2759=item $client->send_msg ($channel, $msg, $color, [extra...]) 2817=item $client->send_msg ($channel, $msg, $color, [extra...])
2760 2818
2761Send 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
2762client if neccessary. C<$type> should be a string identifying the type of 2820client if neccessary. C<$type> should be a string identifying the type of
2763the message, with C<log> being the default. If C<$color> is negative, suppress 2821the message, with C<log> being the default. If C<$color> is negative, suppress
2765 2823
2766=cut 2824=cut
2767 2825
2768# non-persistent channels (usually the info channel) 2826# non-persistent channels (usually the info channel)
2769our %CHANNEL = ( 2827our %CHANNEL = (
2828 "c/motd" => {
2829 id => "infobox",
2830 title => "MOTD",
2831 reply => undef,
2832 tooltip => "The message of the day",
2833 },
2770 "c/identify" => { 2834 "c/identify" => {
2771 id => "infobox", 2835 id => "infobox",
2772 title => "Identify", 2836 title => "Identify",
2773 reply => undef, 2837 reply => undef,
2774 tooltip => "Items recently identified", 2838 tooltip => "Items recently identified",
2776 "c/examine" => { 2840 "c/examine" => {
2777 id => "infobox", 2841 id => "infobox",
2778 title => "Examine", 2842 title => "Examine",
2779 reply => undef, 2843 reply => undef,
2780 tooltip => "Signs and other items you examined", 2844 tooltip => "Signs and other items you examined",
2845 },
2846 "c/shopinfo" => {
2847 id => "infobox",
2848 title => "Shop Info",
2849 reply => undef,
2850 tooltip => "What your bargaining skill tells you about the shop",
2781 }, 2851 },
2782 "c/book" => { 2852 "c/book" => {
2783 id => "infobox", 2853 id => "infobox",
2784 title => "Book", 2854 title => "Book",
2785 reply => undef, 2855 reply => undef,
2901 my $pkt = "msg " 2971 my $pkt = "msg "
2902 . $self->{json_coder}->encode ( 2972 . $self->{json_coder}->encode (
2903 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] 2973 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra]
2904 ); 2974 );
2905 2975
2906 # try lzf for large packets
2907 $pkt = "lzf " . Compress::LZF::compress $pkt
2908 if 1024 <= length $pkt and $self->{can_lzf};
2909
2910 # split very large packets
2911 if (8192 < length $pkt and $self->{can_lzf}) {
2912 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt;
2913 $pkt = "frag";
2914 }
2915
2916 $self->send_packet ($pkt); 2976 $self->send_big_packet ($pkt);
2917} 2977}
2918 2978
2919=item $client->ext_msg ($type, @msg) 2979=item $client->ext_msg ($type, @msg)
2920 2980
2921Sends an ext event to the client. 2981Sends an ext event to the client.
2924 2984
2925sub cf::client::ext_msg($$@) { 2985sub cf::client::ext_msg($$@) {
2926 my ($self, $type, @msg) = @_; 2986 my ($self, $type, @msg) = @_;
2927 2987
2928 if ($self->extcmd == 2) { 2988 if ($self->extcmd == 2) {
2929 $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); 2989 $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg]));
2930 } elsif ($self->extcmd == 1) { # TODO: remove 2990 } elsif ($self->extcmd == 1) { # TODO: remove
2931 push @msg, msgtype => "event_$type"; 2991 push @msg, msgtype => "event_$type";
2932 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); 2992 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2933 } 2993 }
2934} 2994}
2935 2995
2936=item $client->ext_reply ($msgid, @msg) 2996=item $client->ext_reply ($msgid, @msg)
2937 2997
2941 3001
2942sub cf::client::ext_reply($$@) { 3002sub cf::client::ext_reply($$@) {
2943 my ($self, $id, @msg) = @_; 3003 my ($self, $id, @msg) = @_;
2944 3004
2945 if ($self->extcmd == 2) { 3005 if ($self->extcmd == 2) {
2946 $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); 3006 $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg]));
2947 } elsif ($self->extcmd == 1) { 3007 } elsif ($self->extcmd == 1) {
2948 #TODO: version 1, remove 3008 #TODO: version 1, remove
2949 unshift @msg, msgtype => "reply", msgid => $id; 3009 unshift @msg, msgtype => "reply", msgid => $id;
2950 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); 3010 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2951 } 3011 }
2952} 3012}
2953 3013
2954=item $success = $client->query ($flags, "text", \&cb) 3014=item $success = $client->query ($flags, "text", \&cb)
2955 3015

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines