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.494 by root, Mon Oct 26 05:18:00 2009 UTC vs.
Revision 1.505 by root, Sun Jan 31 03:46:20 2010 UTC

1# 1#
2# This file is part of Deliantra, the Roguelike Realtime MMORPG. 2# This file is part of Deliantra, the Roguelike Realtime MMORPG.
3# 3#
4# Copyright (©) 2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4# Copyright (©) 2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5# 5#
6# Deliantra is free software: you can redistribute it and/or modify it under 6# Deliantra is free software: you can redistribute it and/or modify it under
7# the terms of the Affero GNU General Public License as published by the 7# the terms of the Affero GNU General Public License as published by the
8# Free Software Foundation, either version 3 of the License, or (at your 8# Free Software Foundation, either version 3 of the License, or (at your
9# option) any later version. 9# option) any later version.
53use Coro::Util (); 53use Coro::Util ();
54 54
55use JSON::XS 2.01 (); 55use JSON::XS 2.01 ();
56use BDB (); 56use BDB ();
57use Data::Dumper; 57use Data::Dumper;
58use Digest::MD5;
59use Fcntl; 58use Fcntl;
60use YAML::XS (); 59use YAML::XS ();
61use IO::AIO (); 60use IO::AIO ();
62use Time::HiRes; 61use Time::HiRes;
63use Compress::LZF; 62use Compress::LZF;
424 423
425=cut 424=cut
426 425
427our @SLOT_QUEUE; 426our @SLOT_QUEUE;
428our $SLOT_QUEUE; 427our $SLOT_QUEUE;
428our $SLOT_DECAY = 0.9;
429 429
430$SLOT_QUEUE->cancel if $SLOT_QUEUE; 430$SLOT_QUEUE->cancel if $SLOT_QUEUE;
431$SLOT_QUEUE = Coro::async { 431$SLOT_QUEUE = Coro::async {
432 $Coro::current->desc ("timeslot manager"); 432 $Coro::current->desc ("timeslot manager");
433 433
434 my $signal = new Coro::Signal; 434 my $signal = new Coro::Signal;
435 my $busy;
435 436
436 while () { 437 while () {
437 next_job: 438 next_job:
439
438 my $avail = cf::till_tick; 440 my $avail = cf::till_tick;
439 if ($avail > 0.01) { 441
440 for (0 .. $#SLOT_QUEUE) { 442 for (0 .. $#SLOT_QUEUE) {
441 if ($SLOT_QUEUE[$_][0] < $avail) { 443 if ($SLOT_QUEUE[$_][0] <= $avail) {
444 $busy = 0;
442 my $job = splice @SLOT_QUEUE, $_, 1, (); 445 my $job = splice @SLOT_QUEUE, $_, 1, ();
443 $job->[2]->send; 446 $job->[2]->send;
444 Coro::cede; 447 Coro::cede;
445 goto next_job; 448 goto next_job;
446 } 449 } else {
450 $SLOT_QUEUE[$_][0] *= $SLOT_DECAY;
447 } 451 }
448 } 452 }
449 453
450 if (@SLOT_QUEUE) { 454 if (@SLOT_QUEUE) {
451 # we do not use wait_for_tick() as it returns immediately when tick is inactive 455 # we do not use wait_for_tick() as it returns immediately when tick is inactive
452 push @cf::WAIT_FOR_TICK, $signal; 456 push @cf::WAIT_FOR_TICK, $signal;
453 $signal->wait; 457 $signal->wait;
454 } else { 458 } else {
459 $busy = 0;
455 Coro::schedule; 460 Coro::schedule;
456 } 461 }
457 } 462 }
458}; 463};
459 464
460sub get_slot($;$$) { 465sub get_slot($;$$) {
461 return if tick_inhibit || $Coro::current == $Coro::main; 466 return if tick_inhibit || $Coro::current == $Coro::main;
462 467
463 my ($time, $pri, $name) = @_; 468 my ($time, $pri, $name) = @_;
464 469
465 $time = $TICK * .6 if $time > $TICK * .6; 470 $time = clamp $time, 0.01, $TICK * .6;
471
466 my $sig = new Coro::Signal; 472 my $sig = new Coro::Signal;
467 473
468 push @SLOT_QUEUE, [$time, $pri, $sig, $name]; 474 push @SLOT_QUEUE, [$time, $pri, $sig, $name];
469 @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE; 475 @SLOT_QUEUE = sort { $b->[1] <=> $a->[1] } @SLOT_QUEUE;
470 $SLOT_QUEUE->ready; 476 $SLOT_QUEUE->ready;
1393 . "\n};\n1"; 1399 . "\n};\n1";
1394 1400
1395 $todo{$base} = \%ext; 1401 $todo{$base} = \%ext;
1396 } 1402 }
1397 1403
1404 my $pass = 0;
1398 my %done; 1405 my %done;
1399 while (%todo) { 1406 while (%todo) {
1400 my $progress; 1407 my $progress;
1401 1408
1409 ++$pass;
1410
1411 ext:
1402 while (my ($k, $v) = each %todo) { 1412 while (my ($k, $v) = each %todo) {
1403 for (split /,\s*/, $v->{meta}{depends}) { 1413 for (split /,\s*/, $v->{meta}{depends}) {
1404 goto skip 1414 next ext
1405 unless exists $done{$_}; 1415 unless exists $done{$_};
1406 } 1416 }
1407 1417
1408 warn "... loading '$k' into '$v->{pkg}'\n"; 1418 warn "... pass $pass, loading '$k' into '$v->{pkg}'\n";
1409 1419
1410 unless (eval $v->{source}) { 1420 my $active = eval $v->{source};
1421
1422 if (length $@) {
1411 my $msg = $@ ? "$v->{path}: $@\n" 1423 warn "$v->{path}: $@\n";
1412 : "$v->{base}: extension inactive.\n";
1413 1424
1414 if (exists $v->{meta}{mandatory}) {
1415 warn $msg;
1416 cf::cleanup "mandatory extension failed to load, exiting."; 1425 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1417 } 1426 if exists $v->{meta}{mandatory};
1418 1427 } else {
1419 warn $msg; 1428 $done{$k} = delete $todo{$k};
1429 push @EXTS, $v->{pkg};
1430 $progress = 1;
1431
1432 warn "$v->{base}: extension inactive.\n"
1433 unless $active;
1420 } 1434 }
1421
1422 $done{$k} = delete $todo{$k};
1423 push @EXTS, $v->{pkg};
1424 $progress = 1;
1425 } 1435 }
1426 1436
1427 skip: 1437 unless ($progress) {
1428 die "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n" 1438 warn "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n";
1429 unless $progress; 1439
1440 while (my ($k, $v) = each %todo) {
1441 cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting."
1442 if exists $v->{meta}{mandatory};
1443 }
1444 }
1430 } 1445 }
1431 }; 1446 };
1432} 1447}
1433 1448
1434############################################################################# 1449#############################################################################
2010 2025
2011 $cf::MAP{$path} = $map 2026 $cf::MAP{$path} = $map
2012 } 2027 }
2013} 2028}
2014 2029
2015sub pre_load { } 2030sub pre_load { }
2016sub post_load { } 2031#sub post_load { } # XS
2017 2032
2018sub load { 2033sub load {
2019 my ($self) = @_; 2034 my ($self) = @_;
2020 2035
2021 local $self->{deny_reset} = 1; # loading can take a long time 2036 local $self->{deny_reset} = 1; # loading can take a long time
3142our $safe_hole = new Safe::Hole; 3157our $safe_hole = new Safe::Hole;
3143 3158
3144$SIG{FPE} = 'IGNORE'; 3159$SIG{FPE} = 'IGNORE';
3145 3160
3146$safe->permit_only (Opcode::opset qw( 3161$safe->permit_only (Opcode::opset qw(
3147 :base_core :base_mem :base_orig :base_math 3162 :base_core :base_mem :base_orig :base_math :base_loop
3148 grepstart grepwhile mapstart mapwhile 3163 grepstart grepwhile mapstart mapwhile
3149 sort time 3164 sort time
3150)); 3165));
3151 3166
3152# here we export the classes and methods available to script code 3167# here we export the classes and methods available to script code
3204 $qcode =~ s/"/‟/g; # not allowed in #line filenames 3219 $qcode =~ s/"/‟/g; # not allowed in #line filenames
3205 $qcode =~ s/\n/\\n/g; 3220 $qcode =~ s/\n/\\n/g;
3206 3221
3207 %vars = (_dummy => 0) unless %vars; 3222 %vars = (_dummy => 0) unless %vars;
3208 3223
3224 my @res;
3209 local $_; 3225 local $_;
3210 local @safe::cf::_safe_eval_args = values %vars;
3211 3226
3212 my $eval = 3227 my $eval =
3213 "do {\n" 3228 "do {\n"
3214 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 3229 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
3215 . "#line 0 \"{$qcode}\"\n" 3230 . "#line 0 \"{$qcode}\"\n"
3216 . $code 3231 . $code
3217 . "\n}" 3232 . "\n}"
3218 ; 3233 ;
3219 3234
3235 if ($CFG{safe_eval}) {
3220 sub_generation_inc; 3236 sub_generation_inc;
3237 local @safe::cf::_safe_eval_args = values %vars;
3221 my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); 3238 @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval);
3222 sub_generation_inc; 3239 sub_generation_inc;
3240 } else {
3241 local @cf::_safe_eval_args = values %vars;
3242 @res = wantarray ? eval eval : scalar eval $eval;
3243 }
3223 3244
3224 if ($@) { 3245 if ($@) {
3225 warn "$@"; 3246 warn "$@";
3226 warn "while executing safe code '$code'\n"; 3247 warn "while executing safe code '$code'\n";
3227 warn "with arguments " . (join " ", %vars) . "\n"; 3248 warn "with arguments " . (join " ", %vars) . "\n";
3246=cut 3267=cut
3247 3268
3248sub register_script_function { 3269sub register_script_function {
3249 my ($fun, $cb) = @_; 3270 my ($fun, $cb) = @_;
3250 3271
3251 no strict 'refs'; 3272 $fun = "safe::$fun" if $CFG{safe_eval};
3252 *{"safe::$fun"} = $safe_hole->wrap ($cb); 3273 *$fun = $safe_hole->wrap ($cb);
3253} 3274}
3254 3275
3255=back 3276=back
3256 3277
3257=cut 3278=cut
3278 3299
3279 $facedata->{version} == 2 3300 $facedata->{version} == 2
3280 or cf::cleanup "$path: version mismatch, cannot proceed."; 3301 or cf::cleanup "$path: version mismatch, cannot proceed.";
3281 3302
3282 # patch in the exptable 3303 # patch in the exptable
3304 my $exp_table = $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]);
3283 $facedata->{resource}{"res/exp_table"} = { 3305 $facedata->{resource}{"res/exp_table"} = {
3284 type => FT_RSRC, 3306 type => FT_RSRC,
3285 data => $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]), 3307 data => $exp_table,
3308 hash => (Digest::MD5::md5 $exp_table),
3286 }; 3309 };
3287 cf::cede_to_tick; 3310 cf::cede_to_tick;
3288 3311
3289 { 3312 {
3290 my $faces = $facedata->{faceinfo}; 3313 my $faces = $facedata->{faceinfo};
3292 while (my ($face, $info) = each %$faces) { 3315 while (my ($face, $info) = each %$faces) {
3293 my $idx = (cf::face::find $face) || cf::face::alloc $face; 3316 my $idx = (cf::face::find $face) || cf::face::alloc $face;
3294 3317
3295 cf::face::set_visibility $idx, $info->{visibility}; 3318 cf::face::set_visibility $idx, $info->{visibility};
3296 cf::face::set_magicmap $idx, $info->{magicmap}; 3319 cf::face::set_magicmap $idx, $info->{magicmap};
3297 cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; 3320 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3298 cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; 3321 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3299 3322
3300 cf::cede_to_tick; 3323 cf::cede_to_tick;
3301 } 3324 }
3302 3325
3303 while (my ($face, $info) = each %$faces) { 3326 while (my ($face, $info) = each %$faces) {
3327 3350
3328 cf::anim::invalidate_all; # d'oh 3351 cf::anim::invalidate_all; # d'oh
3329 } 3352 }
3330 3353
3331 { 3354 {
3332 # TODO: for gcfclient pleasure, we should give resources
3333 # that gcfclient doesn't grok a >10000 face index.
3334 my $res = $facedata->{resource}; 3355 my $res = $facedata->{resource};
3335 3356
3336 while (my ($name, $info) = each %$res) { 3357 while (my ($name, $info) = each %$res) {
3337 if (defined $info->{type}) { 3358 if (defined $info->{type}) {
3338 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3359 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3339 my $data;
3340 3360
3341 if ($info->{type} & 1) { 3361 cf::face::set_data $idx, 0, $info->{data}, $info->{hash};
3342 # prepend meta info
3343
3344 my $meta = $enc->encode ({
3345 name => $name,
3346 %{ $info->{meta} || {} },
3347 });
3348
3349 $data = pack "(w/a*)*", $meta, $info->{data};
3350 } else {
3351 $data = $info->{data};
3352 }
3353
3354 cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data;
3355 cf::face::set_type $idx, $info->{type}; 3362 cf::face::set_type $idx, $info->{type};
3356 } else { 3363 } else {
3357 $RESOURCE{$name} = $info; 3364 $RESOURCE{$name} = $info;
3358 } 3365 }
3359 3366
3522 }; 3529 };
3523 3530
3524 evthread_start IO::AIO::poll_fileno; 3531 evthread_start IO::AIO::poll_fileno;
3525 3532
3526 cf::sync_job { 3533 cf::sync_job {
3534 cf::load_settings;
3535 cf::load_materials;
3536
3527 reload_resources; 3537 reload_resources;
3528 reload_config; 3538 reload_config;
3529 db_init; 3539 db_init;
3530 3540
3531 cf::load_settings;
3532 cf::load_materials;
3533 cf::init_uuid; 3541 cf::init_uuid;
3534 cf::init_signals; 3542 cf::init_signals;
3535 cf::init_commands; 3543 cf::init_commands;
3536 cf::init_skills; 3544 cf::init_skills;
3537 3545
3572 3580
3573 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; 3581 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef;
3574 3582
3575 my $guard = cf::lock_acquire "write_runtime"; 3583 my $guard = cf::lock_acquire "write_runtime";
3576 3584
3577 my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644 3585 my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT | O_TRUNC, 0644
3578 or return; 3586 or return;
3579 3587
3580 my $value = $cf::RUNTIME + 90 + 10; 3588 my $value = $cf::RUNTIME + 90 + 10;
3581 # 10 is the runtime save interval, for a monotonic clock 3589 # 10 is the runtime save interval, for a monotonic clock
3582 # 60 allows for the watchdog to kill the server. 3590 # 60 allows for the watchdog to kill the server.
3896 3904
3897our @WAIT_FOR_TICK; 3905our @WAIT_FOR_TICK;
3898our @WAIT_FOR_TICK_BEGIN; 3906our @WAIT_FOR_TICK_BEGIN;
3899 3907
3900sub wait_for_tick { 3908sub wait_for_tick {
3901 return if tick_inhibit || $Coro::current == $Coro::main; 3909 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3902 3910
3903 my $signal = new Coro::Signal; 3911 my $signal = new Coro::Signal;
3904 push @WAIT_FOR_TICK, $signal; 3912 push @WAIT_FOR_TICK, $signal;
3905 $signal->wait; 3913 $signal->wait;
3906} 3914}
3907 3915
3908sub wait_for_tick_begin { 3916sub wait_for_tick_begin {
3909 return if tick_inhibit || $Coro::current == $Coro::main; 3917 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3910 3918
3911 my $signal = new Coro::Signal; 3919 my $signal = new Coro::Signal;
3912 push @WAIT_FOR_TICK_BEGIN, $signal; 3920 push @WAIT_FOR_TICK_BEGIN, $signal;
3913 $signal->wait; 3921 $signal->wait;
3914} 3922}
3919 unless ++$bug_warning > 10; 3927 unless ++$bug_warning > 10;
3920 return; 3928 return;
3921 } 3929 }
3922 3930
3923 cf::server_tick; # one server iteration 3931 cf::server_tick; # one server iteration
3932
3933 #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d#
3924 3934
3925 if ($NOW >= $NEXT_RUNTIME_WRITE) { 3935 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3926 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 3936 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
3927 Coro::async_pool { 3937 Coro::async_pool {
3928 $Coro::current->{desc} = "runtime saver"; 3938 $Coro::current->{desc} = "runtime saver";
3951} 3961}
3952 3962
3953{ 3963{
3954 # configure BDB 3964 # configure BDB
3955 3965
3956 BDB::min_parallel 8; 3966 BDB::min_parallel 16;
3957 BDB::max_poll_reqs $TICK * 0.1; 3967 BDB::max_poll_reqs $TICK * 0.1;
3958 $AnyEvent::BDB::WATCHER->priority (1); 3968 $AnyEvent::BDB::WATCHER->priority (1);
3959 3969
3960 unless ($DB_ENV) { 3970 unless ($DB_ENV) {
3961 $DB_ENV = BDB::db_env_create; 3971 $DB_ENV = BDB::db_env_create;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines