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.447 by root, Fri Sep 19 01:39:45 2008 UTC vs.
Revision 1.453 by root, Tue Sep 23 04:29:11 2008 UTC

63 63
64# configure various modules to our taste 64# configure various modules to our taste
65# 65#
66$Storable::canonical = 1; # reduce rsync transfers 66$Storable::canonical = 1; # reduce rsync transfers
67Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator 67Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator
68
69$Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority
70
71# make sure c-lzf reinitialises itself
72Compress::LZF::set_serializer "Storable", "Storable::net_mstore", "Storable::mretrieve";
68Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later 73Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later
69
70$Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority
71 74
72sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload 75sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
73 76
74our %COMMAND = (); 77our %COMMAND = ();
75our %COMMAND_TIME = (); 78our %COMMAND_TIME = ();
78our %EXTCMD = (); 81our %EXTCMD = ();
79our %EXTICMD = (); 82our %EXTICMD = ();
80our %EXT_CORO = (); # coroutines bound to extensions 83our %EXT_CORO = (); # coroutines bound to extensions
81our %EXT_MAP = (); # pluggable maps 84our %EXT_MAP = (); # pluggable maps
82 85
83our $RELOAD; # number of reloads so far 86our $RELOAD; # number of reloads so far, non-zero while in reload
84our @EVENT; 87our @EVENT;
85 88
86our $CONFDIR = confdir; 89our $CONFDIR = confdir;
87our $DATADIR = datadir; 90our $DATADIR = datadir;
88our $LIBDIR = "$DATADIR/ext"; 91our $LIBDIR = "$DATADIR/ext";
125our $LOAD; # a number between 0 (idle) and 1 (too many objects) 128our $LOAD; # a number between 0 (idle) and 1 (too many objects)
126our $LOADAVG; # same thing, but with alpha-smoothing 129our $LOADAVG; # same thing, but with alpha-smoothing
127our $JITTER; # average jitter 130our $JITTER; # average jitter
128our $TICK_START; # for load detecting purposes 131our $TICK_START; # for load detecting purposes
129 132
133our @POST_INIT;
134
130binmode STDOUT; 135binmode STDOUT;
131binmode STDERR; 136binmode STDERR;
132 137
133# read virtual server time, if available 138# read virtual server time, if available
134unless ($RUNTIME || !-e $RUNTIMEFILE) { 139unless ($RUNTIME || !-e $RUNTIMEFILE) {
302our $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
303 308
304sub encode_json($) { $json_coder->encode ($_[0]) } 309sub encode_json($) { $json_coder->encode ($_[0]) }
305sub decode_json($) { $json_coder->decode ($_[0]) } 310sub decode_json($) { $json_coder->decode ($_[0]) }
306 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
307=item cf::lock_wait $string 326=item cf::lock_wait $string
308 327
309Wait until the given lock is available. See cf::lock_acquire. 328Wait until the given lock is available. See cf::lock_acquire.
310 329
311=item my $lock = cf::lock_acquire $string 330=item my $lock = cf::lock_acquire $string
338 return;#d# 357 return;#d#
339 }#d# 358 }#d#
340 359
341 # wait for lock, if any 360 # wait for lock, if any
342 while ($LOCK{$key}) { 361 while ($LOCK{$key}) {
362 #local $Coro::current->{desc} = "$Coro::current->{desc} <waiting for lock $key>";
343 push @{ $LOCK{$key} }, $Coro::current; 363 push @{ $LOCK{$key} }, $Coro::current;
344 Coro::schedule; 364 Coro::schedule;
345 } 365 }
346} 366}
347 367
1441 1461
1442sub exists($) { 1462sub exists($) {
1443 my ($login) = @_; 1463 my ($login) = @_;
1444 1464
1445 $cf::PLAYER{$login} 1465 $cf::PLAYER{$login}
1446 or cf::sync_job { !aio_stat path $login } 1466 or !aio_stat path $login
1447} 1467}
1448 1468
1449sub find($) { 1469sub find($) {
1450 return $cf::PLAYER{$_[0]} || do { 1470 return $cf::PLAYER{$_[0]} || do {
1451 my $login = $_[0]; 1471 my $login = $_[0];
2768 id => "infobox", 2788 id => "infobox",
2769 title => "Body Parts", 2789 title => "Body Parts",
2770 reply => undef, 2790 reply => undef,
2771 tooltip => "Shows which body parts you posess and are available", 2791 tooltip => "Shows which body parts you posess and are available",
2772 }, 2792 },
2793 "c/skills" => {
2794 id => "infobox",
2795 title => "Skills",
2796 reply => undef,
2797 tooltip => "Shows your experience per skill and item power",
2798 },
2773 "c/uptime" => { 2799 "c/uptime" => {
2774 id => "infobox", 2800 id => "infobox",
2775 title => "Uptime", 2801 title => "Uptime",
2776 reply => undef, 2802 reply => undef,
2777 tooltip => "How long the server has been running since last restart", 2803 tooltip => "How long the server has been running since last restart",
3331 reload_treasures; 3357 reload_treasures;
3332 3358
3333 warn "finished reloading resource files\n"; 3359 warn "finished reloading resource files\n";
3334} 3360}
3335 3361
3336sub init {
3337 my $guard = freeze_mainloop;
3338
3339 evthread_start IO::AIO::poll_fileno;
3340
3341 reload_resources;
3342}
3343
3344sub reload_config { 3362sub reload_config {
3345 open my $fh, "<:utf8", "$CONFDIR/config" 3363 open my $fh, "<:utf8", "$CONFDIR/config"
3346 or return; 3364 or return;
3347 3365
3348 local $/; 3366 local $/;
3380 seek $fh, 0, 0; 3398 seek $fh, 0, 0;
3381 print $fh $$; 3399 print $fh $$;
3382} 3400}
3383 3401
3384sub main { 3402sub main {
3385 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
3386 3417
3387 # we must not ever block the main coroutine 3418 # we must not ever block the main coroutine
3388 local $Coro::idle = sub { 3419 local $Coro::idle = sub {
3389 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#
3390 (async { 3421 (async {
3391 $Coro::current->{desc} = "IDLE BUG HANDLER"; 3422 $Coro::current->{desc} = "IDLE BUG HANDLER";
3392 EV::loop EV::LOOP_ONESHOT; 3423 EV::loop EV::LOOP_ONESHOT;
3393 })->prio (Coro::PRIO_MAX); 3424 })->prio (Coro::PRIO_MAX);
3394 }; 3425 };
3395 3426
3396 { 3427 evthread_start IO::AIO::poll_fileno;
3397 my $guard = freeze_mainloop; 3428
3429 cf::sync_job {
3430 reload_resources;
3398 reload_config; 3431 reload_config;
3399 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
3400 load_extensions; 3445 load_extensions;
3401 3446
3402 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3403 }
3404
3405 utime time, time, $RUNTIMEFILE; 3447 utime time, time, $RUNTIMEFILE;
3406 3448
3407 # no (long-running) fork's whatsoever before this point(!) 3449 # no (long-running) fork's whatsoever before this point(!)
3408 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 };
3409 3454
3410 EV::loop; 3455 EV::loop;
3411} 3456}
3412 3457
3413############################################################################# 3458#############################################################################
3571 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; 3616 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
3572 for my $name (keys %$leaf_symtab) { 3617 for my $name (keys %$leaf_symtab) {
3573 _gv_clear *{"$pkg$name"}; 3618 _gv_clear *{"$pkg$name"};
3574# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; 3619# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"};
3575 } 3620 }
3576 warn "cleared package #$pkg\n";#d# 3621 warn "cleared package $pkg\n";#d#
3577} 3622}
3578
3579our $RELOAD; # how many times to reload
3580 3623
3581sub do_reload_perl() { 3624sub do_reload_perl() {
3582 # can/must only be called in main 3625 # can/must only be called in main
3583 if ($Coro::current != $Coro::main) { 3626 if ($Coro::current != $Coro::main) {
3584 warn "can only reload from main coroutine"; 3627 warn "can only reload from main coroutine";
3679 warn "reattaching attachments to maps"; 3722 warn "reattaching attachments to maps";
3680 reattach $_ for values %MAP; 3723 reattach $_ for values %MAP;
3681 warn "reattaching attachments to players"; 3724 warn "reattaching attachments to players";
3682 reattach $_ for values %PLAYER; 3725 reattach $_ for values %PLAYER;
3683 3726
3727 warn "running post_load";
3728 (pop @POST_INIT)->(1) while @POST_INIT;
3729
3684 warn "leaving sync_job"; 3730 warn "leaving sync_job";
3685 3731
3686 1 3732 1
3687 } or do { 3733 } or do {
3688 warn $@; 3734 warn $@;
3698 3744
3699sub reload_perl() { 3745sub reload_perl() {
3700 # doing reload synchronously and two reloads happen back-to-back, 3746 # doing reload synchronously and two reloads happen back-to-back,
3701 # coro crashes during coro_state_free->destroy here. 3747 # coro crashes during coro_state_free->destroy here.
3702 3748
3703 $RELOAD_WATCHER ||= EV::timer 0, 0, sub { 3749 $RELOAD_WATCHER ||= EV::timer $TICK * 1.5, 0, sub {
3704 do_reload_perl; 3750 do_reload_perl;
3705 undef $RELOAD_WATCHER; 3751 undef $RELOAD_WATCHER;
3706 }; 3752 };
3707} 3753}
3708 3754

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines