… | |
… | |
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 | |
… | |
… | |
1533 | $cf::PLAYER{$login} = $pl |
1536 | $cf::PLAYER{$login} = $pl |
1534 | } |
1537 | } |
1535 | } |
1538 | } |
1536 | } |
1539 | } |
1537 | |
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 | |
1538 | sub save($) { |
1554 | sub save($) { |
1539 | my ($pl) = @_; |
1555 | my ($pl) = @_; |
1540 | |
1556 | |
1541 | return if $pl->{deny_save}; |
1557 | return if $pl->{deny_save}; |
1542 | |
1558 | |
… | |
… | |
1547 | |
1563 | |
1548 | aio_mkdir playerdir $pl, 0770; |
1564 | aio_mkdir playerdir $pl, 0770; |
1549 | $pl->{last_save} = $cf::RUNTIME; |
1565 | $pl->{last_save} = $cf::RUNTIME; |
1550 | |
1566 | |
1551 | 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]; |
1552 | |
1571 | |
1553 | $pl->save_pl ($path); |
1572 | $pl->save_pl ($path); |
1554 | cf::cede_to_tick; |
1573 | cf::cede_to_tick; |
1555 | } |
1574 | } |
1556 | |
1575 | |
… | |
… | |
3134 | |
3153 | |
3135 | $coro |
3154 | $coro |
3136 | } |
3155 | } |
3137 | |
3156 | |
3138 | cf::client->attach ( |
3157 | cf::client->attach ( |
3139 | on_destroy => sub { |
3158 | on_client_destroy => sub { |
3140 | my ($ns) = @_; |
3159 | my ($ns) = @_; |
3141 | |
3160 | |
3142 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3161 | $_->cancel for values %{ (delete $ns->{_coro}) || {} }; |
3143 | }, |
3162 | }, |
3144 | ); |
3163 | ); |
… | |
… | |
3541 | reload_config; |
3560 | reload_config; |
3542 | db_init; |
3561 | db_init; |
3543 | |
3562 | |
3544 | cf::init_uuid; |
3563 | cf::init_uuid; |
3545 | cf::init_signals; |
3564 | cf::init_signals; |
3546 | cf::init_commands; |
|
|
3547 | cf::init_skills; |
3565 | cf::init_skills; |
3548 | |
3566 | |
3549 | cf::init_beforeplay; |
3567 | cf::init_beforeplay; |
3550 | |
3568 | |
3551 | atomic; |
3569 | atomic; |
… | |
… | |
3569 | |
3587 | |
3570 | # install some emergency cleanup handlers |
3588 | # install some emergency cleanup handlers |
3571 | BEGIN { |
3589 | BEGIN { |
3572 | our %SIGWATCHER = (); |
3590 | our %SIGWATCHER = (); |
3573 | for my $signal (qw(INT HUP TERM)) { |
3591 | for my $signal (qw(INT HUP TERM)) { |
3574 | $SIGWATCHER{$signal} = EV::signal $signal, sub { |
3592 | $SIGWATCHER{$signal} = AE::signal $signal, sub { |
3575 | cf::cleanup "SIG$signal"; |
3593 | cf::cleanup "SIG$signal"; |
3576 | }; |
3594 | }; |
3577 | } |
3595 | } |
3578 | } |
3596 | } |
3579 | |
3597 | |
3580 | sub write_runtime_sync { |
3598 | sub write_runtime_sync { |
3581 | my $t0 = EV::time; |
3599 | my $t0 = AE::time; |
3582 | |
3600 | |
3583 | # first touch the runtime file to show we are still running: |
3601 | # first touch the runtime file to show we are still running: |
3584 | # the fsync below can take a very very long time. |
3602 | # the fsync below can take a very very long time. |
3585 | |
3603 | |
3586 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3604 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
… | |
… | |
3608 | or return; |
3626 | or return; |
3609 | |
3627 | |
3610 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3628 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3611 | and return; |
3629 | and return; |
3612 | |
3630 | |
3613 | warn sprintf "runtime file written (%gs).\n", EV::time - $t0; |
3631 | warn sprintf "runtime file written (%gs).\n", AE::time - $t0; |
3614 | |
3632 | |
3615 | 1 |
3633 | 1 |
3616 | } |
3634 | } |
3617 | |
3635 | |
3618 | our $uuid_lock; |
3636 | our $uuid_lock; |
… | |
… | |
3756 | return; |
3774 | return; |
3757 | } |
3775 | } |
3758 | |
3776 | |
3759 | return if $RELOAD++; |
3777 | return if $RELOAD++; |
3760 | |
3778 | |
3761 | my $t1 = EV::time; |
3779 | my $t1 = AE::time; |
3762 | |
3780 | |
3763 | while ($RELOAD) { |
3781 | while ($RELOAD) { |
3764 | warn "reloading..."; |
3782 | warn "reloading..."; |
3765 | |
3783 | |
3766 | warn "entering sync_job"; |
3784 | warn "entering sync_job"; |
… | |
… | |
3869 | |
3887 | |
3870 | warn "reloaded"; |
3888 | warn "reloaded"; |
3871 | --$RELOAD; |
3889 | --$RELOAD; |
3872 | } |
3890 | } |
3873 | |
3891 | |
3874 | $t1 = EV::time - $t1; |
3892 | $t1 = AE::time - $t1; |
3875 | warn "reload completed in ${t1}s\n"; |
3893 | warn "reload completed in ${t1}s\n"; |
3876 | }; |
3894 | }; |
3877 | |
3895 | |
3878 | our $RELOAD_WATCHER; # used only during reload |
3896 | our $RELOAD_WATCHER; # used only during reload |
3879 | |
3897 | |
… | |
… | |
3882 | # coro crashes during coro_state_free->destroy here. |
3900 | # coro crashes during coro_state_free->destroy here. |
3883 | |
3901 | |
3884 | $RELOAD_WATCHER ||= cf::async { |
3902 | $RELOAD_WATCHER ||= cf::async { |
3885 | Coro::AIO::aio_wait cache_extensions; |
3903 | Coro::AIO::aio_wait cache_extensions; |
3886 | |
3904 | |
3887 | $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { |
3905 | $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub { |
3888 | do_reload_perl; |
3906 | do_reload_perl; |
3889 | undef $RELOAD_WATCHER; |
3907 | undef $RELOAD_WATCHER; |
3890 | }; |
3908 | }; |
3891 | }; |
3909 | }; |
3892 | } |
3910 | } |
… | |
… | |
3933 | return; |
3951 | return; |
3934 | } |
3952 | } |
3935 | |
3953 | |
3936 | cf::server_tick; # one server iteration |
3954 | cf::server_tick; # one server iteration |
3937 | |
3955 | |
3938 | #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# |
3939 | |
3957 | |
3940 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3958 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3941 | $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.; |
3942 | Coro::async_pool { |
3960 | Coro::async_pool { |
3943 | $Coro::current->{desc} = "runtime saver"; |
3961 | $Coro::current->{desc} = "runtime saver"; |