|
|
1 | # |
|
|
2 | # This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
3 | # |
|
|
4 | # Copyright (©) 2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | # |
|
|
6 | # Deliantra is free software: you can redistribute it and/or modify |
|
|
7 | # it under the terms of the GNU General Public License as published by |
|
|
8 | # the Free Software Foundation, either version 3 of the License, or |
|
|
9 | # (at your option) any later version. |
|
|
10 | # |
|
|
11 | # This program is distributed in the hope that it will be useful, |
|
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
14 | # GNU General Public License for more details. |
|
|
15 | # |
|
|
16 | # You should have received a copy of the GNU General Public License |
|
|
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
|
18 | # |
|
|
19 | # The authors can be reached via e-mail to <support@deliantra.net> |
|
|
20 | # |
|
|
21 | |
1 | package cf; |
22 | package cf; |
2 | |
23 | |
3 | use utf8; |
24 | use utf8; |
4 | use strict; |
25 | use strict; |
5 | |
26 | |
6 | use Symbol; |
27 | use Symbol; |
7 | use List::Util; |
28 | use List::Util; |
8 | use Socket; |
29 | use Socket; |
9 | use EV; |
30 | use EV 3.2; |
10 | use Opcode; |
31 | use Opcode; |
11 | use Safe; |
32 | use Safe; |
12 | use Safe::Hole; |
33 | use Safe::Hole; |
13 | use Storable (); |
34 | use Storable (); |
14 | |
35 | |
15 | use Coro 4.32 (); |
36 | use Coro 4.50 (); |
16 | use Coro::State; |
37 | use Coro::State; |
17 | use Coro::Handle; |
38 | use Coro::Handle; |
18 | use Coro::EV; |
39 | use Coro::EV; |
19 | use Coro::Timer; |
40 | use Coro::Timer; |
20 | use Coro::Signal; |
41 | use Coro::Signal; |
… | |
… | |
27 | use JSON::XS 2.01 (); |
48 | use JSON::XS 2.01 (); |
28 | use BDB (); |
49 | use BDB (); |
29 | use Data::Dumper; |
50 | use Data::Dumper; |
30 | use Digest::MD5; |
51 | use Digest::MD5; |
31 | use Fcntl; |
52 | use Fcntl; |
32 | use YAML::Syck (); |
53 | use YAML (); |
33 | use IO::AIO 2.51 (); |
54 | use IO::AIO 2.51 (); |
34 | use Time::HiRes; |
55 | use Time::HiRes; |
35 | use Compress::LZF; |
56 | use Compress::LZF; |
36 | use Digest::MD5 (); |
57 | use Digest::MD5 (); |
37 | |
58 | |
38 | # configure various modules to our taste |
59 | # configure various modules to our taste |
39 | # |
60 | # |
40 | $Storable::canonical = 1; # reduce rsync transfers |
61 | $Storable::canonical = 1; # reduce rsync transfers |
41 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
62 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
42 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
63 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
43 | |
|
|
44 | # work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode? |
|
|
45 | $YAML::Syck::ImplicitUnicode = 1; |
|
|
46 | |
64 | |
47 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
65 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
48 | |
66 | |
49 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
67 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
50 | |
68 | |
… | |
… | |
69 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
87 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
70 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
88 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
71 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
89 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
72 | our $RANDOMDIR = "$LOCALDIR/random"; |
90 | our $RANDOMDIR = "$LOCALDIR/random"; |
73 | our $BDBDIR = "$LOCALDIR/db"; |
91 | our $BDBDIR = "$LOCALDIR/db"; |
|
|
92 | our %RESOURCE; |
74 | |
93 | |
75 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
94 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
76 | our $TICK_WATCHER; |
|
|
77 | our $AIO_POLL_WATCHER; |
95 | our $AIO_POLL_WATCHER; |
78 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
96 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
79 | our $NEXT_TICK; |
97 | our $NEXT_TICK; |
80 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
98 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
81 | |
99 | |
… | |
… | |
98 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
116 | # used to convert map paths into valid unix filenames by replacing / by ∕ |
99 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
117 | our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons |
100 | |
118 | |
101 | our $LOAD; # a number between 0 (idle) and 1 (too many objects) |
119 | our $LOAD; # a number between 0 (idle) and 1 (too many objects) |
102 | our $LOADAVG; # same thing, but with alpha-smoothing |
120 | our $LOADAVG; # same thing, but with alpha-smoothing |
|
|
121 | our $JITTER; # average jitter |
103 | our $tick_start; # for load detecting purposes |
122 | our $TICK_START; # for load detecting purposes |
104 | |
123 | |
105 | binmode STDOUT; |
124 | binmode STDOUT; |
106 | binmode STDERR; |
125 | binmode STDERR; |
107 | |
126 | |
108 | # read virtual server time, if available |
127 | # read virtual server time, if available |
… | |
… | |
192 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
211 | $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; |
193 | |
212 | |
194 | LOG llevError, $msg; |
213 | LOG llevError, $msg; |
195 | }; |
214 | }; |
196 | } |
215 | } |
|
|
216 | |
|
|
217 | $Coro::State::DIEHOOK = sub { |
|
|
218 | return unless $^S eq 0; # "eq", not "==" |
|
|
219 | |
|
|
220 | if ($Coro::current == $Coro::main) {#d# |
|
|
221 | warn "DIEHOOK called in main context, Coro bug?\n";#d# |
|
|
222 | return;#d# |
|
|
223 | }#d# |
|
|
224 | |
|
|
225 | # kill coroutine otherwise |
|
|
226 | warn Carp::longmess $_[0]; |
|
|
227 | Coro::terminate |
|
|
228 | }; |
|
|
229 | |
|
|
230 | $SIG{__DIE__} = sub { }; #d#? |
197 | |
231 | |
198 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
232 | @safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; |
199 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
233 | @safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; |
200 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
234 | @safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; |
201 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
235 | @safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; |
… | |
… | |
326 | |
360 | |
327 | ! ! $LOCK{$key} |
361 | ! ! $LOCK{$key} |
328 | } |
362 | } |
329 | |
363 | |
330 | sub freeze_mainloop { |
364 | sub freeze_mainloop { |
331 | return unless $TICK_WATCHER->is_active; |
365 | tick_inhibit_inc; |
332 | |
366 | |
333 | my $guard = Coro::guard { |
367 | Coro::guard \&tick_inhibit_dec; |
334 | $TICK_WATCHER->start; |
|
|
335 | }; |
|
|
336 | $TICK_WATCHER->stop; |
|
|
337 | $guard |
|
|
338 | } |
368 | } |
339 | |
369 | |
340 | =item cf::periodic $interval, $cb |
370 | =item cf::periodic $interval, $cb |
341 | |
371 | |
342 | Like EV::periodic, but randomly selects a starting point so that the actions |
372 | Like EV::periodic, but randomly selects a starting point so that the actions |
… | |
… | |
443 | # this is the main coro, too bad, we have to block |
473 | # this is the main coro, too bad, we have to block |
444 | # till the operation succeeds, freezing the server :/ |
474 | # till the operation succeeds, freezing the server :/ |
445 | |
475 | |
446 | LOG llevError, Carp::longmess "sync job";#d# |
476 | LOG llevError, Carp::longmess "sync job";#d# |
447 | |
477 | |
448 | # TODO: use suspend/resume instead |
|
|
449 | # (but this is cancel-safe) |
|
|
450 | my $freeze_guard = freeze_mainloop; |
478 | my $freeze_guard = freeze_mainloop; |
451 | |
479 | |
452 | my $busy = 1; |
480 | my $busy = 1; |
453 | my @res; |
481 | my @res; |
454 | |
482 | |
… | |
… | |
465 | } else { |
493 | } else { |
466 | EV::loop EV::LOOP_ONESHOT; |
494 | EV::loop EV::LOOP_ONESHOT; |
467 | } |
495 | } |
468 | } |
496 | } |
469 | |
497 | |
470 | $time = EV::time - $time; |
498 | my $time = EV::time - $time; |
471 | |
499 | |
472 | LOG llevError | logBacktrace, Carp::longmess "long sync job" |
|
|
473 | if $time > $TICK * 0.5 && $TICK_WATCHER->is_active; |
|
|
474 | |
|
|
475 | $tick_start += $time; # do not account sync jobs to server load |
500 | $TICK_START += $time; # do not account sync jobs to server load |
476 | |
501 | |
477 | wantarray ? @res : $res[0] |
502 | wantarray ? @res : $res[0] |
478 | } else { |
503 | } else { |
479 | # we are in another coroutine, how wonderful, everything just works |
504 | # we are in another coroutine, how wonderful, everything just works |
480 | |
505 | |
… | |
… | |
522 | reset_signals; |
547 | reset_signals; |
523 | &$cb |
548 | &$cb |
524 | }, @args; |
549 | }, @args; |
525 | |
550 | |
526 | wantarray ? @res : $res[-1] |
551 | wantarray ? @res : $res[-1] |
|
|
552 | } |
|
|
553 | |
|
|
554 | =item $coin = coin_from_name $name |
|
|
555 | |
|
|
556 | =cut |
|
|
557 | |
|
|
558 | our %coin_alias = ( |
|
|
559 | "silver" => "silvercoin", |
|
|
560 | "silvercoin" => "silvercoin", |
|
|
561 | "silvercoins" => "silvercoin", |
|
|
562 | "gold" => "goldcoin", |
|
|
563 | "goldcoin" => "goldcoin", |
|
|
564 | "goldcoins" => "goldcoin", |
|
|
565 | "platinum" => "platinacoin", |
|
|
566 | "platinumcoin" => "platinacoin", |
|
|
567 | "platinumcoins" => "platinacoin", |
|
|
568 | "platina" => "platinacoin", |
|
|
569 | "platinacoin" => "platinacoin", |
|
|
570 | "platinacoins" => "platinacoin", |
|
|
571 | "royalty" => "royalty", |
|
|
572 | "royalties" => "royalty", |
|
|
573 | ); |
|
|
574 | |
|
|
575 | sub coin_from_name($) { |
|
|
576 | $coin_alias{$_[0]} |
|
|
577 | ? cf::arch::find $coin_alias{$_[0]} |
|
|
578 | : undef |
527 | } |
579 | } |
528 | |
580 | |
529 | =item $value = cf::db_get $family => $key |
581 | =item $value = cf::db_get $family => $key |
530 | |
582 | |
531 | Returns a single value from the environment database. |
583 | Returns a single value from the environment database. |
… | |
… | |
968 | } |
1020 | } |
969 | |
1021 | |
970 | 0 |
1022 | 0 |
971 | } |
1023 | } |
972 | |
1024 | |
973 | =item $bool = cf::global::invoke (EVENT_CLASS_XXX, ...) |
1025 | =item $bool = cf::global->invoke (EVENT_CLASS_XXX, ...) |
974 | |
1026 | |
975 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
1027 | =item $bool = $attachable->invoke (EVENT_CLASS_XXX, ...) |
976 | |
1028 | |
977 | Generate an object-specific event with the given arguments. |
1029 | Generate an object-specific event with the given arguments. |
978 | |
1030 | |
… | |
… | |
1303 | my $msg = $@ ? "$v->{path}: $@\n" |
1355 | my $msg = $@ ? "$v->{path}: $@\n" |
1304 | : "$v->{base}: extension inactive.\n"; |
1356 | : "$v->{base}: extension inactive.\n"; |
1305 | |
1357 | |
1306 | if (exists $v->{meta}{mandatory}) { |
1358 | if (exists $v->{meta}{mandatory}) { |
1307 | warn $msg; |
1359 | warn $msg; |
1308 | warn "mandatory extension failed to load, exiting.\n"; |
1360 | cf::cleanup "mandatory extension failed to load, exiting."; |
1309 | exit 1; |
|
|
1310 | } |
1361 | } |
1311 | |
1362 | |
1312 | warn $msg; |
1363 | warn $msg; |
1313 | } |
1364 | } |
1314 | |
1365 | |
… | |
… | |
2325 | ? normalise $_ |
2376 | ? normalise $_ |
2326 | : () |
2377 | : () |
2327 | } @{ aio_readdir $UNIQUEDIR or [] } |
2378 | } @{ aio_readdir $UNIQUEDIR or [] } |
2328 | ] |
2379 | ] |
2329 | } |
2380 | } |
2330 | |
|
|
2331 | package cf; |
|
|
2332 | |
2381 | |
2333 | =back |
2382 | =back |
2334 | |
2383 | |
2335 | =head3 cf::object |
2384 | =head3 cf::object |
2336 | |
2385 | |
… | |
… | |
3149 | { |
3198 | { |
3150 | my $faces = $facedata->{faceinfo}; |
3199 | my $faces = $facedata->{faceinfo}; |
3151 | |
3200 | |
3152 | while (my ($face, $info) = each %$faces) { |
3201 | while (my ($face, $info) = each %$faces) { |
3153 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
3202 | my $idx = (cf::face::find $face) || cf::face::alloc $face; |
|
|
3203 | |
3154 | cf::face::set_visibility $idx, $info->{visibility}; |
3204 | cf::face::set_visibility $idx, $info->{visibility}; |
3155 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3205 | cf::face::set_magicmap $idx, $info->{magicmap}; |
3156 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3206 | cf::face::set_data $idx, 0, $info->{data32}, Digest::MD5::md5 $info->{data32}; |
3157 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3207 | cf::face::set_data $idx, 1, $info->{data64}, Digest::MD5::md5 $info->{data64}; |
3158 | |
3208 | |
3159 | cf::cede_to_tick; |
3209 | cf::cede_to_tick; |
3160 | } |
3210 | } |
3161 | |
3211 | |
3162 | while (my ($face, $info) = each %$faces) { |
3212 | while (my ($face, $info) = each %$faces) { |
3163 | next unless $info->{smooth}; |
3213 | next unless $info->{smooth}; |
|
|
3214 | |
3164 | my $idx = cf::face::find $face |
3215 | my $idx = cf::face::find $face |
3165 | or next; |
3216 | or next; |
|
|
3217 | |
3166 | if (my $smooth = cf::face::find $info->{smooth}) { |
3218 | if (my $smooth = cf::face::find $info->{smooth}) { |
3167 | cf::face::set_smooth $idx, $smooth; |
3219 | cf::face::set_smooth $idx, $smooth; |
3168 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3220 | cf::face::set_smoothlevel $idx, $info->{smoothlevel}; |
3169 | } else { |
3221 | } else { |
3170 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
3222 | warn "smooth face '$info->{smooth}' not found for face '$face'"; |
… | |
… | |
3188 | { |
3240 | { |
3189 | # TODO: for gcfclient pleasure, we should give resources |
3241 | # TODO: for gcfclient pleasure, we should give resources |
3190 | # that gcfclient doesn't grok a >10000 face index. |
3242 | # that gcfclient doesn't grok a >10000 face index. |
3191 | my $res = $facedata->{resource}; |
3243 | my $res = $facedata->{resource}; |
3192 | |
3244 | |
3193 | my $soundconf = delete $res->{"res/sound.conf"}; |
|
|
3194 | |
|
|
3195 | while (my ($name, $info) = each %$res) { |
3245 | while (my ($name, $info) = each %$res) { |
|
|
3246 | if (defined $info->{type}) { |
3196 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3247 | my $idx = (cf::face::find $name) || cf::face::alloc $name; |
3197 | my $data; |
3248 | my $data; |
3198 | |
3249 | |
3199 | if ($info->{type} & 1) { |
3250 | if ($info->{type} & 1) { |
3200 | # prepend meta info |
3251 | # prepend meta info |
3201 | |
3252 | |
3202 | my $meta = $enc->encode ({ |
3253 | my $meta = $enc->encode ({ |
3203 | name => $name, |
3254 | name => $name, |
3204 | %{ $info->{meta} || {} }, |
3255 | %{ $info->{meta} || {} }, |
3205 | }); |
3256 | }); |
3206 | |
3257 | |
3207 | $data = pack "(w/a*)*", $meta, $info->{data}; |
3258 | $data = pack "(w/a*)*", $meta, $info->{data}; |
|
|
3259 | } else { |
|
|
3260 | $data = $info->{data}; |
|
|
3261 | } |
|
|
3262 | |
|
|
3263 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3264 | cf::face::set_type $idx, $info->{type}; |
3208 | } else { |
3265 | } else { |
3209 | $data = $info->{data}; |
3266 | $RESOURCE{$name} = $info; |
3210 | } |
3267 | } |
3211 | |
3268 | |
3212 | cf::face::set_data $idx, 0, $data, Digest::MD5::md5 $data; |
|
|
3213 | cf::face::set_type $idx, $info->{type}; |
|
|
3214 | |
|
|
3215 | cf::cede_to_tick; |
3269 | cf::cede_to_tick; |
3216 | } |
3270 | } |
3217 | |
|
|
3218 | if ($soundconf) { |
|
|
3219 | $soundconf = $enc->decode (delete $soundconf->{data}); |
|
|
3220 | |
|
|
3221 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3222 | my $sound = $soundconf->{compat}[$_] |
|
|
3223 | or next; |
|
|
3224 | |
|
|
3225 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3226 | cf::sound::set $sound->[0] => $face; |
|
|
3227 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3228 | } |
|
|
3229 | |
|
|
3230 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3231 | my $face = cf::face::find "sound/$v"; |
|
|
3232 | cf::sound::set $k => $face; |
|
|
3233 | } |
|
|
3234 | } |
|
|
3235 | } |
3271 | } |
|
|
3272 | |
|
|
3273 | cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); |
3236 | |
3274 | |
3237 | 1 |
3275 | 1 |
3238 | } |
3276 | } |
|
|
3277 | |
|
|
3278 | cf::global->attach (on_resource_update => sub { |
|
|
3279 | if (my $soundconf = $RESOURCE{"res/sound.conf"}) { |
|
|
3280 | $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data}); |
|
|
3281 | |
|
|
3282 | for (0 .. SOUND_CAST_SPELL_0 - 1) { |
|
|
3283 | my $sound = $soundconf->{compat}[$_] |
|
|
3284 | or next; |
|
|
3285 | |
|
|
3286 | my $face = cf::face::find "sound/$sound->[1]"; |
|
|
3287 | cf::sound::set $sound->[0] => $face; |
|
|
3288 | cf::sound::old_sound_index $_, $face; # gcfclient-compat |
|
|
3289 | } |
|
|
3290 | |
|
|
3291 | while (my ($k, $v) = each %{$soundconf->{event}}) { |
|
|
3292 | my $face = cf::face::find "sound/$v"; |
|
|
3293 | cf::sound::set $k => $face; |
|
|
3294 | } |
|
|
3295 | } |
|
|
3296 | }); |
3239 | |
3297 | |
3240 | register_exticmd fx_want => sub { |
3298 | register_exticmd fx_want => sub { |
3241 | my ($ns, $want) = @_; |
3299 | my ($ns, $want) = @_; |
3242 | |
3300 | |
3243 | while (my ($k, $v) = each %$want) { |
3301 | while (my ($k, $v) = each %$want) { |
… | |
… | |
3298 | sub reload_config { |
3356 | sub reload_config { |
3299 | open my $fh, "<:utf8", "$CONFDIR/config" |
3357 | open my $fh, "<:utf8", "$CONFDIR/config" |
3300 | or return; |
3358 | or return; |
3301 | |
3359 | |
3302 | local $/; |
3360 | local $/; |
3303 | *CFG = YAML::Syck::Load <$fh>; |
3361 | *CFG = YAML::Load <$fh>; |
3304 | |
3362 | |
3305 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3363 | $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; |
3306 | |
3364 | |
3307 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3365 | $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; |
3308 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
3366 | $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; |
… | |
… | |
3328 | |
3386 | |
3329 | reload_config; |
3387 | reload_config; |
3330 | db_init; |
3388 | db_init; |
3331 | load_extensions; |
3389 | load_extensions; |
3332 | |
3390 | |
3333 | $TICK_WATCHER->start; |
|
|
3334 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3391 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
|
|
3392 | evthread_start IO::AIO::poll_fileno; |
3335 | EV::loop; |
3393 | EV::loop; |
3336 | } |
3394 | } |
3337 | |
3395 | |
3338 | ############################################################################# |
3396 | ############################################################################# |
3339 | # initialisation and cleanup |
3397 | # initialisation and cleanup |
… | |
… | |
3382 | and return; |
3440 | and return; |
3383 | |
3441 | |
3384 | warn "runtime file written.\n"; |
3442 | warn "runtime file written.\n"; |
3385 | |
3443 | |
3386 | 1 |
3444 | 1 |
|
|
3445 | } |
|
|
3446 | |
|
|
3447 | our $uuid_lock; |
|
|
3448 | our $uuid_skip; |
|
|
3449 | |
|
|
3450 | sub write_uuid_sync($) { |
|
|
3451 | $uuid_skip ||= $_[0]; |
|
|
3452 | |
|
|
3453 | return if $uuid_lock; |
|
|
3454 | local $uuid_lock = 1; |
|
|
3455 | |
|
|
3456 | my $uuid = "$LOCALDIR/uuid"; |
|
|
3457 | |
|
|
3458 | my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 |
|
|
3459 | or return; |
|
|
3460 | |
|
|
3461 | my $value = uuid_str $uuid_skip + uuid_seq uuid_cur; |
|
|
3462 | $uuid_skip = 0; |
|
|
3463 | |
|
|
3464 | (aio_write $fh, 0, (length $value), $value, 0) <= 0 |
|
|
3465 | and return; |
|
|
3466 | |
|
|
3467 | # always fsync - this file is important |
|
|
3468 | aio_fsync $fh |
|
|
3469 | and return; |
|
|
3470 | |
|
|
3471 | close $fh |
|
|
3472 | or return; |
|
|
3473 | |
|
|
3474 | aio_rename "$uuid~", $uuid |
|
|
3475 | and return; |
|
|
3476 | |
|
|
3477 | warn "uuid file written ($value).\n"; |
|
|
3478 | |
|
|
3479 | 1 |
|
|
3480 | |
|
|
3481 | } |
|
|
3482 | |
|
|
3483 | sub write_uuid($$) { |
|
|
3484 | my ($skip, $sync) = @_; |
|
|
3485 | |
|
|
3486 | $sync ? write_uuid_sync $skip |
|
|
3487 | : async { write_uuid_sync $skip }; |
3387 | } |
3488 | } |
3388 | |
3489 | |
3389 | sub emergency_save() { |
3490 | sub emergency_save() { |
3390 | my $freeze_guard = cf::freeze_mainloop; |
3491 | my $freeze_guard = cf::freeze_mainloop; |
3391 | |
3492 | |
… | |
… | |
3413 | warn "end emergency map save\n"; |
3514 | warn "end emergency map save\n"; |
3414 | |
3515 | |
3415 | warn "begin emergency database checkpoint\n"; |
3516 | warn "begin emergency database checkpoint\n"; |
3416 | BDB::db_env_txn_checkpoint $DB_ENV; |
3517 | BDB::db_env_txn_checkpoint $DB_ENV; |
3417 | warn "end emergency database checkpoint\n"; |
3518 | warn "end emergency database checkpoint\n"; |
|
|
3519 | |
|
|
3520 | warn "begin write uuid\n"; |
|
|
3521 | write_uuid_sync 1; |
|
|
3522 | warn "end write uuid\n"; |
3418 | }; |
3523 | }; |
3419 | |
3524 | |
3420 | warn "leave emergency perl save\n"; |
3525 | warn "leave emergency perl save\n"; |
3421 | } |
3526 | } |
3422 | |
3527 | |
… | |
… | |
3536 | warn "leaving sync_job"; |
3641 | warn "leaving sync_job"; |
3537 | |
3642 | |
3538 | 1 |
3643 | 1 |
3539 | } or do { |
3644 | } or do { |
3540 | warn $@; |
3645 | warn $@; |
3541 | warn "error while reloading, exiting."; |
3646 | cf::cleanup "error while reloading, exiting."; |
3542 | exit 1; |
|
|
3543 | }; |
3647 | }; |
3544 | |
3648 | |
3545 | warn "reloaded"; |
3649 | warn "reloaded"; |
3546 | }; |
3650 | }; |
3547 | |
3651 | |
… | |
… | |
3550 | sub reload_perl() { |
3654 | sub reload_perl() { |
3551 | # doing reload synchronously and two reloads happen back-to-back, |
3655 | # doing reload synchronously and two reloads happen back-to-back, |
3552 | # coro crashes during coro_state_free->destroy here. |
3656 | # coro crashes during coro_state_free->destroy here. |
3553 | |
3657 | |
3554 | $RELOAD_WATCHER ||= EV::timer 0, 0, sub { |
3658 | $RELOAD_WATCHER ||= EV::timer 0, 0, sub { |
|
|
3659 | do_reload_perl; |
3555 | undef $RELOAD_WATCHER; |
3660 | undef $RELOAD_WATCHER; |
3556 | do_reload_perl; |
|
|
3557 | }; |
3661 | }; |
3558 | } |
3662 | } |
3559 | |
3663 | |
3560 | register_command "reload" => sub { |
3664 | register_command "reload" => sub { |
3561 | my ($who, $arg) = @_; |
3665 | my ($who, $arg) = @_; |
… | |
… | |
3575 | |
3679 | |
3576 | our @WAIT_FOR_TICK; |
3680 | our @WAIT_FOR_TICK; |
3577 | our @WAIT_FOR_TICK_BEGIN; |
3681 | our @WAIT_FOR_TICK_BEGIN; |
3578 | |
3682 | |
3579 | sub wait_for_tick { |
3683 | sub wait_for_tick { |
3580 | return unless $TICK_WATCHER->is_active; |
3684 | return if tick_inhibit; |
3581 | return if $Coro::current == $Coro::main; |
3685 | return if $Coro::current == $Coro::main; |
3582 | |
3686 | |
3583 | my $signal = new Coro::Signal; |
3687 | my $signal = new Coro::Signal; |
3584 | push @WAIT_FOR_TICK, $signal; |
3688 | push @WAIT_FOR_TICK, $signal; |
3585 | $signal->wait; |
3689 | $signal->wait; |
3586 | } |
3690 | } |
3587 | |
3691 | |
3588 | sub wait_for_tick_begin { |
3692 | sub wait_for_tick_begin { |
3589 | return unless $TICK_WATCHER->is_active; |
3693 | return if tick_inhibit; |
3590 | return if $Coro::current == $Coro::main; |
3694 | return if $Coro::current == $Coro::main; |
3591 | |
3695 | |
3592 | my $signal = new Coro::Signal; |
3696 | my $signal = new Coro::Signal; |
3593 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3697 | push @WAIT_FOR_TICK_BEGIN, $signal; |
3594 | $signal->wait; |
3698 | $signal->wait; |
3595 | } |
3699 | } |
3596 | |
3700 | |
3597 | $TICK_WATCHER = EV::periodic_ns 0, $TICK, 0, sub { |
3701 | sub tick { |
3598 | if ($Coro::current != $Coro::main) { |
3702 | if ($Coro::current != $Coro::main) { |
3599 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
3703 | Carp::cluck "major BUG: server tick called outside of main coro, skipping it" |
3600 | unless ++$bug_warning > 10; |
3704 | unless ++$bug_warning > 10; |
3601 | return; |
3705 | return; |
3602 | } |
3706 | } |
3603 | |
3707 | |
3604 | $NOW = $tick_start = EV::now; |
|
|
3605 | |
|
|
3606 | cf::server_tick; # one server iteration |
3708 | cf::server_tick; # one server iteration |
3607 | |
3709 | |
3608 | $RUNTIME += $TICK; |
|
|
3609 | $NEXT_TICK += $TICK; |
|
|
3610 | |
|
|
3611 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3710 | if ($NOW >= $NEXT_RUNTIME_WRITE) { |
3612 | $NEXT_RUNTIME_WRITE = $NOW + 10; |
3711 | $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; |
3613 | Coro::async_pool { |
3712 | Coro::async_pool { |
3614 | $Coro::current->{desc} = "runtime saver"; |
3713 | $Coro::current->{desc} = "runtime saver"; |
3615 | write_runtime |
3714 | write_runtime |
3616 | or warn "ERROR: unable to write runtime file: $!"; |
3715 | or warn "ERROR: unable to write runtime file: $!"; |
3617 | }; |
3716 | }; |
… | |
… | |
3622 | } |
3721 | } |
3623 | while (my $sig = shift @WAIT_FOR_TICK) { |
3722 | while (my $sig = shift @WAIT_FOR_TICK) { |
3624 | $sig->send; |
3723 | $sig->send; |
3625 | } |
3724 | } |
3626 | |
3725 | |
3627 | $LOAD = ($NOW - $tick_start) / $TICK; |
3726 | $LOAD = ($NOW - $TICK_START) / $TICK; |
3628 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
3727 | $LOADAVG = $LOADAVG * 0.75 + $LOAD * 0.25; |
3629 | |
3728 | |
3630 | _post_tick; |
3729 | if (0) { |
3631 | }; |
3730 | if ($NEXT_TICK) { |
3632 | $TICK_WATCHER->priority (EV::MAXPRI); |
3731 | my $jitter = $TICK_START - $NEXT_TICK; |
|
|
3732 | $JITTER = $JITTER * 0.75 + $jitter * 0.25; |
|
|
3733 | warn "jitter $JITTER\n";#d# |
|
|
3734 | } |
|
|
3735 | } |
|
|
3736 | } |
3633 | |
3737 | |
3634 | { |
3738 | { |
|
|
3739 | # configure BDB |
|
|
3740 | |
3635 | BDB::min_parallel 8; |
3741 | BDB::min_parallel 8; |
3636 | BDB::max_poll_reqs $TICK * 0.1; |
3742 | BDB::max_poll_reqs $TICK * 0.1; |
|
|
3743 | $Coro::BDB::WATCHER->priority (1); |
3637 | |
3744 | |
3638 | unless ($DB_ENV) { |
3745 | unless ($DB_ENV) { |
3639 | $DB_ENV = BDB::db_env_create; |
3746 | $DB_ENV = BDB::db_env_create; |
3640 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
3747 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC |
3641 | | BDB::LOG_AUTOREMOVE, 1); |
3748 | | BDB::LOG_AUTOREMOVE, 1); |
… | |
… | |
3668 | BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; |
3775 | BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { }; |
3669 | }; |
3776 | }; |
3670 | } |
3777 | } |
3671 | |
3778 | |
3672 | { |
3779 | { |
|
|
3780 | # configure IO::AIO |
|
|
3781 | |
3673 | IO::AIO::min_parallel 8; |
3782 | IO::AIO::min_parallel 8; |
3674 | |
|
|
3675 | undef $Coro::AIO::WATCHER; |
|
|
3676 | IO::AIO::max_poll_time $TICK * 0.1; |
3783 | IO::AIO::max_poll_time $TICK * 0.1; |
3677 | $AIO_POLL_WATCHER = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb; |
3784 | $Coro::AIO::WATCHER->priority (1); |
3678 | } |
3785 | } |
3679 | |
3786 | |
3680 | my $_log_backtrace; |
3787 | my $_log_backtrace; |
3681 | |
3788 | |
3682 | sub _log_backtrace { |
3789 | sub _log_backtrace { |