… | |
… | |
20 | # The authors can be reached via e-mail to <support@deliantra.net> |
20 | # The authors can be reached via e-mail to <support@deliantra.net> |
21 | # |
21 | # |
22 | |
22 | |
23 | package cf; |
23 | package cf; |
24 | |
24 | |
25 | use 5.10.0; |
25 | use common::sense; |
26 | use utf8; |
|
|
27 | use strict qw(vars subs); |
|
|
28 | |
26 | |
29 | use Symbol; |
27 | use Symbol; |
30 | use List::Util; |
28 | use List::Util; |
31 | use Socket; |
29 | use Socket; |
32 | use EV; |
30 | use EV; |
… | |
… | |
78 | # strictly for debugging |
76 | # strictly for debugging |
79 | $SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; |
77 | $SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; |
80 | |
78 | |
81 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
79 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
82 | |
80 | |
|
|
81 | our @ORIG_INC; |
|
|
82 | |
83 | our %COMMAND = (); |
83 | our %COMMAND = (); |
84 | our %COMMAND_TIME = (); |
84 | our %COMMAND_TIME = (); |
85 | |
85 | |
86 | our @EXTS = (); # list of extension package names |
86 | our @EXTS = (); # list of extension package names |
87 | our %EXTCMD = (); |
87 | our %EXTCMD = (); |
… | |
… | |
124 | our $BDB_DEADLOCK_WATCHER; |
124 | our $BDB_DEADLOCK_WATCHER; |
125 | our $BDB_CHECKPOINT_WATCHER; |
125 | our $BDB_CHECKPOINT_WATCHER; |
126 | our $BDB_TRICKLE_WATCHER; |
126 | our $BDB_TRICKLE_WATCHER; |
127 | our $DB_ENV; |
127 | our $DB_ENV; |
128 | |
128 | |
129 | our @EXTRA_MODULES = qw(pod match mapscript); |
129 | our @EXTRA_MODULES = qw(pod match mapscript incloader); |
130 | |
130 | |
131 | our %CFG; |
131 | our %CFG; |
132 | |
132 | |
133 | our $UPTIME; $UPTIME ||= time; |
133 | our $UPTIME; $UPTIME ||= time; |
134 | our $RUNTIME; |
134 | our $RUNTIME; |
… | |
… | |
233 | from wherever your confdir points to. |
233 | from wherever your confdir points to. |
234 | |
234 | |
235 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
235 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
236 | |
236 | |
237 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
237 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
238 | returns directly I<after> the tick processing (and consequently, can only wake one process |
238 | returns directly I<after> the tick processing (and consequently, can only wake one thread |
239 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
239 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
|
|
240 | |
|
|
241 | =cut |
|
|
242 | |
|
|
243 | sub wait_for_tick(); |
|
|
244 | sub wait_for_tick_begin(); |
240 | |
245 | |
241 | =item @cf::INVOKE_RESULTS |
246 | =item @cf::INVOKE_RESULTS |
242 | |
247 | |
243 | This array contains the results of the last C<invoke ()> call. When |
248 | This array contains the results of the last C<invoke ()> call. When |
244 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
249 | C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of |
… | |
… | |
352 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
357 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
353 | |
358 | |
354 | sub encode_json($) { $json_coder->encode ($_[0]) } |
359 | sub encode_json($) { $json_coder->encode ($_[0]) } |
355 | sub decode_json($) { $json_coder->decode ($_[0]) } |
360 | sub decode_json($) { $json_coder->decode ($_[0]) } |
356 | |
361 | |
|
|
362 | =item $ref = cf::yaml_load $scalar |
|
|
363 | |
|
|
364 | Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). |
|
|
365 | |
|
|
366 | =cut |
|
|
367 | |
|
|
368 | sub fork_call(&@); |
|
|
369 | |
|
|
370 | sub yaml_load($) { |
|
|
371 | fork_call { YAML::XS::Load $_[0] } @_ |
|
|
372 | } |
|
|
373 | |
357 | =item cf::post_init { BLOCK } |
374 | =item cf::post_init { BLOCK } |
358 | |
375 | |
359 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
376 | Execute the given codeblock, I<after> all extensions have been (re-)loaded, |
360 | but I<before> the server starts ticking again. |
377 | but I<before> the server starts ticking again. |
361 | |
378 | |
362 | The cdoeblock will have a single boolean argument to indicate whether this |
379 | The codeblock will have a single boolean argument to indicate whether this |
363 | is a reload or not. |
380 | is a reload or not. |
364 | |
381 | |
365 | =cut |
382 | =cut |
366 | |
383 | |
367 | sub post_init(&) { |
384 | sub post_init(&) { |
… | |
… | |
518 | =cut |
535 | =cut |
519 | |
536 | |
520 | sub sync_job(&) { |
537 | sub sync_job(&) { |
521 | my ($job) = @_; |
538 | my ($job) = @_; |
522 | |
539 | |
523 | if ($Coro::current == $Coro::main) { |
540 | if (in_main) { |
524 | my $time = AE::time; |
541 | my $time = AE::time; |
525 | |
542 | |
526 | # this is the main coro, too bad, we have to block |
543 | # this is the main coro, too bad, we have to block |
527 | # till the operation succeeds, freezing the server :/ |
544 | # till the operation succeeds, freezing the server :/ |
528 | |
545 | |
529 | LOG llevError, Carp::longmess "sync job";#d# |
546 | #LOG llevError, Carp::longmess "sync job";#d# |
530 | |
547 | |
531 | my $freeze_guard = freeze_mainloop; |
548 | my $freeze_guard = freeze_mainloop; |
532 | |
549 | |
533 | my $busy = 1; |
550 | my $busy = 1; |
534 | my @res; |
551 | my @res; |
… | |
… | |
578 | $EXT_CORO{$coro+0} = $coro; |
595 | $EXT_CORO{$coro+0} = $coro; |
579 | |
596 | |
580 | $coro |
597 | $coro |
581 | } |
598 | } |
582 | |
599 | |
583 | =item fork_call { }, $args |
600 | =item fork_call { }, @args |
584 | |
601 | |
585 | Executes the given code block with the given arguments in a seperate |
602 | Executes the given code block with the given arguments in a seperate |
586 | process, returning the results. Everything must be serialisable with |
603 | process, returning the results. Everything must be serialisable with |
587 | Coro::Storable. May, of course, block. Note that the executed sub may |
604 | Coro::Storable. May, of course, block. Note that the executed sub may |
588 | never block itself or use any form of event handling. |
605 | never block itself or use any form of event handling. |
589 | |
606 | |
590 | =cut |
607 | =cut |
591 | |
608 | |
|
|
609 | sub post_fork { |
|
|
610 | reset_signals; |
|
|
611 | } |
|
|
612 | |
592 | sub fork_call(&@) { |
613 | sub fork_call(&@) { |
593 | my ($cb, @args) = @_; |
614 | my ($cb, @args) = @_; |
594 | |
615 | |
595 | # we seemingly have to make a local copy of the whole thing, |
616 | # we seemingly have to make a local copy of the whole thing, |
596 | # otherwise perl prematurely frees the stuff :/ |
617 | # otherwise perl prematurely frees the stuff :/ |
597 | # TODO: investigate and fix (likely this will be rather laborious) |
618 | # TODO: investigate and fix (likely this will be rather laborious) |
598 | |
619 | |
599 | my @res = Coro::Util::fork_eval { |
620 | my @res = Coro::Util::fork_eval { |
600 | reset_signals; |
621 | cf::post_fork; |
601 | &$cb |
622 | &$cb |
602 | }, @args; |
623 | } @args; |
603 | |
624 | |
604 | wantarray ? @res : $res[-1] |
625 | wantarray ? @res : $res[-1] |
605 | } |
626 | } |
606 | |
627 | |
607 | sub objinfo { |
628 | sub objinfo { |
… | |
… | |
657 | within each server. |
678 | within each server. |
658 | |
679 | |
659 | =cut |
680 | =cut |
660 | |
681 | |
661 | sub db_table($) { |
682 | sub db_table($) { |
|
|
683 | cf::error "db_get called from main context" |
|
|
684 | if $Coro::current == $Coro::main; |
|
|
685 | |
662 | my ($name) = @_; |
686 | my ($name) = @_; |
663 | my $db = BDB::db_create $DB_ENV; |
687 | my $db = BDB::db_create $DB_ENV; |
664 | |
688 | |
665 | eval { |
689 | eval { |
666 | $db->set_flags (BDB::CHKSUM); |
690 | $db->set_flags (BDB::CHKSUM); |
… | |
… | |
676 | } |
700 | } |
677 | |
701 | |
678 | our $DB; |
702 | our $DB; |
679 | |
703 | |
680 | sub db_init { |
704 | sub db_init { |
681 | cf::sync_job { |
|
|
682 | $DB ||= db_table "db"; |
705 | $DB ||= db_table "db"; |
683 | }; |
|
|
684 | } |
706 | } |
685 | |
707 | |
686 | sub db_get($$) { |
708 | sub db_get($$) { |
687 | my $key = "$_[0]/$_[1]"; |
709 | my $key = "$_[0]/$_[1]"; |
688 | |
710 | |
689 | cf::sync_job { |
711 | cf::error "db_get called from main context" |
|
|
712 | if $Coro::current == $Coro::main; |
|
|
713 | |
690 | BDB::db_get $DB, undef, $key, my $data; |
714 | BDB::db_get $DB, undef, $key, my $data; |
691 | |
715 | |
692 | $! ? () |
716 | $! ? () |
693 | : $data |
717 | : $data |
694 | } |
|
|
695 | } |
718 | } |
696 | |
719 | |
697 | sub db_put($$$) { |
720 | sub db_put($$$) { |
698 | BDB::dbreq_pri 4; |
721 | BDB::dbreq_pri 4; |
699 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
722 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
… | |
… | |
1421 | |
1444 | |
1422 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1445 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1423 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1446 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1424 | |
1447 | |
1425 | $ext{source} = |
1448 | $ext{source} = |
1426 | "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" |
1449 | "package $pkg; use common::sense;\n" |
1427 | . "#line 1 \"$path\"\n{\n" |
1450 | . "#line 1 \"$path\"\n{\n" |
1428 | . $source |
1451 | . $source |
1429 | . "\n};\n1"; |
1452 | . "\n};\n1"; |
1430 | |
1453 | |
1431 | $todo{$base} = \%ext; |
1454 | $todo{$base} = \%ext; |
… | |
… | |
1639 | my $name = $pl->ob->name; |
1662 | my $name = $pl->ob->name; |
1640 | |
1663 | |
1641 | $pl->{deny_save} = 1; |
1664 | $pl->{deny_save} = 1; |
1642 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1665 | $pl->password ("*"); # this should lock out the player until we have nuked the dir |
1643 | |
1666 | |
1644 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1667 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns; |
1645 | $pl->deactivate; |
1668 | $pl->deactivate; |
|
|
1669 | |
1646 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1670 | my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; |
1647 | $pl->invoke (cf::EVENT_PLAYER_QUIT); |
1671 | $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns; |
|
|
1672 | ext::highscore::check ($pl->ob); |
|
|
1673 | |
1648 | $pl->ns->destroy if $pl->ns; |
1674 | $pl->ns->destroy if $pl->ns; |
1649 | |
1675 | |
1650 | my $path = playerdir $pl; |
1676 | my $path = playerdir $pl; |
1651 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1677 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1652 | aio_rename $path, $temp; |
1678 | aio_rename $path, $temp; |
… | |
… | |
1846 | |
1872 | |
1847 | sub register { |
1873 | sub register { |
1848 | my (undef, $regex, $prio) = @_; |
1874 | my (undef, $regex, $prio) = @_; |
1849 | my $pkg = caller; |
1875 | my $pkg = caller; |
1850 | |
1876 | |
1851 | no strict; |
|
|
1852 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1877 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1853 | |
1878 | |
1854 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1879 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1855 | } |
1880 | } |
1856 | |
1881 | |
… | |
… | |
1868 | } |
1893 | } |
1869 | |
1894 | |
1870 | sub normalise { |
1895 | sub normalise { |
1871 | my ($path, $base) = @_; |
1896 | my ($path, $base) = @_; |
1872 | |
1897 | |
1873 | $path = "$path"; # make sure its a string |
1898 | $path = "$path"; # make sure it's a string |
1874 | |
1899 | |
1875 | $path =~ s/\.map$//; |
1900 | $path =~ s/\.map$//; |
1876 | |
1901 | |
1877 | # map plan: |
1902 | # map plan: |
1878 | # |
1903 | # |
… | |
… | |
1917 | $self->init; # pass $1 etc. |
1942 | $self->init; # pass $1 etc. |
1918 | return $self; |
1943 | return $self; |
1919 | } |
1944 | } |
1920 | } |
1945 | } |
1921 | |
1946 | |
1922 | Carp::cluck "unable to resolve path '$path' (base '$base')."; |
1947 | Carp::cluck "unable to resolve path '$path' (base '$base')"; |
1923 | () |
1948 | () |
1924 | } |
1949 | } |
1925 | |
1950 | |
1926 | sub init { |
1951 | sub init { |
1927 | my ($self) = @_; |
1952 | my ($self) = @_; |
… | |
… | |
2049 | } |
2074 | } |
2050 | |
2075 | |
2051 | sub find; |
2076 | sub find; |
2052 | sub find { |
2077 | sub find { |
2053 | my ($path, $origin) = @_; |
2078 | my ($path, $origin) = @_; |
|
|
2079 | |
|
|
2080 | cf::cede_to_tick; |
2054 | |
2081 | |
2055 | $path = normalise $path, $origin && $origin->path; |
2082 | $path = normalise $path, $origin && $origin->path; |
2056 | |
2083 | |
2057 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2084 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
2058 | my $guard2 = cf::lock_acquire "map_find:$path"; |
2085 | my $guard2 = cf::lock_acquire "map_find:$path"; |
… | |
… | |
2190 | } |
2217 | } |
2191 | |
2218 | |
2192 | sub find_sync { |
2219 | sub find_sync { |
2193 | my ($path, $origin) = @_; |
2220 | my ($path, $origin) = @_; |
2194 | |
2221 | |
2195 | cf::sync_job { find $path, $origin } |
2222 | return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync" |
|
|
2223 | if $Coro::current == $Coro::main; |
|
|
2224 | |
|
|
2225 | find $path, $origin |
2196 | } |
2226 | } |
2197 | |
2227 | |
2198 | sub do_load_sync { |
2228 | sub do_load_sync { |
2199 | my ($map) = @_; |
2229 | my ($map) = @_; |
2200 | |
2230 | |
2201 | cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" |
2231 | return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync" |
2202 | if $Coro::current == $Coro::main; |
2232 | if $Coro::current == $Coro::main; |
2203 | |
2233 | |
2204 | cf::sync_job { $map->load }; |
2234 | $map->load; |
2205 | } |
2235 | } |
2206 | |
2236 | |
2207 | our %MAP_PREFETCH; |
2237 | our %MAP_PREFETCH; |
2208 | our $MAP_PREFETCHER = undef; |
2238 | our $MAP_PREFETCHER = undef; |
2209 | |
2239 | |
… | |
… | |
3268 | decrease split destroy change_exp value msg lore send_msg)], |
3298 | decrease split destroy change_exp value msg lore send_msg)], |
3269 | ["cf::object::player" => qw(player)], |
3299 | ["cf::object::player" => qw(player)], |
3270 | ["cf::player" => qw(peaceful send_msg)], |
3300 | ["cf::player" => qw(peaceful send_msg)], |
3271 | ["cf::map" => qw(trigger)], |
3301 | ["cf::map" => qw(trigger)], |
3272 | ) { |
3302 | ) { |
3273 | no strict 'refs'; |
|
|
3274 | my ($pkg, @funs) = @$_; |
3303 | my ($pkg, @funs) = @$_; |
3275 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3304 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3276 | for @funs; |
3305 | for @funs; |
3277 | } |
3306 | } |
3278 | |
3307 | |
… | |
… | |
3365 | |
3394 | |
3366 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3395 | my $enc = JSON::XS->new->utf8->canonical->relaxed; |
3367 | |
3396 | |
3368 | trace "loading facedata from $path\n"; |
3397 | trace "loading facedata from $path\n"; |
3369 | |
3398 | |
3370 | my $facedata; |
|
|
3371 | 0 < aio_load $path, $facedata |
3399 | 0 < aio_load $path, my $facedata |
3372 | or die "$path: $!"; |
3400 | or die "$path: $!"; |
3373 | |
3401 | |
3374 | $facedata = Coro::Storable::thaw $facedata; |
3402 | $facedata = Coro::Storable::thaw $facedata; |
3375 | |
3403 | |
3376 | $facedata->{version} == 2 |
3404 | $facedata->{version} == 2 |
… | |
… | |
3520 | } |
3548 | } |
3521 | |
3549 | |
3522 | sub reload_resources { |
3550 | sub reload_resources { |
3523 | trace "reloading resource files...\n"; |
3551 | trace "reloading resource files...\n"; |
3524 | |
3552 | |
|
|
3553 | reload_exp_table; |
|
|
3554 | reload_materials; |
3525 | reload_facedata; |
3555 | reload_facedata; |
3526 | reload_sound; |
3556 | reload_sound; |
3527 | reload_archetypes; |
3557 | reload_archetypes; |
3528 | reload_regions; |
3558 | reload_regions; |
3529 | reload_treasures; |
3559 | reload_treasures; |
… | |
… | |
3532 | } |
3562 | } |
3533 | |
3563 | |
3534 | sub reload_config { |
3564 | sub reload_config { |
3535 | trace "reloading config file...\n"; |
3565 | trace "reloading config file...\n"; |
3536 | |
3566 | |
3537 | open my $fh, "<:utf8", "$CONFDIR/config" |
3567 | 0 < aio_load "$CONFDIR/config", my $config |
3538 | or return; |
3568 | or die "$CONFDIR/config: $!"; |
3539 | |
3569 | |
3540 | local $/; |
3570 | utf8::decode $config; |
3541 | *CFG = YAML::XS::Load scalar <$fh>; |
3571 | *CFG = yaml_load $config; |
3542 | |
3572 | |
3543 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3573 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; |
3544 | |
3574 | |
3545 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3575 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3546 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3576 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3550 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3580 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
3551 | and die "WARNING: m(un)lockall failed: $!\n"; |
3581 | and die "WARNING: m(un)lockall failed: $!\n"; |
3552 | }; |
3582 | }; |
3553 | warn $@ if $@; |
3583 | warn $@ if $@; |
3554 | } |
3584 | } |
3555 | |
|
|
3556 | trace "finished reloading resource files\n"; |
|
|
3557 | } |
3585 | } |
3558 | |
3586 | |
3559 | sub pidfile() { |
3587 | sub pidfile() { |
3560 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3588 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
3561 | or die "$PIDFILE: $!"; |
3589 | or die "$PIDFILE: $!"; |
… | |
… | |
3586 | |
3614 | |
3587 | sub main { |
3615 | sub main { |
3588 | cf::init_globals; # initialise logging |
3616 | cf::init_globals; # initialise logging |
3589 | |
3617 | |
3590 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3618 | LOG llevInfo, "Welcome to Deliantra, v" . VERSION; |
3591 | LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3619 | LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; |
3592 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3620 | LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; |
3593 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3621 | LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; |
3594 | |
3622 | |
3595 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3623 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3596 | |
3624 | |
… | |
… | |
3604 | }; |
3632 | }; |
3605 | |
3633 | |
3606 | evthread_start IO::AIO::poll_fileno; |
3634 | evthread_start IO::AIO::poll_fileno; |
3607 | |
3635 | |
3608 | cf::sync_job { |
3636 | cf::sync_job { |
3609 | cf::init_experience; |
3637 | cf::incloader::init (); |
|
|
3638 | |
3610 | cf::init_anim; |
3639 | cf::init_anim; |
3611 | cf::init_attackmess; |
3640 | cf::init_attackmess; |
3612 | cf::init_dynamic; |
3641 | cf::init_dynamic; |
3613 | |
3642 | |
3614 | cf::load_settings; |
3643 | cf::load_settings; |
3615 | cf::load_materials; |
|
|
3616 | |
3644 | |
3617 | reload_resources; |
3645 | reload_resources; |
3618 | reload_config; |
3646 | reload_config; |
3619 | db_init; |
3647 | db_init; |
3620 | |
3648 | |
… | |
… | |
3638 | }; |
3666 | }; |
3639 | |
3667 | |
3640 | cf::object::thawer::errors_are_fatal 0; |
3668 | cf::object::thawer::errors_are_fatal 0; |
3641 | info "parse errors in files are no longer fatal from this point on.\n"; |
3669 | info "parse errors in files are no longer fatal from this point on.\n"; |
3642 | |
3670 | |
|
|
3671 | my $free_main; $free_main = EV::idle sub { |
|
|
3672 | undef $free_main; |
|
|
3673 | undef &main; # free gobs of memory :) |
|
|
3674 | }; |
|
|
3675 | |
3643 | main_loop; |
3676 | goto &main_loop; |
3644 | } |
3677 | } |
3645 | |
3678 | |
3646 | ############################################################################# |
3679 | ############################################################################# |
3647 | # initialisation and cleanup |
3680 | # initialisation and cleanup |
3648 | |
3681 | |
… | |
… | |
3747 | sub emergency_save() { |
3780 | sub emergency_save() { |
3748 | my $freeze_guard = cf::freeze_mainloop; |
3781 | my $freeze_guard = cf::freeze_mainloop; |
3749 | |
3782 | |
3750 | info "emergency_perl_save: enter\n"; |
3783 | info "emergency_perl_save: enter\n"; |
3751 | |
3784 | |
|
|
3785 | # this is a trade-off: we want to be very quick here, so |
|
|
3786 | # save all maps without fsync, and later call a global sync |
|
|
3787 | # (which in turn might be very very slow) |
|
|
3788 | local $USE_FSYNC = 0; |
|
|
3789 | |
3752 | cf::sync_job { |
3790 | cf::sync_job { |
3753 | # this is a trade-off: we want to be very quick here, so |
3791 | cf::write_runtime_sync; # external watchdog should not bark |
3754 | # save all maps without fsync, and later call a global sync |
|
|
3755 | # (which in turn might be very very slow) |
|
|
3756 | local $USE_FSYNC = 0; |
|
|
3757 | |
3792 | |
3758 | # use a peculiar iteration method to avoid tripping on perl |
3793 | # use a peculiar iteration method to avoid tripping on perl |
3759 | # refcount bugs in for. also avoids problems with players |
3794 | # refcount bugs in for. also avoids problems with players |
3760 | # and maps saved/destroyed asynchronously. |
3795 | # and maps saved/destroyed asynchronously. |
3761 | info "emergency_perl_save: begin player save\n"; |
3796 | info "emergency_perl_save: begin player save\n"; |
… | |
… | |
3765 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3800 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3766 | $pl->save; |
3801 | $pl->save; |
3767 | } |
3802 | } |
3768 | info "emergency_perl_save: end player save\n"; |
3803 | info "emergency_perl_save: end player save\n"; |
3769 | |
3804 | |
|
|
3805 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3806 | |
3770 | info "emergency_perl_save: begin map save\n"; |
3807 | info "emergency_perl_save: begin map save\n"; |
3771 | for my $path (keys %cf::MAP) { |
3808 | for my $path (keys %cf::MAP) { |
3772 | my $map = $cf::MAP{$path} or next; |
3809 | my $map = $cf::MAP{$path} or next; |
3773 | $map->valid or next; |
3810 | $map->valid or next; |
3774 | $map->save; |
3811 | $map->save; |
3775 | } |
3812 | } |
3776 | info "emergency_perl_save: end map save\n"; |
3813 | info "emergency_perl_save: end map save\n"; |
3777 | |
3814 | |
|
|
3815 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3816 | |
3778 | info "emergency_perl_save: begin database checkpoint\n"; |
3817 | info "emergency_perl_save: begin database checkpoint\n"; |
3779 | BDB::db_env_txn_checkpoint $DB_ENV; |
3818 | BDB::db_env_txn_checkpoint $DB_ENV; |
3780 | info "emergency_perl_save: end database checkpoint\n"; |
3819 | info "emergency_perl_save: end database checkpoint\n"; |
3781 | |
3820 | |
3782 | info "emergency_perl_save: begin write uuid\n"; |
3821 | info "emergency_perl_save: begin write uuid\n"; |
3783 | write_uuid_sync 1; |
3822 | write_uuid_sync 1; |
3784 | info "emergency_perl_save: end write uuid\n"; |
3823 | info "emergency_perl_save: end write uuid\n"; |
3785 | }; |
|
|
3786 | |
3824 | |
|
|
3825 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3826 | |
|
|
3827 | trace "emergency_perl_save: syncing database to disk"; |
|
|
3828 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
3829 | |
3787 | info "emergency_perl_save: starting sync()\n"; |
3830 | info "emergency_perl_save: starting sync\n"; |
3788 | IO::AIO::aio_sync sub { |
3831 | IO::AIO::aio_sync sub { |
3789 | info "emergency_perl_save: finished sync()\n"; |
3832 | info "emergency_perl_save: finished sync\n"; |
|
|
3833 | }; |
|
|
3834 | |
|
|
3835 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3836 | |
|
|
3837 | trace "emergency_perl_save: flushing outstanding aio requests"; |
|
|
3838 | while (IO::AIO::nreqs || BDB::nreqs) { |
|
|
3839 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
|
|
3840 | } |
|
|
3841 | |
|
|
3842 | cf::write_runtime_sync; # external watchdog should not bark |
3790 | }; |
3843 | }; |
3791 | |
3844 | |
3792 | info "emergency_perl_save: leave\n"; |
3845 | info "emergency_perl_save: leave\n"; |
3793 | } |
3846 | } |
3794 | |
3847 | |
3795 | sub post_cleanup { |
3848 | sub post_cleanup { |
3796 | my ($make_core) = @_; |
3849 | my ($make_core) = @_; |
|
|
3850 | |
|
|
3851 | IO::AIO::flush; |
3797 | |
3852 | |
3798 | error Carp::longmess "post_cleanup backtrace" |
3853 | error Carp::longmess "post_cleanup backtrace" |
3799 | if $make_core; |
3854 | if $make_core; |
3800 | |
3855 | |
3801 | my $fh = pidfile; |
3856 | my $fh = pidfile; |
… | |
… | |
3827 | } |
3882 | } |
3828 | } |
3883 | } |
3829 | |
3884 | |
3830 | sub do_reload_perl() { |
3885 | sub do_reload_perl() { |
3831 | # can/must only be called in main |
3886 | # can/must only be called in main |
3832 | if (in_main) { |
3887 | unless (in_main) { |
3833 | error "can only reload from main coroutine"; |
3888 | error "can only reload from main coroutine"; |
3834 | return; |
3889 | return; |
3835 | } |
3890 | } |
3836 | |
3891 | |
3837 | return if $RELOAD++; |
3892 | return if $RELOAD++; |
3838 | |
3893 | |
3839 | my $t1 = AE::time; |
3894 | my $t1 = AE::time; |
3840 | |
3895 | |
3841 | while ($RELOAD) { |
3896 | while ($RELOAD) { |
|
|
3897 | cf::get_slot 0.1, -1, "reload_perl"; |
3842 | info "reloading..."; |
3898 | info "reloading..."; |
3843 | |
3899 | |
3844 | trace "entering sync_job"; |
3900 | trace "entering sync_job"; |
3845 | |
3901 | |
3846 | cf::sync_job { |
3902 | cf::sync_job { |
3847 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3848 | cf::emergency_save; |
3903 | #cf::emergency_save; |
3849 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3850 | |
|
|
3851 | trace "syncing database to disk"; |
|
|
3852 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
3853 | |
|
|
3854 | # if anything goes wrong in here, we should simply crash as we already saved |
|
|
3855 | |
|
|
3856 | trace "flushing outstanding aio requests"; |
|
|
3857 | while (IO::AIO::nreqs || BDB::nreqs) { |
|
|
3858 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
|
|
3859 | } |
|
|
3860 | |
3904 | |
3861 | trace "cancelling all extension coros"; |
3905 | trace "cancelling all extension coros"; |
3862 | $_->cancel for values %EXT_CORO; |
3906 | $_->cancel for values %EXT_CORO; |
3863 | %EXT_CORO = (); |
3907 | %EXT_CORO = (); |
3864 | |
3908 | |
… | |
… | |
3979 | reload_perl; |
4023 | reload_perl; |
3980 | }; |
4024 | }; |
3981 | } |
4025 | } |
3982 | }; |
4026 | }; |
3983 | |
4027 | |
3984 | unshift @INC, $LIBDIR; |
4028 | ############################################################################# |
3985 | |
4029 | |
3986 | my $bug_warning = 0; |
4030 | my $bug_warning = 0; |
3987 | |
4031 | |
3988 | our @WAIT_FOR_TICK; |
4032 | our @WAIT_FOR_TICK; |
3989 | our @WAIT_FOR_TICK_BEGIN; |
4033 | our @WAIT_FOR_TICK_BEGIN; |
3990 | |
4034 | |
3991 | sub wait_for_tick { |
4035 | sub wait_for_tick() { |
3992 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4036 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
3993 | |
4037 | |
3994 | my $signal = new Coro::Signal; |
4038 | my $signal = new Coro::Signal; |
3995 | push @WAIT_FOR_TICK, $signal; |
4039 | push @WAIT_FOR_TICK, $signal; |
3996 | $signal->wait; |
4040 | $signal->wait; |
3997 | } |
4041 | } |
3998 | |
4042 | |
3999 | sub wait_for_tick_begin { |
4043 | sub wait_for_tick_begin() { |
4000 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4044 | return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; |
4001 | |
4045 | |
4002 | my $signal = new Coro::Signal; |
4046 | my $signal = new Coro::Signal; |
4003 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4047 | push @WAIT_FOR_TICK_BEGIN, $signal; |
4004 | $signal->wait; |
4048 | $signal->wait; |