… | |
… | |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Symbol; |
6 | use Symbol; |
7 | use List::Util; |
7 | use List::Util; |
8 | use Storable; |
8 | use Storable; |
|
|
9 | use Event; |
9 | use Opcode; |
10 | use Opcode; |
10 | use Safe; |
11 | use Safe; |
11 | use Safe::Hole; |
12 | use Safe::Hole; |
12 | |
13 | |
13 | use Coro 3.5 (); |
14 | use Coro 3.61 (); |
|
|
15 | use Coro::State; |
14 | use Coro::Event; |
16 | use Coro::Event; |
15 | use Coro::Timer; |
17 | use Coro::Timer; |
16 | use Coro::Signal; |
18 | use Coro::Signal; |
17 | use Coro::Semaphore; |
19 | use Coro::Semaphore; |
18 | use Coro::AIO; |
20 | use Coro::AIO; |
|
|
21 | use Coro::Storable; |
19 | |
22 | |
20 | use BDB (); |
23 | use BDB (); |
21 | use Data::Dumper; |
24 | use Data::Dumper; |
22 | use Digest::MD5; |
25 | use Digest::MD5; |
23 | use Fcntl; |
26 | use Fcntl; |
|
|
27 | use YAML::Syck (); |
24 | use IO::AIO 2.32 (); |
28 | use IO::AIO 2.32 (); |
25 | use YAML::Syck (); |
|
|
26 | use Time::HiRes; |
29 | use Time::HiRes; |
|
|
30 | use Compress::LZF; |
27 | |
31 | |
|
|
32 | # configure various modules to our taste |
|
|
33 | # |
|
|
34 | $Storable::canonical = 1; # reduce rsync transfers |
|
|
35 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
|
|
36 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
|
|
37 | |
28 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
38 | $Event::Eval = 1; # no idea why this is required, but it is |
29 | |
|
|
30 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
|
|
31 | |
39 | |
32 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
40 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
33 | $YAML::Syck::ImplicitUnicode = 1; |
41 | $YAML::Syck::ImplicitUnicode = 1; |
34 | |
42 | |
35 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
43 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
|
|
44 | |
|
|
45 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
36 | |
46 | |
37 | our %COMMAND = (); |
47 | our %COMMAND = (); |
38 | our %COMMAND_TIME = (); |
48 | our %COMMAND_TIME = (); |
39 | |
49 | |
40 | our @EXTS = (); # list of extension package names |
50 | our @EXTS = (); # list of extension package names |
41 | our %EXTCMD = (); |
51 | our %EXTCMD = (); |
42 | our %EXT_CORO = (); # coroutines bound to extensions |
52 | our %EXT_CORO = (); # coroutines bound to extensions |
43 | our %EXT_MAP = (); # pluggable maps |
53 | our %EXT_MAP = (); # pluggable maps |
44 | |
54 | |
|
|
55 | our $RELOAD; # number of reloads so far |
45 | our @EVENT; |
56 | our @EVENT; |
46 | our $LIBDIR = datadir . "/ext"; |
57 | our $LIBDIR = datadir . "/ext"; |
47 | |
58 | |
48 | our $TICK = MAX_TIME * 1e-6; |
59 | our $TICK = MAX_TIME * 1e-6; |
49 | our $TICK_WATCHER; |
60 | our $TICK_WATCHER; |
50 | our $AIO_POLL_WATCHER; |
61 | our $AIO_POLL_WATCHER; |
51 | our $WRITE_RUNTIME_WATCHER; |
62 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
52 | our $NEXT_TICK; |
63 | our $NEXT_TICK; |
53 | our $NOW; |
64 | our $NOW; |
54 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
65 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
55 | |
66 | |
56 | our $BDB_POLL_WATCHER; |
67 | our $BDB_POLL_WATCHER; |
… | |
… | |
64 | our %PLAYER; # all users |
75 | our %PLAYER; # all users |
65 | our %MAP; # all maps |
76 | our %MAP; # all maps |
66 | our $LINK_MAP; # the special {link} map, which is always available |
77 | our $LINK_MAP; # the special {link} map, which is always available |
67 | our $RANDOM_MAPS = cf::localdir . "/random"; |
78 | our $RANDOM_MAPS = cf::localdir . "/random"; |
68 | our $BDB_ENV_DIR = cf::localdir . "/db"; |
79 | our $BDB_ENV_DIR = cf::localdir . "/db"; |
69 | |
|
|
70 | our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal; |
|
|
71 | our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal; |
|
|
72 | |
80 | |
73 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
81 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
74 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
82 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
75 | |
83 | |
76 | binmode STDOUT; |
84 | binmode STDOUT; |
… | |
… | |
125 | =item %cf::CFG |
133 | =item %cf::CFG |
126 | |
134 | |
127 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
135 | Configuration for the server, loaded from C</etc/crossfire/config>, or |
128 | from wherever your confdir points to. |
136 | from wherever your confdir points to. |
129 | |
137 | |
130 | =item $cf::WAIT_FOR_TICK, $cf::WAIT_FOR_TICK_ONE |
138 | =item cf::wait_for_tick, cf::wait_for_tick_begin |
131 | |
139 | |
132 | These are Coro::Signal objects that are C<< ->broadcast >> (WAIT_FOR_TICK) |
140 | These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only |
133 | or C<< ->send >> (WAIT_FOR_TICK_ONE) on after normal server tick |
141 | returns directly I<after> the tick processing (and consequently, can only wake one process |
134 | processing has been done. Call C<< ->wait >> on them to maximise the |
142 | per tick), while cf::wait_for_tick wakes up all waiters after tick processing. |
135 | window of cpu time available, or simply to synchronise to the server tick. |
|
|
136 | |
143 | |
137 | =back |
144 | =back |
138 | |
145 | |
139 | =cut |
146 | =cut |
140 | |
147 | |
… | |
… | |
199 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
206 | $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; |
200 | $d |
207 | $d |
201 | } || "[unable to dump $_[0]: '$@']"; |
208 | } || "[unable to dump $_[0]: '$@']"; |
202 | } |
209 | } |
203 | |
210 | |
204 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
211 | use JSON::XS qw(to_json from_json); # TODO# replace by JSON::PC once working |
205 | |
212 | |
206 | =item $ref = cf::from_json $json |
213 | =item $ref = cf::from_json $json |
207 | |
214 | |
208 | Converts a JSON string into the corresponding perl data structure. |
215 | Converts a JSON string into the corresponding perl data structure. |
209 | |
216 | |
210 | =cut |
|
|
211 | |
|
|
212 | sub from_json($) { |
|
|
213 | $JSON::Syck::ImplicitUnicode = 1; # work around JSON::Syck bugs |
|
|
214 | JSON::Syck::Load $_[0] |
|
|
215 | } |
|
|
216 | |
|
|
217 | =item $json = cf::to_json $ref |
217 | =item $json = cf::to_json $ref |
218 | |
218 | |
219 | Converts a perl data structure into its JSON representation. |
219 | Converts a perl data structure into its JSON representation. |
220 | |
|
|
221 | =cut |
|
|
222 | |
|
|
223 | sub to_json($) { |
|
|
224 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
|
|
225 | JSON::Syck::Dump $_[0] |
|
|
226 | } |
|
|
227 | |
220 | |
228 | =item cf::lock_wait $string |
221 | =item cf::lock_wait $string |
229 | |
222 | |
230 | Wait until the given lock is available. See cf::lock_acquire. |
223 | Wait until the given lock is available. See cf::lock_acquire. |
231 | |
224 | |
… | |
… | |
270 | sub freeze_mainloop { |
263 | sub freeze_mainloop { |
271 | return unless $TICK_WATCHER->is_active; |
264 | return unless $TICK_WATCHER->is_active; |
272 | |
265 | |
273 | my $guard = Coro::guard { |
266 | my $guard = Coro::guard { |
274 | $TICK_WATCHER->start; |
267 | $TICK_WATCHER->start; |
275 | $WRITE_RUNTIME_WATCHER->start; |
|
|
276 | }; |
268 | }; |
277 | $WRITE_RUNTIME_WATCHER->stop; |
|
|
278 | $TICK_WATCHER->stop; |
269 | $TICK_WATCHER->stop; |
279 | $guard |
270 | $guard |
280 | } |
271 | } |
281 | |
272 | |
282 | =item cf::async { BLOCK } |
273 | =item cf::async { BLOCK } |
… | |
… | |
353 | |
344 | |
354 | $coro |
345 | $coro |
355 | } |
346 | } |
356 | |
347 | |
357 | sub write_runtime { |
348 | sub write_runtime { |
|
|
349 | my $guard = cf::lock_acquire "write_runtime"; |
|
|
350 | |
358 | my $runtime = cf::localdir . "/runtime"; |
351 | my $runtime = cf::localdir . "/runtime"; |
359 | |
352 | |
360 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
353 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
361 | or return; |
354 | or return; |
362 | |
355 | |
… | |
… | |
376 | |
369 | |
377 | aio_rename "$runtime~", $runtime |
370 | aio_rename "$runtime~", $runtime |
378 | and return; |
371 | and return; |
379 | |
372 | |
380 | 1 |
373 | 1 |
|
|
374 | } |
|
|
375 | |
|
|
376 | =item cf::datalog type => key => value, ... |
|
|
377 | |
|
|
378 | Log a datalog packet of the given type with the given key-value pairs. |
|
|
379 | |
|
|
380 | =cut |
|
|
381 | |
|
|
382 | sub datalog($@) { |
|
|
383 | my ($type, %kv) = @_; |
|
|
384 | warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); |
381 | } |
385 | } |
382 | |
386 | |
383 | =back |
387 | =back |
384 | |
388 | |
385 | =cut |
389 | =cut |
… | |
… | |
784 | } |
788 | } |
785 | } else { |
789 | } else { |
786 | aio_unlink $filename; |
790 | aio_unlink $filename; |
787 | aio_unlink "$filename.pst"; |
791 | aio_unlink "$filename.pst"; |
788 | } |
792 | } |
789 | |
|
|
790 | #d##TODO# nuke non .map-files if exist |
|
|
791 | if ($filename =~ s/\.map$//) { |
|
|
792 | aio_unlink $filename; |
|
|
793 | aio_unlink "$filename.pst"; |
|
|
794 | } |
|
|
795 | } |
793 | } |
796 | } |
794 | } |
797 | |
795 | |
798 | sub object_freezer_as_string { |
796 | sub object_freezer_as_string { |
799 | my ($rdata, $objs) = @_; |
797 | my ($rdata, $objs) = @_; |
… | |
… | |
805 | |
803 | |
806 | sub object_thawer_load { |
804 | sub object_thawer_load { |
807 | my ($filename) = @_; |
805 | my ($filename) = @_; |
808 | |
806 | |
809 | my ($data, $av); |
807 | my ($data, $av); |
810 | |
|
|
811 | #d#TODO remove .map if file does not exist |
|
|
812 | aio_stat $filename and $filename =~ s/\.map$//; |
|
|
813 | |
808 | |
814 | (aio_load $filename, $data) >= 0 |
809 | (aio_load $filename, $data) >= 0 |
815 | or return; |
810 | or return; |
816 | |
811 | |
817 | unless (aio_stat "$filename.pst") { |
812 | unless (aio_stat "$filename.pst") { |
… | |
… | |
966 | } |
961 | } |
967 | |
962 | |
968 | sub path($) { |
963 | sub path($) { |
969 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
964 | my $login = ref $_[0] ? $_[0]->ob->name : $_[0]; |
970 | |
965 | |
971 | (playerdir $login) . "/$login.pl" |
966 | (playerdir $login) . "/playerdata" |
972 | } |
967 | } |
973 | |
968 | |
974 | sub find_active($) { |
969 | sub find_active($) { |
975 | $cf::PLAYER{$_[0]} |
970 | $cf::PLAYER{$_[0]} |
976 | and $cf::PLAYER{$_[0]}->active |
971 | and $cf::PLAYER{$_[0]}->active |
… | |
… | |
989 | my $login = $_[0]; |
984 | my $login = $_[0]; |
990 | |
985 | |
991 | my $guard = cf::lock_acquire "user_find:$login"; |
986 | my $guard = cf::lock_acquire "user_find:$login"; |
992 | |
987 | |
993 | $cf::PLAYER{$_[0]} || do { |
988 | $cf::PLAYER{$_[0]} || do { |
|
|
989 | # rename old playerfiles to new ones |
|
|
990 | #TODO: remove when no longer required |
|
|
991 | aio_link +(playerdir $login) . "/$login.pl.pst", (playerdir $login) . "/playerdata.pst"; |
|
|
992 | aio_link +(playerdir $login) . "/$login.pl" , (playerdir $login) . "/playerdata"; |
|
|
993 | aio_unlink +(playerdir $login) . "/$login.pl.pst"; |
|
|
994 | aio_unlink +(playerdir $login) . "/$login.pl"; |
|
|
995 | |
994 | my $pl = load_pl path $login |
996 | my $pl = load_pl path $login |
995 | or return; |
997 | or return; |
996 | $cf::PLAYER{$login} = $pl |
998 | $cf::PLAYER{$login} = $pl |
997 | } |
999 | } |
998 | } |
1000 | } |
… | |
… | |
1035 | |
1037 | |
1036 | =cut |
1038 | =cut |
1037 | |
1039 | |
1038 | sub quit_character { |
1040 | sub quit_character { |
1039 | my ($pl) = @_; |
1041 | my ($pl) = @_; |
|
|
1042 | |
|
|
1043 | my $name = $pl->ob->name; |
1040 | |
1044 | |
1041 | $pl->{deny_save} = 1; |
1045 | $pl->{deny_save} = 1; |
1042 | $pl->password ("*"); # this should lock out the player until we nuked the dir |
1046 | $pl->password ("*"); # this should lock out the player until we nuked the dir |
1043 | |
1047 | |
1044 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
1048 | $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; |
… | |
… | |
1049 | my $path = playerdir $pl; |
1053 | my $path = playerdir $pl; |
1050 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1054 | my $temp = "$path~$cf::RUNTIME~deleting~"; |
1051 | aio_rename $path, $temp; |
1055 | aio_rename $path, $temp; |
1052 | delete $cf::PLAYER{$pl->ob->name}; |
1056 | delete $cf::PLAYER{$pl->ob->name}; |
1053 | $pl->destroy; |
1057 | $pl->destroy; |
|
|
1058 | |
|
|
1059 | my $prefix = qr<^~\Q$name\E/>; |
|
|
1060 | |
|
|
1061 | # nuke player maps |
|
|
1062 | $cf::MAP{$_}->nuke for grep /$prefix/, keys %cf::MAP; |
|
|
1063 | |
1054 | IO::AIO::aio_rmtree $temp; |
1064 | IO::AIO::aio_rmtree $temp; |
|
|
1065 | } |
|
|
1066 | |
|
|
1067 | =item $pl->kick |
|
|
1068 | |
|
|
1069 | Kicks a player out of the game. This destroys the connection. |
|
|
1070 | |
|
|
1071 | =cut |
|
|
1072 | |
|
|
1073 | sub kick { |
|
|
1074 | my ($pl, $kicker) = @_; |
|
|
1075 | |
|
|
1076 | $pl->invoke (cf::EVENT_PLAYER_KICK, $kicker); |
|
|
1077 | $pl->killer ("kicked"); |
|
|
1078 | $pl->ns->destroy; |
1055 | } |
1079 | } |
1056 | |
1080 | |
1057 | =item cf::player::list_logins |
1081 | =item cf::player::list_logins |
1058 | |
1082 | |
1059 | Returns am arrayref of all valid playernames in the system, can take a |
1083 | Returns am arrayref of all valid playernames in the system, can take a |
… | |
… | |
1105 | } |
1129 | } |
1106 | |
1130 | |
1107 | \@paths |
1131 | \@paths |
1108 | } |
1132 | } |
1109 | |
1133 | |
1110 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
1134 | =item $player->ext_reply ($msgid, %msg) |
1111 | |
1135 | |
1112 | Sends an ext reply to the player. |
1136 | Sends an ext reply to the player. |
1113 | |
1137 | |
1114 | =cut |
1138 | =cut |
1115 | |
1139 | |
1116 | sub ext_reply($$$%) { |
1140 | sub ext_reply($$%) { |
1117 | my ($self, $id, %msg) = @_; |
1141 | my ($self, $id, %msg) = @_; |
1118 | |
1142 | |
1119 | $msg{msgid} = $id; |
1143 | $msg{msgid} = $id; |
1120 | |
1144 | |
1121 | $self->send ("ext " . cf::to_json \%msg); |
1145 | $self->send ("ext " . cf::to_json \%msg); |
1122 | } |
1146 | } |
1123 | |
1147 | |
1124 | package cf; |
1148 | =item $player->ext_event ($type, %msg) |
|
|
1149 | |
|
|
1150 | Sends an ext event to the client. |
|
|
1151 | |
|
|
1152 | =cut |
|
|
1153 | |
|
|
1154 | sub ext_event($$%) { |
|
|
1155 | my ($self, $type, %msg) = @_; |
|
|
1156 | |
|
|
1157 | $self->ns->ext_event ($type, %msg); |
|
|
1158 | } |
|
|
1159 | |
|
|
1160 | =head3 cf::region |
|
|
1161 | |
|
|
1162 | =over 4 |
|
|
1163 | |
|
|
1164 | =cut |
|
|
1165 | |
|
|
1166 | package cf::region; |
|
|
1167 | |
|
|
1168 | =item cf::region::find_by_path $path |
|
|
1169 | |
|
|
1170 | Tries to decuce the probable region for a map knowing only its path. |
|
|
1171 | |
|
|
1172 | =cut |
|
|
1173 | |
|
|
1174 | sub find_by_path($) { |
|
|
1175 | my ($path) = @_; |
|
|
1176 | |
|
|
1177 | my ($match, $specificity); |
|
|
1178 | |
|
|
1179 | for my $region (list) { |
|
|
1180 | if ($region->match && $path =~ $region->match) { |
|
|
1181 | ($match, $specificity) = ($region, $region->specificity) |
|
|
1182 | if $region->specificity > $specificity; |
|
|
1183 | } |
|
|
1184 | } |
|
|
1185 | |
|
|
1186 | $match |
|
|
1187 | } |
1125 | |
1188 | |
1126 | =back |
1189 | =back |
1127 | |
|
|
1128 | |
1190 | |
1129 | =head3 cf::map |
1191 | =head3 cf::map |
1130 | |
1192 | |
1131 | =over 4 |
1193 | =over 4 |
1132 | |
1194 | |
… | |
… | |
1304 | my ($self) = @_; |
1366 | my ($self) = @_; |
1305 | |
1367 | |
1306 | utf8::encode (my $save = $self->save_path); |
1368 | utf8::encode (my $save = $self->save_path); |
1307 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1369 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; |
1308 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1370 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; |
1309 | |
|
|
1310 | #d#TODO remove .map and also nuke |
|
|
1311 | $save =~ s/\.map// or return;#d# |
|
|
1312 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save;#d# |
|
|
1313 | IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst";#d# |
|
|
1314 | } |
1371 | } |
1315 | |
1372 | |
1316 | sub load_header_from($) { |
1373 | sub load_header_from($) { |
1317 | my ($self, $path) = @_; |
1374 | my ($self, $path) = @_; |
1318 | |
1375 | |
… | |
… | |
1368 | $self->load_header_orig |
1425 | $self->load_header_orig |
1369 | or return; |
1426 | or return; |
1370 | $self->prepare_orig; |
1427 | $self->prepare_orig; |
1371 | } |
1428 | } |
1372 | |
1429 | |
|
|
1430 | $self->default_region (cf::region::find_by_path $self->{path}) |
|
|
1431 | unless $self->default_region; |
|
|
1432 | |
1373 | 1 |
1433 | 1 |
1374 | } |
1434 | } |
1375 | |
1435 | |
1376 | sub find; |
1436 | sub find; |
1377 | sub find { |
1437 | sub find { |
… | |
… | |
1419 | $self->in_memory (cf::MAP_LOADING); |
1479 | $self->in_memory (cf::MAP_LOADING); |
1420 | |
1480 | |
1421 | $self->alloc; |
1481 | $self->alloc; |
1422 | |
1482 | |
1423 | $self->pre_load; |
1483 | $self->pre_load; |
|
|
1484 | Coro::cede; |
1424 | |
1485 | |
1425 | $self->_load_objects ($self->{load_path}, 1) |
1486 | $self->_load_objects ($self->{load_path}, 1) |
1426 | or return; |
1487 | or return; |
1427 | |
1488 | |
1428 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
1489 | $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) |
… | |
… | |
1435 | $self->_load_objects ($uniq, 0); |
1496 | $self->_load_objects ($uniq, 0); |
1436 | } |
1497 | } |
1437 | } |
1498 | } |
1438 | |
1499 | |
1439 | Coro::cede; |
1500 | Coro::cede; |
1440 | |
|
|
1441 | # now do the right thing for maps |
1501 | # now do the right thing for maps |
1442 | $self->link_multipart_objects; |
1502 | $self->link_multipart_objects; |
|
|
1503 | Coro::cede; |
1443 | |
1504 | |
1444 | unless ($self->{deny_activate}) { |
1505 | unless ($self->{deny_activate}) { |
1445 | $self->decay_objects; |
1506 | $self->decay_objects; |
1446 | $self->fix_auto_apply; |
1507 | $self->fix_auto_apply; |
1447 | $self->update_buttons; |
1508 | $self->update_buttons; |
… | |
… | |
1449 | $self->set_darkness_map; |
1510 | $self->set_darkness_map; |
1450 | $self->difficulty ($self->estimate_difficulty) |
1511 | $self->difficulty ($self->estimate_difficulty) |
1451 | unless $self->difficulty; |
1512 | unless $self->difficulty; |
1452 | Coro::cede; |
1513 | Coro::cede; |
1453 | $self->activate; |
1514 | $self->activate; |
|
|
1515 | Coro::cede; |
1454 | } |
1516 | } |
1455 | |
1517 | |
1456 | $self->post_load; |
1518 | $self->post_load; |
|
|
1519 | Coro::cede; |
1457 | |
1520 | |
1458 | $self->in_memory (cf::MAP_IN_MEMORY); |
1521 | $self->in_memory (cf::MAP_IN_MEMORY); |
1459 | } |
1522 | } |
1460 | |
1523 | |
1461 | sub customise_for { |
1524 | sub customise_for { |
… | |
… | |
1606 | return if $self->players; |
1669 | return if $self->players; |
1607 | return if $self->isa ("ext::map_per_player");#d# |
1670 | return if $self->isa ("ext::map_per_player");#d# |
1608 | |
1671 | |
1609 | warn "resetting map ", $self->path;#d# |
1672 | warn "resetting map ", $self->path;#d# |
1610 | |
1673 | |
|
|
1674 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
1675 | |
|
|
1676 | # need to save uniques path |
|
|
1677 | unless ($self->{deny_save}) { |
|
|
1678 | my $uniq = $self->uniq_path; utf8::encode $uniq; |
|
|
1679 | |
|
|
1680 | $self->_save_objects ($uniq, cf::IO_UNIQUES) |
|
|
1681 | if $uniq; |
|
|
1682 | } |
|
|
1683 | |
1611 | delete $cf::MAP{$self->path}; |
1684 | delete $cf::MAP{$self->path}; |
1612 | |
1685 | |
1613 | $self->in_memory (cf::MAP_SWAPPED); |
|
|
1614 | $self->clear; |
1686 | $self->clear; |
1615 | |
1687 | |
1616 | $_->clear_links_to ($self) for values %cf::MAP; |
1688 | $_->clear_links_to ($self) for values %cf::MAP; |
1617 | |
1689 | |
1618 | $self->unlink_save; |
1690 | $self->unlink_save; |
… | |
… | |
1808 | $map->load; |
1880 | $map->load; |
1809 | $map->load_diag; |
1881 | $map->load_diag; |
1810 | |
1882 | |
1811 | return unless $self->contr->active; |
1883 | return unless $self->contr->active; |
1812 | $self->activate_recursive; |
1884 | $self->activate_recursive; |
|
|
1885 | |
|
|
1886 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
1813 | $self->enter_map ($map, $x, $y); |
1887 | $self->enter_map ($map, $x, $y); |
1814 | } |
1888 | } |
1815 | |
1889 | |
1816 | cf::player->attach ( |
1890 | cf::player->attach ( |
1817 | on_logout => sub { |
1891 | on_logout => sub { |
… | |
… | |
1830 | # try to abort aborted map switching on player login :) |
1904 | # try to abort aborted map switching on player login :) |
1831 | # should happen only on crashes |
1905 | # should happen only on crashes |
1832 | if ($pl->ob->{_link_pos}) { |
1906 | if ($pl->ob->{_link_pos}) { |
1833 | $pl->ob->enter_link; |
1907 | $pl->ob->enter_link; |
1834 | (async { |
1908 | (async { |
1835 | # we need this sleep as the login has a concurrent enter_exit running |
|
|
1836 | # and this sleep increases chances of the player not ending up in scorn |
|
|
1837 | $pl->ob->reply (undef, |
1909 | $pl->ob->reply (undef, |
1838 | "There was an internal problem at your last logout, " |
1910 | "There was an internal problem at your last logout, " |
1839 | . "the server will try to bring you to your intended destination in a second.", |
1911 | . "the server will try to bring you to your intended destination in a second.", |
1840 | cf::NDI_RED); |
1912 | cf::NDI_RED); |
|
|
1913 | # we need this sleep as the login has a concurrent enter_exit running |
|
|
1914 | # and this sleep increases chances of the player not ending up in scorn |
1841 | Coro::Timer::sleep 1; |
1915 | Coro::Timer::sleep 1; |
1842 | $pl->ob->leave_link; |
1916 | $pl->ob->leave_link; |
1843 | })->prio (2); |
1917 | })->prio (2); |
1844 | } |
1918 | } |
1845 | }, |
1919 | }, |
… | |
… | |
1928 | |
2002 | |
1929 | return unless $self->type == cf::PLAYER; |
2003 | return unless $self->type == cf::PLAYER; |
1930 | |
2004 | |
1931 | if ($exit->slaying eq "/!") { |
2005 | if ($exit->slaying eq "/!") { |
1932 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
2006 | #TODO: this should de-fi-ni-te-ly not be a sync-job |
|
|
2007 | # the problem is that $exit might not survive long enough |
|
|
2008 | # so it needs to be done right now, right here |
1933 | cf::sync_job { prepare_random_map $exit }; |
2009 | cf::sync_job { prepare_random_map $exit }; |
1934 | } |
2010 | } |
1935 | |
2011 | |
1936 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
2012 | my $slaying = cf::map::normalise $exit->slaying, $exit->map && $exit->map->path; |
1937 | my $hp = $exit->stats->hp; |
2013 | my $hp = $exit->stats->hp; |
… | |
… | |
1945 | $self->goto ($slaying, $hp, $sp); |
2021 | $self->goto ($slaying, $hp, $sp); |
1946 | |
2022 | |
1947 | 1; |
2023 | 1; |
1948 | }) { |
2024 | }) { |
1949 | $self->message ("Something went wrong deep within the crossfire server. " |
2025 | $self->message ("Something went wrong deep within the crossfire server. " |
1950 | . "I'll try to bring you back to the map you were before. " |
2026 | . "I'll try to bring you back to the map you were before. " |
1951 | . "Please report this to the dungeon master!", |
2027 | . "Please report this to the dungeon master!", |
1952 | cf::NDI_UNIQUE | cf::NDI_RED); |
2028 | cf::NDI_UNIQUE | cf::NDI_RED); |
1953 | |
2029 | |
1954 | warn "ERROR in enter_exit: $@"; |
2030 | warn "ERROR in enter_exit: $@"; |
1955 | $self->leave_link; |
2031 | $self->leave_link; |
1956 | } |
2032 | } |
1957 | })->prio (1); |
2033 | })->prio (1); |
… | |
… | |
1970 | |
2046 | |
1971 | sub cf::client::send_drawinfo { |
2047 | sub cf::client::send_drawinfo { |
1972 | my ($self, $text, $flags) = @_; |
2048 | my ($self, $text, $flags) = @_; |
1973 | |
2049 | |
1974 | utf8::encode $text; |
2050 | utf8::encode $text; |
1975 | $self->send_packet (sprintf "drawinfo %d %s", $flags, $text); |
2051 | $self->send_packet (sprintf "drawinfo %d %s", $flags || cf::NDI_BLACK, $text); |
1976 | } |
2052 | } |
1977 | |
2053 | |
|
|
2054 | =item $client->ext_event ($type, %msg) |
|
|
2055 | |
|
|
2056 | Sends an exti event to the client. |
|
|
2057 | |
|
|
2058 | =cut |
|
|
2059 | |
|
|
2060 | sub cf::client::ext_event($$%) { |
|
|
2061 | my ($self, $type, %msg) = @_; |
|
|
2062 | |
|
|
2063 | $msg{msgtype} = "event_$type"; |
|
|
2064 | $self->send_packet ("ext " . cf::to_json \%msg); |
|
|
2065 | } |
1978 | |
2066 | |
1979 | =item $success = $client->query ($flags, "text", \&cb) |
2067 | =item $success = $client->query ($flags, "text", \&cb) |
1980 | |
2068 | |
1981 | Queues a query to the client, calling the given callback with |
2069 | Queues a query to the client, calling the given callback with |
1982 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
2070 | the reply text on a reply. flags can be C<cf::CS_QUERY_YESNO>, |
… | |
… | |
2079 | |
2167 | |
2080 | # here we export the classes and methods available to script code |
2168 | # here we export the classes and methods available to script code |
2081 | |
2169 | |
2082 | =pod |
2170 | =pod |
2083 | |
2171 | |
2084 | The following fucntions and emthods are available within a safe environment: |
2172 | The following functions and methods are available within a safe environment: |
2085 | |
2173 | |
2086 | cf::object contr pay_amount pay_player map |
2174 | cf::object contr pay_amount pay_player map |
2087 | cf::object::player player |
2175 | cf::object::player player |
2088 | cf::player peaceful |
2176 | cf::player peaceful |
2089 | cf::map trigger |
2177 | cf::map trigger |
… | |
… | |
2182 | The parameter C<$family> should best start with the name of the extension |
2270 | The parameter C<$family> should best start with the name of the extension |
2183 | using it, it should be unique. |
2271 | using it, it should be unique. |
2184 | |
2272 | |
2185 | =over 4 |
2273 | =over 4 |
2186 | |
2274 | |
2187 | =item $hashref = cf::db_get $family |
|
|
2188 | |
|
|
2189 | Return a hashref for use by the extension C<$family>, which can be |
|
|
2190 | modified. After modifications, you have to call C<cf::db_dirty> or |
|
|
2191 | C<cf::db_sync>. |
|
|
2192 | |
|
|
2193 | =item $value = cf::db_get $family => $key |
2275 | =item $value = cf::db_get $family => $key |
2194 | |
2276 | |
2195 | Returns a single value from the database |
2277 | Returns a single value from the database. |
2196 | |
|
|
2197 | =item cf::db_put $family => $hashref |
|
|
2198 | |
|
|
2199 | Stores the given family hashref into the database. Updates are delayed, if |
|
|
2200 | you want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
2201 | |
2278 | |
2202 | =item cf::db_put $family => $key => $value |
2279 | =item cf::db_put $family => $key => $value |
2203 | |
2280 | |
2204 | Stores the given C<$value> in the family hash. Updates are delayed, if you |
2281 | Stores the given C<$value> in the family. |
2205 | want the data to be synced to disk immediately, use C<cf::db_sync>. |
|
|
2206 | |
|
|
2207 | =item cf::db_dirty |
|
|
2208 | |
|
|
2209 | Marks the database as dirty, to be updated at a later time. |
|
|
2210 | |
|
|
2211 | =item cf::db_sync |
|
|
2212 | |
|
|
2213 | Immediately write the database to disk I<if it is dirty>. |
|
|
2214 | |
2282 | |
2215 | =cut |
2283 | =cut |
2216 | |
2284 | |
2217 | our $DB; |
2285 | our $DB; |
2218 | |
2286 | |
2219 | { |
2287 | sub db_init { |
|
|
2288 | unless ($DB) { |
|
|
2289 | $DB = BDB::db_create $DB_ENV; |
|
|
2290 | |
|
|
2291 | cf::sync_job { |
|
|
2292 | eval { |
|
|
2293 | $DB->set_flags (BDB::CHKSUM); |
|
|
2294 | |
|
|
2295 | BDB::db_open $DB, undef, "db", undef, BDB::BTREE, |
|
|
2296 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
|
|
2297 | cf::cleanup "db_open(db): $!" if $!; |
|
|
2298 | }; |
|
|
2299 | cf::cleanup "db_open(db): $@" if $@; |
|
|
2300 | }; |
|
|
2301 | |
2220 | my $path = cf::localdir . "/database.pst"; |
2302 | my $path = cf::localdir . "/database.pst"; |
|
|
2303 | if (stat $path) { |
|
|
2304 | cf::sync_job { |
|
|
2305 | my $pst = Storable::retrieve $path; |
2221 | |
2306 | |
2222 | sub db_load() { |
2307 | cf::db_put (board => data => $pst->{board}); |
2223 | $DB = stat $path ? Storable::retrieve $path : { }; |
2308 | cf::db_put (guildrules => data => $pst->{guildrules}); |
2224 | } |
2309 | cf::db_put (rent => balance => $pst->{rent}{balance}); |
|
|
2310 | BDB::db_env_txn_checkpoint $DB_ENV; |
2225 | |
2311 | |
2226 | my $pid; |
2312 | unlink $path; |
2227 | |
|
|
2228 | sub db_save() { |
|
|
2229 | waitpid $pid, 0 if $pid; |
|
|
2230 | if (0 == ($pid = fork)) { |
|
|
2231 | $DB->{_meta}{version} = 1; |
|
|
2232 | Storable::nstore $DB, "$path~"; |
|
|
2233 | rename "$path~", $path; |
|
|
2234 | cf::_exit 0 if defined $pid; |
|
|
2235 | } |
2313 | }; |
2236 | } |
|
|
2237 | |
|
|
2238 | my $dirty; |
|
|
2239 | |
|
|
2240 | sub db_sync() { |
|
|
2241 | db_save if $dirty; |
|
|
2242 | undef $dirty; |
|
|
2243 | } |
|
|
2244 | |
|
|
2245 | my $idle = Event->idle ( |
|
|
2246 | reentrant => 0, |
|
|
2247 | min => 10, |
|
|
2248 | max => 20, |
|
|
2249 | repeat => 0, |
|
|
2250 | data => WF_AUTOCANCEL, |
|
|
2251 | cb => \&db_sync, |
|
|
2252 | ); |
|
|
2253 | |
|
|
2254 | sub db_dirty() { |
|
|
2255 | $dirty = 1; |
|
|
2256 | $idle->start; |
|
|
2257 | } |
|
|
2258 | |
|
|
2259 | sub db_get($;$) { |
|
|
2260 | @_ >= 2 |
|
|
2261 | ? $DB->{$_[0]}{$_[1]} |
|
|
2262 | : ($DB->{$_[0]} ||= { }) |
|
|
2263 | } |
|
|
2264 | |
|
|
2265 | sub db_put($$;$) { |
|
|
2266 | if (@_ >= 3) { |
|
|
2267 | $DB->{$_[0]}{$_[1]} = $_[2]; |
|
|
2268 | } else { |
|
|
2269 | $DB->{$_[0]} = $_[1]; |
|
|
2270 | } |
|
|
2271 | db_dirty; |
|
|
2272 | } |
|
|
2273 | |
|
|
2274 | cf::global->attach ( |
|
|
2275 | prio => 10000, |
|
|
2276 | on_cleanup => sub { |
|
|
2277 | db_sync; |
|
|
2278 | }, |
2314 | } |
2279 | ); |
2315 | } |
|
|
2316 | } |
|
|
2317 | |
|
|
2318 | sub db_get($$) { |
|
|
2319 | my $key = "$_[0]/$_[1]"; |
|
|
2320 | |
|
|
2321 | cf::sync_job { |
|
|
2322 | BDB::db_get $DB, undef, $key, my $data; |
|
|
2323 | |
|
|
2324 | $! ? () |
|
|
2325 | : Compress::LZF::sthaw $data |
|
|
2326 | } |
|
|
2327 | } |
|
|
2328 | |
|
|
2329 | sub db_put($$$) { |
|
|
2330 | BDB::dbreq_pri 4; |
|
|
2331 | BDB::db_put $DB, undef, "$_[0]/$_[1]", Compress::LZF::sfreeze_cr $_[2], 0, sub { }; |
2280 | } |
2332 | } |
2281 | |
2333 | |
2282 | ############################################################################# |
2334 | ############################################################################# |
2283 | # the server's init and main functions |
2335 | # the server's init and main functions |
2284 | |
2336 | |
|
|
2337 | sub load_facedata { |
|
|
2338 | my $path = sprintf "%s/facedata", cf::datadir; |
|
|
2339 | |
|
|
2340 | warn "loading facedata from $path\n"; |
|
|
2341 | |
|
|
2342 | my $facedata; |
|
|
2343 | 0 < aio_load $path, $facedata |
|
|
2344 | or die "$path: $!"; |
|
|
2345 | |
|
|
2346 | $facedata = Coro::Storable::thaw $facedata; |
|
|
2347 | |
|
|
2348 | $facedata->{version} == 2 |
|
|
2349 | or cf::cleanup "$path: version mismatch, cannot proceed."; |
|
|
2350 | |
|
|
2351 | { |
|
|
2352 | my $faces = $facedata->{faceinfo}; |
|
|
2353 | |
|
|
2354 | while (my ($face, $info) = each %$faces) { |
|
|
2355 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
|
|
2356 | cf::face::set $idx, $info->{visibility}, $info->{magicmap}; |
|
|
2357 | cf::face::set_data $idx, 0, $info->{data32}, $info->{chksum32}; |
|
|
2358 | cf::face::set_data $idx, 1, $info->{data64}, $info->{chksum64}; |
|
|
2359 | Coro::cede; |
|
|
2360 | } |
|
|
2361 | |
|
|
2362 | while (my ($face, $info) = each %$faces) { |
|
|
2363 | next unless $info->{smooth}; |
|
|
2364 | my $idx = cf::face::find $face |
|
|
2365 | or next; |
|
|
2366 | if (my $smooth = cf::face::find $info->{smooth}) { |
|
|
2367 | cf::face::set_smooth $idx, $smooth, $info->{smoothlevel}; |
|
|
2368 | } else { |
|
|
2369 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
|
|
2370 | } |
|
|
2371 | Coro::cede; |
|
|
2372 | } |
|
|
2373 | } |
|
|
2374 | |
|
|
2375 | { |
|
|
2376 | my $anims = $facedata->{animinfo}; |
|
|
2377 | |
|
|
2378 | while (my ($anim, $info) = each %$anims) { |
|
|
2379 | cf::anim::set $anim, $info->{frames}, $info->{facings}; |
|
|
2380 | Coro::cede; |
|
|
2381 | } |
|
|
2382 | |
|
|
2383 | cf::anim::invalidate_all; # d'oh |
|
|
2384 | } |
|
|
2385 | |
|
|
2386 | 1 |
|
|
2387 | } |
|
|
2388 | |
|
|
2389 | sub load_archetypes { |
|
|
2390 | load_archetype_file sprintf "%s/archetypes", cf::datadir; # remove when stable |
|
|
2391 | load_archetype_file sprintf "%s/archetypes", cf::datadir |
|
|
2392 | } |
|
|
2393 | |
|
|
2394 | sub load_treasures { |
|
|
2395 | load_treasure_file sprintf "%s/treasures", cf::datadir |
|
|
2396 | } |
|
|
2397 | |
2285 | sub load_resources { |
2398 | sub reload_resources { |
2286 | load_regions sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2399 | load_resource_file sprintf "%s/%s/regions", cf::datadir, cf::mapdir |
2287 | or die "unable to load regions file\n";#d# |
2400 | or die "unable to load regions file\n"; |
|
|
2401 | load_facedata |
|
|
2402 | or die "unable to load facedata\n"; |
|
|
2403 | load_archetypes |
|
|
2404 | or die "unable to load archetypes\n"; |
|
|
2405 | load_treasures |
|
|
2406 | or die "unable to load treasurelists\n"; |
|
|
2407 | } |
|
|
2408 | |
|
|
2409 | sub init { |
|
|
2410 | reload_resources; |
2288 | } |
2411 | } |
2289 | |
2412 | |
2290 | sub cfg_load { |
2413 | sub cfg_load { |
2291 | open my $fh, "<:utf8", cf::confdir . "/config" |
2414 | open my $fh, "<:utf8", cf::confdir . "/config" |
2292 | or return; |
2415 | or return; |
… | |
… | |
2304 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2427 | $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" |
2305 | and die "WARNING: m(un)lockall failed: $!\n"; |
2428 | and die "WARNING: m(un)lockall failed: $!\n"; |
2306 | }; |
2429 | }; |
2307 | warn $@ if $@; |
2430 | warn $@ if $@; |
2308 | } |
2431 | } |
2309 | } |
|
|
2310 | |
|
|
2311 | sub init { |
|
|
2312 | load_resources; |
|
|
2313 | } |
2432 | } |
2314 | |
2433 | |
2315 | sub main { |
2434 | sub main { |
2316 | # we must not ever block the main coroutine |
2435 | # we must not ever block the main coroutine |
2317 | local $Coro::idle = sub { |
2436 | local $Coro::idle = sub { |
… | |
… | |
2320 | Event::one_event; |
2439 | Event::one_event; |
2321 | })->prio (Coro::PRIO_MAX); |
2440 | })->prio (Coro::PRIO_MAX); |
2322 | }; |
2441 | }; |
2323 | |
2442 | |
2324 | cfg_load; |
2443 | cfg_load; |
2325 | db_load; |
2444 | db_init; |
2326 | load_extensions; |
2445 | load_extensions; |
2327 | |
2446 | |
2328 | $TICK_WATCHER->start; |
2447 | $TICK_WATCHER->start; |
2329 | Event::loop; |
2448 | Event::loop; |
2330 | } |
2449 | } |
… | |
… | |
2369 | my $map = $cf::MAP{$path} or next; |
2488 | my $map = $cf::MAP{$path} or next; |
2370 | $map->valid or next; |
2489 | $map->valid or next; |
2371 | $map->save; |
2490 | $map->save; |
2372 | } |
2491 | } |
2373 | warn "end emergency map save\n"; |
2492 | warn "end emergency map save\n"; |
|
|
2493 | |
|
|
2494 | warn "begin emergency database checkpoint\n"; |
|
|
2495 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
2496 | warn "end emergency database checkpoint\n"; |
2374 | }; |
2497 | }; |
2375 | |
2498 | |
2376 | warn "leave emergency perl save\n"; |
2499 | warn "leave emergency perl save\n"; |
|
|
2500 | } |
|
|
2501 | |
|
|
2502 | sub post_cleanup { |
|
|
2503 | my ($make_core) = @_; |
|
|
2504 | |
|
|
2505 | warn Carp::longmess "post_cleanup backtrace" |
|
|
2506 | if $make_core; |
2377 | } |
2507 | } |
2378 | |
2508 | |
2379 | sub reload() { |
2509 | sub reload() { |
2380 | # can/must only be called in main |
2510 | # can/must only be called in main |
2381 | if ($Coro::current != $Coro::main) { |
2511 | if ($Coro::current != $Coro::main) { |
… | |
… | |
2383 | return; |
2513 | return; |
2384 | } |
2514 | } |
2385 | |
2515 | |
2386 | warn "reloading..."; |
2516 | warn "reloading..."; |
2387 | |
2517 | |
2388 | warn "cancelling server ticker"; |
2518 | warn "entering sync_job"; |
2389 | $TICK_WATCHER->cancel; |
|
|
2390 | |
2519 | |
|
|
2520 | cf::sync_job { |
|
|
2521 | cf::write_runtime; # external watchdog should not bark |
2391 | cf::emergency_save; |
2522 | cf::emergency_save; |
|
|
2523 | cf::write_runtime; # external watchdog should not bark |
2392 | |
2524 | |
2393 | eval { |
2525 | warn "syncing database to disk"; |
|
|
2526 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
2527 | |
2394 | # if anything goes wrong in here, we should simply crash as we already saved |
2528 | # if anything goes wrong in here, we should simply crash as we already saved |
2395 | |
|
|
2396 | warn "syncing database to disk"; |
|
|
2397 | cf::db_sync; |
|
|
2398 | |
2529 | |
2399 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2530 | warn "cancelling all WF_AUTOCANCEL watchers"; |
2400 | for (Event::all_watchers) { |
2531 | for (Event::all_watchers) { |
2401 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2532 | $_->cancel if $_->data & WF_AUTOCANCEL; |
2402 | } |
2533 | } |
2403 | |
2534 | |
2404 | warn "flushing outstanding aio requests"; |
2535 | warn "flushing outstanding aio requests"; |
2405 | for (;;) { |
2536 | for (;;) { |
|
|
2537 | BDB::flush; |
2406 | IO::AIO::flush; |
2538 | IO::AIO::flush; |
2407 | Coro::cede; |
2539 | Coro::cede; |
2408 | last unless IO::AIO::nreqs; |
2540 | last unless IO::AIO::nreqs || BDB::nreqs; |
2409 | warn "iterate..."; |
2541 | warn "iterate..."; |
2410 | } |
2542 | } |
|
|
2543 | |
|
|
2544 | ++$RELOAD; |
2411 | |
2545 | |
2412 | warn "cancelling all extension coros"; |
2546 | warn "cancelling all extension coros"; |
2413 | $_->cancel for values %EXT_CORO; |
2547 | $_->cancel for values %EXT_CORO; |
2414 | %EXT_CORO = (); |
2548 | %EXT_CORO = (); |
2415 | |
2549 | |
… | |
… | |
2468 | require cf; |
2602 | require cf; |
2469 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2603 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
2470 | |
2604 | |
2471 | warn "loading config and database again"; |
2605 | warn "loading config and database again"; |
2472 | cf::cfg_load; |
2606 | cf::cfg_load; |
2473 | cf::db_load; |
|
|
2474 | |
2607 | |
2475 | warn "loading extensions"; |
2608 | warn "loading extensions"; |
2476 | cf::load_extensions; |
2609 | cf::load_extensions; |
2477 | |
2610 | |
2478 | warn "reattaching attachments to objects/players"; |
2611 | warn "reattaching attachments to objects/players"; |
2479 | _global_reattach; |
2612 | _global_reattach; # objects, sockets |
2480 | warn "reattaching attachments to maps"; |
2613 | warn "reattaching attachments to maps"; |
2481 | reattach $_ for values %MAP; |
2614 | reattach $_ for values %MAP; |
|
|
2615 | warn "reattaching attachments to players"; |
|
|
2616 | reattach $_ for values %PLAYER; |
2482 | |
2617 | |
2483 | warn "loading reloadable resources"; |
2618 | warn "loading reloadable resources"; |
2484 | load_resources; |
2619 | reload_resources; |
2485 | |
2620 | |
2486 | warn "restarting server ticker"; |
2621 | warn "leaving sync_job"; |
2487 | |
2622 | |
2488 | $TICK_WATCHER->start; |
2623 | 1 |
2489 | }; |
2624 | } or do { |
2490 | |
|
|
2491 | if ($@) { |
|
|
2492 | warn $@; |
2625 | warn $@; |
2493 | warn "error while reloading, exiting."; |
2626 | warn "error while reloading, exiting."; |
2494 | exit 1; |
2627 | exit 1; |
2495 | } |
2628 | }; |
2496 | |
2629 | |
2497 | warn "reloaded"; |
2630 | warn "reloaded"; |
2498 | }; |
2631 | }; |
2499 | |
2632 | |
2500 | our $RELOAD_WATCHER; # used only during reload |
2633 | our $RELOAD_WATCHER; # used only during reload |
… | |
… | |
2521 | }; |
2654 | }; |
2522 | |
2655 | |
2523 | unshift @INC, $LIBDIR; |
2656 | unshift @INC, $LIBDIR; |
2524 | |
2657 | |
2525 | my $bug_warning = 0; |
2658 | my $bug_warning = 0; |
|
|
2659 | |
|
|
2660 | our @WAIT_FOR_TICK; |
|
|
2661 | our @WAIT_FOR_TICK_BEGIN; |
|
|
2662 | |
|
|
2663 | sub wait_for_tick { |
|
|
2664 | return unless $TICK_WATCHER->is_active; |
|
|
2665 | return if $Coro::current == $Coro::main; |
|
|
2666 | |
|
|
2667 | my $signal = new Coro::Signal; |
|
|
2668 | push @WAIT_FOR_TICK, $signal; |
|
|
2669 | $signal->wait; |
|
|
2670 | } |
|
|
2671 | |
|
|
2672 | sub wait_for_tick_begin { |
|
|
2673 | return unless $TICK_WATCHER->is_active; |
|
|
2674 | return if $Coro::current == $Coro::main; |
|
|
2675 | |
|
|
2676 | my $signal = new Coro::Signal; |
|
|
2677 | push @WAIT_FOR_TICK_BEGIN, $signal; |
|
|
2678 | $signal->wait; |
|
|
2679 | } |
2526 | |
2680 | |
2527 | $TICK_WATCHER = Event->timer ( |
2681 | $TICK_WATCHER = Event->timer ( |
2528 | reentrant => 0, |
2682 | reentrant => 0, |
2529 | parked => 1, |
2683 | parked => 1, |
2530 | prio => 0, |
2684 | prio => 0, |
… | |
… | |
2541 | |
2695 | |
2542 | cf::server_tick; # one server iteration |
2696 | cf::server_tick; # one server iteration |
2543 | $RUNTIME += $TICK; |
2697 | $RUNTIME += $TICK; |
2544 | $NEXT_TICK += $TICK; |
2698 | $NEXT_TICK += $TICK; |
2545 | |
2699 | |
2546 | $WAIT_FOR_TICK->broadcast; |
2700 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
2547 | $WAIT_FOR_TICK_ONE->send if $WAIT_FOR_TICK_ONE->awaited; |
2701 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
2548 | |
2702 | Coro::async_pool { |
2549 | Event::sweep; |
2703 | write_runtime |
2550 | Coro::cede_notself; |
2704 | or warn "ERROR: unable to write runtime file: $!"; |
|
|
2705 | }; |
2551 | |
2706 | } |
|
|
2707 | |
|
|
2708 | if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { |
|
|
2709 | $sig->send; |
|
|
2710 | } |
|
|
2711 | while (my $sig = shift @WAIT_FOR_TICK) { |
|
|
2712 | $sig->send; |
|
|
2713 | } |
|
|
2714 | |
2552 | # my $AFTER = Event::time; |
2715 | # my $AFTER = Event::time; |
2553 | # warn $AFTER - $NOW;#d# |
2716 | # warn $AFTER - $NOW;#d# |
2554 | |
2717 | |
2555 | # if we are delayed by four ticks or more, skip them all |
2718 | # if we are delayed by four ticks or more, skip them all |
2556 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
2719 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
… | |
… | |
2589 | |
2752 | |
2590 | unless ($DB_ENV) { |
2753 | unless ($DB_ENV) { |
2591 | $DB_ENV = BDB::db_env_create; |
2754 | $DB_ENV = BDB::db_env_create; |
2592 | |
2755 | |
2593 | cf::sync_job { |
2756 | cf::sync_job { |
|
|
2757 | eval { |
2594 | BDB::db_env_open |
2758 | BDB::db_env_open |
2595 | $DB_ENV, |
2759 | $DB_ENV, |
2596 | $BDB_ENV_DIR, |
2760 | $BDB_ENV_DIR, |
2597 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
2761 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
2598 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
2762 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
2599 | 0666; |
2763 | 0666; |
2600 | |
2764 | |
2601 | cf::cleanup "$!" if $!; |
2765 | cf::cleanup "db_env_open($BDB_ENV_DIR): $!" if $!; |
2602 | |
2766 | |
2603 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
2767 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); |
|
|
2768 | $DB_ENV->set_lk_detect; |
|
|
2769 | }; |
|
|
2770 | |
|
|
2771 | cf::cleanup "db_env_open(db): $@" if $@; |
2604 | }; |
2772 | }; |
2605 | } |
2773 | } |
2606 | } |
2774 | } |
2607 | |
2775 | |
2608 | { |
2776 | { |
… | |
… | |
2610 | |
2778 | |
2611 | undef $Coro::AIO::WATCHER; |
2779 | undef $Coro::AIO::WATCHER; |
2612 | IO::AIO::max_poll_time $TICK * 0.1; |
2780 | IO::AIO::max_poll_time $TICK * 0.1; |
2613 | $AIO_POLL_WATCHER = Event->io ( |
2781 | $AIO_POLL_WATCHER = Event->io ( |
2614 | reentrant => 0, |
2782 | reentrant => 0, |
|
|
2783 | data => WF_AUTOCANCEL, |
2615 | fd => IO::AIO::poll_fileno, |
2784 | fd => IO::AIO::poll_fileno, |
2616 | poll => 'r', |
2785 | poll => 'r', |
2617 | prio => 6, |
2786 | prio => 6, |
2618 | data => WF_AUTOCANCEL, |
|
|
2619 | cb => \&IO::AIO::poll_cb, |
2787 | cb => \&IO::AIO::poll_cb, |
2620 | ); |
2788 | ); |
2621 | } |
2789 | } |
2622 | |
2790 | |
2623 | $WRITE_RUNTIME_WATCHER = Event->timer ( |
|
|
2624 | reentrant => 0, |
|
|
2625 | data => WF_AUTOCANCEL, |
|
|
2626 | after => 1, |
|
|
2627 | interval => 10, |
|
|
2628 | prio => 6, # keep it lowest so it acts like a watchdog |
|
|
2629 | cb => Coro::unblock_sub { |
|
|
2630 | write_runtime |
|
|
2631 | or warn "ERROR: unable to write runtime file: $!"; |
|
|
2632 | }, |
|
|
2633 | ); |
|
|
2634 | |
|
|
2635 | END { cf::emergency_save } |
2791 | END { cf::emergency_save } |
2636 | |
2792 | |
2637 | 1 |
2793 | 1 |
2638 | |
2794 | |