… | |
… | |
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.4 (); |
13 | use Coro 3.5 (); |
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; |
… | |
… | |
48 | our $TICK_WATCHER; |
48 | our $TICK_WATCHER; |
49 | our $AIO_POLL_WATCHER; |
49 | our $AIO_POLL_WATCHER; |
50 | our $WRITE_RUNTIME_WATCHER; |
50 | our $WRITE_RUNTIME_WATCHER; |
51 | our $NEXT_TICK; |
51 | our $NEXT_TICK; |
52 | our $NOW; |
52 | our $NOW; |
|
|
53 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
53 | |
54 | |
54 | our %CFG; |
55 | our %CFG; |
55 | |
56 | |
56 | our $UPTIME; $UPTIME ||= time; |
57 | our $UPTIME; $UPTIME ||= time; |
57 | our $RUNTIME; |
58 | our $RUNTIME; |
… | |
… | |
82 | mkdir cf::localdir . "/" . cf::tmpdir; |
83 | mkdir cf::localdir . "/" . cf::tmpdir; |
83 | mkdir cf::localdir . "/" . cf::uniquedir; |
84 | mkdir cf::localdir . "/" . cf::uniquedir; |
84 | mkdir $RANDOM_MAPS; |
85 | mkdir $RANDOM_MAPS; |
85 | |
86 | |
86 | our $EMERGENCY_POSITION; |
87 | our $EMERGENCY_POSITION; |
|
|
88 | |
|
|
89 | sub cf::map::normalise; |
87 | |
90 | |
88 | ############################################################################# |
91 | ############################################################################# |
89 | |
92 | |
90 | =head2 GLOBAL VARIABLES |
93 | =head2 GLOBAL VARIABLES |
91 | |
94 | |
… | |
… | |
349 | my $runtime = cf::localdir . "/runtime"; |
352 | my $runtime = cf::localdir . "/runtime"; |
350 | |
353 | |
351 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
354 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
352 | or return; |
355 | or return; |
353 | |
356 | |
|
|
357 | my $value = $cf::RUNTIME + 90 + 10; |
354 | my $value = $cf::RUNTIME + 1 + 10; # 10 is the runtime save interval, for a monotonic clock |
358 | # 10 is the runtime save interval, for a monotonic clock |
|
|
359 | # 60 allows for the watchdog to kill the server. |
|
|
360 | |
355 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
361 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
356 | and return; |
362 | and return; |
357 | |
363 | |
|
|
364 | # always fsync - this file is important |
358 | aio_fsync $fh |
365 | aio_fsync $fh |
359 | and return; |
366 | and return; |
360 | |
367 | |
361 | close $fh |
368 | close $fh |
362 | or return; |
369 | or return; |
… | |
… | |
747 | $filename, length $$rdata, scalar @$objs; |
754 | $filename, length $$rdata, scalar @$objs; |
748 | |
755 | |
749 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
756 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
750 | chmod SAVE_MODE, $fh; |
757 | chmod SAVE_MODE, $fh; |
751 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
758 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
752 | aio_fsync $fh; |
759 | aio_fsync $fh if $cf::USE_FSYNC; |
753 | close $fh; |
760 | close $fh; |
754 | |
761 | |
755 | if (@$objs) { |
762 | if (@$objs) { |
756 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
763 | if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { |
757 | chmod SAVE_MODE, $fh; |
764 | chmod SAVE_MODE, $fh; |
758 | my $data = Storable::nfreeze { version => 1, objs => $objs }; |
765 | my $data = Storable::nfreeze { version => 1, objs => $objs }; |
759 | aio_write $fh, 0, (length $data), $data, 0; |
766 | aio_write $fh, 0, (length $data), $data, 0; |
760 | aio_fsync $fh; |
767 | aio_fsync $fh if $cf::USE_FSYNC; |
761 | close $fh; |
768 | close $fh; |
762 | aio_rename "$filename.pst~", "$filename.pst"; |
769 | aio_rename "$filename.pst~", "$filename.pst"; |
763 | } |
770 | } |
764 | } else { |
771 | } else { |
765 | aio_unlink "$filename.pst"; |
772 | aio_unlink "$filename.pst"; |
… | |
… | |
771 | } |
778 | } |
772 | } else { |
779 | } else { |
773 | aio_unlink $filename; |
780 | aio_unlink $filename; |
774 | aio_unlink "$filename.pst"; |
781 | aio_unlink "$filename.pst"; |
775 | } |
782 | } |
|
|
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 | } |
776 | } |
789 | } |
777 | } |
790 | } |
778 | |
791 | |
779 | sub object_freezer_as_string { |
792 | sub object_freezer_as_string { |
780 | my ($rdata, $objs) = @_; |
793 | my ($rdata, $objs) = @_; |
… | |
… | |
786 | |
799 | |
787 | sub object_thawer_load { |
800 | sub object_thawer_load { |
788 | my ($filename) = @_; |
801 | my ($filename) = @_; |
789 | |
802 | |
790 | my ($data, $av); |
803 | my ($data, $av); |
|
|
804 | |
|
|
805 | #d#TODO remove .map if file does not exist |
|
|
806 | aio_stat $filename and $filename =~ s/\.map$//; |
791 | |
807 | |
792 | (aio_load $filename, $data) >= 0 |
808 | (aio_load $filename, $data) >= 0 |
793 | or return; |
809 | or return; |
794 | |
810 | |
795 | unless (aio_stat "$filename.pst") { |
811 | unless (aio_stat "$filename.pst") { |
… | |
… | |
957 | |
973 | |
958 | sub exists($) { |
974 | sub exists($) { |
959 | my ($login) = @_; |
975 | my ($login) = @_; |
960 | |
976 | |
961 | $cf::PLAYER{$login} |
977 | $cf::PLAYER{$login} |
962 | or cf::sync_job { !aio_stat $login } |
978 | or cf::sync_job { !aio_stat path $login } |
963 | } |
979 | } |
964 | |
980 | |
965 | sub find($) { |
981 | sub find($) { |
966 | return $cf::PLAYER{$_[0]} || do { |
982 | return $cf::PLAYER{$_[0]} || do { |
967 | my $login = $_[0]; |
983 | my $login = $_[0]; |
… | |
… | |
1065 | =cut |
1081 | =cut |
1066 | |
1082 | |
1067 | sub maps($) { |
1083 | sub maps($) { |
1068 | my ($pl) = @_; |
1084 | my ($pl) = @_; |
1069 | |
1085 | |
|
|
1086 | $pl = ref $pl ? $pl->ob->name : $pl; |
|
|
1087 | |
1070 | my $files = aio_readdir playerdir $pl |
1088 | my $files = aio_readdir playerdir $pl |
1071 | or return; |
1089 | or return; |
1072 | |
1090 | |
1073 | my @paths; |
1091 | my @paths; |
1074 | |
1092 | |
1075 | for (@$files) { |
1093 | for (@$files) { |
1076 | utf8::decode $_; |
1094 | utf8::decode $_; |
1077 | next if /\.(?:pl|pst)$/; |
1095 | next if /\.(?:pl|pst)$/; |
1078 | next unless /^$PATH_SEP/o; |
1096 | next unless /^$PATH_SEP/o; |
1079 | |
1097 | |
1080 | s/\.map$//; |
1098 | push @paths, cf::map::normalise "~$pl/$_"; |
1081 | push @paths, "~" . $pl->ob->name . "/" . $_; |
|
|
1082 | } |
1099 | } |
1083 | |
1100 | |
1084 | \@paths |
1101 | \@paths |
1085 | } |
1102 | } |
1086 | |
1103 | |
… | |
… | |
1121 | our $MAX_RESET = 3600; |
1138 | our $MAX_RESET = 3600; |
1122 | our $DEFAULT_RESET = 3000; |
1139 | our $DEFAULT_RESET = 3000; |
1123 | |
1140 | |
1124 | sub generate_random_map { |
1141 | sub generate_random_map { |
1125 | my ($self, $rmp) = @_; |
1142 | my ($self, $rmp) = @_; |
1126 | |
|
|
1127 | # mit "rum" bekleckern, nicht |
1143 | # mit "rum" bekleckern, nicht |
1128 | $self->_create_random_map ( |
1144 | $self->_create_random_map ( |
1129 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1145 | $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, |
1130 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1146 | $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, |
1131 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
1147 | $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, |
… | |
… | |
1138 | $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, |
1154 | $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, |
1139 | (cf::region::find $rmp->{region}), $rmp->{custom} |
1155 | (cf::region::find $rmp->{region}), $rmp->{custom} |
1140 | ) |
1156 | ) |
1141 | } |
1157 | } |
1142 | |
1158 | |
|
|
1159 | =item cf::map->register ($regex, $prio) |
|
|
1160 | |
|
|
1161 | Register a handler for the map path matching the given regex at the |
|
|
1162 | givne priority (higher is better, built-in handlers have priority 0, the |
|
|
1163 | default). |
|
|
1164 | |
|
|
1165 | =cut |
|
|
1166 | |
1143 | sub register { |
1167 | sub register { |
1144 | my (undef, $regex) = @_; |
1168 | my (undef, $regex, $prio) = @_; |
1145 | my $pkg = caller; |
1169 | my $pkg = caller; |
1146 | |
1170 | |
1147 | no strict; |
1171 | no strict; |
1148 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1172 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1149 | |
1173 | |
1150 | $EXT_MAP{$pkg} = qr<$regex>; |
1174 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1151 | } |
1175 | } |
1152 | |
1176 | |
1153 | # also paths starting with '/' |
1177 | # also paths starting with '/' |
1154 | $EXT_MAP{"cf::map"} = qr{^(?=/)}; |
1178 | $EXT_MAP{"cf::map"} = [0, qr{^(?=/)}]; |
1155 | |
1179 | |
1156 | sub thawer_merge { |
1180 | sub thawer_merge { |
1157 | my ($self, $merge) = @_; |
1181 | my ($self, $merge) = @_; |
1158 | |
1182 | |
1159 | # we have to keep some variables in memory intact |
1183 | # we have to keep some variables in memory intact |
… | |
… | |
1165 | $self->SUPER::thawer_merge ($merge); |
1189 | $self->SUPER::thawer_merge ($merge); |
1166 | } |
1190 | } |
1167 | |
1191 | |
1168 | sub normalise { |
1192 | sub normalise { |
1169 | my ($path, $base) = @_; |
1193 | my ($path, $base) = @_; |
|
|
1194 | |
|
|
1195 | $path = "$path"; # make sure its a string |
|
|
1196 | |
|
|
1197 | $path =~ s/\.map$//; |
1170 | |
1198 | |
1171 | # map plan: |
1199 | # map plan: |
1172 | # |
1200 | # |
1173 | # /! non-realised random map exit (special hack!) |
1201 | # /! non-realised random map exit (special hack!) |
1174 | # {... are special paths that are not being touched |
1202 | # {... are special paths that are not being touched |
… | |
… | |
1202 | |
1230 | |
1203 | return $path if UNIVERSAL::isa $path, "cf::map"; # already a map object |
1231 | return $path if UNIVERSAL::isa $path, "cf::map"; # already a map object |
1204 | |
1232 | |
1205 | $path = normalise $path, $base; |
1233 | $path = normalise $path, $base; |
1206 | |
1234 | |
1207 | for my $pkg (keys %EXT_MAP) { |
1235 | for my $pkg (sort { $EXT_MAP{$b}[0] <=> $EXT_MAP{$a}[0] } keys %EXT_MAP) { |
1208 | if ($path =~ $EXT_MAP{$pkg}) { |
1236 | if ($path =~ $EXT_MAP{$pkg}[1]) { |
1209 | my $self = bless cf::map::new, $pkg; |
1237 | my $self = bless cf::map::new, $pkg; |
1210 | $self->{path} = $path; $self->path ($path); |
1238 | $self->{path} = $path; $self->path ($path); |
1211 | $self->init; # pass $1 etc. |
1239 | $self->init; # pass $1 etc. |
1212 | return $self; |
1240 | return $self; |
1213 | } |
1241 | } |
1214 | } |
1242 | } |
1215 | |
1243 | |
1216 | Carp::carp "unable to resolve path '$path'."; |
1244 | Carp::carp "unable to resolve path '$path' (base '$base')."; |
1217 | () |
1245 | () |
1218 | } |
1246 | } |
1219 | |
1247 | |
1220 | sub init { |
1248 | sub init { |
1221 | my ($self) = @_; |
1249 | my ($self) = @_; |
… | |
… | |
1236 | |
1264 | |
1237 | # the original (read-only) location |
1265 | # the original (read-only) location |
1238 | sub load_path { |
1266 | sub load_path { |
1239 | my ($self) = @_; |
1267 | my ($self) = @_; |
1240 | |
1268 | |
1241 | sprintf "%s/%s/%s", cf::datadir, cf::mapdir, $self->{path} |
1269 | sprintf "%s/%s/%s.map", cf::datadir, cf::mapdir, $self->{path} |
1242 | } |
1270 | } |
1243 | |
1271 | |
1244 | # the temporary/swap location |
1272 | # the temporary/swap location |
1245 | sub save_path { |
1273 | sub save_path { |
1246 | my ($self) = @_; |
1274 | my ($self) = @_; |
1247 | |
1275 | |
1248 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1276 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1249 | sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $path |
1277 | sprintf "%s/%s/%s.map", cf::localdir, cf::tmpdir, $path |
1250 | } |
1278 | } |
1251 | |
1279 | |
1252 | # the unique path, undef == no special unique path |
1280 | # the unique path, undef == no special unique path |
1253 | sub uniq_path { |
1281 | sub uniq_path { |
1254 | my ($self) = @_; |
1282 | my ($self) = @_; |
… | |
… | |
1270 | my ($self) = @_; |
1298 | my ($self) = @_; |
1271 | |
1299 | |
1272 | utf8::encode (my $save = $self->save_path); |
1300 | utf8::encode (my $save = $self->save_path); |
1273 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1301 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1274 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1302 | 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# |
1275 | } |
1308 | } |
1276 | |
1309 | |
1277 | sub load_header_from($) { |
1310 | sub load_header_from($) { |
1278 | my ($self, $path) = @_; |
1311 | my ($self, $path) = @_; |
1279 | |
1312 | |
1280 | utf8::encode $path; |
1313 | utf8::encode $path; |
1281 | aio_open $path, O_RDONLY, 0 |
1314 | #aio_open $path, O_RDONLY, 0 |
1282 | or return; |
1315 | # or return; |
1283 | |
1316 | |
1284 | $self->_load_header ($path) |
1317 | $self->_load_header ($path) |
1285 | or return; |
1318 | or return; |
1286 | |
1319 | |
1287 | $self->{load_path} = $path; |
1320 | $self->{load_path} = $path; |
1288 | |
1321 | |
1289 | 1 |
1322 | 1 |
1290 | } |
1323 | } |
1291 | |
1324 | |
1292 | sub load_orig { |
1325 | sub load_header_orig { |
1293 | my ($self) = @_; |
1326 | my ($self) = @_; |
1294 | |
1327 | |
1295 | $self->load_header_from ($self->load_path) |
1328 | $self->load_header_from ($self->load_path) |
1296 | } |
1329 | } |
1297 | |
1330 | |
1298 | sub load_temp { |
1331 | sub load_header_temp { |
1299 | my ($self) = @_; |
1332 | my ($self) = @_; |
1300 | |
1333 | |
1301 | $self->load_header_from ($self->save_path) |
1334 | $self->load_header_from ($self->save_path) |
|
|
1335 | } |
|
|
1336 | |
|
|
1337 | sub prepare_temp { |
|
|
1338 | my ($self) = @_; |
|
|
1339 | |
|
|
1340 | $self->last_access ((delete $self->{last_access}) |
|
|
1341 | || $cf::RUNTIME); #d# |
|
|
1342 | # safety |
|
|
1343 | $self->{instantiate_time} = $cf::RUNTIME |
|
|
1344 | if $self->{instantiate_time} > $cf::RUNTIME; |
|
|
1345 | } |
|
|
1346 | |
|
|
1347 | sub prepare_orig { |
|
|
1348 | my ($self) = @_; |
|
|
1349 | |
|
|
1350 | $self->{load_original} = 1; |
|
|
1351 | $self->{instantiate_time} = $cf::RUNTIME; |
|
|
1352 | $self->last_access ($cf::RUNTIME); |
|
|
1353 | $self->instantiate; |
1302 | } |
1354 | } |
1303 | |
1355 | |
1304 | sub load_header { |
1356 | sub load_header { |
1305 | my ($self) = @_; |
1357 | my ($self) = @_; |
1306 | |
1358 | |
1307 | if ($self->load_temp) { |
1359 | if ($self->load_header_temp) { |
1308 | $self->last_access ((delete $self->{last_access}) |
1360 | $self->prepare_temp; |
1309 | || $cf::RUNTIME); #d# |
|
|
1310 | # safety |
|
|
1311 | $self->{instantiate_time} = $cf::RUNTIME |
|
|
1312 | if $self->{instantiate_time} > $cf::RUNTIME; |
|
|
1313 | } else { |
1361 | } else { |
1314 | $self->load_orig |
1362 | $self->load_header_orig |
1315 | or return; |
1363 | or return; |
1316 | |
1364 | $self->prepare_orig; |
1317 | $self->{load_original} = 1; |
|
|
1318 | $self->{instantiate_time} = $cf::RUNTIME; |
|
|
1319 | $self->last_access ($cf::RUNTIME); |
|
|
1320 | $self->instantiate; |
|
|
1321 | } |
1365 | } |
1322 | |
1366 | |
1323 | 1 |
1367 | 1 |
1324 | } |
1368 | } |
1325 | |
1369 | |
… | |
… | |
1339 | $map->{last_save} = $cf::RUNTIME; |
1383 | $map->{last_save} = $cf::RUNTIME; |
1340 | |
1384 | |
1341 | $map->load_header |
1385 | $map->load_header |
1342 | or return; |
1386 | or return; |
1343 | |
1387 | |
1344 | if ($map->should_reset) { |
1388 | if ($map->should_reset && 0) {#d#TODO# disabled, crashy (locking issue?) |
|
|
1389 | # doing this can freeze the server in a sync job, obviously |
1345 | $cf::WAIT_FOR_TICK->wait; |
1390 | #$cf::WAIT_FOR_TICK->wait; |
1346 | $map->reset; |
1391 | $map->reset; |
1347 | undef $guard; |
1392 | undef $guard; |
1348 | $map = find $path |
1393 | return find $path; |
1349 | or return; |
|
|
1350 | } |
1394 | } |
1351 | |
1395 | |
1352 | $cf::MAP{$path} = $map |
1396 | $cf::MAP{$path} = $map |
1353 | } |
1397 | } |
1354 | } |
1398 | } |
|
|
1399 | |
|
|
1400 | sub pre_load { } |
|
|
1401 | sub post_load { } |
1355 | |
1402 | |
1356 | sub load { |
1403 | sub load { |
1357 | my ($self) = @_; |
1404 | my ($self) = @_; |
1358 | |
1405 | |
|
|
1406 | local $self->{deny_reset} = 1; # loading can take a long time |
|
|
1407 | |
1359 | my $path = $self->{path}; |
1408 | my $path = $self->{path}; |
1360 | my $guard = cf::lock_acquire "map_load:$path"; |
1409 | my $guard = cf::lock_acquire "map_load:$path"; |
1361 | |
1410 | |
1362 | return if $self->in_memory != cf::MAP_SWAPPED; |
1411 | return if $self->in_memory != cf::MAP_SWAPPED; |
1363 | |
1412 | |
1364 | $self->in_memory (cf::MAP_LOADING); |
1413 | $self->in_memory (cf::MAP_LOADING); |
1365 | |
1414 | |
1366 | $self->alloc; |
1415 | $self->alloc; |
|
|
1416 | |
|
|
1417 | $self->pre_load; |
|
|
1418 | |
1367 | $self->_load_objects ($self->{load_path}, 1) |
1419 | $self->_load_objects ($self->{load_path}, 1) |
1368 | or return; |
1420 | or return; |
1369 | |
1421 | |
1370 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
1422 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
1371 | if delete $self->{load_original}; |
1423 | if delete $self->{load_original}; |
… | |
… | |
1393 | unless $self->difficulty; |
1445 | unless $self->difficulty; |
1394 | Coro::cede; |
1446 | Coro::cede; |
1395 | $self->activate; |
1447 | $self->activate; |
1396 | } |
1448 | } |
1397 | |
1449 | |
|
|
1450 | $self->post_load; |
|
|
1451 | |
1398 | $self->in_memory (cf::MAP_IN_MEMORY); |
1452 | $self->in_memory (cf::MAP_IN_MEMORY); |
1399 | } |
1453 | } |
1400 | |
1454 | |
1401 | sub customise_for { |
1455 | sub customise_for { |
1402 | my ($self, $ob) = @_; |
1456 | my ($self, $ob) = @_; |
… | |
… | |
1442 | |
1496 | |
1443 | cf::sync_job { $map->load }; |
1497 | cf::sync_job { $map->load }; |
1444 | } |
1498 | } |
1445 | |
1499 | |
1446 | our %MAP_PREFETCH; |
1500 | our %MAP_PREFETCH; |
1447 | our $MAP_PREFETCHER = Coro::async { |
1501 | our $MAP_PREFETCHER = undef; |
1448 | while () { |
|
|
1449 | for my $path (keys %MAP_PREFETCH) { |
|
|
1450 | my $map = find $path |
|
|
1451 | or next; |
|
|
1452 | $map->load; |
|
|
1453 | |
|
|
1454 | delete $MAP_PREFETCH{$path}; |
|
|
1455 | } |
|
|
1456 | Coro::schedule; |
|
|
1457 | } |
|
|
1458 | }; |
|
|
1459 | |
1502 | |
1460 | sub find_async { |
1503 | sub find_async { |
1461 | my ($path, $origin) = @_; |
1504 | my ($path, $origin) = @_; |
1462 | |
1505 | |
1463 | $path = normalise $path, $origin && $origin->{path}; |
1506 | $path = normalise $path, $origin && $origin->{path}; |
1464 | |
1507 | |
1465 | if (my $map = $cf::MAP{$path}) { |
1508 | if (my $map = $cf::MAP{$path}) { |
1466 | return $map if $map->in_memory == cf::MAP_IN_MEMORY; |
1509 | return $map if $map->in_memory == cf::MAP_IN_MEMORY; |
1467 | } |
1510 | } |
1468 | |
1511 | |
1469 | $MAP_PREFETCH{$path} = 0; |
1512 | undef $MAP_PREFETCH{$path}; |
|
|
1513 | $MAP_PREFETCHER ||= cf::async { |
|
|
1514 | while (%MAP_PREFETCH) { |
|
|
1515 | for my $path (keys %MAP_PREFETCH) { |
|
|
1516 | my $map = find $path |
|
|
1517 | or next; |
|
|
1518 | $map->load; |
|
|
1519 | |
|
|
1520 | delete $MAP_PREFETCH{$path}; |
|
|
1521 | } |
|
|
1522 | } |
|
|
1523 | undef $MAP_PREFETCHER; |
|
|
1524 | }; |
1470 | $MAP_PREFETCHER->ready; |
1525 | $MAP_PREFETCHER->prio (6); |
1471 | |
1526 | |
1472 | () |
1527 | () |
1473 | } |
1528 | } |
1474 | |
1529 | |
1475 | sub save { |
1530 | sub save { |
… | |
… | |
1594 | for (@$files) { |
1649 | for (@$files) { |
1595 | utf8::decode $_; |
1650 | utf8::decode $_; |
1596 | next if /\.pst$/; |
1651 | next if /\.pst$/; |
1597 | next unless /^$PATH_SEP/o; |
1652 | next unless /^$PATH_SEP/o; |
1598 | |
1653 | |
1599 | s/\.map$//; |
1654 | push @paths, cf::map::normalise $_; |
1600 | push @paths, $_; |
|
|
1601 | } |
1655 | } |
1602 | |
1656 | |
1603 | \@paths |
1657 | \@paths |
1604 | } |
1658 | } |
1605 | |
1659 | |
… | |
… | |
1699 | =cut |
1753 | =cut |
1700 | |
1754 | |
1701 | sub link_map { |
1755 | sub link_map { |
1702 | unless ($LINK_MAP) { |
1756 | unless ($LINK_MAP) { |
1703 | $LINK_MAP = cf::map::find "{link}" |
1757 | $LINK_MAP = cf::map::find "{link}" |
1704 | or do { warn "FATAL: unable to provide {link} map, exiting."; exit 1 }; |
1758 | or cf::cleanup "FATAL: unable to provide {link} map, exiting."; |
1705 | $LINK_MAP->load; |
1759 | $LINK_MAP->load; |
1706 | } |
1760 | } |
1707 | |
1761 | |
1708 | $LINK_MAP |
1762 | $LINK_MAP |
1709 | } |
1763 | } |
… | |
… | |
1716 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
1770 | return if UNIVERSAL::isa $self->map, "ext::map_link"; |
1717 | |
1771 | |
1718 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
1772 | $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] |
1719 | if $self->map; |
1773 | if $self->map; |
1720 | |
1774 | |
1721 | $self->enter_map ($LINK_MAP || link_map, 20, 20); |
1775 | $self->enter_map ($LINK_MAP || link_map, 10, 10); |
1722 | } |
1776 | } |
1723 | |
1777 | |
1724 | sub cf::object::player::leave_link { |
1778 | sub cf::object::player::leave_link { |
1725 | my ($self, $map, $x, $y) = @_; |
1779 | my ($self, $map, $x, $y) = @_; |
1726 | |
1780 | |
… | |
… | |
1793 | my ($self, $path, $x, $y) = @_; |
1847 | my ($self, $path, $x, $y) = @_; |
1794 | |
1848 | |
1795 | $self->enter_link; |
1849 | $self->enter_link; |
1796 | |
1850 | |
1797 | (async { |
1851 | (async { |
|
|
1852 | my $map = eval { |
1798 | my $map = cf::map::find $path; |
1853 | my $map = cf::map::find $path; |
1799 | $map = $map->customise_for ($self) if $map; |
1854 | $map = $map->customise_for ($self) if $map; |
1800 | |
1855 | $map |
1801 | # warn "entering ", $map->path, " at ($x, $y)\n" |
1856 | } or |
1802 | # if $map; |
|
|
1803 | |
|
|
1804 | $map or $self->message ("The exit to '" . ($path->visible_name) . "' is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1857 | $self->message ("The exit to '$path' is closed", cf::NDI_UNIQUE | cf::NDI_RED); |
1805 | |
1858 | |
1806 | $self->leave_link ($map, $x, $y); |
1859 | $self->leave_link ($map, $x, $y); |
1807 | })->prio (1); |
1860 | })->prio (1); |
1808 | } |
1861 | } |
1809 | |
1862 | |
… | |
… | |
1813 | |
1866 | |
1814 | sub parse_random_map_params { |
1867 | sub parse_random_map_params { |
1815 | my ($spec) = @_; |
1868 | my ($spec) = @_; |
1816 | |
1869 | |
1817 | my $rmp = { # defaults |
1870 | my $rmp = { # defaults |
1818 | xsize => 10, |
1871 | xsize => (cf::rndm 15, 40), |
1819 | ysize => 10, |
1872 | ysize => (cf::rndm 15, 40), |
|
|
1873 | symmetry => (cf::rndm 1, cf::SYMMETRY_XY), |
|
|
1874 | #layout => string, |
1820 | }; |
1875 | }; |
1821 | |
1876 | |
1822 | for (split /\n/, $spec) { |
1877 | for (split /\n/, $spec) { |
1823 | my ($k, $v) = split /\s+/, $_, 2; |
1878 | my ($k, $v) = split /\s+/, $_, 2; |
1824 | |
1879 | |
… | |
… | |
1828 | $rmp |
1883 | $rmp |
1829 | } |
1884 | } |
1830 | |
1885 | |
1831 | sub prepare_random_map { |
1886 | sub prepare_random_map { |
1832 | my ($exit) = @_; |
1887 | my ($exit) = @_; |
|
|
1888 | |
|
|
1889 | my $guard = cf::lock_acquire "exit_prepare:$exit"; |
1833 | |
1890 | |
1834 | # all this does is basically replace the /! path by |
1891 | # all this does is basically replace the /! path by |
1835 | # a new random map path (?random/...) with a seed |
1892 | # a new random map path (?random/...) with a seed |
1836 | # that depends on the exit object |
1893 | # that depends on the exit object |
1837 | |
1894 | |
1838 | my $rmp = parse_random_map_params $exit->msg; |
1895 | my $rmp = parse_random_map_params $exit->msg; |
1839 | |
1896 | |
1840 | if ($exit->map) { |
1897 | if ($exit->map) { |
1841 | $rmp->{region} = $exit->map->region_name; |
1898 | $rmp->{region} = $exit->region->name; |
1842 | $rmp->{origin_map} = $exit->map->path; |
1899 | $rmp->{origin_map} = $exit->map->path; |
1843 | $rmp->{origin_x} = $exit->x; |
1900 | $rmp->{origin_x} = $exit->x; |
1844 | $rmp->{origin_y} = $exit->y; |
1901 | $rmp->{origin_y} = $exit->y; |
1845 | } |
1902 | } |
1846 | |
1903 | |
1847 | $rmp->{random_seed} ||= $exit->random_seed; |
1904 | $rmp->{random_seed} ||= $exit->random_seed; |
1848 | |
1905 | |
1849 | my $data = cf::to_json $rmp; |
1906 | my $data = cf::to_json $rmp; |
1850 | my $md5 = Digest::MD5::md5_hex $data; |
1907 | my $md5 = Digest::MD5::md5_hex $data; |
|
|
1908 | my $meta = "$cf::RANDOM_MAPS/$md5.meta"; |
1851 | |
1909 | |
1852 | if (my $fh = aio_open "$cf::RANDOM_MAPS/$md5.meta", O_WRONLY | O_CREAT, 0666) { |
1910 | if (my $fh = aio_open "$meta~", O_WRONLY | O_CREAT, 0666) { |
1853 | aio_write $fh, 0, (length $data), $data, 0; |
1911 | aio_write $fh, 0, (length $data), $data, 0; |
|
|
1912 | undef $fh; |
|
|
1913 | aio_rename "$meta~", $meta; |
1854 | |
1914 | |
1855 | $exit->slaying ("?random/$md5"); |
1915 | $exit->slaying ("?random/$md5"); |
1856 | $exit->msg (undef); |
1916 | $exit->msg (undef); |
1857 | } |
1917 | } |
1858 | } |
1918 | } |
1859 | |
1919 | |
1860 | sub cf::object::player::enter_exit { |
1920 | sub cf::object::player::enter_exit { |
1861 | my ($self, $exit) = @_; |
1921 | my ($self, $exit) = @_; |
1862 | |
1922 | |
1863 | return unless $self->type == cf::PLAYER; |
1923 | return unless $self->type == cf::PLAYER; |
|
|
1924 | |
|
|
1925 | if ($exit->slaying eq "/!") { |
|
|
1926 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
|
|
1927 | cf::sync_job { prepare_random_map $exit }; |
|
|
1928 | } |
|
|
1929 | |
|
|
1930 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
|
|
1931 | my $hp = $exit->stats->hp; |
|
|
1932 | my $sp = $exit->stats->sp; |
1864 | |
1933 | |
1865 | $self->enter_link; |
1934 | $self->enter_link; |
1866 | |
1935 | |
1867 | (async { |
1936 | (async { |
1868 | $self->deactivate_recursive; # just to be sure |
1937 | $self->deactivate_recursive; # just to be sure |
1869 | unless (eval { |
1938 | unless (eval { |
1870 | prepare_random_map $exit |
1939 | $self->goto ($slaying, $hp, $sp); |
1871 | if $exit->slaying eq "/!"; |
|
|
1872 | |
|
|
1873 | my $path = new_from_path cf::map $exit->slaying, $exit->map && $exit->map->path; |
|
|
1874 | $self->goto ($path, $exit->stats->hp, $exit->stats->sp); |
|
|
1875 | |
1940 | |
1876 | 1; |
1941 | 1; |
1877 | }) { |
1942 | }) { |
1878 | $self->message ("Something went wrong deep within the crossfire server. " |
1943 | $self->message ("Something went wrong deep within the crossfire server. " |
1879 | . "I'll try to bring you back to the map you were before. " |
1944 | . "I'll try to bring you back to the map you were before. " |
… | |
… | |
1940 | # get handled first |
2005 | # get handled first |
1941 | my $queue = delete $ns->{query_queue} |
2006 | my $queue = delete $ns->{query_queue} |
1942 | or return; # be conservative, not sure how that can happen, but we saw a crash here |
2007 | or return; # be conservative, not sure how that can happen, but we saw a crash here |
1943 | |
2008 | |
1944 | (shift @$queue)->[1]->($msg); |
2009 | (shift @$queue)->[1]->($msg); |
|
|
2010 | return unless $ns->valid; # temporary(?) workaround for callback destroying socket |
1945 | |
2011 | |
1946 | push @{ $ns->{query_queue} }, @$queue; |
2012 | push @{ $ns->{query_queue} }, @$queue; |
1947 | |
2013 | |
1948 | if (@{ $ns->{query_queue} } == @$queue) { |
2014 | if (@{ $ns->{query_queue} } == @$queue) { |
1949 | if (@$queue) { |
2015 | if (@$queue) { |
… | |
… | |
2168 | sub db_sync() { |
2234 | sub db_sync() { |
2169 | db_save if $dirty; |
2235 | db_save if $dirty; |
2170 | undef $dirty; |
2236 | undef $dirty; |
2171 | } |
2237 | } |
2172 | |
2238 | |
2173 | my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, data => WF_AUTOCANCEL, cb => sub { |
2239 | my $idle = Event->idle ( |
2174 | db_sync; |
2240 | reentrant => 0, |
|
|
2241 | min => 10, |
|
|
2242 | max => 20, |
|
|
2243 | repeat => 0, |
|
|
2244 | data => WF_AUTOCANCEL, |
|
|
2245 | cb => \&db_sync, |
2175 | }); |
2246 | ); |
2176 | |
2247 | |
2177 | sub db_dirty() { |
2248 | sub db_dirty() { |
2178 | $dirty = 1; |
2249 | $dirty = 1; |
2179 | $idle->start; |
2250 | $idle->start; |
2180 | } |
2251 | } |
… | |
… | |
2201 | }, |
2272 | }, |
2202 | ); |
2273 | ); |
2203 | } |
2274 | } |
2204 | |
2275 | |
2205 | ############################################################################# |
2276 | ############################################################################# |
2206 | # the server's main() |
2277 | # the server's init and main functions |
|
|
2278 | |
|
|
2279 | sub load_resources { |
|
|
2280 | load_regions sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
|
|
2281 | or die "unable to load regions file\n";#d# |
|
|
2282 | } |
2207 | |
2283 | |
2208 | sub cfg_load { |
2284 | sub cfg_load { |
2209 | open my $fh, "<:utf8", cf::confdir . "/config" |
2285 | open my $fh, "<:utf8", cf::confdir . "/config" |
2210 | or return; |
2286 | or return; |
2211 | |
2287 | |
… | |
… | |
2224 | }; |
2300 | }; |
2225 | warn $@ if $@; |
2301 | warn $@ if $@; |
2226 | } |
2302 | } |
2227 | } |
2303 | } |
2228 | |
2304 | |
|
|
2305 | sub init { |
|
|
2306 | load_resources; |
|
|
2307 | } |
|
|
2308 | |
2229 | sub main { |
2309 | sub main { |
2230 | # we must not ever block the main coroutine |
2310 | # we must not ever block the main coroutine |
2231 | local $Coro::idle = sub { |
2311 | local $Coro::idle = sub { |
2232 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
2312 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
2233 | (async { |
2313 | (async { |
2234 | warn "BP1\n";#d# |
|
|
2235 | Event::one_event; |
2314 | Event::one_event; |
2236 | warn "BP2\n";#d# |
|
|
2237 | })->prio (Coro::PRIO_MAX); |
2315 | })->prio (Coro::PRIO_MAX); |
2238 | }; |
2316 | }; |
2239 | |
2317 | |
2240 | cfg_load; |
2318 | cfg_load; |
2241 | db_load; |
2319 | db_load; |
2242 | load_extensions; |
2320 | load_extensions; |
|
|
2321 | |
|
|
2322 | $TICK_WATCHER->start; |
2243 | Event::loop; |
2323 | Event::loop; |
2244 | } |
2324 | } |
2245 | |
2325 | |
2246 | ############################################################################# |
2326 | ############################################################################# |
2247 | # initialisation and cleanup |
2327 | # initialisation and cleanup |
2248 | |
2328 | |
2249 | # install some emergency cleanup handlers |
2329 | # install some emergency cleanup handlers |
2250 | BEGIN { |
2330 | BEGIN { |
2251 | for my $signal (qw(INT HUP TERM)) { |
2331 | for my $signal (qw(INT HUP TERM)) { |
2252 | Event->signal ( |
2332 | Event->signal ( |
|
|
2333 | reentrant => 0, |
2253 | data => WF_AUTOCANCEL, |
2334 | data => WF_AUTOCANCEL, |
2254 | signal => $signal, |
2335 | signal => $signal, |
|
|
2336 | prio => 0, |
2255 | cb => sub { |
2337 | cb => sub { |
2256 | cf::cleanup "SIG$signal"; |
2338 | cf::cleanup "SIG$signal"; |
2257 | }, |
2339 | }, |
2258 | ); |
2340 | ); |
2259 | } |
2341 | } |
2260 | } |
2342 | } |
… | |
… | |
2289 | } |
2371 | } |
2290 | |
2372 | |
2291 | sub reload() { |
2373 | sub reload() { |
2292 | # can/must only be called in main |
2374 | # can/must only be called in main |
2293 | if ($Coro::current != $Coro::main) { |
2375 | if ($Coro::current != $Coro::main) { |
2294 | warn "can only reload from main coroutine\n"; |
2376 | warn "can only reload from main coroutine"; |
2295 | return; |
2377 | return; |
2296 | } |
2378 | } |
2297 | |
2379 | |
2298 | warn "reloading..."; |
2380 | warn "reloading..."; |
2299 | |
2381 | |
2300 | warn "freezing server"; |
2382 | warn "cancelling server ticker"; |
2301 | my $guard = freeze_mainloop; |
2383 | $TICK_WATCHER->cancel; |
|
|
2384 | |
2302 | cf::emergency_save; |
2385 | cf::emergency_save; |
2303 | |
|
|
2304 | warn "sync database to disk"; |
|
|
2305 | cf::db_sync; |
|
|
2306 | IO::AIO::flush; |
|
|
2307 | |
2386 | |
2308 | eval { |
2387 | eval { |
2309 | # if anything goes wrong in here, we should simply crash as we already saved |
2388 | # if anything goes wrong in here, we should simply crash as we already saved |
2310 | |
2389 | |
|
|
2390 | warn "syncing database to disk"; |
|
|
2391 | cf::db_sync; |
|
|
2392 | |
2311 | warn "cancel all watchers"; |
2393 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2312 | for (Event::all_watchers) { |
2394 | for (Event::all_watchers) { |
2313 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2395 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2314 | } |
2396 | } |
2315 | |
2397 | |
|
|
2398 | warn "flushing outstanding aio requests"; |
|
|
2399 | for (;;) { |
|
|
2400 | IO::AIO::flush; |
|
|
2401 | Coro::cede; |
|
|
2402 | last unless IO::AIO::nreqs; |
|
|
2403 | warn "iterate..."; |
|
|
2404 | } |
|
|
2405 | |
2316 | warn "cancel all extension coros"; |
2406 | warn "cancelling all extension coros"; |
2317 | $_->cancel for values %EXT_CORO; |
2407 | $_->cancel for values %EXT_CORO; |
2318 | %EXT_CORO = (); |
2408 | %EXT_CORO = (); |
2319 | |
2409 | |
2320 | warn "remove commands"; |
2410 | warn "removing commands"; |
2321 | %COMMAND = (); |
2411 | %COMMAND = (); |
2322 | |
2412 | |
2323 | warn "remove ext commands"; |
2413 | warn "removing ext commands"; |
2324 | %EXTCMD = (); |
2414 | %EXTCMD = (); |
2325 | |
2415 | |
2326 | warn "unload/nuke all extensions"; |
2416 | warn "unloading/nuking all extensions"; |
2327 | for my $pkg (@EXTS) { |
2417 | for my $pkg (@EXTS) { |
2328 | warn "... unloading $pkg"; |
2418 | warn "... unloading $pkg"; |
2329 | |
2419 | |
2330 | if (my $cb = $pkg->can ("unload")) { |
2420 | if (my $cb = $pkg->can ("unload")) { |
2331 | eval { |
2421 | eval { |
… | |
… | |
2336 | |
2426 | |
2337 | warn "... nuking $pkg"; |
2427 | warn "... nuking $pkg"; |
2338 | Symbol::delete_package $pkg; |
2428 | Symbol::delete_package $pkg; |
2339 | } |
2429 | } |
2340 | |
2430 | |
2341 | warn "unload all perl modules loaded from $LIBDIR"; |
2431 | warn "unloading all perl modules loaded from $LIBDIR"; |
2342 | while (my ($k, $v) = each %INC) { |
2432 | while (my ($k, $v) = each %INC) { |
2343 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
2433 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
2344 | |
2434 | |
2345 | warn "removing <$k>"; |
2435 | warn "... unloading $k"; |
2346 | delete $INC{$k}; |
2436 | delete $INC{$k}; |
2347 | |
2437 | |
2348 | $k =~ s/\.pm$//; |
2438 | $k =~ s/\.pm$//; |
2349 | $k =~ s/\//::/g; |
2439 | $k =~ s/\//::/g; |
2350 | |
2440 | |
… | |
… | |
2353 | } |
2443 | } |
2354 | |
2444 | |
2355 | Symbol::delete_package $k; |
2445 | Symbol::delete_package $k; |
2356 | } |
2446 | } |
2357 | |
2447 | |
2358 | warn "get rid of safe::, as good as possible"; |
2448 | warn "getting rid of safe::, as good as possible"; |
2359 | Symbol::delete_package "safe::$_" |
2449 | Symbol::delete_package "safe::$_" |
2360 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2450 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
2361 | |
2451 | |
2362 | warn "unload cf.pm \"a bit\""; |
2452 | warn "unloading cf.pm \"a bit\""; |
2363 | delete $INC{"cf.pm"}; |
2453 | delete $INC{"cf.pm"}; |
2364 | |
2454 | |
2365 | # don't, removes xs symbols, too, |
2455 | # don't, removes xs symbols, too, |
2366 | # and global variables created in xs |
2456 | # and global variables created in xs |
2367 | #Symbol::delete_package __PACKAGE__; |
2457 | #Symbol::delete_package __PACKAGE__; |
2368 | |
2458 | |
|
|
2459 | warn "unload completed, starting to reload now"; |
|
|
2460 | |
2369 | warn "reloading cf.pm"; |
2461 | warn "reloading cf.pm"; |
2370 | require cf; |
2462 | require cf; |
2371 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2463 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2372 | |
2464 | |
2373 | warn "load config and database again"; |
2465 | warn "loading config and database again"; |
2374 | cf::cfg_load; |
2466 | cf::cfg_load; |
2375 | cf::db_load; |
2467 | cf::db_load; |
2376 | |
2468 | |
2377 | warn "load extensions"; |
2469 | warn "loading extensions"; |
2378 | cf::load_extensions; |
2470 | cf::load_extensions; |
2379 | |
2471 | |
2380 | warn "reattach attachments to objects/players"; |
2472 | warn "reattaching attachments to objects/players"; |
2381 | _global_reattach; |
2473 | _global_reattach; |
2382 | warn "reattach attachments to maps"; |
2474 | warn "reattaching attachments to maps"; |
2383 | reattach $_ for values %MAP; |
2475 | reattach $_ for values %MAP; |
|
|
2476 | |
|
|
2477 | warn "loading reloadable resources"; |
|
|
2478 | load_resources; |
|
|
2479 | |
|
|
2480 | warn "restarting server ticker"; |
|
|
2481 | |
|
|
2482 | $TICK_WATCHER->start; |
2384 | }; |
2483 | }; |
2385 | |
2484 | |
2386 | if ($@) { |
2485 | if ($@) { |
2387 | warn $@; |
2486 | warn $@; |
2388 | warn "error while reloading, exiting."; |
2487 | warn "error while reloading, exiting."; |
… | |
… | |
2401 | $who->message ("reloading server."); |
2500 | $who->message ("reloading server."); |
2402 | |
2501 | |
2403 | # doing reload synchronously and two reloads happen back-to-back, |
2502 | # doing reload synchronously and two reloads happen back-to-back, |
2404 | # coro crashes during coro_state_free->destroy here. |
2503 | # coro crashes during coro_state_free->destroy here. |
2405 | |
2504 | |
2406 | $RELOAD_WATCHER ||= Event->timer (after => 0, data => WF_AUTOCANCEL, cb => sub { |
2505 | $RELOAD_WATCHER ||= Event->timer ( |
|
|
2506 | reentrant => 0, |
|
|
2507 | after => 0, |
|
|
2508 | data => WF_AUTOCANCEL, |
|
|
2509 | cb => sub { |
2407 | reload; |
2510 | reload; |
2408 | undef $RELOAD_WATCHER; |
2511 | undef $RELOAD_WATCHER; |
|
|
2512 | }, |
2409 | }); |
2513 | ); |
2410 | } |
2514 | } |
2411 | }; |
2515 | }; |
2412 | |
2516 | |
2413 | unshift @INC, $LIBDIR; |
2517 | unshift @INC, $LIBDIR; |
|
|
2518 | |
|
|
2519 | my $bug_warning = 0; |
2414 | |
2520 | |
2415 | $TICK_WATCHER = Event->timer ( |
2521 | $TICK_WATCHER = Event->timer ( |
2416 | reentrant => 0, |
2522 | reentrant => 0, |
|
|
2523 | parked => 1, |
2417 | prio => 0, |
2524 | prio => 0, |
2418 | at => $NEXT_TICK || $TICK, |
2525 | at => $NEXT_TICK || $TICK, |
2419 | data => WF_AUTOCANCEL, |
2526 | data => WF_AUTOCANCEL, |
2420 | cb => sub { |
2527 | cb => sub { |
|
|
2528 | if ($Coro::current != $Coro::main) { |
|
|
2529 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
|
|
2530 | unless ++$bug_warning > 10; |
|
|
2531 | return; |
|
|
2532 | } |
|
|
2533 | |
2421 | $NOW = Event::time; |
2534 | $NOW = Event::time; |
2422 | |
2535 | |
2423 | cf::server_tick; # one server iteration |
2536 | cf::server_tick; # one server iteration |
2424 | $RUNTIME += $TICK; |
2537 | $RUNTIME += $TICK; |
2425 | $NEXT_TICK += $TICK; |
2538 | $NEXT_TICK += $TICK; |
2426 | |
2539 | |
2427 | $WAIT_FOR_TICK->broadcast; |
2540 | $WAIT_FOR_TICK->broadcast; |
2428 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2541 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
|
|
2542 | |
|
|
2543 | Event::sweep; |
|
|
2544 | Coro::cede_notself; |
|
|
2545 | |
|
|
2546 | # my $AFTER = Event::time; |
|
|
2547 | # warn $AFTER - $NOW;#d# |
2429 | |
2548 | |
2430 | # if we are delayed by four ticks or more, skip them all |
2549 | # if we are delayed by four ticks or more, skip them all |
2431 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2550 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2432 | |
2551 | |
2433 | $TICK_WATCHER->at ($NEXT_TICK); |
2552 | $TICK_WATCHER->at ($NEXT_TICK); |
2434 | $TICK_WATCHER->start; |
2553 | $TICK_WATCHER->start; |
2435 | }, |
2554 | }, |
2436 | ); |
2555 | ); |
2437 | |
2556 | |
2438 | IO::AIO::max_poll_time $TICK * 0.2; |
2557 | IO::AIO::max_poll_time $TICK * 0.1; |
2439 | |
2558 | |
|
|
2559 | undef $Coro::AIO::WATCHER; |
2440 | $AIO_POLL_WATCHER = Event->io ( |
2560 | $AIO_POLL_WATCHER = Event->io ( |
|
|
2561 | reentrant => 0, |
2441 | fd => IO::AIO::poll_fileno, |
2562 | fd => IO::AIO::poll_fileno, |
2442 | poll => 'r', |
2563 | poll => 'r', |
2443 | prio => 5, |
2564 | prio => 6, |
2444 | data => WF_AUTOCANCEL, |
2565 | data => WF_AUTOCANCEL, |
2445 | cb => \&IO::AIO::poll_cb, |
2566 | cb => \&IO::AIO::poll_cb, |
2446 | ); |
2567 | ); |
2447 | |
2568 | |
2448 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
2569 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
|
|
2570 | reentrant => 0, |
2449 | data => WF_AUTOCANCEL, |
2571 | data => WF_AUTOCANCEL, |
2450 | after => 0, |
2572 | after => 1, |
2451 | interval => 10, |
2573 | interval => 10, |
2452 | cb => sub { |
2574 | prio => 6, # keep it lowest so it acts like a watchdog |
2453 | (Coro::unblock_sub { |
2575 | cb => Coro::unblock_sub { |
2454 | write_runtime |
2576 | write_runtime |
2455 | or warn "ERROR: unable to write runtime file: $!"; |
2577 | or warn "ERROR: unable to write runtime file: $!"; |
2456 | })->(); |
|
|
2457 | }, |
2578 | }, |
2458 | ); |
2579 | ); |
2459 | |
2580 | |
2460 | END { cf::emergency_save } |
2581 | END { cf::emergency_save } |
2461 | |
2582 | |