… | |
… | |
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; # 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; |
… | |
… | |
358 | # 60 allows for the watchdog to kill the server. |
359 | # 60 allows for the watchdog to kill the server. |
359 | |
360 | |
360 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
361 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
361 | and return; |
362 | and return; |
362 | |
363 | |
|
|
364 | # always fsync - this file is important |
363 | aio_fsync $fh |
365 | aio_fsync $fh |
364 | and return; |
366 | and return; |
365 | |
367 | |
366 | close $fh |
368 | close $fh |
367 | or return; |
369 | or return; |
… | |
… | |
752 | $filename, length $$rdata, scalar @$objs; |
754 | $filename, length $$rdata, scalar @$objs; |
753 | |
755 | |
754 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
756 | if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { |
755 | chmod SAVE_MODE, $fh; |
757 | chmod SAVE_MODE, $fh; |
756 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
758 | aio_write $fh, 0, (length $$rdata), $$rdata, 0; |
757 | aio_fsync $fh; |
759 | aio_fsync $fh if $cf::USE_FSYNC; |
758 | close $fh; |
760 | close $fh; |
759 | |
761 | |
760 | if (@$objs) { |
762 | if (@$objs) { |
761 | 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) { |
762 | chmod SAVE_MODE, $fh; |
764 | chmod SAVE_MODE, $fh; |
763 | my $data = Storable::nfreeze { version => 1, objs => $objs }; |
765 | my $data = Storable::nfreeze { version => 1, objs => $objs }; |
764 | aio_write $fh, 0, (length $data), $data, 0; |
766 | aio_write $fh, 0, (length $data), $data, 0; |
765 | aio_fsync $fh; |
767 | aio_fsync $fh if $cf::USE_FSYNC; |
766 | close $fh; |
768 | close $fh; |
767 | aio_rename "$filename.pst~", "$filename.pst"; |
769 | aio_rename "$filename.pst~", "$filename.pst"; |
768 | } |
770 | } |
769 | } else { |
771 | } else { |
770 | aio_unlink "$filename.pst"; |
772 | aio_unlink "$filename.pst"; |
… | |
… | |
776 | } |
778 | } |
777 | } else { |
779 | } else { |
778 | aio_unlink $filename; |
780 | aio_unlink $filename; |
779 | aio_unlink "$filename.pst"; |
781 | aio_unlink "$filename.pst"; |
780 | } |
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 | } |
781 | } |
789 | } |
782 | } |
790 | } |
783 | |
791 | |
784 | sub object_freezer_as_string { |
792 | sub object_freezer_as_string { |
785 | my ($rdata, $objs) = @_; |
793 | my ($rdata, $objs) = @_; |
… | |
… | |
791 | |
799 | |
792 | sub object_thawer_load { |
800 | sub object_thawer_load { |
793 | my ($filename) = @_; |
801 | my ($filename) = @_; |
794 | |
802 | |
795 | 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$//; |
796 | |
807 | |
797 | (aio_load $filename, $data) >= 0 |
808 | (aio_load $filename, $data) >= 0 |
798 | or return; |
809 | or return; |
799 | |
810 | |
800 | unless (aio_stat "$filename.pst") { |
811 | unless (aio_stat "$filename.pst") { |
… | |
… | |
1070 | =cut |
1081 | =cut |
1071 | |
1082 | |
1072 | sub maps($) { |
1083 | sub maps($) { |
1073 | my ($pl) = @_; |
1084 | my ($pl) = @_; |
1074 | |
1085 | |
|
|
1086 | $pl = ref $pl ? $pl->ob->name : $pl; |
|
|
1087 | |
1075 | my $files = aio_readdir playerdir $pl |
1088 | my $files = aio_readdir playerdir $pl |
1076 | or return; |
1089 | or return; |
1077 | |
1090 | |
1078 | my @paths; |
1091 | my @paths; |
1079 | |
1092 | |
1080 | for (@$files) { |
1093 | for (@$files) { |
1081 | utf8::decode $_; |
1094 | utf8::decode $_; |
1082 | next if /\.(?:pl|pst)$/; |
1095 | next if /\.(?:pl|pst)$/; |
1083 | next unless /^$PATH_SEP/o; |
1096 | next unless /^$PATH_SEP/o; |
1084 | |
1097 | |
1085 | push @paths, cf::map::normalise "~" . $pl->ob->name . "/" . $_; |
1098 | push @paths, cf::map::normalise "~$pl/$_"; |
1086 | } |
1099 | } |
1087 | |
1100 | |
1088 | \@paths |
1101 | \@paths |
1089 | } |
1102 | } |
1090 | |
1103 | |
… | |
… | |
1251 | |
1264 | |
1252 | # the original (read-only) location |
1265 | # the original (read-only) location |
1253 | sub load_path { |
1266 | sub load_path { |
1254 | my ($self) = @_; |
1267 | my ($self) = @_; |
1255 | |
1268 | |
1256 | sprintf "%s/%s/%s", cf::datadir, cf::mapdir, $self->{path} |
1269 | sprintf "%s/%s/%s.map", cf::datadir, cf::mapdir, $self->{path} |
1257 | } |
1270 | } |
1258 | |
1271 | |
1259 | # the temporary/swap location |
1272 | # the temporary/swap location |
1260 | sub save_path { |
1273 | sub save_path { |
1261 | my ($self) = @_; |
1274 | my ($self) = @_; |
1262 | |
1275 | |
1263 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1276 | (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; |
1264 | sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $path |
1277 | sprintf "%s/%s/%s.map", cf::localdir, cf::tmpdir, $path |
1265 | } |
1278 | } |
1266 | |
1279 | |
1267 | # the unique path, undef == no special unique path |
1280 | # the unique path, undef == no special unique path |
1268 | sub uniq_path { |
1281 | sub uniq_path { |
1269 | my ($self) = @_; |
1282 | my ($self) = @_; |
… | |
… | |
1285 | my ($self) = @_; |
1298 | my ($self) = @_; |
1286 | |
1299 | |
1287 | utf8::encode (my $save = $self->save_path); |
1300 | utf8::encode (my $save = $self->save_path); |
1288 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1301 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1289 | 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# |
1290 | } |
1308 | } |
1291 | |
1309 | |
1292 | sub load_header_from($) { |
1310 | sub load_header_from($) { |
1293 | my ($self, $path) = @_; |
1311 | my ($self, $path) = @_; |
1294 | |
1312 | |
1295 | utf8::encode $path; |
1313 | utf8::encode $path; |
1296 | aio_open $path, O_RDONLY, 0 |
1314 | #aio_open $path, O_RDONLY, 0 |
1297 | or return; |
1315 | # or return; |
1298 | |
1316 | |
1299 | $self->_load_header ($path) |
1317 | $self->_load_header ($path) |
1300 | or return; |
1318 | or return; |
1301 | |
1319 | |
1302 | $self->{load_path} = $path; |
1320 | $self->{load_path} = $path; |
… | |
… | |
1987 | # get handled first |
2005 | # get handled first |
1988 | my $queue = delete $ns->{query_queue} |
2006 | my $queue = delete $ns->{query_queue} |
1989 | 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 |
1990 | |
2008 | |
1991 | (shift @$queue)->[1]->($msg); |
2009 | (shift @$queue)->[1]->($msg); |
|
|
2010 | return unless $ns->valid; # temporary(?) workaround for callback destroying socket |
1992 | |
2011 | |
1993 | push @{ $ns->{query_queue} }, @$queue; |
2012 | push @{ $ns->{query_queue} }, @$queue; |
1994 | |
2013 | |
1995 | if (@{ $ns->{query_queue} } == @$queue) { |
2014 | if (@{ $ns->{query_queue} } == @$queue) { |
1996 | if (@$queue) { |
2015 | if (@$queue) { |
… | |
… | |
2253 | }, |
2272 | }, |
2254 | ); |
2273 | ); |
2255 | } |
2274 | } |
2256 | |
2275 | |
2257 | ############################################################################# |
2276 | ############################################################################# |
2258 | # 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 | } |
2259 | |
2283 | |
2260 | sub cfg_load { |
2284 | sub cfg_load { |
2261 | open my $fh, "<:utf8", cf::confdir . "/config" |
2285 | open my $fh, "<:utf8", cf::confdir . "/config" |
2262 | or return; |
2286 | or return; |
2263 | |
2287 | |
… | |
… | |
2274 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2298 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2275 | and die "WARNING: m(un)lockall failed: $!\n"; |
2299 | and die "WARNING: m(un)lockall failed: $!\n"; |
2276 | }; |
2300 | }; |
2277 | warn $@ if $@; |
2301 | warn $@ if $@; |
2278 | } |
2302 | } |
|
|
2303 | } |
|
|
2304 | |
|
|
2305 | sub init { |
|
|
2306 | load_resources; |
2279 | } |
2307 | } |
2280 | |
2308 | |
2281 | sub main { |
2309 | sub main { |
2282 | # we must not ever block the main coroutine |
2310 | # we must not ever block the main coroutine |
2283 | local $Coro::idle = sub { |
2311 | local $Coro::idle = sub { |
… | |
… | |
2444 | warn "reattaching attachments to objects/players"; |
2472 | warn "reattaching attachments to objects/players"; |
2445 | _global_reattach; |
2473 | _global_reattach; |
2446 | warn "reattaching attachments to maps"; |
2474 | warn "reattaching attachments to maps"; |
2447 | reattach $_ for values %MAP; |
2475 | reattach $_ for values %MAP; |
2448 | |
2476 | |
|
|
2477 | warn "loading reloadable resources"; |
|
|
2478 | load_resources; |
|
|
2479 | |
2449 | warn "restarting server ticker"; |
2480 | warn "restarting server ticker"; |
2450 | |
2481 | |
2451 | $TICK_WATCHER->start; |
2482 | $TICK_WATCHER->start; |
2452 | }; |
2483 | }; |
2453 | |
2484 | |