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.487 by root, Fri Oct 16 01:56:41 2009 UTC vs.
Revision 1.497 by root, Fri Dec 18 03:49:46 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;
52use Coro::Util (); 53use Coro::Util ();
53 54
54use JSON::XS 2.01 (); 55use JSON::XS 2.01 ();
55use BDB (); 56use BDB ();
56use Data::Dumper; 57use Data::Dumper;
57use Digest::MD5;
58use Fcntl; 58use Fcntl;
59use YAML::XS (); 59use YAML::XS ();
60use IO::AIO (); 60use IO::AIO ();
61use Time::HiRes; 61use Time::HiRes;
62use Compress::LZF; 62use Compress::LZF;
228returns directly I<after> the tick processing (and consequently, can only wake one process 228returns 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. 229per tick), while cf::wait_for_tick wakes up all waiters after tick processing.
230 230
231=item @cf::INVOKE_RESULTS 231=item @cf::INVOKE_RESULTS
232 232
233This array contains the results of the last C<invoke ()> call. When 233This 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 234C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of
235that call. 235that call.
236 236
237=item %cf::REFLECT 237=item %cf::REFLECT
238 238
289)) { 289)) {
290 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg; 290 @{"safe::$pkg\::wrap::ISA"} = @{"$pkg\::wrap::ISA"} = $pkg;
291} 291}
292 292
293$EV::DIED = sub { 293$EV::DIED = sub {
294 warn "error in event callback: @_"; 294 Carp::cluck "error in event callback: @_";
295}; 295};
296 296
297############################################################################# 297#############################################################################
298 298
299=head2 UTILITY FUNCTIONS 299=head2 UTILITY FUNCTIONS
2341 : normalise $_ 2341 : normalise $_
2342 } @{ aio_readdir $UNIQUEDIR or [] } 2342 } @{ aio_readdir $UNIQUEDIR or [] }
2343 ] 2343 ]
2344} 2344}
2345 2345
2346=item cf::map::static_maps
2347
2348Returns an arrayref if paths of all static maps (all preinstalled F<.map>
2349file in the shared directory excluding F</styles> and F</editor>). May
2350block.
2351
2352=cut
2353
2354sub static_maps() {
2355 my @dirs = "";
2356 my @maps;
2357
2358 while (@dirs) {
2359 my $dir = shift @dirs;
2360
2361 next if $dir eq "/styles" || $dir eq "/editor";
2362
2363 my ($dirs, $files) = Coro::AIO::aio_scandir "$MAPDIR$dir", 2
2364 or return;
2365
2366 for (@$files) {
2367 s/\.map$// or next;
2368 utf8::decode $_;
2369 push @maps, "$dir/$_";
2370 }
2371
2372 push @dirs, map "$dir/$_", @$dirs;
2373 }
2374
2375 \@maps
2376}
2377
2346=back 2378=back
2347 2379
2348=head3 cf::object 2380=head3 cf::object
2349 2381
2350=cut 2382=cut
2545 ($x, $y) = (-1, -1) 2577 ($x, $y) = (-1, -1)
2546 unless (defined $x) && (defined $y); 2578 unless (defined $x) && (defined $y);
2547 2579
2548 # use -1 or undef as default coordinates, not 0, 0 2580 # use -1 or undef as default coordinates, not 0, 0
2549 ($x, $y) = ($map->enter_x, $map->enter_y) 2581 ($x, $y) = ($map->enter_x, $map->enter_y)
2550 if $x <=0 && $y <= 0; 2582 if $x <= 0 && $y <= 0;
2551 2583
2552 $map->load; 2584 $map->load;
2553 $map->load_neighbours; 2585 $map->load_neighbours;
2554 2586
2555 return unless $self->contr->active; 2587 return unless $self->contr->active;
2752sub cf::client::send_drawinfo { 2784sub cf::client::send_drawinfo {
2753 my ($self, $text, $flags) = @_; 2785 my ($self, $text, $flags) = @_;
2754 2786
2755 utf8::encode $text; 2787 utf8::encode $text;
2756 $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); 2788 $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text);
2789}
2790
2791=item $client->send_big_packet ($pkt)
2792
2793Like C<send_packet>, but tries to compress large packets, and fragments
2794them as required.
2795
2796=cut
2797
2798our $MAXFRAGSIZE = cf::MAXSOCKBUF - 64;
2799
2800sub cf::client::send_big_packet {
2801 my ($self, $pkt) = @_;
2802
2803 # try lzf for large packets
2804 $pkt = "lzf " . Compress::LZF::compress $pkt
2805 if 1024 <= length $pkt and $self->{can_lzf};
2806
2807 # split very large packets
2808 if ($MAXFRAGSIZE < length $pkt and $self->{can_lzf}) {
2809 $self->send_packet ("frag $_") for unpack "(a$MAXFRAGSIZE)*", $pkt;
2810 $pkt = "frag";
2811 }
2812
2813 $self->send_packet ($pkt);
2757} 2814}
2758 2815
2759=item $client->send_msg ($channel, $msg, $color, [extra...]) 2816=item $client->send_msg ($channel, $msg, $color, [extra...])
2760 2817
2761Send a drawinfo or msg packet to the client, formatting the msg for the 2818Send a drawinfo or msg packet to the client, formatting the msg for the
2913 my $pkt = "msg " 2970 my $pkt = "msg "
2914 . $self->{json_coder}->encode ( 2971 . $self->{json_coder}->encode (
2915 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] 2972 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra]
2916 ); 2973 );
2917 2974
2918 # try lzf for large packets
2919 $pkt = "lzf " . Compress::LZF::compress $pkt
2920 if 1024 <= length $pkt and $self->{can_lzf};
2921
2922 # split very large packets
2923 if (8192 < length $pkt and $self->{can_lzf}) {
2924 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt;
2925 $pkt = "frag";
2926 }
2927
2928 $self->send_packet ($pkt); 2975 $self->send_big_packet ($pkt);
2929} 2976}
2930 2977
2931=item $client->ext_msg ($type, @msg) 2978=item $client->ext_msg ($type, @msg)
2932 2979
2933Sends an ext event to the client. 2980Sends an ext event to the client.
2936 2983
2937sub cf::client::ext_msg($$@) { 2984sub cf::client::ext_msg($$@) {
2938 my ($self, $type, @msg) = @_; 2985 my ($self, $type, @msg) = @_;
2939 2986
2940 if ($self->extcmd == 2) { 2987 if ($self->extcmd == 2) {
2941 $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); 2988 $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg]));
2942 } elsif ($self->extcmd == 1) { # TODO: remove 2989 } elsif ($self->extcmd == 1) { # TODO: remove
2943 push @msg, msgtype => "event_$type"; 2990 push @msg, msgtype => "event_$type";
2944 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); 2991 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2945 } 2992 }
2946} 2993}
2947 2994
2948=item $client->ext_reply ($msgid, @msg) 2995=item $client->ext_reply ($msgid, @msg)
2949 2996
2953 3000
2954sub cf::client::ext_reply($$@) { 3001sub cf::client::ext_reply($$@) {
2955 my ($self, $id, @msg) = @_; 3002 my ($self, $id, @msg) = @_;
2956 3003
2957 if ($self->extcmd == 2) { 3004 if ($self->extcmd == 2) {
2958 $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); 3005 $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg]));
2959 } elsif ($self->extcmd == 1) { 3006 } elsif ($self->extcmd == 1) {
2960 #TODO: version 1, remove 3007 #TODO: version 1, remove
2961 unshift @msg, msgtype => "reply", msgid => $id; 3008 unshift @msg, msgtype => "reply", msgid => $id;
2962 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); 3009 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2963 } 3010 }
2964} 3011}
2965 3012
2966=item $success = $client->query ($flags, "text", \&cb) 3013=item $success = $client->query ($flags, "text", \&cb)
2967 3014
3244 while (my ($face, $info) = each %$faces) { 3291 while (my ($face, $info) = each %$faces) {
3245 my $idx = (cf::face::find $face) || cf::face::alloc $face; 3292 my $idx = (cf::face::find $face) || cf::face::alloc $face;
3246 3293
3247 cf::face::set_visibility $idx, $info->{visibility}; 3294 cf::face::set_visibility $idx, $info->{visibility};
3248 cf::face::set_magicmap $idx, $info->{magicmap}; 3295 cf::face::set_magicmap $idx, $info->{magicmap};
3249 cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; 3296 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3250 cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; 3297 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3251 3298
3252 cf::cede_to_tick; 3299 cf::cede_to_tick;
3253 } 3300 }
3254 3301
3255 while (my ($face, $info) = each %$faces) { 3302 while (my ($face, $info) = each %$faces) {
3279 3326
3280 cf::anim::invalidate_all; # d'oh 3327 cf::anim::invalidate_all; # d'oh
3281 } 3328 }
3282 3329
3283 { 3330 {
3284 # TODO: for gcfclient pleasure, we should give resources
3285 # that gcfclient doesn't grok a >10000 face index.
3286 my $res = $facedata->{resource}; 3331 my $res = $facedata->{resource};
3287 3332
3288 while (my ($name, $info) = each %$res) { 3333 while (my ($name, $info) = each %$res) {
3289 if (defined $info->{type}) { 3334 if (defined $info->{type}) {
3290 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3335 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3291 my $data;
3292 3336
3293 if ($info->{type} & 1) { 3337 cf::face::set_data $idx, 0, $info->{data}, $info->{hash};
3294 # prepend meta info
3295
3296 my $meta = $enc->encode ({
3297 name => $name,
3298 %{ $info->{meta} || {} },
3299 });
3300
3301 $data = pack "(w/a*)*", $meta, $info->{data};
3302 } else {
3303 $data = $info->{data};
3304 }
3305
3306 cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data;
3307 cf::face::set_type $idx, $info->{type}; 3338 cf::face::set_type $idx, $info->{type};
3308 } else { 3339 } else {
3309 $RESOURCE{$name} = $info; 3340 $RESOURCE{$name} = $info;
3310 } 3341 }
3311 3342
3474 }; 3505 };
3475 3506
3476 evthread_start IO::AIO::poll_fileno; 3507 evthread_start IO::AIO::poll_fileno;
3477 3508
3478 cf::sync_job { 3509 cf::sync_job {
3510 cf::load_settings;
3511 cf::load_materials;
3512
3479 reload_resources; 3513 reload_resources;
3480 reload_config; 3514 reload_config;
3481 db_init; 3515 db_init;
3482 3516
3483 cf::load_settings;
3484 cf::load_materials;
3485 cf::init_uuid; 3517 cf::init_uuid;
3486 cf::init_signals; 3518 cf::init_signals;
3487 cf::init_commands; 3519 cf::init_commands;
3488 cf::init_skills; 3520 cf::init_skills;
3489 3521
3848 3880
3849our @WAIT_FOR_TICK; 3881our @WAIT_FOR_TICK;
3850our @WAIT_FOR_TICK_BEGIN; 3882our @WAIT_FOR_TICK_BEGIN;
3851 3883
3852sub wait_for_tick { 3884sub wait_for_tick {
3853 return if tick_inhibit || $Coro::current == $Coro::main; 3885 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3854 3886
3855 my $signal = new Coro::Signal; 3887 my $signal = new Coro::Signal;
3856 push @WAIT_FOR_TICK, $signal; 3888 push @WAIT_FOR_TICK, $signal;
3857 $signal->wait; 3889 $signal->wait;
3858} 3890}
3859 3891
3860sub wait_for_tick_begin { 3892sub wait_for_tick_begin {
3861 return if tick_inhibit || $Coro::current == $Coro::main; 3893 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3862 3894
3863 my $signal = new Coro::Signal; 3895 my $signal = new Coro::Signal;
3864 push @WAIT_FOR_TICK_BEGIN, $signal; 3896 push @WAIT_FOR_TICK_BEGIN, $signal;
3865 $signal->wait; 3897 $signal->wait;
3866} 3898}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines