… | |
… | |
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"; |
… | |
… | |
776 | } |
784 | } |
777 | } else { |
785 | } else { |
778 | aio_unlink $filename; |
786 | aio_unlink $filename; |
779 | aio_unlink "$filename.pst"; |
787 | aio_unlink "$filename.pst"; |
780 | } |
788 | } |
|
|
789 | |
|
|
790 | #d##TODO# nuke non .map-files if exist |
|
|
791 | if ($filename =~ s/\.map$//) { |
|
|
792 | aio_unlink $filename; |
|
|
793 | aio_unlink "$filename.pst"; |
|
|
794 | } |
781 | } |
795 | } |
782 | } |
796 | } |
783 | |
797 | |
784 | sub object_freezer_as_string { |
798 | sub object_freezer_as_string { |
785 | my ($rdata, $objs) = @_; |
799 | my ($rdata, $objs) = @_; |
… | |
… | |
791 | |
805 | |
792 | sub object_thawer_load { |
806 | sub object_thawer_load { |
793 | my ($filename) = @_; |
807 | my ($filename) = @_; |
794 | |
808 | |
795 | my ($data, $av); |
809 | my ($data, $av); |
|
|
810 | |
|
|
811 | #d#TODO remove .map if file does not exist |
|
|
812 | aio_stat $filename and $filename =~ s/\.map$//; |
796 | |
813 | |
797 | (aio_load $filename, $data) >= 0 |
814 | (aio_load $filename, $data) >= 0 |
798 | or return; |
815 | or return; |
799 | |
816 | |
800 | unless (aio_stat "$filename.pst") { |
817 | unless (aio_stat "$filename.pst") { |
… | |
… | |
1070 | =cut |
1087 | =cut |
1071 | |
1088 | |
1072 | sub maps($) { |
1089 | sub maps($) { |
1073 | my ($pl) = @_; |
1090 | my ($pl) = @_; |
1074 | |
1091 | |
|
|
1092 | $pl = ref $pl ? $pl->ob->name : $pl; |
|
|
1093 | |
1075 | my $files = aio_readdir playerdir $pl |
1094 | my $files = aio_readdir playerdir $pl |
1076 | or return; |
1095 | or return; |
1077 | |
1096 | |
1078 | my @paths; |
1097 | my @paths; |
1079 | |
1098 | |
1080 | for (@$files) { |
1099 | for (@$files) { |
1081 | utf8::decode $_; |
1100 | utf8::decode $_; |
1082 | next if /\.(?:pl|pst)$/; |
1101 | next if /\.(?:pl|pst)$/; |
1083 | next unless /^$PATH_SEP/o; |
1102 | next unless /^$PATH_SEP/o; |
1084 | |
1103 | |
1085 | push @paths, cf::map::normalise "~" . $pl->ob->name . "/" . $_; |
1104 | push @paths, cf::map::normalise "~$pl/$_"; |
1086 | } |
1105 | } |
1087 | |
1106 | |
1088 | \@paths |
1107 | \@paths |
1089 | } |
1108 | } |
1090 | |
1109 | |
… | |
… | |
1251 | |
1270 | |
1252 | # the original (read-only) location |
1271 | # the original (read-only) location |
1253 | sub load_path { |
1272 | sub load_path { |
1254 | my ($self) = @_; |
1273 | my ($self) = @_; |
1255 | |
1274 | |
1256 | sprintf "%s/%s/%s", cf::datadir, cf::mapdir, $self->{path} |
1275 | sprintf "%s/%s/%s.map", cf::datadir, cf::mapdir, $self->{path} |
1257 | } |
1276 | } |
1258 | |
1277 | |
1259 | # the temporary/swap location |
1278 | # the temporary/swap location |
1260 | sub save_path { |
1279 | sub save_path { |
1261 | my ($self) = @_; |
1280 | my ($self) = @_; |
1262 | |
1281 | |
1263 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1282 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1264 | sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $path |
1283 | sprintf "%s/%s/%s.map", cf::localdir, cf::tmpdir, $path |
1265 | } |
1284 | } |
1266 | |
1285 | |
1267 | # the unique path, undef == no special unique path |
1286 | # the unique path, undef == no special unique path |
1268 | sub uniq_path { |
1287 | sub uniq_path { |
1269 | my ($self) = @_; |
1288 | my ($self) = @_; |
… | |
… | |
1285 | my ($self) = @_; |
1304 | my ($self) = @_; |
1286 | |
1305 | |
1287 | utf8::encode (my $save = $self->save_path); |
1306 | utf8::encode (my $save = $self->save_path); |
1288 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1307 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1289 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1308 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
|
|
1309 | |
|
|
1310 | #d#TODO remove .map and also nuke |
|
|
1311 | $save =~ s/\.map// or return;#d# |
|
|
1312 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save;#d# |
|
|
1313 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst";#d# |
1290 | } |
1314 | } |
1291 | |
1315 | |
1292 | sub load_header_from($) { |
1316 | sub load_header_from($) { |
1293 | my ($self, $path) = @_; |
1317 | my ($self, $path) = @_; |
1294 | |
1318 | |
1295 | utf8::encode $path; |
1319 | utf8::encode $path; |
1296 | aio_open $path, O_RDONLY, 0 |
1320 | #aio_open $path, O_RDONLY, 0 |
1297 | or return; |
1321 | # or return; |
1298 | |
1322 | |
1299 | $self->_load_header ($path) |
1323 | $self->_load_header ($path) |
1300 | or return; |
1324 | or return; |
1301 | |
1325 | |
1302 | $self->{load_path} = $path; |
1326 | $self->{load_path} = $path; |
… | |
… | |
1987 | # get handled first |
2011 | # get handled first |
1988 | my $queue = delete $ns->{query_queue} |
2012 | my $queue = delete $ns->{query_queue} |
1989 | 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 |
1990 | |
2014 | |
1991 | (shift @$queue)->[1]->($msg); |
2015 | (shift @$queue)->[1]->($msg); |
|
|
2016 | return unless $ns->valid; # temporary(?) workaround for callback destroying socket |
1992 | |
2017 | |
1993 | push @{ $ns->{query_queue} }, @$queue; |
2018 | push @{ $ns->{query_queue} }, @$queue; |
1994 | |
2019 | |
1995 | if (@{ $ns->{query_queue} } == @$queue) { |
2020 | if (@{ $ns->{query_queue} } == @$queue) { |
1996 | if (@$queue) { |
2021 | if (@$queue) { |
… | |
… | |
2253 | }, |
2278 | }, |
2254 | ); |
2279 | ); |
2255 | } |
2280 | } |
2256 | |
2281 | |
2257 | ############################################################################# |
2282 | ############################################################################# |
2258 | # 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 | } |
2259 | |
2289 | |
2260 | sub cfg_load { |
2290 | sub cfg_load { |
2261 | open my $fh, "<:utf8", cf::confdir . "/config" |
2291 | open my $fh, "<:utf8", cf::confdir . "/config" |
2262 | or return; |
2292 | or return; |
2263 | |
2293 | |
… | |
… | |
2274 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2304 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2275 | and die "WARNING: m(un)lockall failed: $!\n"; |
2305 | and die "WARNING: m(un)lockall failed: $!\n"; |
2276 | }; |
2306 | }; |
2277 | warn $@ if $@; |
2307 | warn $@ if $@; |
2278 | } |
2308 | } |
|
|
2309 | } |
|
|
2310 | |
|
|
2311 | sub init { |
|
|
2312 | load_resources; |
2279 | } |
2313 | } |
2280 | |
2314 | |
2281 | sub main { |
2315 | sub main { |
2282 | # we must not ever block the main coroutine |
2316 | # we must not ever block the main coroutine |
2283 | local $Coro::idle = sub { |
2317 | local $Coro::idle = sub { |
… | |
… | |
2444 | warn "reattaching attachments to objects/players"; |
2478 | warn "reattaching attachments to objects/players"; |
2445 | _global_reattach; |
2479 | _global_reattach; |
2446 | warn "reattaching attachments to maps"; |
2480 | warn "reattaching attachments to maps"; |
2447 | reattach $_ for values %MAP; |
2481 | reattach $_ for values %MAP; |
2448 | |
2482 | |
|
|
2483 | warn "loading reloadable resources"; |
|
|
2484 | load_resources; |
|
|
2485 | |
2449 | warn "restarting server ticker"; |
2486 | warn "restarting server ticker"; |
2450 | |
2487 | |
2451 | $TICK_WATCHER->start; |
2488 | $TICK_WATCHER->start; |
2452 | }; |
2489 | }; |
2453 | |
2490 | |
… | |
… | |
2521 | $TICK_WATCHER->at ($NEXT_TICK); |
2558 | $TICK_WATCHER->at ($NEXT_TICK); |
2522 | $TICK_WATCHER->start; |
2559 | $TICK_WATCHER->start; |
2523 | }, |
2560 | }, |
2524 | ); |
2561 | ); |
2525 | |
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 | }; |
|
|
2605 | } |
|
|
2606 | } |
|
|
2607 | |
|
|
2608 | { |
|
|
2609 | IO::AIO::min_parallel 8; |
|
|
2610 | |
|
|
2611 | undef $Coro::AIO::WATCHER; |
2526 | IO::AIO::max_poll_time $TICK * 0.1; |
2612 | IO::AIO::max_poll_time $TICK * 0.1; |
2527 | |
|
|
2528 | undef $Coro::AIO::WATCHER; |
|
|
2529 | $AIO_POLL_WATCHER = Event->io ( |
2613 | $AIO_POLL_WATCHER = Event->io ( |
2530 | reentrant => 0, |
2614 | reentrant => 0, |
2531 | fd => IO::AIO::poll_fileno, |
2615 | fd => IO::AIO::poll_fileno, |
2532 | poll => 'r', |
2616 | poll => 'r', |
2533 | prio => 6, |
2617 | prio => 6, |
2534 | data => WF_AUTOCANCEL, |
2618 | data => WF_AUTOCANCEL, |
2535 | cb => \&IO::AIO::poll_cb, |
2619 | cb => \&IO::AIO::poll_cb, |
2536 | ); |
2620 | ); |
|
|
2621 | } |
2537 | |
2622 | |
2538 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2623 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2539 | reentrant => 0, |
2624 | reentrant => 0, |
2540 | data => WF_AUTOCANCEL, |
2625 | data => WF_AUTOCANCEL, |
2541 | after => 1, |
2626 | after => 1, |