… | |
… | |
15 | use Coro::Timer; |
15 | use Coro::Timer; |
16 | use Coro::Signal; |
16 | use Coro::Signal; |
17 | use Coro::Semaphore; |
17 | use Coro::Semaphore; |
18 | use Coro::AIO; |
18 | use Coro::AIO; |
19 | |
19 | |
|
|
20 | use BDB (); |
20 | use Data::Dumper; |
21 | use Data::Dumper; |
21 | use Digest::MD5; |
22 | use Digest::MD5; |
22 | use Fcntl; |
23 | use Fcntl; |
23 | use IO::AIO 2.32 (); |
24 | use IO::AIO 2.32 (); |
24 | use YAML::Syck (); |
25 | use YAML::Syck (); |
… | |
… | |
48 | our $TICK_WATCHER; |
49 | our $TICK_WATCHER; |
49 | our $AIO_POLL_WATCHER; |
50 | our $AIO_POLL_WATCHER; |
50 | our $WRITE_RUNTIME_WATCHER; |
51 | our $WRITE_RUNTIME_WATCHER; |
51 | our $NEXT_TICK; |
52 | our $NEXT_TICK; |
52 | our $NOW; |
53 | our $NOW; |
|
|
54 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
|
|
55 | |
|
|
56 | our $BDB_POLL_WATCHER; |
|
|
57 | our $DB_ENV; |
53 | |
58 | |
54 | our %CFG; |
59 | our %CFG; |
55 | |
60 | |
56 | our $UPTIME; $UPTIME ||= time; |
61 | our $UPTIME; $UPTIME ||= time; |
57 | our $RUNTIME; |
62 | our $RUNTIME; |
58 | |
63 | |
59 | our %PLAYER; # all users |
64 | our %PLAYER; # all users |
60 | our %MAP; # all maps |
65 | our %MAP; # all maps |
61 | our $LINK_MAP; # the special {link} map, which is always available |
66 | our $LINK_MAP; # the special {link} map, which is always available |
62 | our $RANDOM_MAPS = cf::localdir . "/random"; |
67 | our $RANDOM_MAPS = cf::localdir . "/random"; |
|
|
68 | our $BDB_ENV_DIR = cf::localdir . "/db"; |
63 | |
69 | |
64 | our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal; |
70 | our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal; |
65 | our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal; |
71 | our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal; |
66 | |
72 | |
67 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
73 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
… | |
… | |
80 | mkdir cf::localdir; |
86 | mkdir cf::localdir; |
81 | mkdir cf::localdir . "/" . cf::playerdir; |
87 | mkdir cf::localdir . "/" . cf::playerdir; |
82 | mkdir cf::localdir . "/" . cf::tmpdir; |
88 | mkdir cf::localdir . "/" . cf::tmpdir; |
83 | mkdir cf::localdir . "/" . cf::uniquedir; |
89 | mkdir cf::localdir . "/" . cf::uniquedir; |
84 | mkdir $RANDOM_MAPS; |
90 | mkdir $RANDOM_MAPS; |
|
|
91 | mkdir $BDB_ENV_DIR; |
85 | |
92 | |
86 | our $EMERGENCY_POSITION; |
93 | our $EMERGENCY_POSITION; |
87 | |
94 | |
88 | sub cf::map::normalise; |
95 | sub cf::map::normalise; |
89 | |
96 | |
… | |
… | |
358 | # 60 allows for the watchdog to kill the server. |
365 | # 60 allows for the watchdog to kill the server. |
359 | |
366 | |
360 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
367 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
361 | and return; |
368 | and return; |
362 | |
369 | |
|
|
370 | # always fsync - this file is important |
363 | aio_fsync $fh |
371 | aio_fsync $fh |
364 | and return; |
372 | and return; |
365 | |
373 | |
366 | close $fh |
374 | close $fh |
367 | or return; |
375 | or return; |
… | |
… | |
752 | $filename, length $$rdata, scalar @$objs; |
760 | $filename, length $$rdata, scalar @$objs; |
753 | |
761 | |
754 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
762 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
755 | chmod SAVE_MODE, $fh; |
763 | chmod SAVE_MODE, $fh; |
756 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
764 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
757 | aio_fsync $fh; |
765 | aio_fsync $fh if $cf::USE_FSYNC; |
758 | close $fh; |
766 | close $fh; |
759 | |
767 | |
760 | if (@$objs) { |
768 | if (@$objs) { |
761 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
769 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
762 | chmod SAVE_MODE, $fh; |
770 | chmod SAVE_MODE, $fh; |
763 | my $data = Storable::nfreeze { version => 1, objs => $objs }; |
771 | my $data = Storable::nfreeze { version => 1, objs => $objs }; |
764 | aio_write $fh, 0, (length $data), $data, 0; |
772 | aio_write $fh, 0, (length $data), $data, 0; |
765 | aio_fsync $fh; |
773 | aio_fsync $fh if $cf::USE_FSYNC; |
766 | close $fh; |
774 | close $fh; |
767 | aio_rename "$filename.pst~", "$filename.pst"; |
775 | aio_rename "$filename.pst~", "$filename.pst"; |
768 | } |
776 | } |
769 | } else { |
777 | } else { |
770 | aio_unlink "$filename.pst"; |
778 | aio_unlink "$filename.pst"; |
… | |
… | |
1079 | =cut |
1087 | =cut |
1080 | |
1088 | |
1081 | sub maps($) { |
1089 | sub maps($) { |
1082 | my ($pl) = @_; |
1090 | my ($pl) = @_; |
1083 | |
1091 | |
|
|
1092 | $pl = ref $pl ? $pl->ob->name : $pl; |
|
|
1093 | |
1084 | my $files = aio_readdir playerdir $pl |
1094 | my $files = aio_readdir playerdir $pl |
1085 | or return; |
1095 | or return; |
1086 | |
1096 | |
1087 | my @paths; |
1097 | my @paths; |
1088 | |
1098 | |
1089 | for (@$files) { |
1099 | for (@$files) { |
1090 | utf8::decode $_; |
1100 | utf8::decode $_; |
1091 | next if /\.(?:pl|pst)$/; |
1101 | next if /\.(?:pl|pst)$/; |
1092 | next unless /^$PATH_SEP/o; |
1102 | next unless /^$PATH_SEP/o; |
1093 | |
1103 | |
1094 | push @paths, cf::map::normalise "~" . $pl->ob->name . "/" . $_; |
1104 | push @paths, cf::map::normalise "~$pl/$_"; |
1095 | } |
1105 | } |
1096 | |
1106 | |
1097 | \@paths |
1107 | \@paths |
1098 | } |
1108 | } |
1099 | |
1109 | |
… | |
… | |
2001 | # get handled first |
2011 | # get handled first |
2002 | my $queue = delete $ns->{query_queue} |
2012 | my $queue = delete $ns->{query_queue} |
2003 | or return; # be conservative, not sure how that can happen, but we saw a crash here |
2013 | or return; # be conservative, not sure how that can happen, but we saw a crash here |
2004 | |
2014 | |
2005 | (shift @$queue)->[1]->($msg); |
2015 | (shift @$queue)->[1]->($msg); |
|
|
2016 | return unless $ns->valid; # temporary(?) workaround for callback destroying socket |
2006 | |
2017 | |
2007 | push @{ $ns->{query_queue} }, @$queue; |
2018 | push @{ $ns->{query_queue} }, @$queue; |
2008 | |
2019 | |
2009 | if (@{ $ns->{query_queue} } == @$queue) { |
2020 | if (@{ $ns->{query_queue} } == @$queue) { |
2010 | if (@$queue) { |
2021 | if (@$queue) { |
… | |
… | |
2267 | }, |
2278 | }, |
2268 | ); |
2279 | ); |
2269 | } |
2280 | } |
2270 | |
2281 | |
2271 | ############################################################################# |
2282 | ############################################################################# |
2272 | # the server's main() |
2283 | # the server's init and main functions |
|
|
2284 | |
|
|
2285 | sub load_resources { |
|
|
2286 | load_regions sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
|
|
2287 | or die "unable to load regions file\n";#d# |
|
|
2288 | } |
2273 | |
2289 | |
2274 | sub cfg_load { |
2290 | sub cfg_load { |
2275 | open my $fh, "<:utf8", cf::confdir . "/config" |
2291 | open my $fh, "<:utf8", cf::confdir . "/config" |
2276 | or return; |
2292 | or return; |
2277 | |
2293 | |
… | |
… | |
2288 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2304 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2289 | and die "WARNING: m(un)lockall failed: $!\n"; |
2305 | and die "WARNING: m(un)lockall failed: $!\n"; |
2290 | }; |
2306 | }; |
2291 | warn $@ if $@; |
2307 | warn $@ if $@; |
2292 | } |
2308 | } |
|
|
2309 | } |
|
|
2310 | |
|
|
2311 | sub init { |
|
|
2312 | load_resources; |
2293 | } |
2313 | } |
2294 | |
2314 | |
2295 | sub main { |
2315 | sub main { |
2296 | # we must not ever block the main coroutine |
2316 | # we must not ever block the main coroutine |
2297 | local $Coro::idle = sub { |
2317 | local $Coro::idle = sub { |
… | |
… | |
2458 | warn "reattaching attachments to objects/players"; |
2478 | warn "reattaching attachments to objects/players"; |
2459 | _global_reattach; |
2479 | _global_reattach; |
2460 | warn "reattaching attachments to maps"; |
2480 | warn "reattaching attachments to maps"; |
2461 | reattach $_ for values %MAP; |
2481 | reattach $_ for values %MAP; |
2462 | |
2482 | |
|
|
2483 | warn "loading reloadable resources"; |
|
|
2484 | load_resources; |
|
|
2485 | |
2463 | warn "restarting server ticker"; |
2486 | warn "restarting server ticker"; |
2464 | |
2487 | |
2465 | $TICK_WATCHER->start; |
2488 | $TICK_WATCHER->start; |
2466 | }; |
2489 | }; |
2467 | |
2490 | |
… | |
… | |
2535 | $TICK_WATCHER->at ($NEXT_TICK); |
2558 | $TICK_WATCHER->at ($NEXT_TICK); |
2536 | $TICK_WATCHER->start; |
2559 | $TICK_WATCHER->start; |
2537 | }, |
2560 | }, |
2538 | ); |
2561 | ); |
2539 | |
2562 | |
|
|
2563 | { |
|
|
2564 | BDB::max_poll_time $TICK * 0.1; |
|
|
2565 | $BDB_POLL_WATCHER = Event->io ( |
|
|
2566 | reentrant => 0, |
|
|
2567 | fd => BDB::poll_fileno, |
|
|
2568 | poll => 'r', |
|
|
2569 | prio => 0, |
|
|
2570 | data => WF_AUTOCANCEL, |
|
|
2571 | cb => \&BDB::poll_cb, |
|
|
2572 | ); |
|
|
2573 | BDB::min_parallel 8; |
|
|
2574 | |
|
|
2575 | BDB::set_sync_prepare { |
|
|
2576 | my $status; |
|
|
2577 | my $current = $Coro::current; |
|
|
2578 | ( |
|
|
2579 | sub { |
|
|
2580 | $status = $!; |
|
|
2581 | $current->ready; undef $current; |
|
|
2582 | }, |
|
|
2583 | sub { |
|
|
2584 | Coro::schedule while defined $current; |
|
|
2585 | $! = $status; |
|
|
2586 | }, |
|
|
2587 | ) |
|
|
2588 | }; |
|
|
2589 | |
|
|
2590 | unless ($DB_ENV) { |
|
|
2591 | $DB_ENV = BDB::db_env_create; |
|
|
2592 | |
|
|
2593 | cf::sync_job { |
|
|
2594 | BDB::db_env_open |
|
|
2595 | $DB_ENV, |
|
|
2596 | $BDB_ENV_DIR, |
|
|
2597 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
|
|
2598 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
|
|
2599 | 0666; |
|
|
2600 | |
|
|
2601 | cf::cleanup "$!" if $!; |
|
|
2602 | |
|
|
2603 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
|
|
2604 | $DB_ENV->set_lk_detect; |
|
|
2605 | }; |
|
|
2606 | } |
|
|
2607 | } |
|
|
2608 | |
|
|
2609 | { |
|
|
2610 | IO::AIO::min_parallel 8; |
|
|
2611 | |
|
|
2612 | undef $Coro::AIO::WATCHER; |
2540 | IO::AIO::max_poll_time $TICK * 0.1; |
2613 | IO::AIO::max_poll_time $TICK * 0.1; |
2541 | |
|
|
2542 | undef $Coro::AIO::WATCHER; |
|
|
2543 | $AIO_POLL_WATCHER = Event->io ( |
2614 | $AIO_POLL_WATCHER = Event->io ( |
2544 | reentrant => 0, |
2615 | reentrant => 0, |
2545 | fd => IO::AIO::poll_fileno, |
2616 | fd => IO::AIO::poll_fileno, |
2546 | poll => 'r', |
2617 | poll => 'r', |
2547 | prio => 6, |
2618 | prio => 6, |
2548 | data => WF_AUTOCANCEL, |
2619 | data => WF_AUTOCANCEL, |
2549 | cb => \&IO::AIO::poll_cb, |
2620 | cb => \&IO::AIO::poll_cb, |
2550 | ); |
2621 | ); |
|
|
2622 | } |
2551 | |
2623 | |
2552 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2624 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2553 | reentrant => 0, |
2625 | reentrant => 0, |
2554 | data => WF_AUTOCANCEL, |
2626 | data => WF_AUTOCANCEL, |
2555 | after => 1, |
2627 | after => 1, |