… | |
… | |
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 (); |
25 | use Time::HiRes; |
26 | use Time::HiRes; |
|
|
27 | use Compress::LZF; |
|
|
28 | |
|
|
29 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
26 | |
30 | |
27 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
31 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
28 | |
32 | |
29 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
33 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
30 | |
34 | |
… | |
… | |
45 | our $LIBDIR = datadir . "/ext"; |
49 | our $LIBDIR = datadir . "/ext"; |
46 | |
50 | |
47 | our $TICK = MAX_TIME * 1e-6; |
51 | our $TICK = MAX_TIME * 1e-6; |
48 | our $TICK_WATCHER; |
52 | our $TICK_WATCHER; |
49 | our $AIO_POLL_WATCHER; |
53 | our $AIO_POLL_WATCHER; |
50 | our $WRITE_RUNTIME_WATCHER; |
54 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
51 | our $NEXT_TICK; |
55 | our $NEXT_TICK; |
52 | our $NOW; |
56 | our $NOW; |
53 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
57 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
|
|
58 | |
|
|
59 | our $BDB_POLL_WATCHER; |
|
|
60 | our $DB_ENV; |
54 | |
61 | |
55 | our %CFG; |
62 | our %CFG; |
56 | |
63 | |
57 | our $UPTIME; $UPTIME ||= time; |
64 | our $UPTIME; $UPTIME ||= time; |
58 | our $RUNTIME; |
65 | our $RUNTIME; |
59 | |
66 | |
60 | our %PLAYER; # all users |
67 | our %PLAYER; # all users |
61 | our %MAP; # all maps |
68 | our %MAP; # all maps |
62 | our $LINK_MAP; # the special {link} map, which is always available |
69 | our $LINK_MAP; # the special {link} map, which is always available |
63 | our $RANDOM_MAPS = cf::localdir . "/random"; |
70 | our $RANDOM_MAPS = cf::localdir . "/random"; |
|
|
71 | our $BDB_ENV_DIR = cf::localdir . "/db"; |
64 | |
72 | |
65 | our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal; |
73 | our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal; |
66 | our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal; |
74 | our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal; |
67 | |
75 | |
68 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
76 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
… | |
… | |
81 | mkdir cf::localdir; |
89 | mkdir cf::localdir; |
82 | mkdir cf::localdir . "/" . cf::playerdir; |
90 | mkdir cf::localdir . "/" . cf::playerdir; |
83 | mkdir cf::localdir . "/" . cf::tmpdir; |
91 | mkdir cf::localdir . "/" . cf::tmpdir; |
84 | mkdir cf::localdir . "/" . cf::uniquedir; |
92 | mkdir cf::localdir . "/" . cf::uniquedir; |
85 | mkdir $RANDOM_MAPS; |
93 | mkdir $RANDOM_MAPS; |
|
|
94 | mkdir $BDB_ENV_DIR; |
86 | |
95 | |
87 | our $EMERGENCY_POSITION; |
96 | our $EMERGENCY_POSITION; |
88 | |
97 | |
89 | sub cf::map::normalise; |
98 | sub cf::map::normalise; |
90 | |
99 | |
… | |
… | |
264 | sub freeze_mainloop { |
273 | sub freeze_mainloop { |
265 | return unless $TICK_WATCHER->is_active; |
274 | return unless $TICK_WATCHER->is_active; |
266 | |
275 | |
267 | my $guard = Coro::guard { |
276 | my $guard = Coro::guard { |
268 | $TICK_WATCHER->start; |
277 | $TICK_WATCHER->start; |
269 | $WRITE_RUNTIME_WATCHER->start; |
|
|
270 | }; |
278 | }; |
271 | $WRITE_RUNTIME_WATCHER->stop; |
|
|
272 | $TICK_WATCHER->stop; |
279 | $TICK_WATCHER->stop; |
273 | $guard |
280 | $guard |
274 | } |
281 | } |
275 | |
282 | |
276 | =item cf::async { BLOCK } |
283 | =item cf::async { BLOCK } |
… | |
… | |
347 | |
354 | |
348 | $coro |
355 | $coro |
349 | } |
356 | } |
350 | |
357 | |
351 | sub write_runtime { |
358 | sub write_runtime { |
|
|
359 | my $guard = cf::lock_acquire "write_runtime"; |
|
|
360 | |
352 | my $runtime = cf::localdir . "/runtime"; |
361 | my $runtime = cf::localdir . "/runtime"; |
353 | |
362 | |
354 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
363 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
355 | or return; |
364 | or return; |
356 | |
365 | |
… | |
… | |
778 | } |
787 | } |
779 | } else { |
788 | } else { |
780 | aio_unlink $filename; |
789 | aio_unlink $filename; |
781 | aio_unlink "$filename.pst"; |
790 | aio_unlink "$filename.pst"; |
782 | } |
791 | } |
783 | |
|
|
784 | #d##TODO# nuke non .map-files if exist |
|
|
785 | if ($filename =~ s/\.map$//) { |
|
|
786 | aio_unlink $filename; |
|
|
787 | aio_unlink "$filename.pst"; |
|
|
788 | } |
|
|
789 | } |
792 | } |
790 | } |
793 | } |
791 | |
794 | |
792 | sub object_freezer_as_string { |
795 | sub object_freezer_as_string { |
793 | my ($rdata, $objs) = @_; |
796 | my ($rdata, $objs) = @_; |
… | |
… | |
799 | |
802 | |
800 | sub object_thawer_load { |
803 | sub object_thawer_load { |
801 | my ($filename) = @_; |
804 | my ($filename) = @_; |
802 | |
805 | |
803 | my ($data, $av); |
806 | my ($data, $av); |
804 | |
|
|
805 | #d#TODO remove .map if file does not exist |
|
|
806 | aio_stat $filename and $filename =~ s/\.map$//; |
|
|
807 | |
807 | |
808 | (aio_load $filename, $data) >= 0 |
808 | (aio_load $filename, $data) >= 0 |
809 | or return; |
809 | or return; |
810 | |
810 | |
811 | unless (aio_stat "$filename.pst") { |
811 | unless (aio_stat "$filename.pst") { |
… | |
… | |
1030 | =cut |
1030 | =cut |
1031 | |
1031 | |
1032 | sub quit_character { |
1032 | sub quit_character { |
1033 | my ($pl) = @_; |
1033 | my ($pl) = @_; |
1034 | |
1034 | |
|
|
1035 | my $name = $pl->ob->name; |
|
|
1036 | |
1035 | $pl->{deny_save} = 1; |
1037 | $pl->{deny_save} = 1; |
1036 | $pl->password ("*"); # this should lock out the player until we nuked the dir |
1038 | $pl->password ("*"); # this should lock out the player until we nuked the dir |
1037 | |
1039 | |
1038 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1040 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1039 | $pl->deactivate; |
1041 | $pl->deactivate; |
… | |
… | |
1043 | my $path = playerdir $pl; |
1045 | my $path = playerdir $pl; |
1044 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1046 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1045 | aio_rename $path, $temp; |
1047 | aio_rename $path, $temp; |
1046 | delete $cf::PLAYER{$pl->ob->name}; |
1048 | delete $cf::PLAYER{$pl->ob->name}; |
1047 | $pl->destroy; |
1049 | $pl->destroy; |
|
|
1050 | |
|
|
1051 | my $prefix = qr<^~\Q$name\E/>; |
|
|
1052 | |
|
|
1053 | # nuke player maps |
|
|
1054 | $cf::MAP{$_}->nuke for grep /$prefix/, keys %cf::MAP; |
|
|
1055 | |
1048 | IO::AIO::aio_rmtree $temp; |
1056 | IO::AIO::aio_rmtree $temp; |
|
|
1057 | } |
|
|
1058 | |
|
|
1059 | =item $pl->kick |
|
|
1060 | |
|
|
1061 | Kicks a player out of the game. This destroys the connection. |
|
|
1062 | |
|
|
1063 | =cut |
|
|
1064 | |
|
|
1065 | sub kick { |
|
|
1066 | my ($pl, $kicker) = @_; |
|
|
1067 | |
|
|
1068 | $pl->invoke (cf::EVENT_PLAYER_KICK, $kicker); |
|
|
1069 | $pl->killer ("kicked"); |
|
|
1070 | $pl->ns->destroy; |
1049 | } |
1071 | } |
1050 | |
1072 | |
1051 | =item cf::player::list_logins |
1073 | =item cf::player::list_logins |
1052 | |
1074 | |
1053 | Returns am arrayref of all valid playernames in the system, can take a |
1075 | Returns am arrayref of all valid playernames in the system, can take a |
… | |
… | |
1298 | my ($self) = @_; |
1320 | my ($self) = @_; |
1299 | |
1321 | |
1300 | utf8::encode (my $save = $self->save_path); |
1322 | utf8::encode (my $save = $self->save_path); |
1301 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1323 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1302 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1324 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1303 | |
|
|
1304 | #d#TODO remove .map and also nuke |
|
|
1305 | $save =~ s/\.map// or return;#d# |
|
|
1306 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save;#d# |
|
|
1307 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst";#d# |
|
|
1308 | } |
1325 | } |
1309 | |
1326 | |
1310 | sub load_header_from($) { |
1327 | sub load_header_from($) { |
1311 | my ($self, $path) = @_; |
1328 | my ($self, $path) = @_; |
1312 | |
1329 | |
… | |
… | |
1600 | return if $self->players; |
1617 | return if $self->players; |
1601 | return if $self->isa ("ext::map_per_player");#d# |
1618 | return if $self->isa ("ext::map_per_player");#d# |
1602 | |
1619 | |
1603 | warn "resetting map ", $self->path;#d# |
1620 | warn "resetting map ", $self->path;#d# |
1604 | |
1621 | |
|
|
1622 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
1623 | |
|
|
1624 | # need to save uniques path |
|
|
1625 | unless ($self->{deny_save}) { |
|
|
1626 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
|
|
1627 | |
|
|
1628 | $self->_save_objects ($uniq, cf::IO_UNIQUES) |
|
|
1629 | if $uniq; |
|
|
1630 | } |
|
|
1631 | |
1605 | delete $cf::MAP{$self->path}; |
1632 | delete $cf::MAP{$self->path}; |
1606 | |
1633 | |
1607 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
1608 | $self->clear; |
1634 | $self->clear; |
1609 | |
1635 | |
1610 | $_->clear_links_to ($self) for values %cf::MAP; |
1636 | $_->clear_links_to ($self) for values %cf::MAP; |
1611 | |
1637 | |
1612 | $self->unlink_save; |
1638 | $self->unlink_save; |
… | |
… | |
1802 | $map->load; |
1828 | $map->load; |
1803 | $map->load_diag; |
1829 | $map->load_diag; |
1804 | |
1830 | |
1805 | return unless $self->contr->active; |
1831 | return unless $self->contr->active; |
1806 | $self->activate_recursive; |
1832 | $self->activate_recursive; |
|
|
1833 | |
|
|
1834 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
1807 | $self->enter_map ($map, $x, $y); |
1835 | $self->enter_map ($map, $x, $y); |
1808 | } |
1836 | } |
1809 | |
1837 | |
1810 | cf::player->attach ( |
1838 | cf::player->attach ( |
1811 | on_logout => sub { |
1839 | on_logout => sub { |
… | |
… | |
1824 | # try to abort aborted map switching on player login :) |
1852 | # try to abort aborted map switching on player login :) |
1825 | # should happen only on crashes |
1853 | # should happen only on crashes |
1826 | if ($pl->ob->{_link_pos}) { |
1854 | if ($pl->ob->{_link_pos}) { |
1827 | $pl->ob->enter_link; |
1855 | $pl->ob->enter_link; |
1828 | (async { |
1856 | (async { |
1829 | # we need this sleep as the login has a concurrent enter_exit running |
|
|
1830 | # and this sleep increases chances of the player not ending up in scorn |
|
|
1831 | $pl->ob->reply (undef, |
1857 | $pl->ob->reply (undef, |
1832 | "There was an internal problem at your last logout, " |
1858 | "There was an internal problem at your last logout, " |
1833 | . "the server will try to bring you to your intended destination in a second.", |
1859 | . "the server will try to bring you to your intended destination in a second.", |
1834 | cf::NDI_RED); |
1860 | cf::NDI_RED); |
|
|
1861 | # we need this sleep as the login has a concurrent enter_exit running |
|
|
1862 | # and this sleep increases chances of the player not ending up in scorn |
1835 | Coro::Timer::sleep 1; |
1863 | Coro::Timer::sleep 1; |
1836 | $pl->ob->leave_link; |
1864 | $pl->ob->leave_link; |
1837 | })->prio (2); |
1865 | })->prio (2); |
1838 | } |
1866 | } |
1839 | }, |
1867 | }, |
… | |
… | |
2176 | The parameter C<$family> should best start with the name of the extension |
2204 | The parameter C<$family> should best start with the name of the extension |
2177 | using it, it should be unique. |
2205 | using it, it should be unique. |
2178 | |
2206 | |
2179 | =over 4 |
2207 | =over 4 |
2180 | |
2208 | |
2181 | =item $hashref = cf::db_get $family |
|
|
2182 | |
|
|
2183 | Return a hashref for use by the extension C<$family>, which can be |
|
|
2184 | modified. After modifications, you have to call C<cf::db_dirty> or |
|
|
2185 | C<cf::db_sync>. |
|
|
2186 | |
|
|
2187 | =item $value = cf::db_get $family => $key |
2209 | =item $value = cf::db_get $family => $key |
2188 | |
2210 | |
2189 | Returns a single value from the database |
2211 | Returns a single value from the database. |
2190 | |
|
|
2191 | =item cf::db_put $family => $hashref |
|
|
2192 | |
|
|
2193 | Stores the given family hashref into the database. Updates are delayed, if |
|
|
2194 | you want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
2195 | |
2212 | |
2196 | =item cf::db_put $family => $key => $value |
2213 | =item cf::db_put $family => $key => $value |
2197 | |
2214 | |
2198 | Stores the given C<$value> in the family hash. Updates are delayed, if you |
2215 | Stores the given C<$value> in the family. |
2199 | want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
2200 | |
|
|
2201 | =item cf::db_dirty |
|
|
2202 | |
|
|
2203 | Marks the database as dirty, to be updated at a later time. |
|
|
2204 | |
|
|
2205 | =item cf::db_sync |
|
|
2206 | |
|
|
2207 | Immediately write the database to disk I<if it is dirty>. |
|
|
2208 | |
2216 | |
2209 | =cut |
2217 | =cut |
2210 | |
2218 | |
2211 | our $DB; |
2219 | our $DB; |
2212 | |
2220 | |
2213 | { |
2221 | sub db_init { |
|
|
2222 | unless ($DB) { |
|
|
2223 | $DB = BDB::db_create $DB_ENV; |
|
|
2224 | |
|
|
2225 | cf::sync_job { |
|
|
2226 | eval { |
|
|
2227 | $DB->set_flags (BDB::CHKSUM); |
|
|
2228 | |
|
|
2229 | BDB::db_open $DB, undef, "db", undef, BDB::BTREE, |
|
|
2230 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
|
|
2231 | cf::cleanup "db_open(db): $!" if $!; |
|
|
2232 | }; |
|
|
2233 | cf::cleanup "db_open(db): $@" if $@; |
|
|
2234 | }; |
|
|
2235 | |
2214 | my $path = cf::localdir . "/database.pst"; |
2236 | my $path = cf::localdir . "/database.pst"; |
|
|
2237 | if (stat $path) { |
|
|
2238 | cf::sync_job { |
|
|
2239 | my $pst = Storable::retrieve $path; |
2215 | |
2240 | |
2216 | sub db_load() { |
2241 | cf::db_put (board => data => $pst->{board}); |
2217 | $DB = stat $path ? Storable::retrieve $path : { }; |
2242 | cf::db_put (guildrules => data => $pst->{guildrules}); |
2218 | } |
2243 | cf::db_put (rent => balance => $pst->{rent}{balance}); |
|
|
2244 | BDB::db_env_txn_checkpoint $DB_ENV; |
2219 | |
2245 | |
2220 | my $pid; |
2246 | unlink $path; |
2221 | |
|
|
2222 | sub db_save() { |
|
|
2223 | waitpid $pid, 0 if $pid; |
|
|
2224 | if (0 == ($pid = fork)) { |
|
|
2225 | $DB->{_meta}{version} = 1; |
|
|
2226 | Storable::nstore $DB, "$path~"; |
|
|
2227 | rename "$path~", $path; |
|
|
2228 | cf::_exit 0 if defined $pid; |
|
|
2229 | } |
2247 | }; |
2230 | } |
|
|
2231 | |
|
|
2232 | my $dirty; |
|
|
2233 | |
|
|
2234 | sub db_sync() { |
|
|
2235 | db_save if $dirty; |
|
|
2236 | undef $dirty; |
|
|
2237 | } |
|
|
2238 | |
|
|
2239 | my $idle = Event->idle ( |
|
|
2240 | reentrant => 0, |
|
|
2241 | min => 10, |
|
|
2242 | max => 20, |
|
|
2243 | repeat => 0, |
|
|
2244 | data => WF_AUTOCANCEL, |
|
|
2245 | cb => \&db_sync, |
|
|
2246 | ); |
|
|
2247 | |
|
|
2248 | sub db_dirty() { |
|
|
2249 | $dirty = 1; |
|
|
2250 | $idle->start; |
|
|
2251 | } |
|
|
2252 | |
|
|
2253 | sub db_get($;$) { |
|
|
2254 | @_ >= 2 |
|
|
2255 | ? $DB->{$_[0]}{$_[1]} |
|
|
2256 | : ($DB->{$_[0]} ||= { }) |
|
|
2257 | } |
|
|
2258 | |
|
|
2259 | sub db_put($$;$) { |
|
|
2260 | if (@_ >= 3) { |
|
|
2261 | $DB->{$_[0]}{$_[1]} = $_[2]; |
|
|
2262 | } else { |
|
|
2263 | $DB->{$_[0]} = $_[1]; |
|
|
2264 | } |
|
|
2265 | db_dirty; |
|
|
2266 | } |
|
|
2267 | |
|
|
2268 | cf::global->attach ( |
|
|
2269 | prio => 10000, |
|
|
2270 | on_cleanup => sub { |
|
|
2271 | db_sync; |
|
|
2272 | }, |
2248 | } |
2273 | ); |
2249 | } |
|
|
2250 | } |
|
|
2251 | |
|
|
2252 | sub db_get($$) { |
|
|
2253 | my $key = "$_[0]/$_[1]"; |
|
|
2254 | |
|
|
2255 | cf::sync_job { |
|
|
2256 | BDB::db_get $DB, undef, $key, my $data; |
|
|
2257 | |
|
|
2258 | $! ? () |
|
|
2259 | : Compress::LZF::sthaw $data |
|
|
2260 | } |
|
|
2261 | } |
|
|
2262 | |
|
|
2263 | sub db_put($$$) { |
|
|
2264 | BDB::dbreq_pri 4; |
|
|
2265 | BDB::db_put $DB, undef, "$_[0]/$_[1]", Compress::LZF::sfreeze_cr $_[2], 0, sub { }; |
2274 | } |
2266 | } |
2275 | |
2267 | |
2276 | ############################################################################# |
2268 | ############################################################################# |
2277 | # the server's init and main functions |
2269 | # the server's init and main functions |
2278 | |
2270 | |
2279 | sub load_resources { |
2271 | sub init_resources { |
2280 | load_regions sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2272 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2281 | or die "unable to load regions file\n";#d# |
2273 | or die "unable to load regions file\n";#d# |
2282 | } |
2274 | } |
2283 | |
2275 | |
2284 | sub cfg_load { |
2276 | sub cfg_load { |
2285 | open my $fh, "<:utf8", cf::confdir . "/config" |
2277 | open my $fh, "<:utf8", cf::confdir . "/config" |
… | |
… | |
2301 | warn $@ if $@; |
2293 | warn $@ if $@; |
2302 | } |
2294 | } |
2303 | } |
2295 | } |
2304 | |
2296 | |
2305 | sub init { |
2297 | sub init { |
2306 | load_resources; |
2298 | init_resources; |
2307 | } |
2299 | } |
2308 | |
2300 | |
2309 | sub main { |
2301 | sub main { |
2310 | # we must not ever block the main coroutine |
2302 | # we must not ever block the main coroutine |
2311 | local $Coro::idle = sub { |
2303 | local $Coro::idle = sub { |
… | |
… | |
2314 | Event::one_event; |
2306 | Event::one_event; |
2315 | })->prio (Coro::PRIO_MAX); |
2307 | })->prio (Coro::PRIO_MAX); |
2316 | }; |
2308 | }; |
2317 | |
2309 | |
2318 | cfg_load; |
2310 | cfg_load; |
2319 | db_load; |
2311 | db_init; |
2320 | load_extensions; |
2312 | load_extensions; |
2321 | |
2313 | |
2322 | $TICK_WATCHER->start; |
2314 | $TICK_WATCHER->start; |
2323 | Event::loop; |
2315 | Event::loop; |
2324 | } |
2316 | } |
… | |
… | |
2363 | my $map = $cf::MAP{$path} or next; |
2355 | my $map = $cf::MAP{$path} or next; |
2364 | $map->valid or next; |
2356 | $map->valid or next; |
2365 | $map->save; |
2357 | $map->save; |
2366 | } |
2358 | } |
2367 | warn "end emergency map save\n"; |
2359 | warn "end emergency map save\n"; |
|
|
2360 | |
|
|
2361 | warn "begin emergency database checkpoint\n"; |
|
|
2362 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
2363 | warn "end emergency database checkpoint\n"; |
2368 | }; |
2364 | }; |
2369 | |
2365 | |
2370 | warn "leave emergency perl save\n"; |
2366 | warn "leave emergency perl save\n"; |
|
|
2367 | } |
|
|
2368 | |
|
|
2369 | sub post_cleanup { |
|
|
2370 | my ($make_core) = @_; |
|
|
2371 | |
|
|
2372 | warn Carp::longmess "post_cleanup backtrace" |
|
|
2373 | if $make_core; |
2371 | } |
2374 | } |
2372 | |
2375 | |
2373 | sub reload() { |
2376 | sub reload() { |
2374 | # can/must only be called in main |
2377 | # can/must only be called in main |
2375 | if ($Coro::current != $Coro::main) { |
2378 | if ($Coro::current != $Coro::main) { |
… | |
… | |
2377 | return; |
2380 | return; |
2378 | } |
2381 | } |
2379 | |
2382 | |
2380 | warn "reloading..."; |
2383 | warn "reloading..."; |
2381 | |
2384 | |
2382 | warn "cancelling server ticker"; |
2385 | warn "entering sync_job"; |
2383 | $TICK_WATCHER->cancel; |
|
|
2384 | |
2386 | |
|
|
2387 | cf::sync_job { |
|
|
2388 | cf::write_runtime; # external watchdog should not bark |
2385 | cf::emergency_save; |
2389 | cf::emergency_save; |
|
|
2390 | cf::write_runtime; # external watchdog should not bark |
2386 | |
2391 | |
2387 | eval { |
2392 | warn "syncing database to disk"; |
|
|
2393 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
2394 | |
2388 | # if anything goes wrong in here, we should simply crash as we already saved |
2395 | # if anything goes wrong in here, we should simply crash as we already saved |
2389 | |
|
|
2390 | warn "syncing database to disk"; |
|
|
2391 | cf::db_sync; |
|
|
2392 | |
2396 | |
2393 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2397 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2394 | for (Event::all_watchers) { |
2398 | for (Event::all_watchers) { |
2395 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2399 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2396 | } |
2400 | } |
2397 | |
2401 | |
2398 | warn "flushing outstanding aio requests"; |
2402 | warn "flushing outstanding aio requests"; |
2399 | for (;;) { |
2403 | for (;;) { |
|
|
2404 | BDB::flush; |
2400 | IO::AIO::flush; |
2405 | IO::AIO::flush; |
2401 | Coro::cede; |
2406 | Coro::cede; |
2402 | last unless IO::AIO::nreqs; |
2407 | last unless IO::AIO::nreqs || BDB::nreqs; |
2403 | warn "iterate..."; |
2408 | warn "iterate..."; |
2404 | } |
2409 | } |
2405 | |
2410 | |
2406 | warn "cancelling all extension coros"; |
2411 | warn "cancelling all extension coros"; |
2407 | $_->cancel for values %EXT_CORO; |
2412 | $_->cancel for values %EXT_CORO; |
… | |
… | |
2462 | require cf; |
2467 | require cf; |
2463 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2468 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2464 | |
2469 | |
2465 | warn "loading config and database again"; |
2470 | warn "loading config and database again"; |
2466 | cf::cfg_load; |
2471 | cf::cfg_load; |
2467 | cf::db_load; |
|
|
2468 | |
2472 | |
2469 | warn "loading extensions"; |
2473 | warn "loading extensions"; |
2470 | cf::load_extensions; |
2474 | cf::load_extensions; |
2471 | |
2475 | |
2472 | warn "reattaching attachments to objects/players"; |
2476 | warn "reattaching attachments to objects/players"; |
2473 | _global_reattach; |
2477 | _global_reattach; |
2474 | warn "reattaching attachments to maps"; |
2478 | warn "reattaching attachments to maps"; |
2475 | reattach $_ for values %MAP; |
2479 | reattach $_ for values %MAP; |
2476 | |
2480 | |
2477 | warn "loading reloadable resources"; |
2481 | warn "loading reloadable resources"; |
2478 | load_resources; |
2482 | init_resources; |
2479 | |
2483 | |
2480 | warn "restarting server ticker"; |
2484 | warn "leaving sync_job"; |
2481 | |
2485 | |
2482 | $TICK_WATCHER->start; |
2486 | 1 |
2483 | }; |
2487 | } or do { |
2484 | |
|
|
2485 | if ($@) { |
|
|
2486 | warn $@; |
2488 | warn $@; |
2487 | warn "error while reloading, exiting."; |
2489 | warn "error while reloading, exiting."; |
2488 | exit 1; |
2490 | exit 1; |
2489 | } |
2491 | }; |
2490 | |
2492 | |
2491 | warn "reloaded"; |
2493 | warn "reloaded"; |
2492 | }; |
2494 | }; |
2493 | |
2495 | |
2494 | our $RELOAD_WATCHER; # used only during reload |
2496 | our $RELOAD_WATCHER; # used only during reload |
… | |
… | |
2535 | |
2537 | |
2536 | cf::server_tick; # one server iteration |
2538 | cf::server_tick; # one server iteration |
2537 | $RUNTIME += $TICK; |
2539 | $RUNTIME += $TICK; |
2538 | $NEXT_TICK += $TICK; |
2540 | $NEXT_TICK += $TICK; |
2539 | |
2541 | |
|
|
2542 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
|
|
2543 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
|
|
2544 | Coro::async_pool { |
|
|
2545 | write_runtime |
|
|
2546 | or warn "ERROR: unable to write runtime file: $!"; |
|
|
2547 | }; |
|
|
2548 | } |
|
|
2549 | |
2540 | $WAIT_FOR_TICK->broadcast; |
2550 | $WAIT_FOR_TICK->broadcast; |
2541 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2551 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2542 | |
2552 | |
2543 | Event::sweep; |
|
|
2544 | Coro::cede_notself; |
|
|
2545 | |
|
|
2546 | # my $AFTER = Event::time; |
2553 | # my $AFTER = Event::time; |
2547 | # warn $AFTER - $NOW;#d# |
2554 | # warn $AFTER - $NOW;#d# |
2548 | |
2555 | |
2549 | # if we are delayed by four ticks or more, skip them all |
2556 | # if we are delayed by four ticks or more, skip them all |
2550 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2557 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
… | |
… | |
2552 | $TICK_WATCHER->at ($NEXT_TICK); |
2559 | $TICK_WATCHER->at ($NEXT_TICK); |
2553 | $TICK_WATCHER->start; |
2560 | $TICK_WATCHER->start; |
2554 | }, |
2561 | }, |
2555 | ); |
2562 | ); |
2556 | |
2563 | |
|
|
2564 | { |
|
|
2565 | BDB::max_poll_time $TICK * 0.1; |
|
|
2566 | $BDB_POLL_WATCHER = Event->io ( |
|
|
2567 | reentrant => 0, |
|
|
2568 | fd => BDB::poll_fileno, |
|
|
2569 | poll => 'r', |
|
|
2570 | prio => 0, |
|
|
2571 | data => WF_AUTOCANCEL, |
|
|
2572 | cb => \&BDB::poll_cb, |
|
|
2573 | ); |
|
|
2574 | BDB::min_parallel 8; |
|
|
2575 | |
|
|
2576 | BDB::set_sync_prepare { |
|
|
2577 | my $status; |
|
|
2578 | my $current = $Coro::current; |
|
|
2579 | ( |
|
|
2580 | sub { |
|
|
2581 | $status = $!; |
|
|
2582 | $current->ready; undef $current; |
|
|
2583 | }, |
|
|
2584 | sub { |
|
|
2585 | Coro::schedule while defined $current; |
|
|
2586 | $! = $status; |
|
|
2587 | }, |
|
|
2588 | ) |
|
|
2589 | }; |
|
|
2590 | |
|
|
2591 | unless ($DB_ENV) { |
|
|
2592 | $DB_ENV = BDB::db_env_create; |
|
|
2593 | |
|
|
2594 | cf::sync_job { |
|
|
2595 | eval { |
|
|
2596 | BDB::db_env_open |
|
|
2597 | $DB_ENV, |
|
|
2598 | $BDB_ENV_DIR, |
|
|
2599 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
|
|
2600 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
|
|
2601 | 0666; |
|
|
2602 | |
|
|
2603 | cf::cleanup "db_env_open($BDB_ENV_DIR): $!" if $!; |
|
|
2604 | |
|
|
2605 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
|
|
2606 | $DB_ENV->set_lk_detect; |
|
|
2607 | }; |
|
|
2608 | |
|
|
2609 | cf::cleanup "db_env_open(db): $@" if $@; |
|
|
2610 | }; |
|
|
2611 | } |
|
|
2612 | } |
|
|
2613 | |
|
|
2614 | { |
|
|
2615 | IO::AIO::min_parallel 8; |
|
|
2616 | |
|
|
2617 | undef $Coro::AIO::WATCHER; |
2557 | IO::AIO::max_poll_time $TICK * 0.1; |
2618 | IO::AIO::max_poll_time $TICK * 0.1; |
2558 | |
|
|
2559 | undef $Coro::AIO::WATCHER; |
|
|
2560 | $AIO_POLL_WATCHER = Event->io ( |
2619 | $AIO_POLL_WATCHER = Event->io ( |
2561 | reentrant => 0, |
2620 | reentrant => 0, |
|
|
2621 | data => WF_AUTOCANCEL, |
2562 | fd => IO::AIO::poll_fileno, |
2622 | fd => IO::AIO::poll_fileno, |
2563 | poll => 'r', |
2623 | poll => 'r', |
2564 | prio => 6, |
2624 | prio => 6, |
2565 | data => WF_AUTOCANCEL, |
|
|
2566 | cb => \&IO::AIO::poll_cb, |
2625 | cb => \&IO::AIO::poll_cb, |
2567 | ); |
2626 | ); |
2568 | |
2627 | } |
2569 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
|
|
2570 | reentrant => 0, |
|
|
2571 | data => WF_AUTOCANCEL, |
|
|
2572 | after => 1, |
|
|
2573 | interval => 10, |
|
|
2574 | prio => 6, # keep it lowest so it acts like a watchdog |
|
|
2575 | cb => Coro::unblock_sub { |
|
|
2576 | write_runtime |
|
|
2577 | or warn "ERROR: unable to write runtime file: $!"; |
|
|
2578 | }, |
|
|
2579 | ); |
|
|
2580 | |
2628 | |
2581 | END { cf::emergency_save } |
2629 | END { cf::emergency_save } |
2582 | |
2630 | |
2583 | 1 |
2631 | 1 |
2584 | |
2632 | |