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.449 by root, Sat Sep 20 00:09:27 2008 UTC vs.
Revision 1.454 by root, Thu Sep 25 12:14:32 2008 UTC

81our %EXTCMD = (); 81our %EXTCMD = ();
82our %EXTICMD = (); 82our %EXTICMD = ();
83our %EXT_CORO = (); # coroutines bound to extensions 83our %EXT_CORO = (); # coroutines bound to extensions
84our %EXT_MAP = (); # pluggable maps 84our %EXT_MAP = (); # pluggable maps
85 85
86our $RELOAD; # number of reloads so far 86our $RELOAD; # number of reloads so far, non-zero while in reload
87our @EVENT; 87our @EVENT;
88 88
89our $CONFDIR = confdir; 89our $CONFDIR = confdir;
90our $DATADIR = datadir; 90our $DATADIR = datadir;
91our $LIBDIR = "$DATADIR/ext"; 91our $LIBDIR = "$DATADIR/ext";
128our $LOAD; # a number between 0 (idle) and 1 (too many objects) 128our $LOAD; # a number between 0 (idle) and 1 (too many objects)
129our $LOADAVG; # same thing, but with alpha-smoothing 129our $LOADAVG; # same thing, but with alpha-smoothing
130our $JITTER; # average jitter 130our $JITTER; # average jitter
131our $TICK_START; # for load detecting purposes 131our $TICK_START; # for load detecting purposes
132 132
133our @POST_INIT;
134
133binmode STDOUT; 135binmode STDOUT;
134binmode STDERR; 136binmode STDERR;
135 137
136# read virtual server time, if available 138# read virtual server time, if available
137unless ($RUNTIME || !-e $RUNTIMEFILE) { 139unless ($RUNTIME || !-e $RUNTIMEFILE) {
305our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max 307our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max
306 308
307sub encode_json($) { $json_coder->encode ($_[0]) } 309sub encode_json($) { $json_coder->encode ($_[0]) }
308sub decode_json($) { $json_coder->decode ($_[0]) } 310sub decode_json($) { $json_coder->decode ($_[0]) }
309 311
312=item cf::post_init { BLOCK }
313
314Execute the given codeblock, I<after> all extensions have been (re-)loaded,
315but I<before> the server starts ticking again.
316
317The cdoeblock will have a single boolean argument to indicate whether this
318is a reload or not.
319
320=cut
321
322sub post_init(&) {
323 push @POST_INIT, shift;
324}
325
310=item cf::lock_wait $string 326=item cf::lock_wait $string
311 327
312Wait until the given lock is available. See cf::lock_acquire. 328Wait until the given lock is available. See cf::lock_acquire.
313 329
314=item my $lock = cf::lock_acquire $string 330=item my $lock = cf::lock_acquire $string
341 return;#d# 357 return;#d#
342 }#d# 358 }#d#
343 359
344 # wait for lock, if any 360 # wait for lock, if any
345 while ($LOCK{$key}) { 361 while ($LOCK{$key}) {
362 #local $Coro::current->{desc} = "$Coro::current->{desc} <waiting for lock $key>";
346 push @{ $LOCK{$key} }, $Coro::current; 363 push @{ $LOCK{$key} }, $Coro::current;
347 Coro::schedule; 364 Coro::schedule;
348 } 365 }
349} 366}
350 367
1444 1461
1445sub exists($) { 1462sub exists($) {
1446 my ($login) = @_; 1463 my ($login) = @_;
1447 1464
1448 $cf::PLAYER{$login} 1465 $cf::PLAYER{$login}
1449 or cf::sync_job { !aio_stat path $login } 1466 or !aio_stat path $login
1450} 1467}
1451 1468
1452sub find($) { 1469sub find($) {
1453 return $cf::PLAYER{$_[0]} || do { 1470 return $cf::PLAYER{$_[0]} || do {
1454 my $login = $_[0]; 1471 my $login = $_[0];
2771 id => "infobox", 2788 id => "infobox",
2772 title => "Body Parts", 2789 title => "Body Parts",
2773 reply => undef, 2790 reply => undef,
2774 tooltip => "Shows which body parts you posess and are available", 2791 tooltip => "Shows which body parts you posess and are available",
2775 }, 2792 },
2793 "c/skills" => {
2794 id => "infobox",
2795 title => "Skills",
2796 reply => undef,
2797 tooltip => "Shows your experience per skill and item power",
2798 },
2776 "c/uptime" => { 2799 "c/uptime" => {
2777 id => "infobox", 2800 id => "infobox",
2778 title => "Uptime", 2801 title => "Uptime",
2779 reply => undef, 2802 reply => undef,
2780 tooltip => "How long the server has been running since last restart", 2803 tooltip => "How long the server has been running since last restart",
3334 reload_treasures; 3357 reload_treasures;
3335 3358
3336 warn "finished reloading resource files\n"; 3359 warn "finished reloading resource files\n";
3337} 3360}
3338 3361
3339sub init {
3340 my $guard = freeze_mainloop;
3341
3342 evthread_start IO::AIO::poll_fileno;
3343
3344 reload_resources;
3345}
3346
3347sub reload_config { 3362sub reload_config {
3348 open my $fh, "<:utf8", "$CONFDIR/config" 3363 open my $fh, "<:utf8", "$CONFDIR/config"
3349 or return; 3364 or return;
3350 3365
3351 local $/; 3366 local $/;
3383 seek $fh, 0, 0; 3398 seek $fh, 0, 0;
3384 print $fh $$; 3399 print $fh $$;
3385} 3400}
3386 3401
3387sub main { 3402sub main {
3388 atomic; 3403 cf::init_globals; # initialise logging
3404
3405 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3406 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3407 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3408 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3409
3410 cf::init_experience;
3411 cf::init_anim;
3412 cf::init_attackmess;
3413 cf::init_dynamic;
3414 cf::init_block;
3415
3416 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3389 3417
3390 # we must not ever block the main coroutine 3418 # we must not ever block the main coroutine
3391 local $Coro::idle = sub { 3419 local $Coro::idle = sub {
3392 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# 3420 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
3393 (async { 3421 (async {
3394 $Coro::current->{desc} = "IDLE BUG HANDLER"; 3422 $Coro::current->{desc} = "IDLE BUG HANDLER";
3395 EV::loop EV::LOOP_ONESHOT; 3423 EV::loop EV::LOOP_ONESHOT;
3396 })->prio (Coro::PRIO_MAX); 3424 })->prio (Coro::PRIO_MAX);
3397 }; 3425 };
3398 3426
3399 { 3427 evthread_start IO::AIO::poll_fileno;
3400 my $guard = freeze_mainloop; 3428
3429 cf::sync_job {
3430 reload_resources;
3401 reload_config; 3431 reload_config;
3402 db_init; 3432 db_init;
3433
3434 cf::load_settings;
3435 cf::load_materials;
3436 cf::init_uuid;
3437 cf::init_signals;
3438 cf::init_commands;
3439 cf::init_skills;
3440
3441 cf::init_beforeplay;
3442
3443 atomic;
3444
3403 load_extensions; 3445 load_extensions;
3404 3446
3405 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3406 }
3407
3408 utime time, time, $RUNTIMEFILE; 3447 utime time, time, $RUNTIMEFILE;
3409 3448
3410 # no (long-running) fork's whatsoever before this point(!) 3449 # no (long-running) fork's whatsoever before this point(!)
3411 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; 3450 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3451
3452 (pop @POST_INIT)->(0) while @POST_INIT;
3453 };
3412 3454
3413 EV::loop; 3455 EV::loop;
3414} 3456}
3415 3457
3416############################################################################# 3458#############################################################################
3474 my $uuid = "$LOCALDIR/uuid"; 3516 my $uuid = "$LOCALDIR/uuid";
3475 3517
3476 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 3518 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644
3477 or return; 3519 or return;
3478 3520
3479 my $value = uuid_str $uuid_skip + uuid_seq uuid_cur; 3521 my $value = uuid_seq uuid_cur;
3522
3523 if ($value < 1024) {
3524 warn "cowardly refusing to write low uuid value ($value)!\n";
3525 return;
3526 }
3527
3528 my $value = uuid_str $value + $uuid_skip;
3480 $uuid_skip = 0; 3529 $uuid_skip = 0;
3481 3530
3482 (aio_write $fh, 0, (length $value), $value, 0) <= 0 3531 (aio_write $fh, 0, (length $value), $value, 0) <= 0
3483 and return; 3532 and return;
3484 3533
3574 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; 3623 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
3575 for my $name (keys %$leaf_symtab) { 3624 for my $name (keys %$leaf_symtab) {
3576 _gv_clear *{"$pkg$name"}; 3625 _gv_clear *{"$pkg$name"};
3577# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; 3626# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"};
3578 } 3627 }
3579 warn "cleared package #$pkg\n";#d# 3628 warn "cleared package $pkg\n";#d#
3580} 3629}
3581
3582our $RELOAD; # how many times to reload
3583 3630
3584sub do_reload_perl() { 3631sub do_reload_perl() {
3585 # can/must only be called in main 3632 # can/must only be called in main
3586 if ($Coro::current != $Coro::main) { 3633 if ($Coro::current != $Coro::main) {
3587 warn "can only reload from main coroutine"; 3634 warn "can only reload from main coroutine";
3682 warn "reattaching attachments to maps"; 3729 warn "reattaching attachments to maps";
3683 reattach $_ for values %MAP; 3730 reattach $_ for values %MAP;
3684 warn "reattaching attachments to players"; 3731 warn "reattaching attachments to players";
3685 reattach $_ for values %PLAYER; 3732 reattach $_ for values %PLAYER;
3686 3733
3734 warn "running post_load";
3735 (pop @POST_INIT)->(1) while @POST_INIT;
3736
3687 warn "leaving sync_job"; 3737 warn "leaving sync_job";
3688 3738
3689 1 3739 1
3690 } or do { 3740 } or do {
3691 warn $@; 3741 warn $@;
3701 3751
3702sub reload_perl() { 3752sub reload_perl() {
3703 # doing reload synchronously and two reloads happen back-to-back, 3753 # doing reload synchronously and two reloads happen back-to-back,
3704 # coro crashes during coro_state_free->destroy here. 3754 # coro crashes during coro_state_free->destroy here.
3705 3755
3706 $RELOAD_WATCHER ||= EV::timer 0, 0, sub { 3756 $RELOAD_WATCHER ||= EV::timer $TICK * 1.5, 0, sub {
3707 do_reload_perl; 3757 do_reload_perl;
3708 undef $RELOAD_WATCHER; 3758 undef $RELOAD_WATCHER;
3709 }; 3759 };
3710} 3760}
3711 3761

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines