… | |
… | |
503 | |
503 | |
504 | sub sync_job(&) { |
504 | sub sync_job(&) { |
505 | my ($job) = @_; |
505 | my ($job) = @_; |
506 | |
506 | |
507 | if ($Coro::current == $Coro::main) { |
507 | if ($Coro::current == $Coro::main) { |
508 | my $time = EV::time; |
508 | my $time = AE::time; |
509 | |
509 | |
510 | # this is the main coro, too bad, we have to block |
510 | # this is the main coro, too bad, we have to block |
511 | # till the operation succeeds, freezing the server :/ |
511 | # till the operation succeeds, freezing the server :/ |
512 | |
512 | |
513 | LOG llevError, Carp::longmess "sync job";#d# |
513 | LOG llevError, Carp::longmess "sync job";#d# |
… | |
… | |
530 | } else { |
530 | } else { |
531 | EV::loop EV::LOOP_ONESHOT; |
531 | EV::loop EV::LOOP_ONESHOT; |
532 | } |
532 | } |
533 | } |
533 | } |
534 | |
534 | |
535 | my $time = EV::time - $time; |
535 | my $time = AE::time - $time; |
536 | |
536 | |
537 | $TICK_START += $time; # do not account sync jobs to server load |
537 | $TICK_START += $time; # do not account sync jobs to server load |
538 | |
538 | |
539 | wantarray ? @res : $res[0] |
539 | wantarray ? @res : $res[0] |
540 | } else { |
540 | } else { |
… | |
… | |
1310 | } |
1310 | } |
1311 | |
1311 | |
1312 | use File::Glob (); |
1312 | use File::Glob (); |
1313 | |
1313 | |
1314 | cf::player->attach ( |
1314 | cf::player->attach ( |
1315 | on_command => sub { |
1315 | on_unknown_command => sub { |
1316 | my ($pl, $name, $params) = @_; |
1316 | my ($pl, $name, $params) = @_; |
1317 | |
1317 | |
1318 | my $cb = $COMMAND{$name} |
1318 | my $cb = $COMMAND{$name} |
1319 | or return; |
1319 | or return; |
1320 | |
1320 | |
… | |
… | |
1533 | $cf::PLAYER{$login} = $pl |
1533 | $cf::PLAYER{$login} = $pl |
1534 | } |
1534 | } |
1535 | } |
1535 | } |
1536 | } |
1536 | } |
1537 | |
1537 | |
|
|
1538 | cf::player->attach ( |
|
|
1539 | on_load => sub { |
|
|
1540 | my ($pl, $path) = @_; |
|
|
1541 | |
|
|
1542 | # restore slots saved in save, below |
|
|
1543 | my $slots = delete $pl->{_slots}; |
|
|
1544 | |
|
|
1545 | $pl->ob->current_weapon ($slots->[0]); |
|
|
1546 | $pl->combat_ob ($slots->[1]); |
|
|
1547 | $pl->ranged_ob ($slots->[2]); |
|
|
1548 | }, |
|
|
1549 | ); |
|
|
1550 | |
1538 | sub save($) { |
1551 | sub save($) { |
1539 | my ($pl) = @_; |
1552 | my ($pl) = @_; |
1540 | |
1553 | |
1541 | return if $pl->{deny_save}; |
1554 | return if $pl->{deny_save}; |
1542 | |
1555 | |
… | |
… | |
1547 | |
1560 | |
1548 | aio_mkdir playerdir $pl, 0770; |
1561 | aio_mkdir playerdir $pl, 0770; |
1549 | $pl->{last_save} = $cf::RUNTIME; |
1562 | $pl->{last_save} = $cf::RUNTIME; |
1550 | |
1563 | |
1551 | cf::get_slot 0.01; |
1564 | cf::get_slot 0.01; |
|
|
1565 | |
|
|
1566 | # save slots, to be restored later |
|
|
1567 | local $pl->{_slots} = [$pl->ob->current_weapon, $pl->combat_ob, $pl->ranged_ob]; |
1552 | |
1568 | |
1553 | $pl->save_pl ($path); |
1569 | $pl->save_pl ($path); |
1554 | cf::cede_to_tick; |
1570 | cf::cede_to_tick; |
1555 | } |
1571 | } |
1556 | |
1572 | |
… | |
… | |
3134 | |
3150 | |
3135 | $coro |
3151 | $coro |
3136 | } |
3152 | } |
3137 | |
3153 | |
3138 | cf::client->attach ( |
3154 | cf::client->attach ( |
3139 | on_destroy => sub { |
3155 | on_client_destroy => sub { |
3140 | my ($ns) = @_; |
3156 | my ($ns) = @_; |
3141 | |
3157 | |
3142 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3158 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3143 | }, |
3159 | }, |
3144 | ); |
3160 | ); |
… | |
… | |
3541 | reload_config; |
3557 | reload_config; |
3542 | db_init; |
3558 | db_init; |
3543 | |
3559 | |
3544 | cf::init_uuid; |
3560 | cf::init_uuid; |
3545 | cf::init_signals; |
3561 | cf::init_signals; |
3546 | cf::init_commands; |
|
|
3547 | cf::init_skills; |
3562 | cf::init_skills; |
3548 | |
3563 | |
3549 | cf::init_beforeplay; |
3564 | cf::init_beforeplay; |
3550 | |
3565 | |
3551 | atomic; |
3566 | atomic; |
… | |
… | |
3569 | |
3584 | |
3570 | # install some emergency cleanup handlers |
3585 | # install some emergency cleanup handlers |
3571 | BEGIN { |
3586 | BEGIN { |
3572 | our %SIGWATCHER = (); |
3587 | our %SIGWATCHER = (); |
3573 | for my $signal (qw(INT HUP TERM)) { |
3588 | for my $signal (qw(INT HUP TERM)) { |
3574 | $SIGWATCHER{$signal} = EV::signal $signal, sub { |
3589 | $SIGWATCHER{$signal} = AE::signal $signal, sub { |
3575 | cf::cleanup "SIG$signal"; |
3590 | cf::cleanup "SIG$signal"; |
3576 | }; |
3591 | }; |
3577 | } |
3592 | } |
3578 | } |
3593 | } |
3579 | |
3594 | |
3580 | sub write_runtime_sync { |
3595 | sub write_runtime_sync { |
3581 | my $t0 = EV::time; |
3596 | my $t0 = AE::time; |
3582 | |
3597 | |
3583 | # first touch the runtime file to show we are still running: |
3598 | # first touch the runtime file to show we are still running: |
3584 | # the fsync below can take a very very long time. |
3599 | # the fsync below can take a very very long time. |
3585 | |
3600 | |
3586 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3601 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
… | |
… | |
3608 | or return; |
3623 | or return; |
3609 | |
3624 | |
3610 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3625 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3611 | and return; |
3626 | and return; |
3612 | |
3627 | |
3613 | warn sprintf "runtime file written (%gs).\n", EV::time - $t0; |
3628 | warn sprintf "runtime file written (%gs).\n", AE::time - $t0; |
3614 | |
3629 | |
3615 | 1 |
3630 | 1 |
3616 | } |
3631 | } |
3617 | |
3632 | |
3618 | our $uuid_lock; |
3633 | our $uuid_lock; |
… | |
… | |
3756 | return; |
3771 | return; |
3757 | } |
3772 | } |
3758 | |
3773 | |
3759 | return if $RELOAD++; |
3774 | return if $RELOAD++; |
3760 | |
3775 | |
3761 | my $t1 = EV::time; |
3776 | my $t1 = AE::time; |
3762 | |
3777 | |
3763 | while ($RELOAD) { |
3778 | while ($RELOAD) { |
3764 | warn "reloading..."; |
3779 | warn "reloading..."; |
3765 | |
3780 | |
3766 | warn "entering sync_job"; |
3781 | warn "entering sync_job"; |
… | |
… | |
3869 | |
3884 | |
3870 | warn "reloaded"; |
3885 | warn "reloaded"; |
3871 | --$RELOAD; |
3886 | --$RELOAD; |
3872 | } |
3887 | } |
3873 | |
3888 | |
3874 | $t1 = EV::time - $t1; |
3889 | $t1 = AE::time - $t1; |
3875 | warn "reload completed in ${t1}s\n"; |
3890 | warn "reload completed in ${t1}s\n"; |
3876 | }; |
3891 | }; |
3877 | |
3892 | |
3878 | our $RELOAD_WATCHER; # used only during reload |
3893 | our $RELOAD_WATCHER; # used only during reload |
3879 | |
3894 | |
… | |
… | |
3882 | # coro crashes during coro_state_free->destroy here. |
3897 | # coro crashes during coro_state_free->destroy here. |
3883 | |
3898 | |
3884 | $RELOAD_WATCHER ||= cf::async { |
3899 | $RELOAD_WATCHER ||= cf::async { |
3885 | Coro::AIO::aio_wait cache_extensions; |
3900 | Coro::AIO::aio_wait cache_extensions; |
3886 | |
3901 | |
3887 | $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { |
3902 | $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub { |
3888 | do_reload_perl; |
3903 | do_reload_perl; |
3889 | undef $RELOAD_WATCHER; |
3904 | undef $RELOAD_WATCHER; |
3890 | }; |
3905 | }; |
3891 | }; |
3906 | }; |
3892 | } |
3907 | } |
… | |
… | |
3933 | return; |
3948 | return; |
3934 | } |
3949 | } |
3935 | |
3950 | |
3936 | cf::server_tick; # one server iteration |
3951 | cf::server_tick; # one server iteration |
3937 | |
3952 | |
3938 | #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d# |
3953 | #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d# |
3939 | |
3954 | |
3940 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3955 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3941 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3956 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3942 | Coro::async_pool { |
3957 | Coro::async_pool { |
3943 | $Coro::current->{desc} = "runtime saver"; |
3958 | $Coro::current->{desc} = "runtime saver"; |