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.492 by root, Mon Oct 26 01:22:34 2009 UTC vs.
Revision 1.500 by root, Sat Jan 16 00:08:33 2010 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;
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
2009 2009
2010 $cf::MAP{$path} = $map 2010 $cf::MAP{$path} = $map
2011 } 2011 }
2012} 2012}
2013 2013
2014sub pre_load { } 2014sub pre_load { }
2015sub post_load { } 2015#sub post_load { } # XS
2016 2016
2017sub load { 2017sub load {
2018 my ($self) = @_; 2018 my ($self) = @_;
2019 2019
2020 local $self->{deny_reset} = 1; # loading can take a long time 2020 local $self->{deny_reset} = 1; # loading can take a long time
2784sub cf::client::send_drawinfo { 2784sub cf::client::send_drawinfo {
2785 my ($self, $text, $flags) = @_; 2785 my ($self, $text, $flags) = @_;
2786 2786
2787 utf8::encode $text; 2787 utf8::encode $text;
2788 $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);
2789} 2814}
2790 2815
2791=item $client->send_msg ($channel, $msg, $color, [extra...]) 2816=item $client->send_msg ($channel, $msg, $color, [extra...])
2792 2817
2793Send 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
2945 my $pkt = "msg " 2970 my $pkt = "msg "
2946 . $self->{json_coder}->encode ( 2971 . $self->{json_coder}->encode (
2947 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra] 2972 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra]
2948 ); 2973 );
2949 2974
2950 # try lzf for large packets
2951 $pkt = "lzf " . Compress::LZF::compress $pkt
2952 if 1024 <= length $pkt and $self->{can_lzf};
2953
2954 # split very large packets
2955 if (8192 < length $pkt and $self->{can_lzf}) {
2956 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt;
2957 $pkt = "frag";
2958 }
2959
2960 $self->send_packet ($pkt); 2975 $self->send_big_packet ($pkt);
2961} 2976}
2962 2977
2963=item $client->ext_msg ($type, @msg) 2978=item $client->ext_msg ($type, @msg)
2964 2979
2965Sends an ext event to the client. 2980Sends an ext event to the client.
2968 2983
2969sub cf::client::ext_msg($$@) { 2984sub cf::client::ext_msg($$@) {
2970 my ($self, $type, @msg) = @_; 2985 my ($self, $type, @msg) = @_;
2971 2986
2972 if ($self->extcmd == 2) { 2987 if ($self->extcmd == 2) {
2973 $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); 2988 $self->send_big_packet ("ext " . $self->{json_coder}->encode ([$type, @msg]));
2974 } elsif ($self->extcmd == 1) { # TODO: remove 2989 } elsif ($self->extcmd == 1) { # TODO: remove
2975 push @msg, msgtype => "event_$type"; 2990 push @msg, msgtype => "event_$type";
2976 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); 2991 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2977 } 2992 }
2978} 2993}
2979 2994
2980=item $client->ext_reply ($msgid, @msg) 2995=item $client->ext_reply ($msgid, @msg)
2981 2996
2985 3000
2986sub cf::client::ext_reply($$@) { 3001sub cf::client::ext_reply($$@) {
2987 my ($self, $id, @msg) = @_; 3002 my ($self, $id, @msg) = @_;
2988 3003
2989 if ($self->extcmd == 2) { 3004 if ($self->extcmd == 2) {
2990 $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); 3005 $self->send_big_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg]));
2991 } elsif ($self->extcmd == 1) { 3006 } elsif ($self->extcmd == 1) {
2992 #TODO: version 1, remove 3007 #TODO: version 1, remove
2993 unshift @msg, msgtype => "reply", msgid => $id; 3008 unshift @msg, msgtype => "reply", msgid => $id;
2994 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); 3009 $self->send_big_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2995 } 3010 }
2996} 3011}
2997 3012
2998=item $success = $client->query ($flags, "text", \&cb) 3013=item $success = $client->query ($flags, "text", \&cb)
2999 3014
3126our $safe_hole = new Safe::Hole; 3141our $safe_hole = new Safe::Hole;
3127 3142
3128$SIG{FPE} = 'IGNORE'; 3143$SIG{FPE} = 'IGNORE';
3129 3144
3130$safe->permit_only (Opcode::opset qw( 3145$safe->permit_only (Opcode::opset qw(
3131 :base_core :base_mem :base_orig :base_math 3146 :base_core :base_mem :base_orig :base_math :base_loop
3132 grepstart grepwhile mapstart mapwhile 3147 grepstart grepwhile mapstart mapwhile
3133 sort time 3148 sort time
3134)); 3149));
3135 3150
3136# here we export the classes and methods available to script code 3151# here we export the classes and methods available to script code
3188 $qcode =~ s/"/‟/g; # not allowed in #line filenames 3203 $qcode =~ s/"/‟/g; # not allowed in #line filenames
3189 $qcode =~ s/\n/\\n/g; 3204 $qcode =~ s/\n/\\n/g;
3190 3205
3191 %vars = (_dummy => 0) unless %vars; 3206 %vars = (_dummy => 0) unless %vars;
3192 3207
3208 my @res;
3193 local $_; 3209 local $_;
3194 local @safe::cf::_safe_eval_args = values %vars;
3195 3210
3196 my $eval = 3211 my $eval =
3197 "do {\n" 3212 "do {\n"
3198 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 3213 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
3199 . "#line 0 \"{$qcode}\"\n" 3214 . "#line 0 \"{$qcode}\"\n"
3200 . $code 3215 . $code
3201 . "\n}" 3216 . "\n}"
3202 ; 3217 ;
3203 3218
3219 if ($CFG{safe_eval}) {
3204 sub_generation_inc; 3220 sub_generation_inc;
3221 local @safe::cf::_safe_eval_args = values %vars;
3205 my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); 3222 @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval);
3206 sub_generation_inc; 3223 sub_generation_inc;
3224 } else {
3225 local @cf::_safe_eval_args = values %vars;
3226 @res = wantarray ? eval eval : scalar eval $eval;
3227 }
3207 3228
3208 if ($@) { 3229 if ($@) {
3209 warn "$@"; 3230 warn "$@";
3210 warn "while executing safe code '$code'\n"; 3231 warn "while executing safe code '$code'\n";
3211 warn "with arguments " . (join " ", %vars) . "\n"; 3232 warn "with arguments " . (join " ", %vars) . "\n";
3262 3283
3263 $facedata->{version} == 2 3284 $facedata->{version} == 2
3264 or cf::cleanup "$path: version mismatch, cannot proceed."; 3285 or cf::cleanup "$path: version mismatch, cannot proceed.";
3265 3286
3266 # patch in the exptable 3287 # patch in the exptable
3288 my $exp_table = $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]);
3267 $facedata->{resource}{"res/exp_table"} = { 3289 $facedata->{resource}{"res/exp_table"} = {
3268 type => FT_RSRC, 3290 type => FT_RSRC,
3269 data => $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]), 3291 data => $exp_table,
3292 hash => (Digest::MD5::md5 $exp_table),
3270 }; 3293 };
3271 cf::cede_to_tick; 3294 cf::cede_to_tick;
3272 3295
3273 { 3296 {
3274 my $faces = $facedata->{faceinfo}; 3297 my $faces = $facedata->{faceinfo};
3276 while (my ($face, $info) = each %$faces) { 3299 while (my ($face, $info) = each %$faces) {
3277 my $idx = (cf::face::find $face) || cf::face::alloc $face; 3300 my $idx = (cf::face::find $face) || cf::face::alloc $face;
3278 3301
3279 cf::face::set_visibility $idx, $info->{visibility}; 3302 cf::face::set_visibility $idx, $info->{visibility};
3280 cf::face::set_magicmap $idx, $info->{magicmap}; 3303 cf::face::set_magicmap $idx, $info->{magicmap};
3281 cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; 3304 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3282 cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; 3305 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3283 3306
3284 cf::cede_to_tick; 3307 cf::cede_to_tick;
3285 } 3308 }
3286 3309
3287 while (my ($face, $info) = each %$faces) { 3310 while (my ($face, $info) = each %$faces) {
3311 3334
3312 cf::anim::invalidate_all; # d'oh 3335 cf::anim::invalidate_all; # d'oh
3313 } 3336 }
3314 3337
3315 { 3338 {
3316 # TODO: for gcfclient pleasure, we should give resources
3317 # that gcfclient doesn't grok a >10000 face index.
3318 my $res = $facedata->{resource}; 3339 my $res = $facedata->{resource};
3319 3340
3320 while (my ($name, $info) = each %$res) { 3341 while (my ($name, $info) = each %$res) {
3321 if (defined $info->{type}) { 3342 if (defined $info->{type}) {
3322 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3343 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3323 my $data;
3324 3344
3325 if ($info->{type} & 1) { 3345 cf::face::set_data $idx, 0, $info->{data}, $info->{hash};
3326 # prepend meta info
3327
3328 my $meta = $enc->encode ({
3329 name => $name,
3330 %{ $info->{meta} || {} },
3331 });
3332
3333 $data = pack "(w/a*)*", $meta, $info->{data};
3334 } else {
3335 $data = $info->{data};
3336 }
3337
3338 cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data;
3339 cf::face::set_type $idx, $info->{type}; 3346 cf::face::set_type $idx, $info->{type};
3340 } else { 3347 } else {
3341 $RESOURCE{$name} = $info; 3348 $RESOURCE{$name} = $info;
3342 } 3349 }
3343 3350
3506 }; 3513 };
3507 3514
3508 evthread_start IO::AIO::poll_fileno; 3515 evthread_start IO::AIO::poll_fileno;
3509 3516
3510 cf::sync_job { 3517 cf::sync_job {
3518 cf::load_settings;
3519 cf::load_materials;
3520
3511 reload_resources; 3521 reload_resources;
3512 reload_config; 3522 reload_config;
3513 db_init; 3523 db_init;
3514 3524
3515 cf::load_settings;
3516 cf::load_materials;
3517 cf::init_uuid; 3525 cf::init_uuid;
3518 cf::init_signals; 3526 cf::init_signals;
3519 cf::init_commands; 3527 cf::init_commands;
3520 cf::init_skills; 3528 cf::init_skills;
3521 3529
3880 3888
3881our @WAIT_FOR_TICK; 3889our @WAIT_FOR_TICK;
3882our @WAIT_FOR_TICK_BEGIN; 3890our @WAIT_FOR_TICK_BEGIN;
3883 3891
3884sub wait_for_tick { 3892sub wait_for_tick {
3885 return if tick_inhibit || $Coro::current == $Coro::main; 3893 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3886 3894
3887 my $signal = new Coro::Signal; 3895 my $signal = new Coro::Signal;
3888 push @WAIT_FOR_TICK, $signal; 3896 push @WAIT_FOR_TICK, $signal;
3889 $signal->wait; 3897 $signal->wait;
3890} 3898}
3891 3899
3892sub wait_for_tick_begin { 3900sub wait_for_tick_begin {
3893 return if tick_inhibit || $Coro::current == $Coro::main; 3901 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3894 3902
3895 my $signal = new Coro::Signal; 3903 my $signal = new Coro::Signal;
3896 push @WAIT_FOR_TICK_BEGIN, $signal; 3904 push @WAIT_FOR_TICK_BEGIN, $signal;
3897 $signal->wait; 3905 $signal->wait;
3898} 3906}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines