… | |
… | |
49 | our $LIBDIR = datadir . "/ext"; |
49 | our $LIBDIR = datadir . "/ext"; |
50 | |
50 | |
51 | our $TICK = MAX_TIME * 1e-6; |
51 | our $TICK = MAX_TIME * 1e-6; |
52 | our $TICK_WATCHER; |
52 | our $TICK_WATCHER; |
53 | our $AIO_POLL_WATCHER; |
53 | our $AIO_POLL_WATCHER; |
54 | our $WRITE_RUNTIME_WATCHER; |
54 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
55 | our $NEXT_TICK; |
55 | our $NEXT_TICK; |
56 | our $NOW; |
56 | our $NOW; |
57 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
57 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
58 | |
58 | |
59 | our $BDB_POLL_WATCHER; |
59 | our $BDB_POLL_WATCHER; |
… | |
… | |
273 | sub freeze_mainloop { |
273 | sub freeze_mainloop { |
274 | return unless $TICK_WATCHER->is_active; |
274 | return unless $TICK_WATCHER->is_active; |
275 | |
275 | |
276 | my $guard = Coro::guard { |
276 | my $guard = Coro::guard { |
277 | $TICK_WATCHER->start; |
277 | $TICK_WATCHER->start; |
278 | $WRITE_RUNTIME_WATCHER->start; |
|
|
279 | }; |
278 | }; |
280 | $WRITE_RUNTIME_WATCHER->stop; |
|
|
281 | $TICK_WATCHER->stop; |
279 | $TICK_WATCHER->stop; |
282 | $guard |
280 | $guard |
283 | } |
281 | } |
284 | |
282 | |
285 | =item cf::async { BLOCK } |
283 | =item cf::async { BLOCK } |
… | |
… | |
356 | |
354 | |
357 | $coro |
355 | $coro |
358 | } |
356 | } |
359 | |
357 | |
360 | sub write_runtime { |
358 | sub write_runtime { |
361 | $TICK_WATCHER->is_active or cf::cleanup "mainloop frozen but runtime active", 1; |
|
|
362 | |
|
|
363 | my $runtime = cf::localdir . "/runtime"; |
359 | my $runtime = cf::localdir . "/runtime"; |
364 | |
360 | |
365 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
361 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
366 | or return; |
362 | or return; |
367 | |
363 | |
… | |
… | |
789 | } |
785 | } |
790 | } else { |
786 | } else { |
791 | aio_unlink $filename; |
787 | aio_unlink $filename; |
792 | aio_unlink "$filename.pst"; |
788 | aio_unlink "$filename.pst"; |
793 | } |
789 | } |
794 | |
|
|
795 | #d##TODO# nuke non .map-files if exist |
|
|
796 | if ($filename =~ s/\.map$//) { |
|
|
797 | aio_unlink $filename; |
|
|
798 | aio_unlink "$filename.pst"; |
|
|
799 | } |
|
|
800 | } |
790 | } |
801 | } |
791 | } |
802 | |
792 | |
803 | sub object_freezer_as_string { |
793 | sub object_freezer_as_string { |
804 | my ($rdata, $objs) = @_; |
794 | my ($rdata, $objs) = @_; |
… | |
… | |
810 | |
800 | |
811 | sub object_thawer_load { |
801 | sub object_thawer_load { |
812 | my ($filename) = @_; |
802 | my ($filename) = @_; |
813 | |
803 | |
814 | my ($data, $av); |
804 | my ($data, $av); |
815 | |
|
|
816 | #d#TODO remove .map if file does not exist |
|
|
817 | aio_stat $filename and $filename =~ s/\.map$//; |
|
|
818 | |
805 | |
819 | (aio_load $filename, $data) >= 0 |
806 | (aio_load $filename, $data) >= 0 |
820 | or return; |
807 | or return; |
821 | |
808 | |
822 | unless (aio_stat "$filename.pst") { |
809 | unless (aio_stat "$filename.pst") { |
… | |
… | |
1309 | my ($self) = @_; |
1296 | my ($self) = @_; |
1310 | |
1297 | |
1311 | utf8::encode (my $save = $self->save_path); |
1298 | utf8::encode (my $save = $self->save_path); |
1312 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1299 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1313 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1300 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1314 | |
|
|
1315 | #d#TODO remove .map and also nuke |
|
|
1316 | $save =~ s/\.map// or return;#d# |
|
|
1317 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save;#d# |
|
|
1318 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst";#d# |
|
|
1319 | } |
1301 | } |
1320 | |
1302 | |
1321 | sub load_header_from($) { |
1303 | sub load_header_from($) { |
1322 | my ($self, $path) = @_; |
1304 | my ($self, $path) = @_; |
1323 | |
1305 | |
… | |
… | |
1822 | $map->load; |
1804 | $map->load; |
1823 | $map->load_diag; |
1805 | $map->load_diag; |
1824 | |
1806 | |
1825 | return unless $self->contr->active; |
1807 | return unless $self->contr->active; |
1826 | $self->activate_recursive; |
1808 | $self->activate_recursive; |
|
|
1809 | |
|
|
1810 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
1827 | $self->enter_map ($map, $x, $y); |
1811 | $self->enter_map ($map, $x, $y); |
1828 | } |
1812 | } |
1829 | |
1813 | |
1830 | cf::player->attach ( |
1814 | cf::player->attach ( |
1831 | on_logout => sub { |
1815 | on_logout => sub { |
… | |
… | |
1844 | # try to abort aborted map switching on player login :) |
1828 | # try to abort aborted map switching on player login :) |
1845 | # should happen only on crashes |
1829 | # should happen only on crashes |
1846 | if ($pl->ob->{_link_pos}) { |
1830 | if ($pl->ob->{_link_pos}) { |
1847 | $pl->ob->enter_link; |
1831 | $pl->ob->enter_link; |
1848 | (async { |
1832 | (async { |
1849 | # we need this sleep as the login has a concurrent enter_exit running |
|
|
1850 | # and this sleep increases chances of the player not ending up in scorn |
|
|
1851 | $pl->ob->reply (undef, |
1833 | $pl->ob->reply (undef, |
1852 | "There was an internal problem at your last logout, " |
1834 | "There was an internal problem at your last logout, " |
1853 | . "the server will try to bring you to your intended destination in a second.", |
1835 | . "the server will try to bring you to your intended destination in a second.", |
1854 | cf::NDI_RED); |
1836 | cf::NDI_RED); |
|
|
1837 | # we need this sleep as the login has a concurrent enter_exit running |
|
|
1838 | # and this sleep increases chances of the player not ending up in scorn |
1855 | Coro::Timer::sleep 1; |
1839 | Coro::Timer::sleep 1; |
1856 | $pl->ob->leave_link; |
1840 | $pl->ob->leave_link; |
1857 | })->prio (2); |
1841 | })->prio (2); |
1858 | } |
1842 | } |
1859 | }, |
1843 | }, |
… | |
… | |
2258 | } |
2242 | } |
2259 | |
2243 | |
2260 | ############################################################################# |
2244 | ############################################################################# |
2261 | # the server's init and main functions |
2245 | # the server's init and main functions |
2262 | |
2246 | |
2263 | sub load_resources { |
2247 | sub init_resources { |
2264 | load_regions sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2248 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2265 | or die "unable to load regions file\n";#d# |
2249 | or die "unable to load regions file\n";#d# |
2266 | } |
2250 | } |
2267 | |
2251 | |
2268 | sub cfg_load { |
2252 | sub cfg_load { |
2269 | open my $fh, "<:utf8", cf::confdir . "/config" |
2253 | open my $fh, "<:utf8", cf::confdir . "/config" |
… | |
… | |
2285 | warn $@ if $@; |
2269 | warn $@ if $@; |
2286 | } |
2270 | } |
2287 | } |
2271 | } |
2288 | |
2272 | |
2289 | sub init { |
2273 | sub init { |
2290 | load_resources; |
2274 | init_resources; |
2291 | } |
2275 | } |
2292 | |
2276 | |
2293 | sub main { |
2277 | sub main { |
2294 | # we must not ever block the main coroutine |
2278 | # we must not ever block the main coroutine |
2295 | local $Coro::idle = sub { |
2279 | local $Coro::idle = sub { |
… | |
… | |
2372 | return; |
2356 | return; |
2373 | } |
2357 | } |
2374 | |
2358 | |
2375 | warn "reloading..."; |
2359 | warn "reloading..."; |
2376 | |
2360 | |
2377 | warn "cancelling server ticker"; |
2361 | warn "entering sync_job"; |
2378 | $TICK_WATCHER->cancel; |
|
|
2379 | |
2362 | |
|
|
2363 | cf::sync_job { |
|
|
2364 | cf::write_runtime; # external watchdog should not bark |
2380 | cf::emergency_save; |
2365 | cf::emergency_save; |
|
|
2366 | cf::write_runtime; # external watchdog should not bark |
2381 | |
2367 | |
2382 | eval { |
2368 | warn "syncing database to disk"; |
|
|
2369 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
2370 | |
2383 | # if anything goes wrong in here, we should simply crash as we already saved |
2371 | # if anything goes wrong in here, we should simply crash as we already saved |
2384 | |
2372 | |
2385 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2373 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2386 | for (Event::all_watchers) { |
2374 | for (Event::all_watchers) { |
2387 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2375 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2388 | } |
2376 | } |
2389 | |
|
|
2390 | warn "syncing database to disk"; |
|
|
2391 | BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
|
|
2392 | |
2377 | |
2393 | warn "flushing outstanding aio requests"; |
2378 | warn "flushing outstanding aio requests"; |
2394 | for (;;) { |
2379 | for (;;) { |
2395 | BDB::flush; |
2380 | BDB::flush; |
2396 | IO::AIO::flush; |
2381 | IO::AIO::flush; |
… | |
… | |
2468 | _global_reattach; |
2453 | _global_reattach; |
2469 | warn "reattaching attachments to maps"; |
2454 | warn "reattaching attachments to maps"; |
2470 | reattach $_ for values %MAP; |
2455 | reattach $_ for values %MAP; |
2471 | |
2456 | |
2472 | warn "loading reloadable resources"; |
2457 | warn "loading reloadable resources"; |
2473 | load_resources; |
2458 | init_resources; |
2474 | |
2459 | |
2475 | warn "restarting server ticker"; |
2460 | warn "leaving sync_job"; |
2476 | |
2461 | |
2477 | $TICK_WATCHER->start; |
2462 | 1 |
2478 | }; |
2463 | } or do { |
2479 | |
|
|
2480 | if ($@) { |
|
|
2481 | warn $@; |
2464 | warn $@; |
2482 | warn "error while reloading, exiting."; |
2465 | warn "error while reloading, exiting."; |
2483 | exit 1; |
2466 | exit 1; |
2484 | } |
2467 | }; |
2485 | |
2468 | |
2486 | warn "reloaded"; |
2469 | warn "reloaded"; |
2487 | }; |
2470 | }; |
2488 | |
2471 | |
2489 | our $RELOAD_WATCHER; # used only during reload |
2472 | our $RELOAD_WATCHER; # used only during reload |
… | |
… | |
2530 | |
2513 | |
2531 | cf::server_tick; # one server iteration |
2514 | cf::server_tick; # one server iteration |
2532 | $RUNTIME += $TICK; |
2515 | $RUNTIME += $TICK; |
2533 | $NEXT_TICK += $TICK; |
2516 | $NEXT_TICK += $TICK; |
2534 | |
2517 | |
|
|
2518 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
|
|
2519 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
|
|
2520 | Coro::async_pool { |
|
|
2521 | write_runtime |
|
|
2522 | or warn "ERROR: unable to write runtime file: $!"; |
|
|
2523 | }; |
|
|
2524 | } |
|
|
2525 | |
|
|
2526 | |
2535 | $WAIT_FOR_TICK->broadcast; |
2527 | $WAIT_FOR_TICK->broadcast; |
2536 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2528 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2537 | |
2529 | |
2538 | Event::sweep; |
|
|
2539 | Coro::cede_notself; |
|
|
2540 | |
|
|
2541 | # my $AFTER = Event::time; |
2530 | # my $AFTER = Event::time; |
2542 | # warn $AFTER - $NOW;#d# |
2531 | # warn $AFTER - $NOW;#d# |
2543 | |
2532 | |
2544 | # if we are delayed by four ticks or more, skip them all |
2533 | # if we are delayed by four ticks or more, skip them all |
2545 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2534 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
… | |
… | |
2604 | |
2593 | |
2605 | undef $Coro::AIO::WATCHER; |
2594 | undef $Coro::AIO::WATCHER; |
2606 | IO::AIO::max_poll_time $TICK * 0.1; |
2595 | IO::AIO::max_poll_time $TICK * 0.1; |
2607 | $AIO_POLL_WATCHER = Event->io ( |
2596 | $AIO_POLL_WATCHER = Event->io ( |
2608 | reentrant => 0, |
2597 | reentrant => 0, |
|
|
2598 | data => WF_AUTOCANCEL, |
2609 | fd => IO::AIO::poll_fileno, |
2599 | fd => IO::AIO::poll_fileno, |
2610 | poll => 'r', |
2600 | poll => 'r', |
2611 | prio => 6, |
2601 | prio => 6, |
2612 | data => WF_AUTOCANCEL, |
|
|
2613 | cb => \&IO::AIO::poll_cb, |
2602 | cb => \&IO::AIO::poll_cb, |
2614 | ); |
2603 | ); |
2615 | } |
2604 | } |
2616 | |
2605 | |
2617 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
|
|
2618 | reentrant => 0, |
|
|
2619 | data => WF_AUTOCANCEL, |
|
|
2620 | after => 1, |
|
|
2621 | interval => 10, |
|
|
2622 | prio => 6, # keep it lowest so it acts like a watchdog |
|
|
2623 | cb => Coro::unblock_sub { |
|
|
2624 | write_runtime |
|
|
2625 | or warn "ERROR: unable to write runtime file: $!"; |
|
|
2626 | }, |
|
|
2627 | ); |
|
|
2628 | |
|
|
2629 | END { cf::emergency_save } |
2606 | END { cf::emergency_save } |
2630 | |
2607 | |
2631 | 1 |
2608 | 1 |
2632 | |
2609 | |