ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.519 by root, Fri Apr 16 02:32:25 2010 UTC vs.
Revision 1.559 by root, Sat Feb 26 12:50:27 2011 UTC

20# The authors can be reached via e-mail to <support@deliantra.net> 20# The authors can be reached via e-mail to <support@deliantra.net>
21# 21#
22 22
23package cf; 23package cf;
24 24
25use 5.10.0; 25use common::sense;
26use utf8;
27use strict qw(vars subs);
28 26
29use Symbol; 27use Symbol;
30use List::Util; 28use List::Util;
31use Socket; 29use Socket;
32use EV; 30use EV;
78# strictly for debugging 76# strictly for debugging
79$SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; 77$SIG{QUIT} = sub { Carp::cluck "SIGQUIT" };
80 78
81sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload 79sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
82 80
81our @ORIG_INC;
82
83our %COMMAND = (); 83our %COMMAND = ();
84our %COMMAND_TIME = (); 84our %COMMAND_TIME = ();
85 85
86our @EXTS = (); # list of extension package names 86our @EXTS = (); # list of extension package names
87our %EXTCMD = (); 87our %EXTCMD = ();
93our @EVENT; 93our @EVENT;
94our @REFLECT; # set by XS 94our @REFLECT; # set by XS
95our %REFLECT; # set by us 95our %REFLECT; # set by us
96 96
97our $CONFDIR = confdir; 97our $CONFDIR = confdir;
98
98our $DATADIR = datadir; 99our $DATADIR = datadir;
99our $LIBDIR = "$DATADIR/ext"; 100our $LIBDIR = "$DATADIR/ext";
100our $PODDIR = "$DATADIR/pod"; 101our $PODDIR = "$DATADIR/pod";
101our $MAPDIR = "$DATADIR/" . mapdir; 102our $MAPDIR = "$DATADIR/" . mapdir;
103
102our $LOCALDIR = localdir; 104our $LOCALDIR = localdir;
103our $TMPDIR = "$LOCALDIR/" . tmpdir; 105our $TMPDIR = "$LOCALDIR/" . tmpdir;
104our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; 106our $UNIQUEDIR = "$LOCALDIR/" . uniquedir;
105our $PLAYERDIR = "$LOCALDIR/" . playerdir; 107our $PLAYERDIR = "$LOCALDIR/" . playerdir;
106our $RANDOMDIR = "$LOCALDIR/random"; 108our $RANDOMDIR = "$LOCALDIR/random";
107our $BDBDIR = "$LOCALDIR/db"; 109our $BDBDIR = "$LOCALDIR/db";
108our $PIDFILE = "$LOCALDIR/pid"; 110our $PIDFILE = "$LOCALDIR/pid";
109our $RUNTIMEFILE = "$LOCALDIR/runtime"; 111our $RUNTIMEFILE = "$LOCALDIR/runtime";
110 112
111our %RESOURCE; 113our %RESOURCE; # unused
112 114
113our $OUTPUT_RATE_MIN = 4000; 115our $OUTPUT_RATE_MIN = 3000;
114our $OUTPUT_RATE_MAX = 100000; 116our $OUTPUT_RATE_MAX = 1000000;
117
118our $MAX_LINKS = 32; # how many chained exits to follow
119our $VERBOSE_IO = 1;
115 120
116our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) 121our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!)
117our $NEXT_RUNTIME_WRITE; # when should the runtime file be written 122our $NEXT_RUNTIME_WRITE; # when should the runtime file be written
118our $NEXT_TICK; 123our $NEXT_TICK;
119our $USE_FSYNC = 1; # use fsync to write maps - default on 124our $USE_FSYNC = 1; # use fsync to write maps - default on
121our $BDB_DEADLOCK_WATCHER; 126our $BDB_DEADLOCK_WATCHER;
122our $BDB_CHECKPOINT_WATCHER; 127our $BDB_CHECKPOINT_WATCHER;
123our $BDB_TRICKLE_WATCHER; 128our $BDB_TRICKLE_WATCHER;
124our $DB_ENV; 129our $DB_ENV;
125 130
126our @EXTRA_MODULES = qw(pod match mapscript); 131our @EXTRA_MODULES = qw(pod match mapscript incloader);
127 132
128our %CFG; 133our %CFG;
129 134
130our $UPTIME; $UPTIME ||= time; 135our $UPTIME; $UPTIME ||= time;
131our $RUNTIME; 136our $RUNTIME;
165 170
166our $EMERGENCY_POSITION; 171our $EMERGENCY_POSITION;
167 172
168sub cf::map::normalise; 173sub cf::map::normalise;
169 174
175sub in_main() {
176 $Coro::current == $Coro::main
177}
178
170############################################################################# 179#############################################################################
171 180
172%REFLECT = (); 181%REFLECT = ();
173for (@REFLECT) { 182for (@REFLECT) {
174 my $reflect = JSON::XS::decode_json $_; 183 my $reflect = JSON::XS::decode_json $_;
226from wherever your confdir points to. 235from wherever your confdir points to.
227 236
228=item cf::wait_for_tick, cf::wait_for_tick_begin 237=item cf::wait_for_tick, cf::wait_for_tick_begin
229 238
230These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only 239These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only
231returns directly I<after> the tick processing (and consequently, can only wake one process 240returns directly I<after> the tick processing (and consequently, can only wake one thread
232per tick), while cf::wait_for_tick wakes up all waiters after tick processing. 241per tick), while cf::wait_for_tick wakes up all waiters after tick processing.
242
243=cut
244
245sub wait_for_tick();
246sub wait_for_tick_begin();
233 247
234=item @cf::INVOKE_RESULTS 248=item @cf::INVOKE_RESULTS
235 249
236This array contains the results of the last C<invoke ()> call. When 250This array contains the results of the last C<invoke ()> call. When
237C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of 251C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of
245 259
246=back 260=back
247 261
248=cut 262=cut
249 263
264sub error(@) { LOG llevError, join "", @_ }
265sub warn (@) { LOG llevWarn , join "", @_ }
266sub info (@) { LOG llevInfo , join "", @_ }
267sub debug(@) { LOG llevDebug, join "", @_ }
268sub trace(@) { LOG llevTrace, join "", @_ }
269
250$Coro::State::WARNHOOK = sub { 270$Coro::State::WARNHOOK = sub {
251 my $msg = join "", @_; 271 my $msg = join "", @_;
252 272
253 $msg .= "\n" 273 $msg .= "\n"
254 unless $msg =~ /\n$/; 274 unless $msg =~ /\n$/;
255 275
256 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; 276 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge;
257 277
258 LOG llevError, $msg; 278 LOG llevWarn, $msg;
259}; 279};
260 280
261$Coro::State::DIEHOOK = sub { 281$Coro::State::DIEHOOK = sub {
262 return unless $^S eq 0; # "eq", not "==" 282 return unless $^S eq 0; # "eq", not "=="
263 283
264 warn Carp::longmess $_[0]; 284 error Carp::longmess $_[0];
265 285
266 if ($Coro::current == $Coro::main) {#d# 286 if (in_main) {#d#
267 warn "DIEHOOK called in main context, Coro bug?\n";#d# 287 error "DIEHOOK called in main context, Coro bug?\n";#d#
268 return;#d# 288 return;#d#
269 }#d# 289 }#d#
270 290
271 # kill coroutine otherwise 291 # kill coroutine otherwise
272 Coro::terminate 292 Coro::terminate
339our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max 359our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max
340 360
341sub encode_json($) { $json_coder->encode ($_[0]) } 361sub encode_json($) { $json_coder->encode ($_[0]) }
342sub decode_json($) { $json_coder->decode ($_[0]) } 362sub decode_json($) { $json_coder->decode ($_[0]) }
343 363
364=item $ref = cf::yaml_load $scalar
365
366Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks).
367
368=cut
369
370sub fork_call(&@);
371
372sub yaml_load($) {
373 fork_call { YAML::XS::Load $_[0] } @_
374}
375
344=item cf::post_init { BLOCK } 376=item cf::post_init { BLOCK }
345 377
346Execute the given codeblock, I<after> all extensions have been (re-)loaded, 378Execute the given codeblock, I<after> all extensions have been (re-)loaded,
347but I<before> the server starts ticking again. 379but I<before> the server starts ticking again.
348 380
349The cdoeblock will have a single boolean argument to indicate whether this 381The codeblock will have a single boolean argument to indicate whether this
350is a reload or not. 382is a reload or not.
351 383
352=cut 384=cut
353 385
354sub post_init(&) { 386sub post_init(&) {
355 push @POST_INIT, shift; 387 push @POST_INIT, shift;
388}
389
390sub _post_init {
391 trace "running post_init jobs";
392
393 # run them in parallel...
394
395 my @join;
396
397 while () {
398 push @join, map &Coro::async ($_, 0), @POST_INIT;
399 @POST_INIT = ();
400
401 @join or last;
402
403 (pop @join)->join;
404 }
356} 405}
357 406
358=item cf::lock_wait $string 407=item cf::lock_wait $string
359 408
360Wait until the given lock is available. See cf::lock_acquire. 409Wait until the given lock is available. See cf::lock_acquire.
505=cut 554=cut
506 555
507sub sync_job(&) { 556sub sync_job(&) {
508 my ($job) = @_; 557 my ($job) = @_;
509 558
510 if ($Coro::current == $Coro::main) { 559 if (in_main) {
511 my $time = AE::time; 560 my $time = AE::time;
512 561
513 # this is the main coro, too bad, we have to block 562 # this is the main coro, too bad, we have to block
514 # till the operation succeeds, freezing the server :/ 563 # till the operation succeeds, freezing the server :/
515 564
516 LOG llevError, Carp::longmess "sync job";#d# 565 #LOG llevError, Carp::longmess "sync job";#d#
517 566
518 my $freeze_guard = freeze_mainloop; 567 my $freeze_guard = freeze_mainloop;
519 568
520 my $busy = 1; 569 my $busy = 1;
521 my @res; 570 my @res;
522 571
523 (async { 572 (async {
524 $Coro::current->desc ("sync job coro"); 573 $Coro::current->desc ("sync job coro");
525 @res = eval { $job->() }; 574 @res = eval { $job->() };
526 warn $@ if $@; 575 error $@ if $@;
527 undef $busy; 576 undef $busy;
528 })->prio (Coro::PRIO_MAX); 577 })->prio (Coro::PRIO_MAX);
529 578
530 while ($busy) { 579 while ($busy) {
531 if (Coro::nready) { 580 if (Coro::nready) {
565 $EXT_CORO{$coro+0} = $coro; 614 $EXT_CORO{$coro+0} = $coro;
566 615
567 $coro 616 $coro
568} 617}
569 618
570=item fork_call { }, $args 619=item fork_call { }, @args
571 620
572Executes the given code block with the given arguments in a seperate 621Executes the given code block with the given arguments in a seperate
573process, returning the results. Everything must be serialisable with 622process, returning the results. Everything must be serialisable with
574Coro::Storable. May, of course, block. Note that the executed sub may 623Coro::Storable. May, of course, block. Note that the executed sub may
575never block itself or use any form of event handling. 624never block itself or use any form of event handling.
576 625
577=cut 626=cut
578 627
628sub post_fork {
629 reset_signals;
630}
631
579sub fork_call(&@) { 632sub fork_call(&@) {
580 my ($cb, @args) = @_; 633 my ($cb, @args) = @_;
581 634
582 # we seemingly have to make a local copy of the whole thing, 635 # we seemingly have to make a local copy of the whole thing,
583 # otherwise perl prematurely frees the stuff :/ 636 # otherwise perl prematurely frees the stuff :/
584 # TODO: investigate and fix (likely this will be rather laborious) 637 # TODO: investigate and fix (likely this will be rather laborious)
585 638
586 my @res = Coro::Util::fork_eval { 639 my @res = Coro::Util::fork_eval {
587 reset_signals; 640 cf::post_fork;
588 &$cb 641 &$cb
589 }, @args; 642 } @args;
590 643
591 wantarray ? @res : $res[-1] 644 wantarray ? @res : $res[-1]
592} 645}
593 646
594sub objinfo { 647sub objinfo {
644within each server. 697within each server.
645 698
646=cut 699=cut
647 700
648sub db_table($) { 701sub db_table($) {
702 cf::error "db_get called from main context"
703 if $Coro::current == $Coro::main;
704
649 my ($name) = @_; 705 my ($name) = @_;
650 my $db = BDB::db_create $DB_ENV; 706 my $db = BDB::db_create $DB_ENV;
651 707
652 eval { 708 eval {
653 $db->set_flags (BDB::CHKSUM); 709 $db->set_flags (BDB::CHKSUM);
663} 719}
664 720
665our $DB; 721our $DB;
666 722
667sub db_init { 723sub db_init {
668 cf::sync_job {
669 $DB ||= db_table "db"; 724 $DB ||= db_table "db";
670 };
671} 725}
672 726
673sub db_get($$) { 727sub db_get($$) {
674 my $key = "$_[0]/$_[1]"; 728 my $key = "$_[0]/$_[1]";
675 729
676 cf::sync_job { 730 cf::error "db_get called from main context"
731 if $Coro::current == $Coro::main;
732
677 BDB::db_get $DB, undef, $key, my $data; 733 BDB::db_get $DB, undef, $key, my $data;
678 734
679 $! ? () 735 $! ? ()
680 : $data 736 : $data
681 }
682} 737}
683 738
684sub db_put($$$) { 739sub db_put($$$) {
685 BDB::dbreq_pri 4; 740 BDB::dbreq_pri 4;
686 BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; 741 BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { };
742 797
743 my $t1 = Time::HiRes::time; 798 my $t1 = Time::HiRes::time;
744 my $data = $process->(\@data); 799 my $data = $process->(\@data);
745 my $t2 = Time::HiRes::time; 800 my $t2 = Time::HiRes::time;
746 801
747 warn "cache: '$id' processed in ", $t2 - $t1, "s\n"; 802 info "cache: '$id' processed in ", $t2 - $t1, "s\n";
748 803
749 db_put cache => "$id/data", $data; 804 db_put cache => "$id/data", $data;
750 db_put cache => "$id/md5" , $md5; 805 db_put cache => "$id/md5" , $md5;
751 db_put cache => "$id/meta", $meta; 806 db_put cache => "$id/meta", $meta;
752 807
762 817
763=cut 818=cut
764 819
765sub datalog($@) { 820sub datalog($@) {
766 my ($type, %kv) = @_; 821 my ($type, %kv) = @_;
767 warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); 822 info "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type });
768} 823}
769 824
770=back 825=back
771 826
772=cut 827=cut
967 1022
968 } elsif (exists $cb_id{$type}) { 1023 } elsif (exists $cb_id{$type}) {
969 _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; 1024 _attach_cb $registry, $cb_id{$type}, $prio, shift @arg;
970 1025
971 } elsif (ref $type) { 1026 } elsif (ref $type) {
972 warn "attaching objects not supported, ignoring.\n"; 1027 error "attaching objects not supported, ignoring.\n";
973 1028
974 } else { 1029 } else {
975 shift @arg; 1030 shift @arg;
976 warn "attach argument '$type' not supported, ignoring.\n"; 1031 error "attach argument '$type' not supported, ignoring.\n";
977 } 1032 }
978 } 1033 }
979} 1034}
980 1035
981sub _object_attach { 1036sub _object_attach {
991 _attach $registry, $klass, @attach; 1046 _attach $registry, $klass, @attach;
992 } 1047 }
993 1048
994 $obj->{$name} = \%arg; 1049 $obj->{$name} = \%arg;
995 } else { 1050 } else {
996 warn "object uses attachment '$name' which is not available, postponing.\n"; 1051 info "object uses attachment '$name' which is not available, postponing.\n";
997 } 1052 }
998 1053
999 $obj->{_attachment}{$name} = undef; 1054 $obj->{_attachment}{$name} = undef;
1000} 1055}
1001 1056
1060 1115
1061 for (@$callbacks) { 1116 for (@$callbacks) {
1062 eval { &{$_->[1]} }; 1117 eval { &{$_->[1]} };
1063 1118
1064 if ($@) { 1119 if ($@) {
1065 warn "$@";
1066 warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; 1120 error "$@", "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n";
1067 override; 1121 override;
1068 } 1122 }
1069 1123
1070 return 1 if $override; 1124 return 1 if $override;
1071 } 1125 }
1150 for (@$attach) { 1204 for (@$attach) {
1151 my ($klass, @attach) = @$_; 1205 my ($klass, @attach) = @$_;
1152 _attach $registry, $klass, @attach; 1206 _attach $registry, $klass, @attach;
1153 } 1207 }
1154 } else { 1208 } else {
1155 warn "object uses attachment '$name' that is not available, postponing.\n"; 1209 info "object uses attachment '$name' that is not available, postponing.\n";
1156 } 1210 }
1157 } 1211 }
1158} 1212}
1159 1213
1160cf::attachable->attach ( 1214cf::attachable->attach (
1187 my ($filename, $rdata, $objs) = @_; 1241 my ($filename, $rdata, $objs) = @_;
1188 1242
1189 sync_job { 1243 sync_job {
1190 if (length $$rdata) { 1244 if (length $$rdata) {
1191 utf8::decode (my $decname = $filename); 1245 utf8::decode (my $decname = $filename);
1192 warn sprintf "saving %s (%d,%d)\n", 1246 trace sprintf "saving %s (%d,%d)\n",
1193 $decname, length $$rdata, scalar @$objs; 1247 $decname, length $$rdata, scalar @$objs
1248 if $VERBOSE_IO;
1194 1249
1195 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { 1250 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) {
1196 aio_chmod $fh, SAVE_MODE; 1251 aio_chmod $fh, SAVE_MODE;
1197 aio_write $fh, 0, (length $$rdata), $$rdata, 0; 1252 aio_write $fh, 0, (length $$rdata), $$rdata, 0;
1198 if ($cf::USE_FSYNC) { 1253 if ($cf::USE_FSYNC) {
1220 aio_rename "$filename~", $filename; 1275 aio_rename "$filename~", $filename;
1221 1276
1222 $filename =~ s%/[^/]+$%%; 1277 $filename =~ s%/[^/]+$%%;
1223 aio_pathsync $filename if $cf::USE_FSYNC; 1278 aio_pathsync $filename if $cf::USE_FSYNC;
1224 } else { 1279 } else {
1225 warn "unable to save objects: $filename~: $!\n"; 1280 error "unable to save objects: $filename~: $!\n";
1226 } 1281 }
1227 } else { 1282 } else {
1228 aio_unlink $filename; 1283 aio_unlink $filename;
1229 aio_unlink "$filename.pst"; 1284 aio_unlink "$filename.pst";
1230 } 1285 }
1254 my $st = eval { Coro::Storable::thaw $av }; 1309 my $st = eval { Coro::Storable::thaw $av };
1255 $av = $st->{objs}; 1310 $av = $st->{objs};
1256 } 1311 }
1257 1312
1258 utf8::decode (my $decname = $filename); 1313 utf8::decode (my $decname = $filename);
1259 warn sprintf "loading %s (%d,%d)\n", 1314 trace sprintf "loading %s (%d,%d)\n",
1260 $decname, length $data, scalar @{$av || []}; 1315 $decname, length $data, scalar @{$av || []}
1316 if $VERBOSE_IO;
1261 1317
1262 ($data, $av) 1318 ($data, $av)
1263} 1319}
1264 1320
1265=head2 COMMAND CALLBACKS 1321=head2 COMMAND CALLBACKS
1357 1413
1358 $pl->ext_reply ($reply, @reply) 1414 $pl->ext_reply ($reply, @reply)
1359 if $reply; 1415 if $reply;
1360 1416
1361 } else { 1417 } else {
1362 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 1418 error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
1363 } 1419 }
1364 1420
1365 cf::override; 1421 cf::override;
1366 }, 1422 },
1367); 1423);
1378 1434
1379 $grp 1435 $grp
1380} 1436}
1381 1437
1382sub load_extensions { 1438sub load_extensions {
1439 info "loading extensions...";
1440
1383 cf::sync_job { 1441 cf::sync_job {
1384 my %todo; 1442 my %todo;
1385 1443
1386 for my $path (<$LIBDIR/*.ext>) { 1444 for my $path (<$LIBDIR/*.ext>) {
1387 next unless -r $path; 1445 next unless -r $path;
1405 1463
1406 $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } 1464 $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 }
1407 if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; 1465 if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m;
1408 1466
1409 $ext{source} = 1467 $ext{source} =
1410 "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" 1468 "package $pkg; use common::sense;\n"
1411 . "#line 1 \"$path\"\n{\n" 1469 . "#line 1 \"$path\"\n{\n"
1412 . $source 1470 . $source
1413 . "\n};\n1"; 1471 . "\n};\n1";
1414 1472
1415 $todo{$base} = \%ext; 1473 $todo{$base} = \%ext;
1427 for (split /,\s*/, $v->{meta}{depends}) { 1485 for (split /,\s*/, $v->{meta}{depends}) {
1428 next ext 1486 next ext
1429 unless exists $done{$_}; 1487 unless exists $done{$_};
1430 } 1488 }
1431 1489
1432 warn "... pass $pass, loading '$k' into '$v->{pkg}'\n"; 1490 trace "... pass $pass, loading '$k' into '$v->{pkg}'\n";
1433 1491
1434 my $active = eval $v->{source}; 1492 my $active = eval $v->{source};
1435 1493
1436 if (length $@) { 1494 if (length $@) {
1437 warn "$v->{path}: $@\n"; 1495 error "$v->{path}: $@\n";
1438 1496
1439 cf::cleanup "mandatory extension '$k' failed to load, exiting." 1497 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1440 if exists $v->{meta}{mandatory}; 1498 if exists $v->{meta}{mandatory};
1499
1500 warn "$v->{base}: optional extension cannot be loaded, skipping.\n";
1501 delete $todo{$k};
1441 } else { 1502 } else {
1442 $done{$k} = delete $todo{$k}; 1503 $done{$k} = delete $todo{$k};
1443 push @EXTS, $v->{pkg}; 1504 push @EXTS, $v->{pkg};
1444 $progress = 1; 1505 $progress = 1;
1445 1506
1446 warn "$v->{base}: extension inactive.\n" 1507 info "$v->{base}: extension inactive.\n"
1447 unless $active; 1508 unless $active;
1448 } 1509 }
1449 } 1510 }
1450 1511
1451 unless ($progress) { 1512 unless ($progress) {
1620 my $name = $pl->ob->name; 1681 my $name = $pl->ob->name;
1621 1682
1622 $pl->{deny_save} = 1; 1683 $pl->{deny_save} = 1;
1623 $pl->password ("*"); # this should lock out the player until we have nuked the dir 1684 $pl->password ("*"); # this should lock out the player until we have nuked the dir
1624 1685
1625 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; 1686 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns;
1626 $pl->deactivate; 1687 $pl->deactivate;
1688
1627 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; 1689 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy;
1628 $pl->ob->check_score;
1629 $pl->invoke (cf::EVENT_PLAYER_QUIT); 1690 $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns;
1691 ext::highscore::check ($pl->ob);
1692
1630 $pl->ns->destroy if $pl->ns; 1693 $pl->ns->destroy if $pl->ns;
1631 1694
1632 my $path = playerdir $pl; 1695 my $path = playerdir $pl;
1633 my $temp = "$path~$cf::RUNTIME~deleting~"; 1696 my $temp = "$path~$cf::RUNTIME~deleting~";
1634 aio_rename $path, $temp; 1697 aio_rename $path, $temp;
1688 \@logins 1751 \@logins
1689} 1752}
1690 1753
1691=item $player->maps 1754=item $player->maps
1692 1755
1756=item cf::player::maps $login
1757
1693Returns an arrayref of map paths that are private for this 1758Returns an arrayref of map paths that are private for this
1694player. May block. 1759player. May block.
1695 1760
1696=cut 1761=cut
1697 1762
1759=cut 1824=cut
1760 1825
1761sub find_by_path($) { 1826sub find_by_path($) {
1762 my ($path) = @_; 1827 my ($path) = @_;
1763 1828
1829 $path =~ s/^~[^\/]*//; # skip ~login
1830
1764 my ($match, $specificity); 1831 my ($match, $specificity);
1765 1832
1766 for my $region (list) { 1833 for my $region (list) {
1767 if ($region->{match} && $path =~ $region->{match}) { 1834 if ($region->{match} && $path =~ $region->{match}) {
1768 ($match, $specificity) = ($region, $region->specificity) 1835 ($match, $specificity) = ($region, $region->specificity)
1796sub generate_random_map { 1863sub generate_random_map {
1797 my ($self, $rmp) = @_; 1864 my ($self, $rmp) = @_;
1798 1865
1799 my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM 1866 my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM
1800 1867
1801 # mit "rum" bekleckern, nicht
1802 $self->_create_random_map ( 1868 $self->_create_random_map ($rmp);
1803 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
1804 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle},
1805 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
1806 $rmp->{exit_on_final_map},
1807 $rmp->{xsize}, $rmp->{ysize},
1808 $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3},
1809 $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase},
1810 $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation},
1811 $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp},
1812 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used},
1813 (cf::region::find $rmp->{region}), $rmp->{custom}
1814 )
1815} 1869}
1816 1870
1817=item cf::map->register ($regex, $prio) 1871=item cf::map->register ($regex, $prio)
1818 1872
1819Register a handler for the map path matching the given regex at the 1873Register a handler for the map path matching the given regex at the
1824 1878
1825sub register { 1879sub register {
1826 my (undef, $regex, $prio) = @_; 1880 my (undef, $regex, $prio) = @_;
1827 my $pkg = caller; 1881 my $pkg = caller;
1828 1882
1829 no strict;
1830 push @{"$pkg\::ISA"}, __PACKAGE__; 1883 push @{"$pkg\::ISA"}, __PACKAGE__;
1831 1884
1832 $EXT_MAP{$pkg} = [$prio, qr<$regex>]; 1885 $EXT_MAP{$pkg} = [$prio, qr<$regex>];
1833} 1886}
1834 1887
1835# also paths starting with '/' 1888# also paths starting with '/'
1836$EXT_MAP{"cf::map"} = [0, qr{^(?=/)}]; 1889$EXT_MAP{"cf::map::wrap"} = [0, qr{^(?=/)}];
1837 1890
1838sub thawer_merge { 1891sub thawer_merge {
1839 my ($self, $merge) = @_; 1892 my ($self, $merge) = @_;
1840 1893
1841 # we have to keep some variables in memory intact 1894 # we have to keep some variables in memory intact
1846} 1899}
1847 1900
1848sub normalise { 1901sub normalise {
1849 my ($path, $base) = @_; 1902 my ($path, $base) = @_;
1850 1903
1851 $path = "$path"; # make sure its a string 1904 $path = "$path"; # make sure it's a string
1852 1905
1853 $path =~ s/\.map$//; 1906 $path =~ s/\.map$//;
1854 1907
1855 # map plan: 1908 # map plan:
1856 # 1909 #
1871 $base =~ s{[^/]+/?$}{}; 1924 $base =~ s{[^/]+/?$}{};
1872 $path = "$base/$path"; 1925 $path = "$base/$path";
1873 } 1926 }
1874 1927
1875 for ($path) { 1928 for ($path) {
1876 redo if s{//}{/};
1877 redo if s{/\.?/}{/}; 1929 redo if s{/\.?/}{/};
1878 redo if s{/[^/]+/\.\./}{/}; 1930 redo if s{/[^/]+/\.\./}{/};
1879 } 1931 }
1880 1932
1881 $path 1933 $path
1895 $self->init; # pass $1 etc. 1947 $self->init; # pass $1 etc.
1896 return $self; 1948 return $self;
1897 } 1949 }
1898 } 1950 }
1899 1951
1900 Carp::cluck "unable to resolve path '$path' (base '$base')."; 1952 Carp::cluck "unable to resolve path '$path' (base '$base')";
1901 () 1953 ()
1902} 1954}
1903 1955
1904sub init { 1956sub init {
1905 my ($self) = @_; 1957 my ($self) = @_;
2027} 2079}
2028 2080
2029sub find; 2081sub find;
2030sub find { 2082sub find {
2031 my ($path, $origin) = @_; 2083 my ($path, $origin) = @_;
2084
2085 cf::cede_to_tick;
2032 2086
2033 $path = normalise $path, $origin && $origin->path; 2087 $path = normalise $path, $origin && $origin->path;
2034 2088
2035 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove 2089 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
2036 my $guard2 = cf::lock_acquire "map_find:$path"; 2090 my $guard2 = cf::lock_acquire "map_find:$path";
2121 2175
2122 $self->in_memory (cf::MAP_ACTIVE); 2176 $self->in_memory (cf::MAP_ACTIVE);
2123 } 2177 }
2124 2178
2125 $self->post_load; 2179 $self->post_load;
2180
2181 1
2126} 2182}
2127 2183
2128# customize the map for a given player, i.e. 2184# customize the map for a given player, i.e.
2129# return the _real_ map. used by e.g. per-player 2185# return the _real_ map. used by e.g. per-player
2130# maps to change the path to ~playername/mappath 2186# maps to change the path to ~playername/mappath
2151 or next; 2207 or next;
2152 $neigh = find $neigh, $map 2208 $neigh = find $neigh, $map
2153 or next; 2209 or next;
2154 $neigh->load; 2210 $neigh->load;
2155 2211
2212 # now find the diagonal neighbours
2156 push @neigh, 2213 push @neigh,
2157 [$neigh->tile_path (($_ + 3) % 4), $neigh], 2214 [$neigh->tile_path (($_ + 3) % 4), $neigh],
2158 [$neigh->tile_path (($_ + 1) % 4), $neigh]; 2215 [$neigh->tile_path (($_ + 1) % 4), $neigh];
2159 } 2216 }
2160 2217
2167} 2224}
2168 2225
2169sub find_sync { 2226sub find_sync {
2170 my ($path, $origin) = @_; 2227 my ($path, $origin) = @_;
2171 2228
2172 cf::sync_job { find $path, $origin } 2229 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync"
2230 if $Coro::current == $Coro::main;
2231
2232 find $path, $origin
2173} 2233}
2174 2234
2175sub do_load_sync { 2235sub do_load_sync {
2176 my ($map) = @_; 2236 my ($map) = @_;
2177 2237
2178 cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" 2238 return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync"
2179 if $Coro::current == $Coro::main; 2239 if $Coro::current == $Coro::main;
2180 2240
2181 cf::sync_job { $map->load }; 2241 $map->load;
2182} 2242}
2183 2243
2184our %MAP_PREFETCH; 2244our %MAP_PREFETCH;
2185our $MAP_PREFETCHER = undef; 2245our $MAP_PREFETCHER = undef;
2186 2246
2298 2358
2299 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2359 my $lock = cf::lock_acquire "map_data:$self->{path}";
2300 2360
2301 return if $self->players; 2361 return if $self->players;
2302 2362
2303 warn "resetting map ", $self->path, "\n"; 2363 cf::trace "resetting map ", $self->path, "\n";
2304 2364
2305 $self->in_memory (cf::MAP_SWAPPED); 2365 $self->in_memory (cf::MAP_SWAPPED);
2306 2366
2307 # need to save uniques path 2367 # need to save uniques path
2308 unless ($self->{deny_save}) { 2368 unless ($self->{deny_save}) {
2332 2392
2333 delete $cf::MAP{$self->path}; 2393 delete $cf::MAP{$self->path};
2334 2394
2335 $self->unlink_save; 2395 $self->unlink_save;
2336 2396
2337 bless $self, "cf::map"; 2397 bless $self, "cf::map::wrap";
2338 delete $self->{deny_reset}; 2398 delete $self->{deny_reset};
2339 $self->{deny_save} = 1; 2399 $self->{deny_save} = 1;
2340 $self->reset_timeout (1); 2400 $self->reset_timeout (1);
2341 $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++)); 2401 $self->path ($self->{path} = "{nuke}/" . ($nuke_counter++));
2342 2402
2572 2632
2573Freezes the player and moves him/her to a special map (C<{link}>). 2633Freezes the player and moves him/her to a special map (C<{link}>).
2574 2634
2575The player should be reasonably safe there for short amounts of time (e.g. 2635The player should be reasonably safe there for short amounts of time (e.g.
2576for loading a map). You I<MUST> call C<leave_link> as soon as possible, 2636for loading a map). You I<MUST> call C<leave_link> as soon as possible,
2577though, as the palyer cannot control the character while it is on the link 2637though, as the player cannot control the character while it is on the link
2578map. 2638map.
2579 2639
2580Will never block. 2640Will never block.
2581 2641
2582=item $player_object->leave_link ($map, $x, $y) 2642=item $player_object->leave_link ($map, $x, $y)
2603sub cf::object::player::enter_link { 2663sub cf::object::player::enter_link {
2604 my ($self) = @_; 2664 my ($self) = @_;
2605 2665
2606 $self->deactivate_recursive; 2666 $self->deactivate_recursive;
2607 2667
2668 ++$self->{_link_recursion};
2669
2608 return if UNIVERSAL::isa $self->map, "ext::map_link"; 2670 return if UNIVERSAL::isa $self->map, "ext::map_link";
2609 2671
2610 $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] 2672 $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y]
2611 if $self->map && $self->map->{path} ne "{link}"; 2673 if $self->map && $self->map->{path} ne "{link}";
2612 2674
2643 $map->load_neighbours; 2705 $map->load_neighbours;
2644 2706
2645 return unless $self->contr->active; 2707 return unless $self->contr->active;
2646 2708
2647 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext 2709 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext
2648 $self->enter_map ($map, $x, $y); 2710 if ($self->enter_map ($map, $x, $y)) {
2649 2711 # entering was successful
2712 delete $self->{_link_recursion};
2650 # only activate afterwards, to support waiting in hooks 2713 # only activate afterwards, to support waiting in hooks
2651 $self->activate_recursive; 2714 $self->activate_recursive;
2652} 2715 }
2653 2716
2717}
2718
2654=item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) 2719=item $player_object->goto ($path, $x, $y[, $check->($map, $x, $y, $player)[, $done->($player)]])
2655 2720
2656Moves the player to the given map-path and coordinates by first freezing 2721Moves the player to the given map-path and coordinates by first freezing
2657her, loading and preparing them map, calling the provided $check callback 2722her, loading and preparing them map, calling the provided $check callback
2658that has to return the map if sucecssful, and then unfreezes the player on 2723that has to return the map if sucecssful, and then unfreezes the player on
2659the new (success) or old (failed) map position. In either case, $done will 2724the new (success) or old (failed) map position. In either case, $done will
2666 2731
2667our $GOTOGEN; 2732our $GOTOGEN;
2668 2733
2669sub cf::object::player::goto { 2734sub cf::object::player::goto {
2670 my ($self, $path, $x, $y, $check, $done) = @_; 2735 my ($self, $path, $x, $y, $check, $done) = @_;
2736
2737 if ($self->{_link_recursion} >= $MAX_LINKS) {
2738 error "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting.";
2739 $self->failmsg ("Something went wrong inside the server - please contact an administrator!");
2740 ($path, $x, $y) = @$EMERGENCY_POSITION;
2741 }
2671 2742
2672 # do generation counting so two concurrent goto's will be executed in-order 2743 # do generation counting so two concurrent goto's will be executed in-order
2673 my $gen = $self->{_goto_generation} = ++$GOTOGEN; 2744 my $gen = $self->{_goto_generation} = ++$GOTOGEN;
2674 2745
2675 $self->enter_link; 2746 $self->enter_link;
2699 my $map = eval { 2770 my $map = eval {
2700 my $map = defined $path ? cf::map::find $path : undef; 2771 my $map = defined $path ? cf::map::find $path : undef;
2701 2772
2702 if ($map) { 2773 if ($map) {
2703 $map = $map->customise_for ($self); 2774 $map = $map->customise_for ($self);
2704 $map = $check->($map) if $check && $map; 2775 $map = $check->($map, $x, $y, $self) if $check && $map;
2705 } else { 2776 } else {
2706 $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED); 2777 $self->message ("The exit to '$path' is closed.", cf::NDI_UNIQUE | cf::NDI_RED);
2707 } 2778 }
2708 2779
2709 $map 2780 $map
2717 if ($gen == $self->{_goto_generation}) { 2788 if ($gen == $self->{_goto_generation}) {
2718 delete $self->{_goto_generation}; 2789 delete $self->{_goto_generation};
2719 $self->leave_link ($map, $x, $y); 2790 $self->leave_link ($map, $x, $y);
2720 } 2791 }
2721 2792
2722 $done->() if $done; 2793 $done->($self) if $done;
2723 })->prio (1); 2794 })->prio (1);
2724} 2795}
2725 2796
2726=item $player_object->enter_exit ($exit_object) 2797=item $player_object->enter_exit ($exit_object)
2727 2798
2820 $self->message ("Something went wrong deep within the deliantra server. " 2891 $self->message ("Something went wrong deep within the deliantra server. "
2821 . "I'll try to bring you back to the map you were before. " 2892 . "I'll try to bring you back to the map you were before. "
2822 . "Please report this to the dungeon master!", 2893 . "Please report this to the dungeon master!",
2823 cf::NDI_UNIQUE | cf::NDI_RED); 2894 cf::NDI_UNIQUE | cf::NDI_RED);
2824 2895
2825 warn "ERROR in enter_exit: $@"; 2896 error "ERROR in enter_exit: $@";
2826 $self->leave_link; 2897 $self->leave_link;
2827 } 2898 }
2828 })->prio (1); 2899 })->prio (1);
2829} 2900}
2830 2901
3143 3214
3144 $ns->ext_reply ($reply, @reply) 3215 $ns->ext_reply ($reply, @reply)
3145 if $reply; 3216 if $reply;
3146 3217
3147 } else { 3218 } else {
3148 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 3219 error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
3149 } 3220 }
3150 3221
3151 cf::override; 3222 cf::override;
3152 }, 3223 },
3153); 3224);
3234 decrease split destroy change_exp value msg lore send_msg)], 3305 decrease split destroy change_exp value msg lore send_msg)],
3235 ["cf::object::player" => qw(player)], 3306 ["cf::object::player" => qw(player)],
3236 ["cf::player" => qw(peaceful send_msg)], 3307 ["cf::player" => qw(peaceful send_msg)],
3237 ["cf::map" => qw(trigger)], 3308 ["cf::map" => qw(trigger)],
3238) { 3309) {
3239 no strict 'refs';
3240 my ($pkg, @funs) = @$_; 3310 my ($pkg, @funs) = @$_;
3241 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 3311 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
3242 for @funs; 3312 for @funs;
3243} 3313}
3244 3314
3283 local @cf::_safe_eval_args = values %vars; 3353 local @cf::_safe_eval_args = values %vars;
3284 @res = wantarray ? eval eval : scalar eval $eval; 3354 @res = wantarray ? eval eval : scalar eval $eval;
3285 } 3355 }
3286 3356
3287 if ($@) { 3357 if ($@) {
3288 warn "$@"; 3358 warn "$@",
3289 warn "while executing safe code '$code'\n"; 3359 "while executing safe code '$code'\n",
3290 warn "with arguments " . (join " ", %vars) . "\n"; 3360 "with arguments " . (join " ", %vars) . "\n";
3291 } 3361 }
3292 3362
3293 wantarray ? @res : $res[0] 3363 wantarray ? @res : $res[0]
3294} 3364}
3295 3365
3329 # for this (global event?) 3399 # for this (global event?)
3330 %ext::player_env::MUSIC_FACE_CACHE = (); 3400 %ext::player_env::MUSIC_FACE_CACHE = ();
3331 3401
3332 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3402 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3333 3403
3334 warn "loading facedata from $path\n"; 3404 trace "loading facedata from $path\n";
3335 3405
3336 my $facedata;
3337 0 < aio_load $path, $facedata 3406 0 < aio_load $path, my $facedata
3338 or die "$path: $!"; 3407 or die "$path: $!";
3339 3408
3340 $facedata = Coro::Storable::thaw $facedata; 3409 $facedata = Coro::Storable::thaw $facedata;
3341 3410
3342 $facedata->{version} == 2 3411 $facedata->{version} == 2
3359 3428
3360 cf::face::set_visibility $idx, $info->{visibility}; 3429 cf::face::set_visibility $idx, $info->{visibility};
3361 cf::face::set_magicmap $idx, $info->{magicmap}; 3430 cf::face::set_magicmap $idx, $info->{magicmap};
3362 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; 3431 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3363 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; 3432 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3433 cf::face::set_data $idx, 2, $info->{glyph} , $info->{glyph} ;
3364 3434
3365 cf::cede_to_tick; 3435 cf::cede_to_tick;
3366 } 3436 }
3367 3437
3368 while (my ($face, $info) = each %$faces) { 3438 while (my ($face, $info) = each %$faces) {
3373 3443
3374 if (my $smooth = cf::face::find $info->{smooth}) { 3444 if (my $smooth = cf::face::find $info->{smooth}) {
3375 cf::face::set_smooth $idx, $smooth; 3445 cf::face::set_smooth $idx, $smooth;
3376 cf::face::set_smoothlevel $idx, $info->{smoothlevel}; 3446 cf::face::set_smoothlevel $idx, $info->{smoothlevel};
3377 } else { 3447 } else {
3378 warn "smooth face '$info->{smooth}' not found for face '$face'"; 3448 error "smooth face '$info->{smooth}' not found for face '$face'";
3379 } 3449 }
3380 3450
3381 cf::cede_to_tick; 3451 cf::cede_to_tick;
3382 } 3452 }
3383 } 3453 }
3401 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3471 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3402 3472
3403 cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; 3473 cf::face::set_data $idx, 0, $info->{data}, $info->{hash};
3404 cf::face::set_type $idx, $info->{type}; 3474 cf::face::set_type $idx, $info->{type};
3405 } else { 3475 } else {
3406 $RESOURCE{$name} = $info; 3476 $RESOURCE{$name} = $info; # unused
3407 } 3477 }
3408 3478
3409 cf::cede_to_tick; 3479 cf::cede_to_tick;
3410 } 3480 }
3411 } 3481 }
3412 3482
3413 cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); 3483 cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE);
3414 3484
3415 1 3485 1
3416} 3486}
3417
3418cf::global->attach (on_resource_update => sub {
3419 if (my $soundconf = $RESOURCE{"res/sound.conf"}) {
3420 $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data});
3421
3422 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3423 my $sound = $soundconf->{compat}[$_]
3424 or next;
3425
3426 my $face = cf::face::find "sound/$sound->[1]";
3427 cf::sound::set $sound->[0] => $face;
3428 cf::sound::old_sound_index $_, $face; # gcfclient-compat
3429 }
3430
3431 while (my ($k, $v) = each %{$soundconf->{event}}) {
3432 my $face = cf::face::find "sound/$v";
3433 cf::sound::set $k => $face;
3434 }
3435 }
3436});
3437 3487
3438register_exticmd fx_want => sub { 3488register_exticmd fx_want => sub {
3439 my ($ns, $want) = @_; 3489 my ($ns, $want) = @_;
3440 3490
3441 while (my ($k, $v) = each %$want) { 3491 while (my ($k, $v) = each %$want) {
3480sub reload_treasures { 3530sub reload_treasures {
3481 load_resource_file "$DATADIR/treasures" 3531 load_resource_file "$DATADIR/treasures"
3482 or die "unable to load treasurelists\n"; 3532 or die "unable to load treasurelists\n";
3483} 3533}
3484 3534
3535sub reload_sound {
3536 trace "loading sound config from $DATADIR/sound\n";
3537
3538 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data
3539 or die "$DATADIR/sound $!";
3540
3541 my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data);
3542
3543 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3544 my $sound = $soundconf->{compat}[$_]
3545 or next;
3546
3547 my $face = cf::face::find "sound/$sound->[1]";
3548 cf::sound::set $sound->[0] => $face;
3549 cf::sound::old_sound_index $_, $face; # gcfclient-compat
3550 }
3551
3552 while (my ($k, $v) = each %{$soundconf->{event}}) {
3553 my $face = cf::face::find "sound/$v";
3554 cf::sound::set $k => $face;
3555 }
3556}
3557
3485sub reload_resources { 3558sub reload_resources {
3486 warn "reloading resource files...\n"; 3559 trace "reloading resource files...\n";
3487 3560
3561 reload_exp_table;
3562 reload_materials;
3488 reload_facedata; 3563 reload_facedata;
3564 reload_sound;
3489 reload_archetypes; 3565 reload_archetypes;
3490 reload_regions; 3566 reload_regions;
3491 reload_treasures; 3567 reload_treasures;
3492 3568
3493 warn "finished reloading resource files\n"; 3569 trace "finished reloading resource files\n";
3494} 3570}
3495 3571
3496sub reload_config { 3572sub reload_config {
3497 warn "reloading config file...\n"; 3573 trace "reloading config file...\n";
3498 3574
3499 open my $fh, "<:utf8", "$CONFDIR/config" 3575 0 < aio_load "$CONFDIR/config", my $config
3500 or return; 3576 or die "$CONFDIR/config: $!";
3501 3577
3502 local $/; 3578 utf8::decode $config;
3503 *CFG = YAML::XS::Load scalar <$fh>; 3579 *CFG = yaml_load $config;
3504 3580
3505 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; 3581 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3506 3582
3507 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3583 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset};
3508 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; 3584 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3509 3585
3510 if (exists $CFG{mlockall}) { 3586 if (exists $CFG{mlockall}) {
3512 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" 3588 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()"
3513 and die "WARNING: m(un)lockall failed: $!\n"; 3589 and die "WARNING: m(un)lockall failed: $!\n";
3514 }; 3590 };
3515 warn $@ if $@; 3591 warn $@ if $@;
3516 } 3592 }
3517
3518 warn "finished reloading resource files\n";
3519} 3593}
3520 3594
3521sub pidfile() { 3595sub pidfile() {
3522 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT 3596 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3523 or die "$PIDFILE: $!"; 3597 or die "$PIDFILE: $!";
3536 seek $fh, 0, 0; 3610 seek $fh, 0, 0;
3537 print $fh $$; 3611 print $fh $$;
3538} 3612}
3539 3613
3540sub main_loop { 3614sub main_loop {
3541 warn "EV::loop starting\n"; 3615 trace "EV::loop starting\n";
3542 if (1) { 3616 if (1) {
3543 EV::loop; 3617 EV::loop;
3544 } 3618 }
3545 warn "EV::loop returned\n"; 3619 trace "EV::loop returned\n";
3546 goto &main_loop unless $REALLY_UNLOOP; 3620 goto &main_loop unless $REALLY_UNLOOP;
3547} 3621}
3548 3622
3549sub main { 3623sub main {
3550 cf::init_globals; # initialise logging 3624 cf::init_globals; # initialise logging
3551 3625
3552 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3626 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3553 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3627 LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3554 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3628 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3555 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3629 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3556 3630
3557 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3631 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3558 3632
3566 }; 3640 };
3567 3641
3568 evthread_start IO::AIO::poll_fileno; 3642 evthread_start IO::AIO::poll_fileno;
3569 3643
3570 cf::sync_job { 3644 cf::sync_job {
3571 cf::init_experience; 3645 cf::incloader::init ();
3646
3572 cf::init_anim; 3647 cf::init_anim;
3573 cf::init_attackmess; 3648 cf::init_attackmess;
3574 cf::init_dynamic; 3649 cf::init_dynamic;
3575 3650
3576 cf::load_settings; 3651 cf::load_settings;
3577 cf::load_materials;
3578 3652
3579 reload_resources; 3653 reload_resources;
3580 reload_config; 3654 reload_config;
3581 db_init; 3655 db_init;
3582 3656
3594 3668
3595 # no (long-running) fork's whatsoever before this point(!) 3669 # no (long-running) fork's whatsoever before this point(!)
3596 use POSIX (); 3670 use POSIX ();
3597 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; 3671 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3598 3672
3599 (pop @POST_INIT)->(0) while @POST_INIT; 3673 cf::_post_init 0;
3600 }; 3674 };
3601 3675
3602 cf::object::thawer::errors_are_fatal 0; 3676 cf::object::thawer::errors_are_fatal 0;
3603 warn "parse errors in files are no longer fatal from this point on.\n"; 3677 info "parse errors in files are no longer fatal from this point on.\n";
3604 3678
3679 my $free_main; $free_main = EV::idle sub {
3680 undef $free_main;
3681 undef &main; # free gobs of memory :)
3682 };
3683
3605 main_loop; 3684 goto &main_loop;
3606} 3685}
3607 3686
3608############################################################################# 3687#############################################################################
3609# initialisation and cleanup 3688# initialisation and cleanup
3610 3689
3649 or return; 3728 or return;
3650 3729
3651 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE 3730 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE
3652 and return; 3731 and return;
3653 3732
3654 warn sprintf "runtime file written (%gs).\n", AE::time - $t0; 3733 trace sprintf "runtime file written (%gs).\n", AE::time - $t0;
3655 3734
3656 1 3735 1
3657} 3736}
3658 3737
3659our $uuid_lock; 3738our $uuid_lock;
3671 or return; 3750 or return;
3672 3751
3673 my $value = uuid_seq uuid_cur; 3752 my $value = uuid_seq uuid_cur;
3674 3753
3675 unless ($value) { 3754 unless ($value) {
3676 warn "cowardly refusing to write zero uuid value!\n"; 3755 info "cowardly refusing to write zero uuid value!\n";
3677 return; 3756 return;
3678 } 3757 }
3679 3758
3680 my $value = uuid_str $value + $uuid_skip; 3759 my $value = uuid_str $value + $uuid_skip;
3681 $uuid_skip = 0; 3760 $uuid_skip = 0;
3691 or return; 3770 or return;
3692 3771
3693 aio_rename "$uuid~", $uuid 3772 aio_rename "$uuid~", $uuid
3694 and return; 3773 and return;
3695 3774
3696 warn "uuid file written ($value).\n"; 3775 trace "uuid file written ($value).\n";
3697 3776
3698 1 3777 1
3699 3778
3700} 3779}
3701 3780
3707} 3786}
3708 3787
3709sub emergency_save() { 3788sub emergency_save() {
3710 my $freeze_guard = cf::freeze_mainloop; 3789 my $freeze_guard = cf::freeze_mainloop;
3711 3790
3712 warn "emergency_perl_save: enter\n"; 3791 info "emergency_perl_save: enter\n";
3792
3793 # this is a trade-off: we want to be very quick here, so
3794 # save all maps without fsync, and later call a global sync
3795 # (which in turn might be very very slow)
3796 local $USE_FSYNC = 0;
3713 3797
3714 cf::sync_job { 3798 cf::sync_job {
3715 # this is a trade-off: we want to be very quick here, so 3799 cf::write_runtime_sync; # external watchdog should not bark
3716 # save all maps without fsync, and later call a global sync
3717 # (which in turn might be very very slow)
3718 local $USE_FSYNC = 0;
3719 3800
3720 # use a peculiar iteration method to avoid tripping on perl 3801 # use a peculiar iteration method to avoid tripping on perl
3721 # refcount bugs in for. also avoids problems with players 3802 # refcount bugs in for. also avoids problems with players
3722 # and maps saved/destroyed asynchronously. 3803 # and maps saved/destroyed asynchronously.
3723 warn "emergency_perl_save: begin player save\n"; 3804 info "emergency_perl_save: begin player save\n";
3724 for my $login (keys %cf::PLAYER) { 3805 for my $login (keys %cf::PLAYER) {
3725 my $pl = $cf::PLAYER{$login} or next; 3806 my $pl = $cf::PLAYER{$login} or next;
3726 $pl->valid or next; 3807 $pl->valid or next;
3727 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3808 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3728 $pl->save; 3809 $pl->save;
3729 } 3810 }
3730 warn "emergency_perl_save: end player save\n"; 3811 info "emergency_perl_save: end player save\n";
3731 3812
3813 cf::write_runtime_sync; # external watchdog should not bark
3814
3732 warn "emergency_perl_save: begin map save\n"; 3815 info "emergency_perl_save: begin map save\n";
3733 for my $path (keys %cf::MAP) { 3816 for my $path (keys %cf::MAP) {
3734 my $map = $cf::MAP{$path} or next; 3817 my $map = $cf::MAP{$path} or next;
3735 $map->valid or next; 3818 $map->valid or next;
3736 $map->save; 3819 $map->save;
3737 } 3820 }
3738 warn "emergency_perl_save: end map save\n"; 3821 info "emergency_perl_save: end map save\n";
3739 3822
3823 cf::write_runtime_sync; # external watchdog should not bark
3824
3740 warn "emergency_perl_save: begin database checkpoint\n"; 3825 info "emergency_perl_save: begin database checkpoint\n";
3741 BDB::db_env_txn_checkpoint $DB_ENV; 3826 BDB::db_env_txn_checkpoint $DB_ENV;
3742 warn "emergency_perl_save: end database checkpoint\n"; 3827 info "emergency_perl_save: end database checkpoint\n";
3743 3828
3744 warn "emergency_perl_save: begin write uuid\n"; 3829 info "emergency_perl_save: begin write uuid\n";
3745 write_uuid_sync 1; 3830 write_uuid_sync 1;
3746 warn "emergency_perl_save: end write uuid\n"; 3831 info "emergency_perl_save: end write uuid\n";
3832
3833 cf::write_runtime_sync; # external watchdog should not bark
3834
3835 trace "emergency_perl_save: syncing database to disk";
3836 BDB::db_env_txn_checkpoint $DB_ENV;
3837
3838 info "emergency_perl_save: starting sync\n";
3839 IO::AIO::aio_sync sub {
3840 info "emergency_perl_save: finished sync\n";
3841 };
3842
3843 cf::write_runtime_sync; # external watchdog should not bark
3844
3845 trace "emergency_perl_save: flushing outstanding aio requests";
3846 while (IO::AIO::nreqs || BDB::nreqs) {
3847 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3848 }
3849
3850 cf::write_runtime_sync; # external watchdog should not bark
3747 }; 3851 };
3748 3852
3749 warn "emergency_perl_save: starting sync()\n";
3750 IO::AIO::aio_sync sub {
3751 warn "emergency_perl_save: finished sync()\n";
3752 };
3753
3754 warn "emergency_perl_save: leave\n"; 3853 info "emergency_perl_save: leave\n";
3755} 3854}
3756 3855
3757sub post_cleanup { 3856sub post_cleanup {
3758 my ($make_core) = @_; 3857 my ($make_core) = @_;
3759 3858
3859 IO::AIO::flush;
3860
3760 warn Carp::longmess "post_cleanup backtrace" 3861 error Carp::longmess "post_cleanup backtrace"
3761 if $make_core; 3862 if $make_core;
3762 3863
3763 my $fh = pidfile; 3864 my $fh = pidfile;
3764 unlink $PIDFILE if <$fh> == $$; 3865 unlink $PIDFILE if <$fh> == $$;
3765} 3866}
3785 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; 3886 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
3786 for my $name (keys %$leaf_symtab) { 3887 for my $name (keys %$leaf_symtab) {
3787 _gv_clear *{"$pkg$name"}; 3888 _gv_clear *{"$pkg$name"};
3788# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; 3889# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"};
3789 } 3890 }
3790 warn "cleared package $pkg\n";#d#
3791} 3891}
3792 3892
3793sub do_reload_perl() { 3893sub do_reload_perl() {
3794 # can/must only be called in main 3894 # can/must only be called in main
3795 if ($Coro::current != $Coro::main) { 3895 unless (in_main) {
3796 warn "can only reload from main coroutine"; 3896 error "can only reload from main coroutine";
3797 return; 3897 return;
3798 } 3898 }
3799 3899
3800 return if $RELOAD++; 3900 return if $RELOAD++;
3801 3901
3802 my $t1 = AE::time; 3902 my $t1 = AE::time;
3803 3903
3804 while ($RELOAD) { 3904 while ($RELOAD) {
3805 warn "reloading..."; 3905 cf::get_slot 0.1, -1, "reload_perl";
3906 info "perl_reload: reloading...";
3806 3907
3807 warn "entering sync_job"; 3908 trace "perl_reload: entering sync_job";
3808 3909
3809 cf::sync_job { 3910 cf::sync_job {
3810 cf::write_runtime_sync; # external watchdog should not bark
3811 cf::emergency_save; 3911 #cf::emergency_save;
3812 cf::write_runtime_sync; # external watchdog should not bark
3813 3912
3814 warn "syncing database to disk";
3815 BDB::db_env_txn_checkpoint $DB_ENV;
3816
3817 # if anything goes wrong in here, we should simply crash as we already saved
3818
3819 warn "flushing outstanding aio requests";
3820 while (IO::AIO::nreqs || BDB::nreqs) {
3821 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3822 }
3823
3824 warn "cancelling all extension coros"; 3913 trace "perl_reload: cancelling all extension coros";
3825 $_->cancel for values %EXT_CORO; 3914 $_->cancel for values %EXT_CORO;
3826 %EXT_CORO = (); 3915 %EXT_CORO = ();
3827 3916
3828 warn "removing commands"; 3917 trace "perl_reload: removing commands";
3829 %COMMAND = (); 3918 %COMMAND = ();
3830 3919
3831 warn "removing ext/exti commands"; 3920 trace "perl_reload: removing ext/exti commands";
3832 %EXTCMD = (); 3921 %EXTCMD = ();
3833 %EXTICMD = (); 3922 %EXTICMD = ();
3834 3923
3835 warn "unloading/nuking all extensions"; 3924 trace "perl_reload: unloading/nuking all extensions";
3836 for my $pkg (@EXTS) { 3925 for my $pkg (@EXTS) {
3837 warn "... unloading $pkg"; 3926 trace "... unloading $pkg";
3838 3927
3839 if (my $cb = $pkg->can ("unload")) { 3928 if (my $cb = $pkg->can ("unload")) {
3840 eval { 3929 eval {
3841 $cb->($pkg); 3930 $cb->($pkg);
3842 1 3931 1
3843 } or warn "$pkg unloaded, but with errors: $@"; 3932 } or error "$pkg unloaded, but with errors: $@";
3844 } 3933 }
3845 3934
3846 warn "... clearing $pkg"; 3935 trace "... clearing $pkg";
3847 clear_package $pkg; 3936 clear_package $pkg;
3848 } 3937 }
3849 3938
3850 warn "unloading all perl modules loaded from $LIBDIR"; 3939 trace "perl_reload: unloading all perl modules loaded from $LIBDIR";
3851 while (my ($k, $v) = each %INC) { 3940 while (my ($k, $v) = each %INC) {
3852 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; 3941 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
3853 3942
3854 warn "... unloading $k"; 3943 trace "... unloading $k";
3855 delete $INC{$k}; 3944 delete $INC{$k};
3856 3945
3857 $k =~ s/\.pm$//; 3946 $k =~ s/\.pm$//;
3858 $k =~ s/\//::/g; 3947 $k =~ s/\//::/g;
3859 3948
3862 } 3951 }
3863 3952
3864 clear_package $k; 3953 clear_package $k;
3865 } 3954 }
3866 3955
3867 warn "getting rid of safe::, as good as possible"; 3956 trace "perl_reload: getting rid of safe::, as good as possible";
3868 clear_package "safe::$_" 3957 clear_package "safe::$_"
3869 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 3958 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
3870 3959
3871 warn "unloading cf.pm \"a bit\""; 3960 trace "perl_reload: unloading cf.pm \"a bit\"";
3872 delete $INC{"cf.pm"}; 3961 delete $INC{"cf.pm"};
3873 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; 3962 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES;
3874 3963
3875 # don't, removes xs symbols, too, 3964 # don't, removes xs symbols, too,
3876 # and global variables created in xs 3965 # and global variables created in xs
3877 #clear_package __PACKAGE__; 3966 #clear_package __PACKAGE__;
3878 3967
3879 warn "unload completed, starting to reload now"; 3968 info "perl_reload: unload completed, starting to reload now";
3880 3969
3881 warn "reloading cf.pm"; 3970 trace "perl_reload: reloading cf.pm";
3882 require cf; 3971 require cf;
3883 cf::_connect_to_perl_1; 3972 cf::_connect_to_perl_1;
3884 3973
3885 warn "loading config and database again"; 3974 trace "perl_reload: loading config and database again";
3886 cf::reload_config; 3975 cf::reload_config;
3887 3976
3888 warn "loading extensions"; 3977 trace "perl_reload: loading extensions";
3889 cf::load_extensions; 3978 cf::load_extensions;
3890 3979
3891 if ($REATTACH_ON_RELOAD) { 3980 if ($REATTACH_ON_RELOAD) {
3892 warn "reattaching attachments to objects/players"; 3981 trace "perl_reload: reattaching attachments to objects/players";
3893 _global_reattach; # objects, sockets 3982 _global_reattach; # objects, sockets
3894 warn "reattaching attachments to maps"; 3983 trace "perl_reload: reattaching attachments to maps";
3895 reattach $_ for values %MAP; 3984 reattach $_ for values %MAP;
3896 warn "reattaching attachments to players"; 3985 trace "perl_reload: reattaching attachments to players";
3897 reattach $_ for values %PLAYER; 3986 reattach $_ for values %PLAYER;
3898 } 3987 }
3899 3988
3900 warn "running post_init jobs"; 3989 cf::_post_init 1;
3901 (pop @POST_INIT)->(1) while @POST_INIT;
3902 3990
3903 warn "leaving sync_job"; 3991 trace "perl_reload: leaving sync_job";
3904 3992
3905 1 3993 1
3906 } or do { 3994 } or do {
3907 warn $@; 3995 error $@;
3908 cf::cleanup "error while reloading, exiting."; 3996 cf::cleanup "perl_reload: error, exiting.";
3909 }; 3997 };
3910 3998
3911 warn "reloaded";
3912 --$RELOAD; 3999 --$RELOAD;
3913 } 4000 }
3914 4001
3915 $t1 = AE::time - $t1; 4002 $t1 = AE::time - $t1;
3916 warn "reload completed in ${t1}s\n"; 4003 info "perl_reload: completed in ${t1}s\n";
3917}; 4004};
3918 4005
3919our $RELOAD_WATCHER; # used only during reload 4006our $RELOAD_WATCHER; # used only during reload
3920 4007
3921sub reload_perl() { 4008sub reload_perl() {
3942 reload_perl; 4029 reload_perl;
3943 }; 4030 };
3944 } 4031 }
3945}; 4032};
3946 4033
3947unshift @INC, $LIBDIR; 4034#############################################################################
3948 4035
3949my $bug_warning = 0; 4036my $bug_warning = 0;
3950 4037
3951our @WAIT_FOR_TICK; 4038our @WAIT_FOR_TICK;
3952our @WAIT_FOR_TICK_BEGIN; 4039our @WAIT_FOR_TICK_BEGIN;
3953 4040
3954sub wait_for_tick { 4041sub wait_for_tick() {
3955 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4042 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3956 4043
3957 my $signal = new Coro::Signal; 4044 my $signal = new Coro::Signal;
3958 push @WAIT_FOR_TICK, $signal; 4045 push @WAIT_FOR_TICK, $signal;
3959 $signal->wait; 4046 $signal->wait;
3960} 4047}
3961 4048
3962sub wait_for_tick_begin { 4049sub wait_for_tick_begin() {
3963 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4050 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
3964 4051
3965 my $signal = new Coro::Signal; 4052 my $signal = new Coro::Signal;
3966 push @WAIT_FOR_TICK_BEGIN, $signal; 4053 push @WAIT_FOR_TICK_BEGIN, $signal;
3967 $signal->wait; 4054 $signal->wait;
3981 if ($NOW >= $NEXT_RUNTIME_WRITE) { 4068 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3982 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 4069 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
3983 Coro::async_pool { 4070 Coro::async_pool {
3984 $Coro::current->{desc} = "runtime saver"; 4071 $Coro::current->{desc} = "runtime saver";
3985 write_runtime_sync 4072 write_runtime_sync
3986 or warn "ERROR: unable to write runtime file: $!"; 4073 or error "ERROR: unable to write runtime file: $!";
3987 }; 4074 };
3988 } 4075 }
3989 4076
3990 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { 4077 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
3991 $sig->send; 4078 $sig->send;
3999 4086
4000 if (0) { 4087 if (0) {
4001 if ($NEXT_TICK) { 4088 if ($NEXT_TICK) {
4002 my $jitter = $TICK_START - $NEXT_TICK; 4089 my $jitter = $TICK_START - $NEXT_TICK;
4003 $JITTER = $JITTER * 0.75 + $jitter * 0.25; 4090 $JITTER = $JITTER * 0.75 + $jitter * 0.25;
4004 warn "jitter $JITTER\n";#d# 4091 debug "jitter $JITTER\n";#d#
4005 } 4092 }
4006 } 4093 }
4007} 4094}
4008 4095
4009{ 4096{
4054 IO::AIO::min_parallel 8; 4141 IO::AIO::min_parallel 8;
4055 IO::AIO::max_poll_time $TICK * 0.1; 4142 IO::AIO::max_poll_time $TICK * 0.1;
4056 undef $AnyEvent::AIO::WATCHER; 4143 undef $AnyEvent::AIO::WATCHER;
4057} 4144}
4058 4145
4059my $_log_backtrace; 4146our $_log_backtrace;
4147our $_log_backtrace_last;
4060 4148
4061sub _log_backtrace { 4149sub _log_backtrace {
4062 my ($msg, @addr) = @_; 4150 my ($msg, @addr) = @_;
4063 4151
4064 $msg =~ s/\n//; 4152 $msg =~ s/\n$//;
4065 4153
4154 if ($_log_backtrace_last eq $msg) {
4155 LOG llevInfo, "[ABT] $msg\n";
4156 LOG llevInfo, "[ABT] [duplicate, suppressed]\n";
4066 # limit the # of concurrent backtraces 4157 # limit the # of concurrent backtraces
4067 if ($_log_backtrace < 2) { 4158 } elsif ($_log_backtrace < 2) {
4159 $_log_backtrace_last = $msg;
4068 ++$_log_backtrace; 4160 ++$_log_backtrace;
4069 my $perl_bt = Carp::longmess $msg; 4161 my $perl_bt = Carp::longmess $msg;
4070 async { 4162 async {
4071 $Coro::current->{desc} = "abt $msg"; 4163 $Coro::current->{desc} = "abt $msg";
4072 4164
4092 LOG llevInfo, "[ABT] $_\n" for @bt; 4184 LOG llevInfo, "[ABT] $_\n" for @bt;
4093 --$_log_backtrace; 4185 --$_log_backtrace;
4094 }; 4186 };
4095 } else { 4187 } else {
4096 LOG llevInfo, "[ABT] $msg\n"; 4188 LOG llevInfo, "[ABT] $msg\n";
4097 LOG llevInfo, "[ABT] [suppressed]\n"; 4189 LOG llevInfo, "[ABT] [overload, suppressed]\n";
4098 } 4190 }
4099} 4191}
4100 4192
4101# load additional modules 4193# load additional modules
4102require "cf/$_.pm" for @EXTRA_MODULES; 4194require "cf/$_.pm" for @EXTRA_MODULES;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines