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.561 by root, Fri Apr 22 06:10:33 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) {
1514 1555
1515 while (my ($k, $v) = each %todo) { 1556 while (my ($k, $v) = each %todo) {
1516 cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." 1557 cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting."
1517 if exists $v->{meta}{mandatory}; 1558 if exists $v->{meta}{mandatory};
1518 } 1559 }
1560
1561 last;
1519 } 1562 }
1520 } 1563 }
1521 }; 1564 };
1522} 1565}
1523 1566
1951 1994
1952 Carp::cluck "unable to resolve path '$path' (base '$base')"; 1995 Carp::cluck "unable to resolve path '$path' (base '$base')";
1953 () 1996 ()
1954} 1997}
1955 1998
1999# may re-bless or do other evil things
1956sub init { 2000sub init {
1957 my ($self) = @_; 2001 my ($self) = @_;
1958 2002
1959 $self 2003 $self
1960} 2004}
3401 3445
3402 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3446 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3403 3447
3404 trace "loading facedata from $path\n"; 3448 trace "loading facedata from $path\n";
3405 3449
3406 0 < aio_load $path, my $facedata 3450 my $facedata = decode_storable load_file $path;
3407 or die "$path: $!";
3408
3409 $facedata = Coro::Storable::thaw $facedata;
3410 3451
3411 $facedata->{version} == 2 3452 $facedata->{version} == 2
3412 or cf::cleanup "$path: version mismatch, cannot proceed."; 3453 or cf::cleanup "$path: version mismatch, cannot proceed.";
3413 3454
3414 # patch in the exptable 3455 # patch in the exptable
3533} 3574}
3534 3575
3535sub reload_sound { 3576sub reload_sound {
3536 trace "loading sound config from $DATADIR/sound\n"; 3577 trace "loading sound config from $DATADIR/sound\n";
3537 3578
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); 3579 my $soundconf = JSON::XS->new->utf8->relaxed->decode (load_file "$DATADIR/sound");
3542 3580
3543 for (0 .. SOUND_CAST_SPELL_0 - 1) { 3581 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3544 my $sound = $soundconf->{compat}[$_] 3582 my $sound = $soundconf->{compat}[$_]
3545 or next; 3583 or next;
3546 3584
3570} 3608}
3571 3609
3572sub reload_config { 3610sub reload_config {
3573 trace "reloading config file...\n"; 3611 trace "reloading config file...\n";
3574 3612
3575 0 < aio_load "$CONFDIR/config", my $config 3613 my $config = load_file "$CONFDIR/config";
3576 or die "$CONFDIR/config: $!";
3577
3578 utf8::decode $config; 3614 utf8::decode $config;
3579 *CFG = yaml_load $config; 3615 *CFG = decode_yaml $config;
3580 3616
3581 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; 3617 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3582 3618
3583 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3619 $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}; 3620 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3622 3658
3623sub main { 3659sub main {
3624 cf::init_globals; # initialise logging 3660 cf::init_globals; # initialise logging
3625 3661
3626 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3662 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3627 LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3663 LOG llevInfo, "Copyright (C) 2005-2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3628 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3664 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3629 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3665 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3630 3666
3631 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3667 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3632 3668

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines