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.559 by root, Sat Feb 26 12:50:27 2011 UTC vs.
Revision 1.560 by root, Fri Apr 22 02:03:12 2011 UTC

317 Carp::cluck "error in event callback: @_"; 317 Carp::cluck "error in event callback: @_";
318}; 318};
319 319
320############################################################################# 320#############################################################################
321 321
322sub fork_call(&@);
323sub get_slot($;$$);
324
325#############################################################################
326
322=head2 UTILITY FUNCTIONS 327=head2 UTILITY FUNCTIONS
323 328
324=over 4 329=over 4
325 330
326=item dumpval $ref 331=item dumpval $ref
344 $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; 349 $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge;
345 $d 350 $d
346 } || "[unable to dump $_[0]: '$@']"; 351 } || "[unable to dump $_[0]: '$@']";
347} 352}
348 353
354=item $scalar = load_file $path
355
356Loads the given file from path and returns its contents. Croaks on error
357and can block.
358
359=cut
360
361sub load_file($) {
362 0 <= aio_load $_[0], my $data
363 or Carp::croak "$_[0]: $!";
364
365 $data
366}
367
349=item $ref = cf::decode_json $json 368=item $ref = cf::decode_json $json
350 369
351Converts a JSON string into the corresponding perl data structure. 370Converts a JSON string into the corresponding perl data structure.
352 371
353=item $json = cf::encode_json $ref 372=item $json = cf::encode_json $ref
359our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max 378our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max
360 379
361sub encode_json($) { $json_coder->encode ($_[0]) } 380sub encode_json($) { $json_coder->encode ($_[0]) }
362sub decode_json($) { $json_coder->decode ($_[0]) } 381sub decode_json($) { $json_coder->decode ($_[0]) }
363 382
383=item $ref = cf::decode_storable $scalar
384
385Same as Coro::Storable::thaw, so blocks.
386
387=cut
388
389BEGIN { *decode_storable = \&Coro::Storable::thaw }
390
364=item $ref = cf::yaml_load $scalar 391=item $ref = cf::decode_yaml $scalar
365 392
366Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). 393Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks).
367 394
368=cut 395=cut
369 396
370sub fork_call(&@); 397sub decode_yaml($) {
371
372sub yaml_load($) {
373 fork_call { YAML::XS::Load $_[0] } @_ 398 fork_call { YAML::XS::Load $_[0] } @_
399}
400
401=item $scalar = cf::unlzf $scalar
402
403Same as Compress::LZF::compress, but takes server ticks into account, so
404blocks.
405
406=cut
407
408sub unlzf($) {
409 # we assume 100mb/s minimum decompression speed (noncompressible data on a ~2ghz machine)
410 cf::get_slot +(length $_[0]) / 100_000_000, 0, "unlzf";
411 Compress::LZF::decompress $_[0]
374} 412}
375 413
376=item cf::post_init { BLOCK } 414=item cf::post_init { BLOCK }
377 415
378Execute the given codeblock, I<after> all extensions have been (re-)loaded, 416Execute the given codeblock, I<after> all extensions have been (re-)loaded,
462 EV::periodic $start, $interval, 0, $cb 500 EV::periodic $start, $interval, 0, $cb
463} 501}
464 502
465=item cf::get_slot $time[, $priority[, $name]] 503=item cf::get_slot $time[, $priority[, $name]]
466 504
467Allocate $time seconds of blocking CPU time at priority C<$priority>: 505Allocate $time seconds of blocking CPU time at priority C<$priority>
468This call blocks and returns only when you have at least C<$time> seconds 506(default: 0): This call blocks and returns only when you have at least
469of cpu time till the next tick. The slot is only valid till the next cede. 507C<$time> seconds of cpu time till the next tick. The slot is only valid
508till the next cede.
509
510Background jobs should use a priority les than zero, interactive jobs
511should use 100 or more.
470 512
471The optional C<$name> can be used to identify the job to run. It might be 513The optional C<$name> can be used to identify the job to run. It might be
472used for statistical purposes and should identify the same time-class. 514used for statistical purposes and should identify the same time-class.
473 515
474Useful for short background jobs. 516Useful for short background jobs.
769 811
770 my @data; 812 my @data;
771 my $md5; 813 my $md5;
772 814
773 for (0 .. $#$src) { 815 for (0 .. $#$src) {
774 0 <= aio_load $src->[$_], $data[$_] 816 $data[$_] = load_file $src->[$_];
775 or Carp::croak "$src->[$_]: $!";
776 } 817 }
777 818
778 # if processing is expensive, check 819 # if processing is expensive, check
779 # checksum first 820 # checksum first
780 if (1) { 821 if (1) {
3401 3442
3402 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3443 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3403 3444
3404 trace "loading facedata from $path\n"; 3445 trace "loading facedata from $path\n";
3405 3446
3406 0 < aio_load $path, my $facedata 3447 my $facedata = decode_storable load_file $path;
3407 or die "$path: $!";
3408
3409 $facedata = Coro::Storable::thaw $facedata;
3410 3448
3411 $facedata->{version} == 2 3449 $facedata->{version} == 2
3412 or cf::cleanup "$path: version mismatch, cannot proceed."; 3450 or cf::cleanup "$path: version mismatch, cannot proceed.";
3413 3451
3414 # patch in the exptable 3452 # patch in the exptable
3533} 3571}
3534 3572
3535sub reload_sound { 3573sub reload_sound {
3536 trace "loading sound config from $DATADIR/sound\n"; 3574 trace "loading sound config from $DATADIR/sound\n";
3537 3575
3538 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data
3539 or die "$DATADIR/sound $!";
3540
3541 my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data); 3576 my $soundconf = JSON::XS->new->utf8->relaxed->decode (load_file "$DATADIR/sound");
3542 3577
3543 for (0 .. SOUND_CAST_SPELL_0 - 1) { 3578 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3544 my $sound = $soundconf->{compat}[$_] 3579 my $sound = $soundconf->{compat}[$_]
3545 or next; 3580 or next;
3546 3581
3570} 3605}
3571 3606
3572sub reload_config { 3607sub reload_config {
3573 trace "reloading config file...\n"; 3608 trace "reloading config file...\n";
3574 3609
3575 0 < aio_load "$CONFDIR/config", my $config 3610 my $config = load_file "$CONFDIR/config";
3576 or die "$CONFDIR/config: $!";
3577
3578 utf8::decode $config; 3611 utf8::decode $config;
3579 *CFG = yaml_load $config; 3612 *CFG = decode_yaml $config;
3580 3613
3581 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; 3614 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3582 3615
3583 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3616 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset};
3584 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; 3617 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines