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.504 by root, Sat Jan 30 23:50:16 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;
2010 2016
2011 $cf::MAP{$path} = $map 2017 $cf::MAP{$path} = $map
2012 } 2018 }
2013} 2019}
2014 2020
2015sub pre_load { } 2021sub pre_load { }
2016sub post_load { } 2022#sub post_load { } # XS
2017 2023
2018sub load { 2024sub load {
2019 my ($self) = @_; 2025 my ($self) = @_;
2020 2026
2021 local $self->{deny_reset} = 1; # loading can take a long time 2027 local $self->{deny_reset} = 1; # loading can take a long time
3142our $safe_hole = new Safe::Hole; 3148our $safe_hole = new Safe::Hole;
3143 3149
3144$SIG{FPE} = 'IGNORE'; 3150$SIG{FPE} = 'IGNORE';
3145 3151
3146$safe->permit_only (Opcode::opset qw( 3152$safe->permit_only (Opcode::opset qw(
3147 :base_core :base_mem :base_orig :base_math 3153 :base_core :base_mem :base_orig :base_math :base_loop
3148 grepstart grepwhile mapstart mapwhile 3154 grepstart grepwhile mapstart mapwhile
3149 sort time 3155 sort time
3150)); 3156));
3151 3157
3152# here we export the classes and methods available to script code 3158# here we export the classes and methods available to script code
3204 $qcode =~ s/"/‟/g; # not allowed in #line filenames 3210 $qcode =~ s/"/‟/g; # not allowed in #line filenames
3205 $qcode =~ s/\n/\\n/g; 3211 $qcode =~ s/\n/\\n/g;
3206 3212
3207 %vars = (_dummy => 0) unless %vars; 3213 %vars = (_dummy => 0) unless %vars;
3208 3214
3215 my @res;
3209 local $_; 3216 local $_;
3210 local @safe::cf::_safe_eval_args = values %vars;
3211 3217
3212 my $eval = 3218 my $eval =
3213 "do {\n" 3219 "do {\n"
3214 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n" 3220 . "my (" . (join ",", map "\$$_", keys %vars) . ") = \@cf::_safe_eval_args;\n"
3215 . "#line 0 \"{$qcode}\"\n" 3221 . "#line 0 \"{$qcode}\"\n"
3216 . $code 3222 . $code
3217 . "\n}" 3223 . "\n}"
3218 ; 3224 ;
3219 3225
3226 if ($CFG{safe_eval}) {
3220 sub_generation_inc; 3227 sub_generation_inc;
3228 local @safe::cf::_safe_eval_args = values %vars;
3221 my @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval); 3229 @res = wantarray ? $safe->reval ($eval) : scalar $safe->reval ($eval);
3222 sub_generation_inc; 3230 sub_generation_inc;
3231 } else {
3232 local @cf::_safe_eval_args = values %vars;
3233 @res = wantarray ? eval eval : scalar eval $eval;
3234 }
3223 3235
3224 if ($@) { 3236 if ($@) {
3225 warn "$@"; 3237 warn "$@";
3226 warn "while executing safe code '$code'\n"; 3238 warn "while executing safe code '$code'\n";
3227 warn "with arguments " . (join " ", %vars) . "\n"; 3239 warn "with arguments " . (join " ", %vars) . "\n";
3246=cut 3258=cut
3247 3259
3248sub register_script_function { 3260sub register_script_function {
3249 my ($fun, $cb) = @_; 3261 my ($fun, $cb) = @_;
3250 3262
3251 no strict 'refs'; 3263 $fun = "safe::$fun" if $CFG{safe_eval};
3252 *{"safe::$fun"} = $safe_hole->wrap ($cb); 3264 *$fun = $safe_hole->wrap ($cb);
3253} 3265}
3254 3266
3255=back 3267=back
3256 3268
3257=cut 3269=cut
3278 3290
3279 $facedata->{version} == 2 3291 $facedata->{version} == 2
3280 or cf::cleanup "$path: version mismatch, cannot proceed."; 3292 or cf::cleanup "$path: version mismatch, cannot proceed.";
3281 3293
3282 # patch in the exptable 3294 # patch in the exptable
3295 my $exp_table = $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]);
3283 $facedata->{resource}{"res/exp_table"} = { 3296 $facedata->{resource}{"res/exp_table"} = {
3284 type => FT_RSRC, 3297 type => FT_RSRC,
3285 data => $enc->encode ([map cf::level_to_min_exp $_, 1 .. cf::settings->max_level]), 3298 data => $exp_table,
3299 hash => (Digest::MD5::md5 $exp_table),
3286 }; 3300 };
3287 cf::cede_to_tick; 3301 cf::cede_to_tick;
3288 3302
3289 { 3303 {
3290 my $faces = $facedata->{faceinfo}; 3304 my $faces = $facedata->{faceinfo};
3292 while (my ($face, $info) = each %$faces) { 3306 while (my ($face, $info) = each %$faces) {
3293 my $idx = (cf::face::find $face) || cf::face::alloc $face; 3307 my $idx = (cf::face::find $face) || cf::face::alloc $face;
3294 3308
3295 cf::face::set_visibility $idx, $info->{visibility}; 3309 cf::face::set_visibility $idx, $info->{visibility};
3296 cf::face::set_magicmap $idx, $info->{magicmap}; 3310 cf::face::set_magicmap $idx, $info->{magicmap};
3297 cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; 3311 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3298 cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; 3312 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3299 3313
3300 cf::cede_to_tick; 3314 cf::cede_to_tick;
3301 } 3315 }
3302 3316
3303 while (my ($face, $info) = each %$faces) { 3317 while (my ($face, $info) = each %$faces) {
3327 3341
3328 cf::anim::invalidate_all; # d'oh 3342 cf::anim::invalidate_all; # d'oh
3329 } 3343 }
3330 3344
3331 { 3345 {
3332 # TODO: for gcfclient pleasure, we should give resources
3333 # that gcfclient doesn't grok a >10000 face index.
3334 my $res = $facedata->{resource}; 3346 my $res = $facedata->{resource};
3335 3347
3336 while (my ($name, $info) = each %$res) { 3348 while (my ($name, $info) = each %$res) {
3337 if (defined $info->{type}) { 3349 if (defined $info->{type}) {
3338 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3350 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3339 my $data;
3340 3351
3341 if ($info->{type} & 1) { 3352 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}; 3353 cf::face::set_type $idx, $info->{type};
3356 } else { 3354 } else {
3357 $RESOURCE{$name} = $info; 3355 $RESOURCE{$name} = $info;
3358 } 3356 }
3359 3357
3522 }; 3520 };
3523 3521
3524 evthread_start IO::AIO::poll_fileno; 3522 evthread_start IO::AIO::poll_fileno;
3525 3523
3526 cf::sync_job { 3524 cf::sync_job {
3525 cf::load_settings;
3526 cf::load_materials;
3527
3527 reload_resources; 3528 reload_resources;
3528 reload_config; 3529 reload_config;
3529 db_init; 3530 db_init;
3530 3531
3531 cf::load_settings;
3532 cf::load_materials;
3533 cf::init_uuid; 3532 cf::init_uuid;
3534 cf::init_signals; 3533 cf::init_signals;
3535 cf::init_commands; 3534 cf::init_commands;
3536 cf::init_skills; 3535 cf::init_skills;
3537 3536
3896 3895
3897our @WAIT_FOR_TICK; 3896our @WAIT_FOR_TICK;
3898our @WAIT_FOR_TICK_BEGIN; 3897our @WAIT_FOR_TICK_BEGIN;
3899 3898
3900sub wait_for_tick { 3899sub wait_for_tick {
3901 return if tick_inhibit || $Coro::current == $Coro::main; 3900 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3902 3901
3903 my $signal = new Coro::Signal; 3902 my $signal = new Coro::Signal;
3904 push @WAIT_FOR_TICK, $signal; 3903 push @WAIT_FOR_TICK, $signal;
3905 $signal->wait; 3904 $signal->wait;
3906} 3905}
3907 3906
3908sub wait_for_tick_begin { 3907sub wait_for_tick_begin {
3909 return if tick_inhibit || $Coro::current == $Coro::main; 3908 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3910 3909
3911 my $signal = new Coro::Signal; 3910 my $signal = new Coro::Signal;
3912 push @WAIT_FOR_TICK_BEGIN, $signal; 3911 push @WAIT_FOR_TICK_BEGIN, $signal;
3913 $signal->wait; 3912 $signal->wait;
3914} 3913}
3919 unless ++$bug_warning > 10; 3918 unless ++$bug_warning > 10;
3920 return; 3919 return;
3921 } 3920 }
3922 3921
3923 cf::server_tick; # one server iteration 3922 cf::server_tick; # one server iteration
3923
3924 #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d#
3924 3925
3925 if ($NOW >= $NEXT_RUNTIME_WRITE) { 3926 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3926 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 3927 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
3927 Coro::async_pool { 3928 Coro::async_pool {
3928 $Coro::current->{desc} = "runtime saver"; 3929 $Coro::current->{desc} = "runtime saver";
3951} 3952}
3952 3953
3953{ 3954{
3954 # configure BDB 3955 # configure BDB
3955 3956
3956 BDB::min_parallel 8; 3957 BDB::min_parallel 16;
3957 BDB::max_poll_reqs $TICK * 0.1; 3958 BDB::max_poll_reqs $TICK * 0.1;
3958 $AnyEvent::BDB::WATCHER->priority (1); 3959 $AnyEvent::BDB::WATCHER->priority (1);
3959 3960
3960 unless ($DB_ENV) { 3961 unless ($DB_ENV) {
3961 $DB_ENV = BDB::db_env_create; 3962 $DB_ENV = BDB::db_env_create;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines