… | |
… | |
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; |
13 | use Coro 3.3; |
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 | |
18 | |
… | |
… | |
23 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
23 | use Event; $Event::Eval = 1; # no idea why this is required, but it is |
24 | |
24 | |
25 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
25 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
26 | $YAML::Syck::ImplicitUnicode = 1; |
26 | $YAML::Syck::ImplicitUnicode = 1; |
27 | |
27 | |
28 | $Coro::main->prio (Coro::PRIO_MIN); |
28 | $Coro::main->prio (2); # run main coroutine ("the server") with very high priority |
29 | |
29 | |
30 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
30 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
31 | |
31 | |
32 | our %COMMAND = (); |
32 | our %COMMAND = (); |
33 | our %COMMAND_TIME = (); |
33 | our %COMMAND_TIME = (); |
… | |
… | |
37 | our $LIBDIR = datadir . "/ext"; |
37 | our $LIBDIR = datadir . "/ext"; |
38 | |
38 | |
39 | our $TICK = MAX_TIME * 1e-6; |
39 | our $TICK = MAX_TIME * 1e-6; |
40 | our $TICK_WATCHER; |
40 | our $TICK_WATCHER; |
41 | our $NEXT_TICK; |
41 | our $NEXT_TICK; |
|
|
42 | our $NOW; |
42 | |
43 | |
43 | our %CFG; |
44 | our %CFG; |
44 | |
45 | |
45 | our $UPTIME; $UPTIME ||= time; |
46 | our $UPTIME; $UPTIME ||= time; |
|
|
47 | our $RUNTIME; |
|
|
48 | |
|
|
49 | our %MAP; # all maps |
|
|
50 | our $LINK_MAP; # the special {link} map |
|
|
51 | our $FREEZE; |
|
|
52 | |
|
|
53 | binmode STDOUT; |
|
|
54 | binmode STDERR; |
|
|
55 | |
|
|
56 | # read virtual server time, if available |
|
|
57 | unless ($RUNTIME || !-e cf::localdir . "/runtime") { |
|
|
58 | open my $fh, "<", cf::localdir . "/runtime" |
|
|
59 | or die "unable to read runtime file: $!"; |
|
|
60 | $RUNTIME = <$fh> + 0.; |
|
|
61 | } |
|
|
62 | |
|
|
63 | mkdir cf::localdir; |
|
|
64 | mkdir cf::localdir . "/" . cf::playerdir; |
|
|
65 | mkdir cf::localdir . "/" . cf::tmpdir; |
|
|
66 | mkdir cf::localdir . "/" . cf::uniquedir; |
|
|
67 | |
|
|
68 | our %EXT_CORO; |
46 | |
69 | |
47 | ############################################################################# |
70 | ############################################################################# |
48 | |
71 | |
49 | =head2 GLOBAL VARIABLES |
72 | =head2 GLOBAL VARIABLES |
50 | |
73 | |
51 | =over 4 |
74 | =over 4 |
52 | |
75 | |
53 | =item $cf::UPTIME |
76 | =item $cf::UPTIME |
54 | |
77 | |
55 | The timestamp of the server start (so not actually an uptime). |
78 | The timestamp of the server start (so not actually an uptime). |
|
|
79 | |
|
|
80 | =item $cf::RUNTIME |
|
|
81 | |
|
|
82 | The time this server has run, starts at 0 and is increased by $cf::TICK on |
|
|
83 | every server tick. |
56 | |
84 | |
57 | =item $cf::LIBDIR |
85 | =item $cf::LIBDIR |
58 | |
86 | |
59 | The perl library directory, where extensions and cf-specific modules can |
87 | The perl library directory, where extensions and cf-specific modules can |
60 | be found. It will be added to C<@INC> automatically. |
88 | be found. It will be added to C<@INC> automatically. |
|
|
89 | |
|
|
90 | =item $cf::NOW |
|
|
91 | |
|
|
92 | The time of the last (current) server tick. |
61 | |
93 | |
62 | =item $cf::TICK |
94 | =item $cf::TICK |
63 | |
95 | |
64 | The interval between server ticks, in seconds. |
96 | The interval between server ticks, in seconds. |
65 | |
97 | |
… | |
… | |
73 | =cut |
105 | =cut |
74 | |
106 | |
75 | BEGIN { |
107 | BEGIN { |
76 | *CORE::GLOBAL::warn = sub { |
108 | *CORE::GLOBAL::warn = sub { |
77 | my $msg = join "", @_; |
109 | my $msg = join "", @_; |
|
|
110 | utf8::encode $msg; |
|
|
111 | |
78 | $msg .= "\n" |
112 | $msg .= "\n" |
79 | unless $msg =~ /\n$/; |
113 | unless $msg =~ /\n$/; |
80 | |
114 | |
81 | print STDERR "cfperl: $msg"; |
|
|
82 | LOG llevError, "cfperl: $msg"; |
115 | LOG llevError, "cfperl: $msg"; |
83 | }; |
116 | }; |
84 | } |
117 | } |
85 | |
118 | |
86 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
119 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
… | |
… | |
139 | sub to_json($) { |
172 | sub to_json($) { |
140 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
173 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
141 | JSON::Syck::Dump $_[0] |
174 | JSON::Syck::Dump $_[0] |
142 | } |
175 | } |
143 | |
176 | |
|
|
177 | =item $coro = cf::coro { BLOCK } |
|
|
178 | |
|
|
179 | Creates and returns a new coro. This coro is automcatially being canceled |
|
|
180 | when the extension calling this is being unloaded. |
|
|
181 | |
|
|
182 | =cut |
|
|
183 | |
|
|
184 | sub coro(&) { |
|
|
185 | my $cb = shift; |
|
|
186 | |
|
|
187 | my $coro; $coro = async { |
|
|
188 | eval { |
|
|
189 | $cb->(); |
|
|
190 | }; |
|
|
191 | warn $@ if $@; |
|
|
192 | }; |
|
|
193 | |
|
|
194 | $coro->on_destroy (sub { |
|
|
195 | delete $EXT_CORO{$coro+0}; |
|
|
196 | }); |
|
|
197 | $EXT_CORO{$coro+0} = $coro; |
|
|
198 | |
|
|
199 | $coro |
|
|
200 | } |
|
|
201 | |
144 | =back |
202 | =back |
145 | |
203 | |
146 | =cut |
204 | =cut |
147 | |
205 | |
148 | ############################################################################# |
206 | ############################################################################# |
… | |
… | |
916 | my $coro; $coro = async { |
974 | my $coro; $coro = async { |
917 | eval { |
975 | eval { |
918 | $cb->(); |
976 | $cb->(); |
919 | }; |
977 | }; |
920 | warn $@ if $@; |
978 | warn $@ if $@; |
|
|
979 | }; |
|
|
980 | |
|
|
981 | $coro->on_destroy (sub { |
921 | delete $self->{_coro}{$coro+0}; |
982 | delete $self->{_coro}{$coro+0}; |
922 | }; |
983 | }); |
923 | |
984 | |
924 | $self->{_coro}{$coro+0} = $coro; |
985 | $self->{_coro}{$coro+0} = $coro; |
|
|
986 | |
|
|
987 | $coro |
925 | } |
988 | } |
926 | |
989 | |
927 | cf::client->attach ( |
990 | cf::client->attach ( |
928 | on_destroy => sub { |
991 | on_destroy => sub { |
929 | my ($ns) = @_; |
992 | my ($ns) = @_; |
… | |
… | |
1170 | } |
1233 | } |
1171 | |
1234 | |
1172 | ############################################################################# |
1235 | ############################################################################# |
1173 | # initialisation |
1236 | # initialisation |
1174 | |
1237 | |
1175 | sub _perl_reload(&) { |
1238 | sub _perl_reload() { |
1176 | my ($msg) = @_; |
1239 | warn "reloading..."; |
1177 | |
|
|
1178 | $msg->("reloading..."); |
|
|
1179 | |
1240 | |
1180 | eval { |
1241 | eval { |
|
|
1242 | local $FREEZE = 1; |
|
|
1243 | |
|
|
1244 | cf::emergency_save; |
|
|
1245 | |
1181 | # cancel all watchers |
1246 | # cancel all watchers |
1182 | for (Event::all_watchers) { |
1247 | for (Event::all_watchers) { |
1183 | $_->cancel if $_->data & WF_AUTOCANCEL; |
1248 | $_->cancel if $_->data & WF_AUTOCANCEL; |
1184 | } |
1249 | } |
1185 | |
1250 | |
|
|
1251 | # cancel all extension coros |
|
|
1252 | $_->cancel for values %EXT_CORO; |
|
|
1253 | %EXT_CORO = (); |
|
|
1254 | |
1186 | # unload all extensions |
1255 | # unload all extensions |
1187 | for (@exts) { |
1256 | for (@exts) { |
1188 | $msg->("unloading <$_>"); |
1257 | warn "unloading <$_>"; |
1189 | unload_extension $_; |
1258 | unload_extension $_; |
1190 | } |
1259 | } |
1191 | |
1260 | |
1192 | # unload all modules loaded from $LIBDIR |
1261 | # unload all modules loaded from $LIBDIR |
1193 | while (my ($k, $v) = each %INC) { |
1262 | while (my ($k, $v) = each %INC) { |
1194 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
1263 | next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; |
1195 | |
1264 | |
1196 | $msg->("removing <$k>"); |
1265 | warn "removing <$k>"; |
1197 | delete $INC{$k}; |
1266 | delete $INC{$k}; |
1198 | |
1267 | |
1199 | $k =~ s/\.pm$//; |
1268 | $k =~ s/\.pm$//; |
1200 | $k =~ s/\//::/g; |
1269 | $k =~ s/\//::/g; |
1201 | |
1270 | |
… | |
… | |
1206 | Symbol::delete_package $k; |
1275 | Symbol::delete_package $k; |
1207 | } |
1276 | } |
1208 | |
1277 | |
1209 | # sync database to disk |
1278 | # sync database to disk |
1210 | cf::db_sync; |
1279 | cf::db_sync; |
|
|
1280 | IO::AIO::flush; |
1211 | |
1281 | |
1212 | # get rid of safe::, as good as possible |
1282 | # get rid of safe::, as good as possible |
1213 | Symbol::delete_package "safe::$_" |
1283 | Symbol::delete_package "safe::$_" |
1214 | for qw(cf::object cf::object::player cf::player cf::map cf::party cf::region); |
1284 | for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); |
1215 | |
1285 | |
1216 | # remove register_script_function callbacks |
1286 | # remove register_script_function callbacks |
1217 | # TODO |
1287 | # TODO |
1218 | |
1288 | |
1219 | # unload cf.pm "a bit" |
1289 | # unload cf.pm "a bit" |
… | |
… | |
1222 | # don't, removes xs symbols, too, |
1292 | # don't, removes xs symbols, too, |
1223 | # and global variables created in xs |
1293 | # and global variables created in xs |
1224 | #Symbol::delete_package __PACKAGE__; |
1294 | #Symbol::delete_package __PACKAGE__; |
1225 | |
1295 | |
1226 | # reload cf.pm |
1296 | # reload cf.pm |
1227 | $msg->("reloading cf.pm"); |
1297 | warn "reloading cf.pm"; |
1228 | require cf; |
1298 | require cf; |
1229 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
1299 | cf::_connect_to_perl; # nominally unnecessary, but cannot hurt |
1230 | |
1300 | |
1231 | # load config and database again |
1301 | # load config and database again |
1232 | cf::cfg_load; |
1302 | cf::cfg_load; |
1233 | cf::db_load; |
1303 | cf::db_load; |
1234 | |
1304 | |
1235 | # load extensions |
1305 | # load extensions |
1236 | $msg->("load extensions"); |
1306 | warn "load extensions"; |
1237 | cf::load_extensions; |
1307 | cf::load_extensions; |
1238 | |
1308 | |
1239 | # reattach attachments to objects |
1309 | # reattach attachments to objects |
1240 | $msg->("reattach"); |
1310 | warn "reattach"; |
1241 | _global_reattach; |
1311 | _global_reattach; |
1242 | }; |
1312 | }; |
1243 | $msg->($@) if $@; |
1313 | warn $@ if $@; |
1244 | |
1314 | |
1245 | $msg->("reloaded"); |
1315 | warn "reloaded"; |
1246 | }; |
1316 | }; |
1247 | |
1317 | |
1248 | sub perl_reload() { |
1318 | sub perl_reload() { |
1249 | _perl_reload { |
1319 | _perl_reload; |
1250 | warn $_[0]; |
|
|
1251 | print "$_[0]\n"; |
|
|
1252 | }; |
|
|
1253 | } |
1320 | } |
1254 | |
1321 | |
1255 | register "<global>", __PACKAGE__; |
1322 | register "<global>", __PACKAGE__; |
1256 | |
1323 | |
1257 | register_command "perl-reload" => sub { |
1324 | register_command "perl-reload" => sub { |
1258 | my ($who, $arg) = @_; |
1325 | my ($who, $arg) = @_; |
1259 | |
1326 | |
1260 | if ($who->flag (FLAG_WIZ)) { |
1327 | if ($who->flag (FLAG_WIZ)) { |
|
|
1328 | $who->message ("reloading..."); |
1261 | _perl_reload { |
1329 | _perl_reload; |
1262 | warn $_[0]; |
|
|
1263 | $who->message ($_[0]); |
|
|
1264 | }; |
|
|
1265 | } |
1330 | } |
1266 | }; |
1331 | }; |
1267 | |
1332 | |
1268 | unshift @INC, $LIBDIR; |
1333 | unshift @INC, $LIBDIR; |
1269 | |
1334 | |
1270 | $TICK_WATCHER = Event->timer ( |
1335 | $TICK_WATCHER = Event->timer ( |
1271 | prio => 0, |
1336 | prio => 0, |
1272 | at => $NEXT_TICK || 1, |
1337 | at => $NEXT_TICK || $TICK, |
1273 | data => WF_AUTOCANCEL, |
1338 | data => WF_AUTOCANCEL, |
1274 | cb => sub { |
1339 | cb => sub { |
|
|
1340 | unless ($FREEZE) { |
1275 | cf::server_tick; # one server iteration |
1341 | cf::server_tick; # one server iteration |
|
|
1342 | $RUNTIME += $TICK; |
|
|
1343 | } |
1276 | |
1344 | |
1277 | my $NOW = Event::time; |
|
|
1278 | $NEXT_TICK += $TICK; |
1345 | $NEXT_TICK += $TICK; |
1279 | |
1346 | |
1280 | # if we are delayed by four ticks or more, skip them all |
1347 | # if we are delayed by four ticks or more, skip them all |
1281 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1348 | $NEXT_TICK = Event::time if Event::time >= $NEXT_TICK + $TICK * 4; |
1282 | |
1349 | |
1283 | $TICK_WATCHER->at ($NEXT_TICK); |
1350 | $TICK_WATCHER->at ($NEXT_TICK); |
1284 | $TICK_WATCHER->start; |
1351 | $TICK_WATCHER->start; |
1285 | }, |
1352 | }, |
1286 | ); |
1353 | ); |
… | |
… | |
1291 | poll => 'r', |
1358 | poll => 'r', |
1292 | prio => 5, |
1359 | prio => 5, |
1293 | data => WF_AUTOCANCEL, |
1360 | data => WF_AUTOCANCEL, |
1294 | cb => \&IO::AIO::poll_cb); |
1361 | cb => \&IO::AIO::poll_cb); |
1295 | |
1362 | |
|
|
1363 | # we must not ever block the main coroutine |
|
|
1364 | $Coro::idle = sub { |
|
|
1365 | #Carp::cluck "FATAL: Coro::idle was called, major BUG\n";#d# |
|
|
1366 | warn "FATAL: Coro::idle was called, major BUG\n"; |
|
|
1367 | (Coro::unblock_sub { |
|
|
1368 | Event::one_event; |
|
|
1369 | })->(); |
|
|
1370 | }; |
|
|
1371 | |
1296 | 1 |
1372 | 1 |
1297 | |
1373 | |