… | |
… | |
8 | use Storable; |
8 | use Storable; |
9 | use Opcode; |
9 | use Opcode; |
10 | use Safe; |
10 | use Safe; |
11 | use Safe::Hole; |
11 | use Safe::Hole; |
12 | |
12 | |
13 | use Coro 3.3 (); |
13 | use Coro 3.4 (); |
14 | use Coro::Event; |
14 | use Coro::Event; |
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; |
… | |
… | |
349 | my $runtime = cf::localdir . "/runtime"; |
349 | my $runtime = cf::localdir . "/runtime"; |
350 | |
350 | |
351 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
351 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
352 | or return; |
352 | or return; |
353 | |
353 | |
|
|
354 | my $value = $cf::RUNTIME + 90 + 10; |
354 | my $value = $cf::RUNTIME + 1 + 10; # 10 is the runtime save interval, for a monotonic clock |
355 | # 10 is the runtime save interval, for a monotonic clock |
|
|
356 | # 60 allows for the watchdog to kill the server. |
|
|
357 | |
355 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
358 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
356 | and return; |
359 | and return; |
357 | |
360 | |
358 | aio_fsync $fh |
361 | aio_fsync $fh |
359 | and return; |
362 | and return; |
… | |
… | |
502 | our @CB_CLIENT = (); |
505 | our @CB_CLIENT = (); |
503 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
506 | our @CB_TYPE = (); # registry for type (cf-object class) based events |
504 | our @CB_MAP = (); |
507 | our @CB_MAP = (); |
505 | |
508 | |
506 | my %attachment; |
509 | my %attachment; |
|
|
510 | |
|
|
511 | sub cf::attachable::thawer_merge { |
|
|
512 | # simply override everything except _meta |
|
|
513 | local $_[0]{_meta}; |
|
|
514 | %{$_[0]} = %{$_[1]}; |
|
|
515 | } |
507 | |
516 | |
508 | sub _attach_cb($$$$) { |
517 | sub _attach_cb($$$$) { |
509 | my ($registry, $event, $prio, $cb) = @_; |
518 | my ($registry, $event, $prio, $cb) = @_; |
510 | |
519 | |
511 | use sort 'stable'; |
520 | use sort 'stable'; |
… | |
… | |
951 | |
960 | |
952 | sub exists($) { |
961 | sub exists($) { |
953 | my ($login) = @_; |
962 | my ($login) = @_; |
954 | |
963 | |
955 | $cf::PLAYER{$login} |
964 | $cf::PLAYER{$login} |
956 | or cf::sync_job { !aio_stat $login } |
965 | or cf::sync_job { !aio_stat path $login } |
957 | } |
966 | } |
958 | |
967 | |
959 | sub find($) { |
968 | sub find($) { |
960 | return $cf::PLAYER{$_[0]} || do { |
969 | return $cf::PLAYER{$_[0]} || do { |
961 | my $login = $_[0]; |
970 | my $login = $_[0]; |
… | |
… | |
1107 | |
1116 | |
1108 | use Fcntl; |
1117 | use Fcntl; |
1109 | use Coro::AIO; |
1118 | use Coro::AIO; |
1110 | |
1119 | |
1111 | use overload |
1120 | use overload |
1112 | '""' => \&as_string; |
1121 | '""' => \&as_string, |
|
|
1122 | fallback => 1; |
1113 | |
1123 | |
1114 | our $MAX_RESET = 3600; |
1124 | our $MAX_RESET = 3600; |
1115 | our $DEFAULT_RESET = 3000; |
1125 | our $DEFAULT_RESET = 3000; |
1116 | |
1126 | |
1117 | sub generate_random_map { |
1127 | sub generate_random_map { |
1118 | my ($self, $rmp) = @_; |
1128 | my ($self, $rmp) = @_; |
1119 | |
|
|
1120 | # mit "rum" bekleckern, nicht |
1129 | # mit "rum" bekleckern, nicht |
1121 | $self->_create_random_map ( |
1130 | $self->_create_random_map ( |
1122 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1131 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1123 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1132 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1124 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1133 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
… | |
… | |
1144 | } |
1153 | } |
1145 | |
1154 | |
1146 | # also paths starting with '/' |
1155 | # also paths starting with '/' |
1147 | $EXT_MAP{"cf::map"} = qr{^(?=/)}; |
1156 | $EXT_MAP{"cf::map"} = qr{^(?=/)}; |
1148 | |
1157 | |
|
|
1158 | sub thawer_merge { |
|
|
1159 | my ($self, $merge) = @_; |
|
|
1160 | |
|
|
1161 | # we have to keep some variables in memory intact |
|
|
1162 | local $self->{path}; |
|
|
1163 | local $self->{load_path}; |
|
|
1164 | local $self->{deny_save}; |
|
|
1165 | local $self->{deny_reset}; |
|
|
1166 | |
|
|
1167 | $self->SUPER::thawer_merge ($merge); |
|
|
1168 | } |
|
|
1169 | |
1149 | sub normalise { |
1170 | sub normalise { |
1150 | my ($path, $base) = @_; |
1171 | my ($path, $base) = @_; |
1151 | |
1172 | |
1152 | # map plan: |
1173 | # map plan: |
1153 | # |
1174 | # |
… | |
… | |
1249 | |
1270 | |
1250 | sub unlink_save { |
1271 | sub unlink_save { |
1251 | my ($self) = @_; |
1272 | my ($self) = @_; |
1252 | |
1273 | |
1253 | utf8::encode (my $save = $self->save_path); |
1274 | utf8::encode (my $save = $self->save_path); |
1254 | IO::AIO::aioreq_pri 4; IO::AIO::aio_unlink $save; |
1275 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1255 | IO::AIO::aioreq_pri 4; IO::AIO::aio_unlink "$save.pst"; |
1276 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1256 | } |
1277 | } |
1257 | |
1278 | |
1258 | sub load_header_from($) { |
1279 | sub load_header_from($) { |
1259 | my ($self, $path) = @_; |
1280 | my ($self, $path) = @_; |
1260 | |
1281 | |
… | |
… | |
1321 | |
1342 | |
1322 | $map->load_header |
1343 | $map->load_header |
1323 | or return; |
1344 | or return; |
1324 | |
1345 | |
1325 | if ($map->should_reset) { |
1346 | if ($map->should_reset) { |
|
|
1347 | # doing this can freeze the server in a sync job, obviously |
|
|
1348 | #$cf::WAIT_FOR_TICK->wait; |
1326 | $map->reset; |
1349 | $map->reset; |
1327 | undef $guard; |
1350 | undef $guard; |
1328 | $map = find $path |
1351 | $map = find $path |
1329 | or return; |
1352 | or return; |
1330 | } |
1353 | } |
… | |
… | |
1422 | |
1445 | |
1423 | cf::sync_job { $map->load }; |
1446 | cf::sync_job { $map->load }; |
1424 | } |
1447 | } |
1425 | |
1448 | |
1426 | our %MAP_PREFETCH; |
1449 | our %MAP_PREFETCH; |
1427 | our $MAP_PREFETCHER = Coro::async { |
1450 | our $MAP_PREFETCHER = undef; |
1428 | while () { |
|
|
1429 | for my $path (keys %MAP_PREFETCH) { |
|
|
1430 | my $map = find $path |
|
|
1431 | or next; |
|
|
1432 | $map->load; |
|
|
1433 | |
|
|
1434 | delete $MAP_PREFETCH{$path}; |
|
|
1435 | } |
|
|
1436 | Coro::schedule; |
|
|
1437 | } |
|
|
1438 | }; |
|
|
1439 | |
1451 | |
1440 | sub find_async { |
1452 | sub find_async { |
1441 | my ($path, $origin) = @_; |
1453 | my ($path, $origin) = @_; |
1442 | |
1454 | |
1443 | $path = normalise $path, $origin && $origin->{path}; |
1455 | $path = normalise $path, $origin && $origin->{path}; |
1444 | |
1456 | |
1445 | if (my $map = $cf::MAP{$path}) { |
1457 | if (my $map = $cf::MAP{$path}) { |
1446 | return $map if $map->in_memory == cf::MAP_IN_MEMORY; |
1458 | return $map if $map->in_memory == cf::MAP_IN_MEMORY; |
1447 | } |
1459 | } |
1448 | |
1460 | |
1449 | $MAP_PREFETCH{$path} = 0; |
1461 | undef $MAP_PREFETCH{$path}; |
1450 | $MAP_PREFETCHER->ready; |
1462 | $MAP_PREFETCHER ||= cf::async { |
|
|
1463 | while (%MAP_PREFETCH) { |
|
|
1464 | for my $path (keys %MAP_PREFETCH) { |
|
|
1465 | my $map = find $path |
|
|
1466 | or next; |
|
|
1467 | $map->load; |
|
|
1468 | |
|
|
1469 | delete $MAP_PREFETCH{$path}; |
|
|
1470 | } |
|
|
1471 | } |
|
|
1472 | undef $MAP_PREFETCHER; |
|
|
1473 | }; |
1451 | |
1474 | |
1452 | () |
1475 | () |
1453 | } |
1476 | } |
1454 | |
1477 | |
1455 | sub save { |
1478 | sub save { |
… | |
… | |
1515 | my ($self) = @_; |
1538 | my ($self) = @_; |
1516 | |
1539 | |
1517 | $self->reset_at <= $cf::RUNTIME |
1540 | $self->reset_at <= $cf::RUNTIME |
1518 | } |
1541 | } |
1519 | |
1542 | |
1520 | sub rename { |
|
|
1521 | my ($self, $new_path) = @_; |
|
|
1522 | |
|
|
1523 | normalise $new_path; |
|
|
1524 | |
|
|
1525 | $self->unlink_save; |
|
|
1526 | |
|
|
1527 | delete $cf::MAP{$self->path}; |
|
|
1528 | $self->{path} = $new_path; $self->path ($self->{path}); |
|
|
1529 | $cf::MAP{$self->path} = $self; |
|
|
1530 | |
|
|
1531 | $self->save; |
|
|
1532 | } |
|
|
1533 | |
|
|
1534 | sub reset { |
1543 | sub reset { |
1535 | my ($self) = @_; |
1544 | my ($self) = @_; |
1536 | |
1545 | |
1537 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
1546 | my $lock = cf::lock_acquire "map_data:$self->{path}"; |
1538 | |
1547 | |
… | |
… | |
1555 | my $nuke_counter = "aaaa"; |
1564 | my $nuke_counter = "aaaa"; |
1556 | |
1565 | |
1557 | sub nuke { |
1566 | sub nuke { |
1558 | my ($self) = @_; |
1567 | my ($self) = @_; |
1559 | |
1568 | |
|
|
1569 | delete $cf::MAP{$self->path}; |
|
|
1570 | |
|
|
1571 | $self->unlink_save; |
|
|
1572 | |
|
|
1573 | bless $self, "cf::map"; |
|
|
1574 | delete $self->{deny_reset}; |
1560 | $self->{deny_save} = 1; |
1575 | $self->{deny_save} = 1; |
1561 | $self->reset_timeout (1); |
1576 | $self->reset_timeout (1); |
1562 | $self->rename ("{nuke}/" . ($nuke_counter++)); |
1577 | $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); |
|
|
1578 | |
|
|
1579 | $cf::MAP{$self->path} = $self; |
|
|
1580 | |
1563 | $self->reset; # polite request, might not happen |
1581 | $self->reset; # polite request, might not happen |
1564 | } |
1582 | } |
1565 | |
1583 | |
1566 | =item cf::map::unique_maps |
1584 | =item cf::map::unique_maps |
1567 | |
1585 | |
… | |
… | |
1798 | |
1816 | |
1799 | sub parse_random_map_params { |
1817 | sub parse_random_map_params { |
1800 | my ($spec) = @_; |
1818 | my ($spec) = @_; |
1801 | |
1819 | |
1802 | my $rmp = { # defaults |
1820 | my $rmp = { # defaults |
1803 | xsize => 10, |
1821 | xsize => (cf::rndm 15, 40), |
1804 | ysize => 10, |
1822 | ysize => (cf::rndm 15, 40), |
|
|
1823 | symmetry => (cf::rndm 1, cf::SYMMETRY_XY), |
|
|
1824 | #layout => string, |
1805 | }; |
1825 | }; |
1806 | |
1826 | |
1807 | for (split /\n/, $spec) { |
1827 | for (split /\n/, $spec) { |
1808 | my ($k, $v) = split /\s+/, $_, 2; |
1828 | my ($k, $v) = split /\s+/, $_, 2; |
1809 | |
1829 | |
… | |
… | |
1813 | $rmp |
1833 | $rmp |
1814 | } |
1834 | } |
1815 | |
1835 | |
1816 | sub prepare_random_map { |
1836 | sub prepare_random_map { |
1817 | my ($exit) = @_; |
1837 | my ($exit) = @_; |
|
|
1838 | |
|
|
1839 | my $guard = cf::lock_acquire "exit_prepare:$exit"; |
1818 | |
1840 | |
1819 | # all this does is basically replace the /! path by |
1841 | # all this does is basically replace the /! path by |
1820 | # a new random map path (?random/...) with a seed |
1842 | # a new random map path (?random/...) with a seed |
1821 | # that depends on the exit object |
1843 | # that depends on the exit object |
1822 | |
1844 | |
… | |
… | |
1831 | |
1853 | |
1832 | $rmp->{random_seed} ||= $exit->random_seed; |
1854 | $rmp->{random_seed} ||= $exit->random_seed; |
1833 | |
1855 | |
1834 | my $data = cf::to_json $rmp; |
1856 | my $data = cf::to_json $rmp; |
1835 | my $md5 = Digest::MD5::md5_hex $data; |
1857 | my $md5 = Digest::MD5::md5_hex $data; |
|
|
1858 | my $meta = "$cf::RANDOM_MAPS/$md5.meta"; |
1836 | |
1859 | |
1837 | if (my $fh = aio_open "$cf::RANDOM_MAPS/$md5.meta", O_WRONLY | O_CREAT, 0666) { |
1860 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
1838 | aio_write $fh, 0, (length $data), $data, 0; |
1861 | aio_write $fh, 0, (length $data), $data, 0; |
|
|
1862 | undef $fh; |
|
|
1863 | aio_rename "$meta~", $meta; |
1839 | |
1864 | |
1840 | $exit->slaying ("?random/$md5"); |
1865 | $exit->slaying ("?random/$md5"); |
1841 | $exit->msg (undef); |
1866 | $exit->msg (undef); |
1842 | } |
1867 | } |
1843 | } |
1868 | } |
… | |
… | |
2153 | sub db_sync() { |
2178 | sub db_sync() { |
2154 | db_save if $dirty; |
2179 | db_save if $dirty; |
2155 | undef $dirty; |
2180 | undef $dirty; |
2156 | } |
2181 | } |
2157 | |
2182 | |
2158 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, data => WF_AUTOCANCEL, cb => sub { |
2183 | my $idle = Event->idle (min => 10, max => 20, repeat => 0, data => WF_AUTOCANCEL, cb => sub { |
2159 | db_sync; |
2184 | db_sync; |
2160 | }); |
2185 | }); |
2161 | |
2186 | |
2162 | sub db_dirty() { |
2187 | sub db_dirty() { |
2163 | $dirty = 1; |
2188 | $dirty = 1; |
… | |
… | |
2213 | |
2238 | |
2214 | sub main { |
2239 | sub main { |
2215 | # we must not ever block the main coroutine |
2240 | # we must not ever block the main coroutine |
2216 | local $Coro::idle = sub { |
2241 | local $Coro::idle = sub { |
2217 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
2242 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
|
|
2243 | (async { |
2218 | async { Event::one_event }; |
2244 | Event::one_event; |
|
|
2245 | })->prio (Coro::PRIO_MAX); |
2219 | }; |
2246 | }; |
2220 | |
2247 | |
2221 | cfg_load; |
2248 | cfg_load; |
2222 | db_load; |
2249 | db_load; |
2223 | load_extensions; |
2250 | load_extensions; |
|
|
2251 | |
|
|
2252 | $TICK_WATCHER->start; |
2224 | Event::loop; |
2253 | Event::loop; |
2225 | } |
2254 | } |
2226 | |
2255 | |
2227 | ############################################################################# |
2256 | ############################################################################# |
2228 | # initialisation and cleanup |
2257 | # initialisation and cleanup |
… | |
… | |
2270 | } |
2299 | } |
2271 | |
2300 | |
2272 | sub reload() { |
2301 | sub reload() { |
2273 | # can/must only be called in main |
2302 | # can/must only be called in main |
2274 | if ($Coro::current != $Coro::main) { |
2303 | if ($Coro::current != $Coro::main) { |
2275 | warn "can only reload from main coroutine\n"; |
2304 | warn "can only reload from main coroutine"; |
2276 | return; |
2305 | return; |
2277 | } |
2306 | } |
2278 | |
2307 | |
2279 | warn "reloading..."; |
2308 | warn "reloading..."; |
2280 | |
2309 | |
2281 | warn "freezing server"; |
2310 | warn "cancelling server ticker"; |
2282 | my $guard = freeze_mainloop; |
2311 | $TICK_WATCHER->cancel; |
|
|
2312 | |
2283 | cf::emergency_save; |
2313 | cf::emergency_save; |
2284 | |
|
|
2285 | warn "sync database to disk"; |
|
|
2286 | cf::db_sync; |
|
|
2287 | IO::AIO::flush; |
|
|
2288 | |
2314 | |
2289 | eval { |
2315 | eval { |
2290 | # if anything goes wrong in here, we should simply crash as we already saved |
2316 | # if anything goes wrong in here, we should simply crash as we already saved |
2291 | |
2317 | |
|
|
2318 | warn "syncing database to disk"; |
|
|
2319 | cf::db_sync; |
|
|
2320 | |
2292 | warn "cancel all watchers"; |
2321 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2293 | for (Event::all_watchers) { |
2322 | for (Event::all_watchers) { |
2294 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2323 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2295 | } |
2324 | } |
2296 | |
2325 | |
|
|
2326 | warn "flushing outstanding aio requests"; |
|
|
2327 | for (;;) { |
|
|
2328 | IO::AIO::flush; |
|
|
2329 | Coro::cede; |
|
|
2330 | last unless IO::AIO::nreqs; |
|
|
2331 | warn "iterate..."; |
|
|
2332 | } |
|
|
2333 | |
2297 | warn "cancel all extension coros"; |
2334 | warn "cancelling all extension coros"; |
2298 | $_->cancel for values %EXT_CORO; |
2335 | $_->cancel for values %EXT_CORO; |
2299 | %EXT_CORO = (); |
2336 | %EXT_CORO = (); |
2300 | |
2337 | |
2301 | warn "remove commands"; |
2338 | warn "removing commands"; |
2302 | %COMMAND = (); |
2339 | %COMMAND = (); |
2303 | |
2340 | |
2304 | warn "remove ext commands"; |
2341 | warn "removing ext commands"; |
2305 | %EXTCMD = (); |
2342 | %EXTCMD = (); |
2306 | |
2343 | |
2307 | warn "unload/nuke all extensions"; |
2344 | warn "unloading/nuking all extensions"; |
2308 | for my $pkg (@EXTS) { |
2345 | for my $pkg (@EXTS) { |
2309 | warn "... unloading $pkg"; |
2346 | warn "... unloading $pkg"; |
2310 | |
2347 | |
2311 | if (my $cb = $pkg->can ("unload")) { |
2348 | if (my $cb = $pkg->can ("unload")) { |
2312 | eval { |
2349 | eval { |
… | |
… | |
2317 | |
2354 | |
2318 | warn "... nuking $pkg"; |
2355 | warn "... nuking $pkg"; |
2319 | Symbol::delete_package $pkg; |
2356 | Symbol::delete_package $pkg; |
2320 | } |
2357 | } |
2321 | |
2358 | |
2322 | warn "unload all perl modules loaded from $LIBDIR"; |
2359 | warn "unloading all perl modules loaded from $LIBDIR"; |
2323 | while (my ($k, $v) = each %INC) { |
2360 | while (my ($k, $v) = each %INC) { |
2324 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
2361 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
2325 | |
2362 | |
2326 | warn "removing <$k>"; |
2363 | warn "... unloading $k"; |
2327 | delete $INC{$k}; |
2364 | delete $INC{$k}; |
2328 | |
2365 | |
2329 | $k =~ s/\.pm$//; |
2366 | $k =~ s/\.pm$//; |
2330 | $k =~ s/\//::/g; |
2367 | $k =~ s/\//::/g; |
2331 | |
2368 | |
… | |
… | |
2334 | } |
2371 | } |
2335 | |
2372 | |
2336 | Symbol::delete_package $k; |
2373 | Symbol::delete_package $k; |
2337 | } |
2374 | } |
2338 | |
2375 | |
2339 | warn "get rid of safe::, as good as possible"; |
2376 | warn "getting rid of safe::, as good as possible"; |
2340 | Symbol::delete_package "safe::$_" |
2377 | Symbol::delete_package "safe::$_" |
2341 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2378 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2342 | |
2379 | |
2343 | warn "unload cf.pm \"a bit\""; |
2380 | warn "unloading cf.pm \"a bit\""; |
2344 | delete $INC{"cf.pm"}; |
2381 | delete $INC{"cf.pm"}; |
2345 | |
2382 | |
2346 | # don't, removes xs symbols, too, |
2383 | # don't, removes xs symbols, too, |
2347 | # and global variables created in xs |
2384 | # and global variables created in xs |
2348 | #Symbol::delete_package __PACKAGE__; |
2385 | #Symbol::delete_package __PACKAGE__; |
2349 | |
2386 | |
|
|
2387 | warn "unload completed, starting to reload now"; |
|
|
2388 | |
2350 | warn "reloading cf.pm"; |
2389 | warn "reloading cf.pm"; |
2351 | require cf; |
2390 | require cf; |
2352 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2391 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2353 | |
2392 | |
2354 | warn "load config and database again"; |
2393 | warn "loading config and database again"; |
2355 | cf::cfg_load; |
2394 | cf::cfg_load; |
2356 | cf::db_load; |
2395 | cf::db_load; |
2357 | |
2396 | |
2358 | warn "load extensions"; |
2397 | warn "loading extensions"; |
2359 | cf::load_extensions; |
2398 | cf::load_extensions; |
2360 | |
2399 | |
2361 | warn "reattach attachments to objects/players"; |
2400 | warn "reattaching attachments to objects/players"; |
2362 | _global_reattach; |
2401 | _global_reattach; |
2363 | warn "reattach attachments to maps"; |
2402 | warn "reattaching attachments to maps"; |
2364 | reattach $_ for values %MAP; |
2403 | reattach $_ for values %MAP; |
|
|
2404 | |
|
|
2405 | warn "restarting server ticker"; |
|
|
2406 | |
|
|
2407 | $TICK_WATCHER->start; |
2365 | }; |
2408 | }; |
2366 | |
2409 | |
2367 | if ($@) { |
2410 | if ($@) { |
2368 | warn $@; |
2411 | warn $@; |
2369 | warn "error while reloading, exiting."; |
2412 | warn "error while reloading, exiting."; |
… | |
… | |
2371 | } |
2414 | } |
2372 | |
2415 | |
2373 | warn "reloaded"; |
2416 | warn "reloaded"; |
2374 | }; |
2417 | }; |
2375 | |
2418 | |
|
|
2419 | our $RELOAD_WATCHER; # used only during reload |
|
|
2420 | |
2376 | register_command "reload" => sub { |
2421 | register_command "reload" => sub { |
2377 | my ($who, $arg) = @_; |
2422 | my ($who, $arg) = @_; |
2378 | |
2423 | |
2379 | if ($who->flag (FLAG_WIZ)) { |
2424 | if ($who->flag (FLAG_WIZ)) { |
2380 | $who->message ("start of reload."); |
2425 | $who->message ("reloading server."); |
|
|
2426 | |
|
|
2427 | # doing reload synchronously and two reloads happen back-to-back, |
|
|
2428 | # coro crashes during coro_state_free->destroy here. |
|
|
2429 | |
|
|
2430 | $RELOAD_WATCHER ||= Event->timer (after => 0, data => WF_AUTOCANCEL, cb => sub { |
2381 | reload; |
2431 | reload; |
2382 | $who->message ("end of reload."); |
2432 | undef $RELOAD_WATCHER; |
|
|
2433 | }); |
2383 | } |
2434 | } |
2384 | }; |
2435 | }; |
2385 | |
2436 | |
2386 | unshift @INC, $LIBDIR; |
2437 | unshift @INC, $LIBDIR; |
|
|
2438 | |
|
|
2439 | my $bug_warning = 0; |
2387 | |
2440 | |
2388 | $TICK_WATCHER = Event->timer ( |
2441 | $TICK_WATCHER = Event->timer ( |
2389 | reentrant => 0, |
2442 | reentrant => 0, |
|
|
2443 | parked => 1, |
2390 | prio => 0, |
2444 | prio => 0, |
2391 | at => $NEXT_TICK || $TICK, |
2445 | at => $NEXT_TICK || $TICK, |
2392 | data => WF_AUTOCANCEL, |
2446 | data => WF_AUTOCANCEL, |
2393 | cb => sub { |
2447 | cb => sub { |
|
|
2448 | if ($Coro::current != $Coro::main) { |
|
|
2449 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
|
|
2450 | unless ++$bug_warning > 10; |
|
|
2451 | return; |
|
|
2452 | } |
|
|
2453 | |
2394 | $NOW = Event::time; |
2454 | $NOW = Event::time; |
2395 | |
2455 | |
2396 | cf::server_tick; # one server iteration |
2456 | cf::server_tick; # one server iteration |
2397 | $RUNTIME += $TICK; |
2457 | $RUNTIME += $TICK; |
2398 | $NEXT_TICK += $TICK; |
2458 | $NEXT_TICK += $TICK; |
… | |
… | |
2418 | cb => \&IO::AIO::poll_cb, |
2478 | cb => \&IO::AIO::poll_cb, |
2419 | ); |
2479 | ); |
2420 | |
2480 | |
2421 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2481 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2422 | data => WF_AUTOCANCEL, |
2482 | data => WF_AUTOCANCEL, |
2423 | after => 0, |
2483 | after => 1, |
2424 | interval => 10, |
2484 | interval => 10, |
2425 | cb => sub { |
2485 | prio => 6, # keep it lowest so it acts like a watchdog |
2426 | (Coro::unblock_sub { |
2486 | cb => Coro::unblock_sub { |
2427 | write_runtime |
2487 | write_runtime |
2428 | or warn "ERROR: unable to write runtime file: $!"; |
2488 | or warn "ERROR: unable to write runtime file: $!"; |
2429 | })->(); |
|
|
2430 | }, |
2489 | }, |
2431 | ); |
2490 | ); |
2432 | |
2491 | |
2433 | END { cf::emergency_save } |
2492 | END { cf::emergency_save } |
2434 | |
2493 | |