… | |
… | |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Symbol; |
6 | use Symbol; |
7 | use List::Util; |
7 | use List::Util; |
8 | use Socket; |
8 | use Socket; |
9 | use EV; |
9 | use EV 1.86; |
10 | use Opcode; |
10 | use Opcode; |
11 | use Safe; |
11 | use Safe; |
12 | use Safe::Hole; |
12 | use Safe::Hole; |
13 | use Storable (); |
13 | use Storable (); |
14 | |
14 | |
15 | use Coro 4.1 (); |
15 | use Coro 4.32 (); |
16 | use Coro::State; |
16 | use Coro::State; |
17 | use Coro::Handle; |
17 | use Coro::Handle; |
18 | use Coro::EV; |
18 | use Coro::EV; |
19 | use Coro::Timer; |
19 | use Coro::Timer; |
20 | use Coro::Signal; |
20 | use Coro::Signal; |
21 | use Coro::Semaphore; |
21 | use Coro::Semaphore; |
22 | use Coro::AIO; |
22 | use Coro::AIO; |
|
|
23 | use Coro::BDB; |
23 | use Coro::Storable; |
24 | use Coro::Storable; |
24 | use Coro::Util (); |
25 | use Coro::Util (); |
25 | |
26 | |
26 | use JSON::XS (); |
27 | use JSON::XS 2.01 (); |
27 | use BDB (); |
28 | use BDB (); |
28 | use Data::Dumper; |
29 | use Data::Dumper; |
29 | use Digest::MD5; |
30 | use Digest::MD5; |
30 | use Fcntl; |
31 | use Fcntl; |
31 | use YAML::Syck (); |
32 | use YAML::Syck (); |
… | |
… | |
74 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
75 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
75 | our $TICK_WATCHER; |
76 | our $TICK_WATCHER; |
76 | our $AIO_POLL_WATCHER; |
77 | our $AIO_POLL_WATCHER; |
77 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
78 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
78 | our $NEXT_TICK; |
79 | our $NEXT_TICK; |
79 | our $NOW; |
|
|
80 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
80 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
81 | |
81 | |
82 | our $BDB_POLL_WATCHER; |
82 | our $BDB_POLL_WATCHER; |
83 | our $BDB_DEADLOCK_WATCHER; |
83 | our $BDB_DEADLOCK_WATCHER; |
84 | our $BDB_CHECKPOINT_WATCHER; |
84 | our $BDB_CHECKPOINT_WATCHER; |
… | |
… | |
87 | |
87 | |
88 | our %CFG; |
88 | our %CFG; |
89 | |
89 | |
90 | our $UPTIME; $UPTIME ||= time; |
90 | our $UPTIME; $UPTIME ||= time; |
91 | our $RUNTIME; |
91 | our $RUNTIME; |
|
|
92 | our $NOW; |
92 | |
93 | |
93 | our (%PLAYER, %PLAYER_LOADING); # all users |
94 | our (%PLAYER, %PLAYER_LOADING); # all users |
94 | our (%MAP, %MAP_LOADING ); # all maps |
95 | our (%MAP, %MAP_LOADING ); # all maps |
95 | our $LINK_MAP; # the special {link} map, which is always available |
96 | our $LINK_MAP; # the special {link} map, which is always available |
96 | |
97 | |
… | |
… | |
246 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
247 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
247 | $d |
248 | $d |
248 | } || "[unable to dump $_[0]: '$@']"; |
249 | } || "[unable to dump $_[0]: '$@']"; |
249 | } |
250 | } |
250 | |
251 | |
251 | =item $ref = cf::from_json $json |
252 | =item $ref = cf::decode_json $json |
252 | |
253 | |
253 | Converts a JSON string into the corresponding perl data structure. |
254 | Converts a JSON string into the corresponding perl data structure. |
254 | |
255 | |
255 | =item $json = cf::to_json $ref |
256 | =item $json = cf::encode_json $ref |
256 | |
257 | |
257 | Converts a perl data structure into its JSON representation. |
258 | Converts a perl data structure into its JSON representation. |
258 | |
259 | |
259 | =cut |
260 | =cut |
260 | |
261 | |
261 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
262 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
262 | |
263 | |
263 | sub to_json ($) { $json_coder->encode ($_[0]) } |
264 | sub encode_json($) { $json_coder->encode ($_[0]) } |
264 | sub from_json ($) { $json_coder->decode ($_[0]) } |
265 | sub decode_json($) { $json_coder->decode ($_[0]) } |
265 | |
266 | |
266 | =item cf::lock_wait $string |
267 | =item cf::lock_wait $string |
267 | |
268 | |
268 | Wait until the given lock is available. See cf::lock_acquire. |
269 | Wait until the given lock is available. See cf::lock_acquire. |
269 | |
270 | |
… | |
… | |
1055 | cf::attachable->attach ( |
1056 | cf::attachable->attach ( |
1056 | prio => -1000000, |
1057 | prio => -1000000, |
1057 | on_instantiate => sub { |
1058 | on_instantiate => sub { |
1058 | my ($obj, $data) = @_; |
1059 | my ($obj, $data) = @_; |
1059 | |
1060 | |
1060 | $data = from_json $data; |
1061 | $data = decode_json $data; |
1061 | |
1062 | |
1062 | for (@$data) { |
1063 | for (@$data) { |
1063 | my ($name, $args) = @$_; |
1064 | my ($name, $args) = @$_; |
1064 | |
1065 | |
1065 | $obj->attach ($name, %{$args || {} }); |
1066 | $obj->attach ($name, %{$args || {} }); |
… | |
… | |
2638 | $rmp->{origin_y} = $exit->y; |
2639 | $rmp->{origin_y} = $exit->y; |
2639 | } |
2640 | } |
2640 | |
2641 | |
2641 | $rmp->{random_seed} ||= $exit->random_seed; |
2642 | $rmp->{random_seed} ||= $exit->random_seed; |
2642 | |
2643 | |
2643 | my $data = cf::to_json $rmp; |
2644 | my $data = cf::encode_json $rmp; |
2644 | my $md5 = Digest::MD5::md5_hex $data; |
2645 | my $md5 = Digest::MD5::md5_hex $data; |
2645 | my $meta = "$RANDOMDIR/$md5.meta"; |
2646 | my $meta = "$RANDOMDIR/$md5.meta"; |
2646 | |
2647 | |
2647 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
2648 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
2648 | aio_write $fh, 0, (length $data), $data, 0; |
2649 | aio_write $fh, 0, (length $data), $data, 0; |
… | |
… | |
3602 | |
3603 | |
3603 | $NOW = $tick_start = EV::now; |
3604 | $NOW = $tick_start = EV::now; |
3604 | |
3605 | |
3605 | cf::server_tick; # one server iteration |
3606 | cf::server_tick; # one server iteration |
3606 | |
3607 | |
3607 | $RUNTIME += $TICK; |
3608 | $RUNTIME += $TICK; |
3608 | $NEXT_TICK += $TICK; |
3609 | $NEXT_TICK = $_[0]->at; |
3609 | |
3610 | |
3610 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3611 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3611 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
3612 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3612 | Coro::async_pool { |
3613 | Coro::async_pool { |
3613 | $Coro::current->{desc} = "runtime saver"; |
3614 | $Coro::current->{desc} = "runtime saver"; |
3614 | write_runtime |
3615 | write_runtime |
3615 | or warn "ERROR: unable to write runtime file: $!"; |
3616 | or warn "ERROR: unable to write runtime file: $!"; |
3616 | }; |
3617 | }; |
… | |
… | |
3629 | _post_tick; |
3630 | _post_tick; |
3630 | }; |
3631 | }; |
3631 | $TICK_WATCHER->priority (EV::MAXPRI); |
3632 | $TICK_WATCHER->priority (EV::MAXPRI); |
3632 | |
3633 | |
3633 | { |
3634 | { |
|
|
3635 | # configure BDB |
|
|
3636 | |
3634 | BDB::min_parallel 8; |
3637 | BDB::min_parallel 8; |
3635 | BDB::max_poll_time $TICK * 0.1; |
3638 | BDB::max_poll_reqs $TICK * 0.1; |
3636 | $BDB_POLL_WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
3639 | $Coro::BDB::WATCHER->priority (1); |
3637 | |
|
|
3638 | BDB::set_sync_prepare { |
|
|
3639 | my $status; |
|
|
3640 | my $current = $Coro::current; |
|
|
3641 | ( |
|
|
3642 | sub { |
|
|
3643 | $status = $!; |
|
|
3644 | $current->ready; undef $current; |
|
|
3645 | }, |
|
|
3646 | sub { |
|
|
3647 | Coro::schedule while defined $current; |
|
|
3648 | $! = $status; |
|
|
3649 | }, |
|
|
3650 | ) |
|
|
3651 | }; |
|
|
3652 | |
3640 | |
3653 | unless ($DB_ENV) { |
3641 | unless ($DB_ENV) { |
3654 | $DB_ENV = BDB::db_env_create; |
3642 | $DB_ENV = BDB::db_env_create; |
3655 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
3643 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
3656 | | BDB::LOG_AUTOREMOVE, 1); |
3644 | | BDB::LOG_AUTOREMOVE, 1); |
… | |
… | |
3683 | BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; |
3671 | BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; |
3684 | }; |
3672 | }; |
3685 | } |
3673 | } |
3686 | |
3674 | |
3687 | { |
3675 | { |
|
|
3676 | # configure IO::AIO |
|
|
3677 | |
3688 | IO::AIO::min_parallel 8; |
3678 | IO::AIO::min_parallel 8; |
3689 | |
|
|
3690 | undef $Coro::AIO::WATCHER; |
|
|
3691 | IO::AIO::max_poll_time $TICK * 0.1; |
3679 | IO::AIO::max_poll_time $TICK * 0.1; |
3692 | $AIO_POLL_WATCHER = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb; |
3680 | $Coro::AIO::WATCHER->priority (1); |
3693 | } |
3681 | } |
3694 | |
3682 | |
3695 | my $_log_backtrace; |
3683 | my $_log_backtrace; |
3696 | |
3684 | |
3697 | sub _log_backtrace { |
3685 | sub _log_backtrace { |