… | |
… | |
108 | our $PIDFILE = "$LOCALDIR/pid"; |
108 | our $PIDFILE = "$LOCALDIR/pid"; |
109 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
109 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
110 | |
110 | |
111 | our %RESOURCE; |
111 | our %RESOURCE; |
112 | |
112 | |
|
|
113 | our $OUTPUT_RATE_MIN = 4000; |
|
|
114 | our $OUTPUT_RATE_MAX = 100000; |
|
|
115 | |
113 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
116 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
114 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
117 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
115 | our $NEXT_TICK; |
118 | our $NEXT_TICK; |
116 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
119 | our $USE_FSYNC = 1; # use fsync to write maps - default on |
117 | |
120 | |
… | |
… | |
503 | |
506 | |
504 | sub sync_job(&) { |
507 | sub sync_job(&) { |
505 | my ($job) = @_; |
508 | my ($job) = @_; |
506 | |
509 | |
507 | if ($Coro::current == $Coro::main) { |
510 | if ($Coro::current == $Coro::main) { |
508 | my $time = EV::time; |
511 | my $time = AE::time; |
509 | |
512 | |
510 | # this is the main coro, too bad, we have to block |
513 | # this is the main coro, too bad, we have to block |
511 | # till the operation succeeds, freezing the server :/ |
514 | # till the operation succeeds, freezing the server :/ |
512 | |
515 | |
513 | LOG llevError, Carp::longmess "sync job";#d# |
516 | LOG llevError, Carp::longmess "sync job";#d# |
… | |
… | |
530 | } else { |
533 | } else { |
531 | EV::loop EV::LOOP_ONESHOT; |
534 | EV::loop EV::LOOP_ONESHOT; |
532 | } |
535 | } |
533 | } |
536 | } |
534 | |
537 | |
535 | my $time = EV::time - $time; |
538 | my $time = AE::time - $time; |
536 | |
539 | |
537 | $TICK_START += $time; # do not account sync jobs to server load |
540 | $TICK_START += $time; # do not account sync jobs to server load |
538 | |
541 | |
539 | wantarray ? @res : $res[0] |
542 | wantarray ? @res : $res[0] |
540 | } else { |
543 | } else { |
… | |
… | |
1310 | } |
1313 | } |
1311 | |
1314 | |
1312 | use File::Glob (); |
1315 | use File::Glob (); |
1313 | |
1316 | |
1314 | cf::player->attach ( |
1317 | cf::player->attach ( |
1315 | on_command => sub { |
1318 | on_unknown_command => sub { |
1316 | my ($pl, $name, $params) = @_; |
1319 | my ($pl, $name, $params) = @_; |
1317 | |
1320 | |
1318 | my $cb = $COMMAND{$name} |
1321 | my $cb = $COMMAND{$name} |
1319 | or return; |
1322 | or return; |
1320 | |
1323 | |
… | |
… | |
1399 | . "\n};\n1"; |
1402 | . "\n};\n1"; |
1400 | |
1403 | |
1401 | $todo{$base} = \%ext; |
1404 | $todo{$base} = \%ext; |
1402 | } |
1405 | } |
1403 | |
1406 | |
|
|
1407 | my $pass = 0; |
1404 | my %done; |
1408 | my %done; |
1405 | while (%todo) { |
1409 | while (%todo) { |
1406 | my $progress; |
1410 | my $progress; |
1407 | |
1411 | |
|
|
1412 | ++$pass; |
|
|
1413 | |
|
|
1414 | ext: |
1408 | while (my ($k, $v) = each %todo) { |
1415 | while (my ($k, $v) = each %todo) { |
1409 | for (split /,\s*/, $v->{meta}{depends}) { |
1416 | for (split /,\s*/, $v->{meta}{depends}) { |
1410 | goto skip |
1417 | next ext |
1411 | unless exists $done{$_}; |
1418 | unless exists $done{$_}; |
1412 | } |
1419 | } |
1413 | |
1420 | |
1414 | warn "... loading '$k' into '$v->{pkg}'\n"; |
1421 | warn "... pass $pass, loading '$k' into '$v->{pkg}'\n"; |
1415 | |
1422 | |
1416 | unless (eval $v->{source}) { |
1423 | my $active = eval $v->{source}; |
|
|
1424 | |
|
|
1425 | if (length $@) { |
1417 | my $msg = $@ ? "$v->{path}: $@\n" |
1426 | warn "$v->{path}: $@\n"; |
1418 | : "$v->{base}: extension inactive.\n"; |
|
|
1419 | |
1427 | |
1420 | if (exists $v->{meta}{mandatory}) { |
|
|
1421 | warn $msg; |
|
|
1422 | cf::cleanup "mandatory extension failed to load, exiting."; |
1428 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1423 | } |
1429 | if exists $v->{meta}{mandatory}; |
1424 | |
1430 | } else { |
1425 | warn $msg; |
1431 | $done{$k} = delete $todo{$k}; |
|
|
1432 | push @EXTS, $v->{pkg}; |
|
|
1433 | $progress = 1; |
|
|
1434 | |
|
|
1435 | warn "$v->{base}: extension inactive.\n" |
|
|
1436 | unless $active; |
1426 | } |
1437 | } |
1427 | |
|
|
1428 | $done{$k} = delete $todo{$k}; |
|
|
1429 | push @EXTS, $v->{pkg}; |
|
|
1430 | $progress = 1; |
|
|
1431 | } |
1438 | } |
1432 | |
1439 | |
1433 | skip: |
1440 | unless ($progress) { |
1434 | die "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n" |
1441 | warn "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n"; |
1435 | unless $progress; |
1442 | |
|
|
1443 | while (my ($k, $v) = each %todo) { |
|
|
1444 | cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting." |
|
|
1445 | if exists $v->{meta}{mandatory}; |
|
|
1446 | } |
|
|
1447 | } |
1436 | } |
1448 | } |
1437 | }; |
1449 | }; |
1438 | } |
1450 | } |
1439 | |
1451 | |
1440 | ############################################################################# |
1452 | ############################################################################# |
… | |
… | |
1524 | $cf::PLAYER{$login} = $pl |
1536 | $cf::PLAYER{$login} = $pl |
1525 | } |
1537 | } |
1526 | } |
1538 | } |
1527 | } |
1539 | } |
1528 | |
1540 | |
|
|
1541 | cf::player->attach ( |
|
|
1542 | on_load => sub { |
|
|
1543 | my ($pl, $path) = @_; |
|
|
1544 | |
|
|
1545 | # restore slots saved in save, below |
|
|
1546 | my $slots = delete $pl->{_slots}; |
|
|
1547 | |
|
|
1548 | $pl->ob->current_weapon ($slots->[0]); |
|
|
1549 | $pl->combat_ob ($slots->[1]); |
|
|
1550 | $pl->ranged_ob ($slots->[2]); |
|
|
1551 | }, |
|
|
1552 | ); |
|
|
1553 | |
1529 | sub save($) { |
1554 | sub save($) { |
1530 | my ($pl) = @_; |
1555 | my ($pl) = @_; |
1531 | |
1556 | |
1532 | return if $pl->{deny_save}; |
1557 | return if $pl->{deny_save}; |
1533 | |
1558 | |
… | |
… | |
1538 | |
1563 | |
1539 | aio_mkdir playerdir $pl, 0770; |
1564 | aio_mkdir playerdir $pl, 0770; |
1540 | $pl->{last_save} = $cf::RUNTIME; |
1565 | $pl->{last_save} = $cf::RUNTIME; |
1541 | |
1566 | |
1542 | cf::get_slot 0.01; |
1567 | cf::get_slot 0.01; |
|
|
1568 | |
|
|
1569 | # save slots, to be restored later |
|
|
1570 | local $pl->{_slots} = [$pl->ob->current_weapon, $pl->combat_ob, $pl->ranged_ob]; |
1543 | |
1571 | |
1544 | $pl->save_pl ($path); |
1572 | $pl->save_pl ($path); |
1545 | cf::cede_to_tick; |
1573 | cf::cede_to_tick; |
1546 | } |
1574 | } |
1547 | |
1575 | |
… | |
… | |
1760 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1788 | my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM |
1761 | |
1789 | |
1762 | # mit "rum" bekleckern, nicht |
1790 | # mit "rum" bekleckern, nicht |
1763 | $self->_create_random_map ( |
1791 | $self->_create_random_map ( |
1764 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1792 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1765 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1793 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle}, |
1766 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1794 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1767 | $rmp->{exit_on_final_map}, |
1795 | $rmp->{exit_on_final_map}, |
1768 | $rmp->{xsize}, $rmp->{ysize}, |
1796 | $rmp->{xsize}, $rmp->{ysize}, |
1769 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
1797 | $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, |
1770 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
1798 | $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, |
… | |
… | |
2084 | } |
2112 | } |
2085 | |
2113 | |
2086 | $self->post_load; |
2114 | $self->post_load; |
2087 | } |
2115 | } |
2088 | |
2116 | |
|
|
2117 | # customize the map for a given player, i.e. |
|
|
2118 | # return the _real_ map. used by e.g. per-player |
|
|
2119 | # maps to change the path to ~playername/mappath |
2089 | sub customise_for { |
2120 | sub customise_for { |
2090 | my ($self, $ob) = @_; |
2121 | my ($self, $ob) = @_; |
2091 | |
2122 | |
2092 | return find "~" . $ob->name . "/" . $self->{path} |
2123 | return find "~" . $ob->name . "/" . $self->{path} |
2093 | if $self->per_player; |
2124 | if $self->per_player; |
… | |
… | |
3122 | |
3153 | |
3123 | $coro |
3154 | $coro |
3124 | } |
3155 | } |
3125 | |
3156 | |
3126 | cf::client->attach ( |
3157 | cf::client->attach ( |
3127 | on_destroy => sub { |
3158 | on_client_destroy => sub { |
3128 | my ($ns) = @_; |
3159 | my ($ns) = @_; |
3129 | |
3160 | |
3130 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3161 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3131 | }, |
3162 | }, |
3132 | ); |
3163 | ); |
… | |
… | |
3529 | reload_config; |
3560 | reload_config; |
3530 | db_init; |
3561 | db_init; |
3531 | |
3562 | |
3532 | cf::init_uuid; |
3563 | cf::init_uuid; |
3533 | cf::init_signals; |
3564 | cf::init_signals; |
3534 | cf::init_commands; |
|
|
3535 | cf::init_skills; |
3565 | cf::init_skills; |
3536 | |
3566 | |
3537 | cf::init_beforeplay; |
3567 | cf::init_beforeplay; |
3538 | |
3568 | |
3539 | atomic; |
3569 | atomic; |
… | |
… | |
3557 | |
3587 | |
3558 | # install some emergency cleanup handlers |
3588 | # install some emergency cleanup handlers |
3559 | BEGIN { |
3589 | BEGIN { |
3560 | our %SIGWATCHER = (); |
3590 | our %SIGWATCHER = (); |
3561 | for my $signal (qw(INT HUP TERM)) { |
3591 | for my $signal (qw(INT HUP TERM)) { |
3562 | $SIGWATCHER{$signal} = EV::signal $signal, sub { |
3592 | $SIGWATCHER{$signal} = AE::signal $signal, sub { |
3563 | cf::cleanup "SIG$signal"; |
3593 | cf::cleanup "SIG$signal"; |
3564 | }; |
3594 | }; |
3565 | } |
3595 | } |
3566 | } |
3596 | } |
3567 | |
3597 | |
3568 | sub write_runtime_sync { |
3598 | sub write_runtime_sync { |
|
|
3599 | my $t0 = AE::time; |
|
|
3600 | |
3569 | # first touch the runtime file to show we are still running: |
3601 | # first touch the runtime file to show we are still running: |
3570 | # the fsync below can take a very very long time. |
3602 | # the fsync below can take a very very long time. |
3571 | |
3603 | |
3572 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3604 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3573 | |
3605 | |
3574 | my $guard = cf::lock_acquire "write_runtime"; |
3606 | my $guard = cf::lock_acquire "write_runtime"; |
3575 | |
3607 | |
3576 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644 |
3608 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT | O_TRUNC, 0644 |
3577 | or return; |
3609 | or return; |
3578 | |
3610 | |
3579 | my $value = $cf::RUNTIME + 90 + 10; |
3611 | my $value = $cf::RUNTIME + 90 + 10; |
3580 | # 10 is the runtime save interval, for a monotonic clock |
3612 | # 10 is the runtime save interval, for a monotonic clock |
3581 | # 60 allows for the watchdog to kill the server. |
3613 | # 60 allows for the watchdog to kill the server. |
… | |
… | |
3594 | or return; |
3626 | or return; |
3595 | |
3627 | |
3596 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3628 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3597 | and return; |
3629 | and return; |
3598 | |
3630 | |
3599 | warn "runtime file written.\n"; |
3631 | warn sprintf "runtime file written (%gs).\n", AE::time - $t0; |
3600 | |
3632 | |
3601 | 1 |
3633 | 1 |
3602 | } |
3634 | } |
3603 | |
3635 | |
3604 | our $uuid_lock; |
3636 | our $uuid_lock; |
… | |
… | |
3742 | return; |
3774 | return; |
3743 | } |
3775 | } |
3744 | |
3776 | |
3745 | return if $RELOAD++; |
3777 | return if $RELOAD++; |
3746 | |
3778 | |
3747 | my $t1 = EV::time; |
3779 | my $t1 = AE::time; |
3748 | |
3780 | |
3749 | while ($RELOAD) { |
3781 | while ($RELOAD) { |
3750 | warn "reloading..."; |
3782 | warn "reloading..."; |
3751 | |
3783 | |
3752 | warn "entering sync_job"; |
3784 | warn "entering sync_job"; |
… | |
… | |
3855 | |
3887 | |
3856 | warn "reloaded"; |
3888 | warn "reloaded"; |
3857 | --$RELOAD; |
3889 | --$RELOAD; |
3858 | } |
3890 | } |
3859 | |
3891 | |
3860 | $t1 = EV::time - $t1; |
3892 | $t1 = AE::time - $t1; |
3861 | warn "reload completed in ${t1}s\n"; |
3893 | warn "reload completed in ${t1}s\n"; |
3862 | }; |
3894 | }; |
3863 | |
3895 | |
3864 | our $RELOAD_WATCHER; # used only during reload |
3896 | our $RELOAD_WATCHER; # used only during reload |
3865 | |
3897 | |
… | |
… | |
3868 | # coro crashes during coro_state_free->destroy here. |
3900 | # coro crashes during coro_state_free->destroy here. |
3869 | |
3901 | |
3870 | $RELOAD_WATCHER ||= cf::async { |
3902 | $RELOAD_WATCHER ||= cf::async { |
3871 | Coro::AIO::aio_wait cache_extensions; |
3903 | Coro::AIO::aio_wait cache_extensions; |
3872 | |
3904 | |
3873 | $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { |
3905 | $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub { |
3874 | do_reload_perl; |
3906 | do_reload_perl; |
3875 | undef $RELOAD_WATCHER; |
3907 | undef $RELOAD_WATCHER; |
3876 | }; |
3908 | }; |
3877 | }; |
3909 | }; |
3878 | } |
3910 | } |
… | |
… | |
3919 | return; |
3951 | return; |
3920 | } |
3952 | } |
3921 | |
3953 | |
3922 | cf::server_tick; # one server iteration |
3954 | cf::server_tick; # one server iteration |
3923 | |
3955 | |
3924 | #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d# |
3956 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
3925 | |
3957 | |
3926 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3958 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3927 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3959 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3928 | Coro::async_pool { |
3960 | Coro::async_pool { |
3929 | $Coro::current->{desc} = "runtime saver"; |
3961 | $Coro::current->{desc} = "runtime saver"; |