… | |
… | |
81 | our %EXTCMD = (); |
81 | our %EXTCMD = (); |
82 | our %EXTICMD = (); |
82 | our %EXTICMD = (); |
83 | our %EXT_CORO = (); # coroutines bound to extensions |
83 | our %EXT_CORO = (); # coroutines bound to extensions |
84 | our %EXT_MAP = (); # pluggable maps |
84 | our %EXT_MAP = (); # pluggable maps |
85 | |
85 | |
86 | our $RELOAD; # number of reloads so far |
86 | our $RELOAD; # number of reloads so far, non-zero while in reload |
87 | our @EVENT; |
87 | our @EVENT; |
88 | |
88 | |
89 | our $CONFDIR = confdir; |
89 | our $CONFDIR = confdir; |
90 | our $DATADIR = datadir; |
90 | our $DATADIR = datadir; |
91 | our $LIBDIR = "$DATADIR/ext"; |
91 | our $LIBDIR = "$DATADIR/ext"; |
… | |
… | |
128 | our $LOAD; # a number between 0 (idle) and 1 (too many objects) |
128 | our $LOAD; # a number between 0 (idle) and 1 (too many objects) |
129 | our $LOADAVG; # same thing, but with alpha-smoothing |
129 | our $LOADAVG; # same thing, but with alpha-smoothing |
130 | our $JITTER; # average jitter |
130 | our $JITTER; # average jitter |
131 | our $TICK_START; # for load detecting purposes |
131 | our $TICK_START; # for load detecting purposes |
132 | |
132 | |
|
|
133 | our @POST_INIT; |
|
|
134 | |
133 | binmode STDOUT; |
135 | binmode STDOUT; |
134 | binmode STDERR; |
136 | binmode STDERR; |
135 | |
137 | |
136 | # read virtual server time, if available |
138 | # read virtual server time, if available |
137 | unless ($RUNTIME || !-e $RUNTIMEFILE) { |
139 | unless ($RUNTIME || !-e $RUNTIMEFILE) { |
… | |
… | |
305 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
307 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
306 | |
308 | |
307 | sub encode_json($) { $json_coder->encode ($_[0]) } |
309 | sub encode_json($) { $json_coder->encode ($_[0]) } |
308 | sub decode_json($) { $json_coder->decode ($_[0]) } |
310 | sub decode_json($) { $json_coder->decode ($_[0]) } |
309 | |
311 | |
|
|
312 | =item cf::post_init { BLOCK } |
|
|
313 | |
|
|
314 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
|
|
315 | but I<before> the server starts ticking again. |
|
|
316 | |
|
|
317 | The cdoeblock will have a single boolean argument to indicate whether this |
|
|
318 | is a reload or not. |
|
|
319 | |
|
|
320 | =cut |
|
|
321 | |
|
|
322 | sub post_init(&) { |
|
|
323 | push @POST_INIT, shift; |
|
|
324 | } |
|
|
325 | |
310 | =item cf::lock_wait $string |
326 | =item cf::lock_wait $string |
311 | |
327 | |
312 | Wait until the given lock is available. See cf::lock_acquire. |
328 | Wait 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 | |
1445 | sub exists($) { |
1462 | sub 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 | |
1452 | sub find($) { |
1469 | sub find($) { |
1453 | return $cf::PLAYER{$_[0]} || do { |
1470 | return $cf::PLAYER{$_[0]} || do { |
1454 | my $login = $_[0]; |
1471 | my $login = $_[0]; |
… | |
… | |
3340 | reload_treasures; |
3357 | reload_treasures; |
3341 | |
3358 | |
3342 | warn "finished reloading resource files\n"; |
3359 | warn "finished reloading resource files\n"; |
3343 | } |
3360 | } |
3344 | |
3361 | |
3345 | sub init { |
|
|
3346 | my $guard = freeze_mainloop; |
|
|
3347 | |
|
|
3348 | evthread_start IO::AIO::poll_fileno; |
|
|
3349 | |
|
|
3350 | reload_resources; |
|
|
3351 | } |
|
|
3352 | |
|
|
3353 | sub reload_config { |
3362 | sub reload_config { |
3354 | open my $fh, "<:utf8", "$CONFDIR/config" |
3363 | open my $fh, "<:utf8", "$CONFDIR/config" |
3355 | or return; |
3364 | or return; |
3356 | |
3365 | |
3357 | local $/; |
3366 | local $/; |
… | |
… | |
3389 | seek $fh, 0, 0; |
3398 | seek $fh, 0, 0; |
3390 | print $fh $$; |
3399 | print $fh $$; |
3391 | } |
3400 | } |
3392 | |
3401 | |
3393 | sub main { |
3402 | sub main { |
3394 | 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 |
3395 | |
3417 | |
3396 | # we must not ever block the main coroutine |
3418 | # we must not ever block the main coroutine |
3397 | local $Coro::idle = sub { |
3419 | local $Coro::idle = sub { |
3398 | 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# |
3399 | (async { |
3421 | (async { |
3400 | $Coro::current->{desc} = "IDLE BUG HANDLER"; |
3422 | $Coro::current->{desc} = "IDLE BUG HANDLER"; |
3401 | EV::loop EV::LOOP_ONESHOT; |
3423 | EV::loop EV::LOOP_ONESHOT; |
3402 | })->prio (Coro::PRIO_MAX); |
3424 | })->prio (Coro::PRIO_MAX); |
3403 | }; |
3425 | }; |
3404 | |
3426 | |
3405 | { |
3427 | evthread_start IO::AIO::poll_fileno; |
3406 | my $guard = freeze_mainloop; |
3428 | |
|
|
3429 | cf::sync_job { |
|
|
3430 | reload_resources; |
3407 | reload_config; |
3431 | reload_config; |
3408 | 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 | |
3409 | load_extensions; |
3445 | load_extensions; |
3410 | |
3446 | |
3411 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
|
|
3412 | } |
|
|
3413 | |
|
|
3414 | utime time, time, $RUNTIMEFILE; |
3447 | utime time, time, $RUNTIMEFILE; |
3415 | |
3448 | |
3416 | # no (long-running) fork's whatsoever before this point(!) |
3449 | # no (long-running) fork's whatsoever before this point(!) |
3417 | 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 | }; |
3418 | |
3454 | |
3419 | EV::loop; |
3455 | EV::loop; |
3420 | } |
3456 | } |
3421 | |
3457 | |
3422 | ############################################################################# |
3458 | ############################################################################# |
… | |
… | |
3580 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; |
3616 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; |
3581 | for my $name (keys %$leaf_symtab) { |
3617 | for my $name (keys %$leaf_symtab) { |
3582 | _gv_clear *{"$pkg$name"}; |
3618 | _gv_clear *{"$pkg$name"}; |
3583 | # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; |
3619 | # use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; |
3584 | } |
3620 | } |
3585 | warn "cleared package #$pkg\n";#d# |
3621 | warn "cleared package $pkg\n";#d# |
3586 | } |
3622 | } |
3587 | |
|
|
3588 | our $RELOAD; # how many times to reload |
|
|
3589 | |
3623 | |
3590 | sub do_reload_perl() { |
3624 | sub do_reload_perl() { |
3591 | # can/must only be called in main |
3625 | # can/must only be called in main |
3592 | if ($Coro::current != $Coro::main) { |
3626 | if ($Coro::current != $Coro::main) { |
3593 | warn "can only reload from main coroutine"; |
3627 | warn "can only reload from main coroutine"; |
… | |
… | |
3688 | warn "reattaching attachments to maps"; |
3722 | warn "reattaching attachments to maps"; |
3689 | reattach $_ for values %MAP; |
3723 | reattach $_ for values %MAP; |
3690 | warn "reattaching attachments to players"; |
3724 | warn "reattaching attachments to players"; |
3691 | reattach $_ for values %PLAYER; |
3725 | reattach $_ for values %PLAYER; |
3692 | |
3726 | |
|
|
3727 | warn "running post_load"; |
|
|
3728 | (pop @POST_INIT)->(1) while @POST_INIT; |
|
|
3729 | |
3693 | warn "leaving sync_job"; |
3730 | warn "leaving sync_job"; |
3694 | |
3731 | |
3695 | 1 |
3732 | 1 |
3696 | } or do { |
3733 | } or do { |
3697 | warn $@; |
3734 | warn $@; |
… | |
… | |
3707 | |
3744 | |
3708 | sub reload_perl() { |
3745 | sub reload_perl() { |
3709 | # doing reload synchronously and two reloads happen back-to-back, |
3746 | # doing reload synchronously and two reloads happen back-to-back, |
3710 | # coro crashes during coro_state_free->destroy here. |
3747 | # coro crashes during coro_state_free->destroy here. |
3711 | |
3748 | |
3712 | $RELOAD_WATCHER ||= EV::timer 0, 0, sub { |
3749 | $RELOAD_WATCHER ||= EV::timer $TICK * 1.5, 0, sub { |
3713 | do_reload_perl; |
3750 | do_reload_perl; |
3714 | undef $RELOAD_WATCHER; |
3751 | undef $RELOAD_WATCHER; |
3715 | }; |
3752 | }; |
3716 | } |
3753 | } |
3717 | |
3754 | |