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.527 by root, Fri Apr 23 04:32:47 2010 UTC vs.
Revision 1.533 by root, Thu Apr 29 07:59:17 2010 UTC

106our $RANDOMDIR = "$LOCALDIR/random"; 106our $RANDOMDIR = "$LOCALDIR/random";
107our $BDBDIR = "$LOCALDIR/db"; 107our $BDBDIR = "$LOCALDIR/db";
108our $PIDFILE = "$LOCALDIR/pid"; 108our $PIDFILE = "$LOCALDIR/pid";
109our $RUNTIMEFILE = "$LOCALDIR/runtime"; 109our $RUNTIMEFILE = "$LOCALDIR/runtime";
110 110
111our %RESOURCE; 111our %RESOURCE; # unused
112 112
113our $OUTPUT_RATE_MIN = 3000; 113our $OUTPUT_RATE_MIN = 3000;
114our $OUTPUT_RATE_MAX = 1000000; 114our $OUTPUT_RATE_MAX = 1000000;
115 115
116our $MAX_LINKS = 32; # how many chained exits to follow 116our $MAX_LINKS = 32; # how many chained exits to follow
117our $VERBOSE_IO = 0;#d# 117our $VERBOSE_IO = 1;
118 118
119our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) 119our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!)
120our $NEXT_RUNTIME_WRITE; # when should the runtime file be written 120our $NEXT_RUNTIME_WRITE; # when should the runtime file be written
121our $NEXT_TICK; 121our $NEXT_TICK;
122our $USE_FSYNC = 1; # use fsync to write maps - default on 122our $USE_FSYNC = 1; # use fsync to write maps - default on
252 252
253=back 253=back
254 254
255=cut 255=cut
256 256
257sub error(@) { LOG llevError, join "", @_ }
258sub warn (@) { LOG llevWarn , join "", @_ }
259sub info (@) { LOG llevInfo , join "", @_ }
260sub debug(@) { LOG llevDebug, join "", @_ }
261sub trace(@) { LOG llevTrace, join "", @_ }
262
257$Coro::State::WARNHOOK = sub { 263$Coro::State::WARNHOOK = sub {
258 my $msg = join "", @_; 264 my $msg = join "", @_;
259 265
260 $msg .= "\n" 266 $msg .= "\n"
261 unless $msg =~ /\n$/; 267 unless $msg =~ /\n$/;
262 268
263 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; 269 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge;
264 270
265 LOG llevError, $msg; 271 LOG llevWarn, $msg;
266}; 272};
267 273
268$Coro::State::DIEHOOK = sub { 274$Coro::State::DIEHOOK = sub {
269 return unless $^S eq 0; # "eq", not "==" 275 return unless $^S eq 0; # "eq", not "=="
270 276
271 warn Carp::longmess $_[0]; 277 error Carp::longmess $_[0];
272 278
273 if (in_main) {#d# 279 if (in_main) {#d#
274 warn "DIEHOOK called in main context, Coro bug?\n";#d# 280 error "DIEHOOK called in main context, Coro bug?\n";#d#
275 return;#d# 281 return;#d#
276 }#d# 282 }#d#
277 283
278 # kill coroutine otherwise 284 # kill coroutine otherwise
279 Coro::terminate 285 Coro::terminate
528 my @res; 534 my @res;
529 535
530 (async { 536 (async {
531 $Coro::current->desc ("sync job coro"); 537 $Coro::current->desc ("sync job coro");
532 @res = eval { $job->() }; 538 @res = eval { $job->() };
533 warn $@ if $@; 539 error $@ if $@;
534 undef $busy; 540 undef $busy;
535 })->prio (Coro::PRIO_MAX); 541 })->prio (Coro::PRIO_MAX);
536 542
537 while ($busy) { 543 while ($busy) {
538 if (Coro::nready) { 544 if (Coro::nready) {
749 755
750 my $t1 = Time::HiRes::time; 756 my $t1 = Time::HiRes::time;
751 my $data = $process->(\@data); 757 my $data = $process->(\@data);
752 my $t2 = Time::HiRes::time; 758 my $t2 = Time::HiRes::time;
753 759
754 warn "cache: '$id' processed in ", $t2 - $t1, "s\n"; 760 info "cache: '$id' processed in ", $t2 - $t1, "s\n";
755 761
756 db_put cache => "$id/data", $data; 762 db_put cache => "$id/data", $data;
757 db_put cache => "$id/md5" , $md5; 763 db_put cache => "$id/md5" , $md5;
758 db_put cache => "$id/meta", $meta; 764 db_put cache => "$id/meta", $meta;
759 765
769 775
770=cut 776=cut
771 777
772sub datalog($@) { 778sub datalog($@) {
773 my ($type, %kv) = @_; 779 my ($type, %kv) = @_;
774 warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); 780 info "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type });
775} 781}
776 782
777=back 783=back
778 784
779=cut 785=cut
974 980
975 } elsif (exists $cb_id{$type}) { 981 } elsif (exists $cb_id{$type}) {
976 _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; 982 _attach_cb $registry, $cb_id{$type}, $prio, shift @arg;
977 983
978 } elsif (ref $type) { 984 } elsif (ref $type) {
979 warn "attaching objects not supported, ignoring.\n"; 985 error "attaching objects not supported, ignoring.\n";
980 986
981 } else { 987 } else {
982 shift @arg; 988 shift @arg;
983 warn "attach argument '$type' not supported, ignoring.\n"; 989 error "attach argument '$type' not supported, ignoring.\n";
984 } 990 }
985 } 991 }
986} 992}
987 993
988sub _object_attach { 994sub _object_attach {
998 _attach $registry, $klass, @attach; 1004 _attach $registry, $klass, @attach;
999 } 1005 }
1000 1006
1001 $obj->{$name} = \%arg; 1007 $obj->{$name} = \%arg;
1002 } else { 1008 } else {
1003 warn "object uses attachment '$name' which is not available, postponing.\n"; 1009 info "object uses attachment '$name' which is not available, postponing.\n";
1004 } 1010 }
1005 1011
1006 $obj->{_attachment}{$name} = undef; 1012 $obj->{_attachment}{$name} = undef;
1007} 1013}
1008 1014
1067 1073
1068 for (@$callbacks) { 1074 for (@$callbacks) {
1069 eval { &{$_->[1]} }; 1075 eval { &{$_->[1]} };
1070 1076
1071 if ($@) { 1077 if ($@) {
1072 warn "$@";
1073 warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; 1078 error "$@", "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n";
1074 override; 1079 override;
1075 } 1080 }
1076 1081
1077 return 1 if $override; 1082 return 1 if $override;
1078 } 1083 }
1157 for (@$attach) { 1162 for (@$attach) {
1158 my ($klass, @attach) = @$_; 1163 my ($klass, @attach) = @$_;
1159 _attach $registry, $klass, @attach; 1164 _attach $registry, $klass, @attach;
1160 } 1165 }
1161 } else { 1166 } else {
1162 warn "object uses attachment '$name' that is not available, postponing.\n"; 1167 info "object uses attachment '$name' that is not available, postponing.\n";
1163 } 1168 }
1164 } 1169 }
1165} 1170}
1166 1171
1167cf::attachable->attach ( 1172cf::attachable->attach (
1194 my ($filename, $rdata, $objs) = @_; 1199 my ($filename, $rdata, $objs) = @_;
1195 1200
1196 sync_job { 1201 sync_job {
1197 if (length $$rdata) { 1202 if (length $$rdata) {
1198 utf8::decode (my $decname = $filename); 1203 utf8::decode (my $decname = $filename);
1199 warn sprintf "saving %s (%d,%d)\n", 1204 trace sprintf "saving %s (%d,%d)\n",
1200 $decname, length $$rdata, scalar @$objs 1205 $decname, length $$rdata, scalar @$objs
1201 if $VERBOSE_IO; 1206 if $VERBOSE_IO;
1202 1207
1203 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { 1208 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) {
1204 aio_chmod $fh, SAVE_MODE; 1209 aio_chmod $fh, SAVE_MODE;
1205 aio_write $fh, 0, (length $$rdata), $$rdata, 0; 1210 aio_write $fh, 0, (length $$rdata), $$rdata, 0;
1228 aio_rename "$filename~", $filename; 1233 aio_rename "$filename~", $filename;
1229 1234
1230 $filename =~ s%/[^/]+$%%; 1235 $filename =~ s%/[^/]+$%%;
1231 aio_pathsync $filename if $cf::USE_FSYNC; 1236 aio_pathsync $filename if $cf::USE_FSYNC;
1232 } else { 1237 } else {
1233 warn "unable to save objects: $filename~: $!\n"; 1238 error "unable to save objects: $filename~: $!\n";
1234 } 1239 }
1235 } else { 1240 } else {
1236 aio_unlink $filename; 1241 aio_unlink $filename;
1237 aio_unlink "$filename.pst"; 1242 aio_unlink "$filename.pst";
1238 } 1243 }
1262 my $st = eval { Coro::Storable::thaw $av }; 1267 my $st = eval { Coro::Storable::thaw $av };
1263 $av = $st->{objs}; 1268 $av = $st->{objs};
1264 } 1269 }
1265 1270
1266 utf8::decode (my $decname = $filename); 1271 utf8::decode (my $decname = $filename);
1267 warn sprintf "loading %s (%d,%d)\n", 1272 trace sprintf "loading %s (%d,%d)\n",
1268 $decname, length $data, scalar @{$av || []} 1273 $decname, length $data, scalar @{$av || []}
1269 if $VERBOSE_IO; 1274 if $VERBOSE_IO;
1270 1275
1271 ($data, $av) 1276 ($data, $av)
1272} 1277}
1273 1278
1366 1371
1367 $pl->ext_reply ($reply, @reply) 1372 $pl->ext_reply ($reply, @reply)
1368 if $reply; 1373 if $reply;
1369 1374
1370 } else { 1375 } else {
1371 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 1376 error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
1372 } 1377 }
1373 1378
1374 cf::override; 1379 cf::override;
1375 }, 1380 },
1376); 1381);
1387 1392
1388 $grp 1393 $grp
1389} 1394}
1390 1395
1391sub load_extensions { 1396sub load_extensions {
1397 info "loading extensions...";
1398
1392 cf::sync_job { 1399 cf::sync_job {
1393 my %todo; 1400 my %todo;
1394 1401
1395 for my $path (<$LIBDIR/*.ext>) { 1402 for my $path (<$LIBDIR/*.ext>) {
1396 next unless -r $path; 1403 next unless -r $path;
1436 for (split /,\s*/, $v->{meta}{depends}) { 1443 for (split /,\s*/, $v->{meta}{depends}) {
1437 next ext 1444 next ext
1438 unless exists $done{$_}; 1445 unless exists $done{$_};
1439 } 1446 }
1440 1447
1441 warn "... pass $pass, loading '$k' into '$v->{pkg}'\n"; 1448 trace "... pass $pass, loading '$k' into '$v->{pkg}'\n";
1442 1449
1443 my $active = eval $v->{source}; 1450 my $active = eval $v->{source};
1444 1451
1445 if (length $@) { 1452 if (length $@) {
1446 warn "$v->{path}: $@\n"; 1453 error "$v->{path}: $@\n";
1447 1454
1448 cf::cleanup "mandatory extension '$k' failed to load, exiting." 1455 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1449 if exists $v->{meta}{mandatory}; 1456 if exists $v->{meta}{mandatory};
1450 1457
1451 warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; 1458 warn "$v->{base}: optional extension cannot be loaded, skipping.\n";
1453 } else { 1460 } else {
1454 $done{$k} = delete $todo{$k}; 1461 $done{$k} = delete $todo{$k};
1455 push @EXTS, $v->{pkg}; 1462 push @EXTS, $v->{pkg};
1456 $progress = 1; 1463 $progress = 1;
1457 1464
1458 warn "$v->{base}: extension inactive.\n" 1465 info "$v->{base}: extension inactive.\n"
1459 unless $active; 1466 unless $active;
1460 } 1467 }
1461 } 1468 }
1462 1469
1463 unless ($progress) { 1470 unless ($progress) {
1635 $pl->password ("*"); # this should lock out the player until we have nuked the dir 1642 $pl->password ("*"); # this should lock out the player until we have nuked the dir
1636 1643
1637 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; 1644 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active;
1638 $pl->deactivate; 1645 $pl->deactivate;
1639 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; 1646 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy;
1640 $pl->ob->check_score;
1641 $pl->invoke (cf::EVENT_PLAYER_QUIT); 1647 $pl->invoke (cf::EVENT_PLAYER_QUIT);
1642 $pl->ns->destroy if $pl->ns; 1648 $pl->ns->destroy if $pl->ns;
1643 1649
1644 my $path = playerdir $pl; 1650 my $path = playerdir $pl;
1645 my $temp = "$path~$cf::RUNTIME~deleting~"; 1651 my $temp = "$path~$cf::RUNTIME~deleting~";
2315 2321
2316 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2322 my $lock = cf::lock_acquire "map_data:$self->{path}";
2317 2323
2318 return if $self->players; 2324 return if $self->players;
2319 2325
2320 warn "resetting map ", $self->path, "\n"; 2326 cf::trace "resetting map ", $self->path, "\n";
2321 2327
2322 $self->in_memory (cf::MAP_SWAPPED); 2328 $self->in_memory (cf::MAP_SWAPPED);
2323 2329
2324 # need to save uniques path 2330 # need to save uniques path
2325 unless ($self->{deny_save}) { 2331 unless ($self->{deny_save}) {
2690 2696
2691sub cf::object::player::goto { 2697sub cf::object::player::goto {
2692 my ($self, $path, $x, $y, $check, $done) = @_; 2698 my ($self, $path, $x, $y, $check, $done) = @_;
2693 2699
2694 if ($self->{_link_recursion} >= $MAX_LINKS) { 2700 if ($self->{_link_recursion} >= $MAX_LINKS) {
2695 warn "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting."; 2701 error "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting.";
2696 $self->failmsg ("Something went wrong inside the server - please contact an administrator!"); 2702 $self->failmsg ("Something went wrong inside the server - please contact an administrator!");
2697 ($path, $x, $y) = @$EMERGENCY_POSITION; 2703 ($path, $x, $y) = @$EMERGENCY_POSITION;
2698 } 2704 }
2699 2705
2700 # do generation counting so two concurrent goto's will be executed in-order 2706 # do generation counting so two concurrent goto's will be executed in-order
2848 $self->message ("Something went wrong deep within the deliantra server. " 2854 $self->message ("Something went wrong deep within the deliantra server. "
2849 . "I'll try to bring you back to the map you were before. " 2855 . "I'll try to bring you back to the map you were before. "
2850 . "Please report this to the dungeon master!", 2856 . "Please report this to the dungeon master!",
2851 cf::NDI_UNIQUE | cf::NDI_RED); 2857 cf::NDI_UNIQUE | cf::NDI_RED);
2852 2858
2853 warn "ERROR in enter_exit: $@"; 2859 error "ERROR in enter_exit: $@";
2854 $self->leave_link; 2860 $self->leave_link;
2855 } 2861 }
2856 })->prio (1); 2862 })->prio (1);
2857} 2863}
2858 2864
3171 3177
3172 $ns->ext_reply ($reply, @reply) 3178 $ns->ext_reply ($reply, @reply)
3173 if $reply; 3179 if $reply;
3174 3180
3175 } else { 3181 } else {
3176 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 3182 error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
3177 } 3183 }
3178 3184
3179 cf::override; 3185 cf::override;
3180 }, 3186 },
3181); 3187);
3311 local @cf::_safe_eval_args = values %vars; 3317 local @cf::_safe_eval_args = values %vars;
3312 @res = wantarray ? eval eval : scalar eval $eval; 3318 @res = wantarray ? eval eval : scalar eval $eval;
3313 } 3319 }
3314 3320
3315 if ($@) { 3321 if ($@) {
3316 warn "$@"; 3322 warn "$@",
3317 warn "while executing safe code '$code'\n"; 3323 "while executing safe code '$code'\n",
3318 warn "with arguments " . (join " ", %vars) . "\n"; 3324 "with arguments " . (join " ", %vars) . "\n";
3319 } 3325 }
3320 3326
3321 wantarray ? @res : $res[0] 3327 wantarray ? @res : $res[0]
3322} 3328}
3323 3329
3357 # for this (global event?) 3363 # for this (global event?)
3358 %ext::player_env::MUSIC_FACE_CACHE = (); 3364 %ext::player_env::MUSIC_FACE_CACHE = ();
3359 3365
3360 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3366 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3361 3367
3362 warn "loading facedata from $path\n"; 3368 trace "loading facedata from $path\n";
3363 3369
3364 my $facedata; 3370 my $facedata;
3365 0 < aio_load $path, $facedata 3371 0 < aio_load $path, $facedata
3366 or die "$path: $!"; 3372 or die "$path: $!";
3367 3373
3401 3407
3402 if (my $smooth = cf::face::find $info->{smooth}) { 3408 if (my $smooth = cf::face::find $info->{smooth}) {
3403 cf::face::set_smooth $idx, $smooth; 3409 cf::face::set_smooth $idx, $smooth;
3404 cf::face::set_smoothlevel $idx, $info->{smoothlevel}; 3410 cf::face::set_smoothlevel $idx, $info->{smoothlevel};
3405 } else { 3411 } else {
3406 warn "smooth face '$info->{smooth}' not found for face '$face'"; 3412 error "smooth face '$info->{smooth}' not found for face '$face'";
3407 } 3413 }
3408 3414
3409 cf::cede_to_tick; 3415 cf::cede_to_tick;
3410 } 3416 }
3411 } 3417 }
3429 my $idx = (cf::face::find $name) || cf::face::alloc $name; 3435 my $idx = (cf::face::find $name) || cf::face::alloc $name;
3430 3436
3431 cf::face::set_data $idx, 0, $info->{data}, $info->{hash}; 3437 cf::face::set_data $idx, 0, $info->{data}, $info->{hash};
3432 cf::face::set_type $idx, $info->{type}; 3438 cf::face::set_type $idx, $info->{type};
3433 } else { 3439 } else {
3434 $RESOURCE{$name} = $info; 3440 $RESOURCE{$name} = $info; # unused
3435 } 3441 }
3436 3442
3437 cf::cede_to_tick; 3443 cf::cede_to_tick;
3438 } 3444 }
3439 } 3445 }
3440 3446
3441 cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE); 3447 cf::global->invoke (EVENT_GLOBAL_RESOURCE_UPDATE);
3442 3448
3443 1 3449 1
3444} 3450}
3445
3446cf::global->attach (on_resource_update => sub {
3447 if (my $soundconf = $RESOURCE{"res/sound.conf"}) {
3448 $soundconf = JSON::XS->new->utf8->relaxed->decode ($soundconf->{data});
3449
3450 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3451 my $sound = $soundconf->{compat}[$_]
3452 or next;
3453
3454 my $face = cf::face::find "sound/$sound->[1]";
3455 cf::sound::set $sound->[0] => $face;
3456 cf::sound::old_sound_index $_, $face; # gcfclient-compat
3457 }
3458
3459 while (my ($k, $v) = each %{$soundconf->{event}}) {
3460 my $face = cf::face::find "sound/$v";
3461 cf::sound::set $k => $face;
3462 }
3463 }
3464});
3465 3451
3466register_exticmd fx_want => sub { 3452register_exticmd fx_want => sub {
3467 my ($ns, $want) = @_; 3453 my ($ns, $want) = @_;
3468 3454
3469 while (my ($k, $v) = each %$want) { 3455 while (my ($k, $v) = each %$want) {
3508sub reload_treasures { 3494sub reload_treasures {
3509 load_resource_file "$DATADIR/treasures" 3495 load_resource_file "$DATADIR/treasures"
3510 or die "unable to load treasurelists\n"; 3496 or die "unable to load treasurelists\n";
3511} 3497}
3512 3498
3499sub reload_sound {
3500 trace "loading sound config from $DATADIR/sound\n";
3501
3502 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data
3503 or die "$DATADIR/sound $!";
3504
3505 my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data);
3506
3507 for (0 .. SOUND_CAST_SPELL_0 - 1) {
3508 my $sound = $soundconf->{compat}[$_]
3509 or next;
3510
3511 my $face = cf::face::find "sound/$sound->[1]";
3512 cf::sound::set $sound->[0] => $face;
3513 cf::sound::old_sound_index $_, $face; # gcfclient-compat
3514 }
3515
3516 while (my ($k, $v) = each %{$soundconf->{event}}) {
3517 my $face = cf::face::find "sound/$v";
3518 cf::sound::set $k => $face;
3519 }
3520}
3521
3513sub reload_resources { 3522sub reload_resources {
3514 warn "reloading resource files...\n"; 3523 trace "reloading resource files...\n";
3515 3524
3516 reload_facedata; 3525 reload_facedata;
3526 reload_sound;
3517 reload_archetypes; 3527 reload_archetypes;
3518 reload_regions; 3528 reload_regions;
3519 reload_treasures; 3529 reload_treasures;
3520 3530
3521 warn "finished reloading resource files\n"; 3531 trace "finished reloading resource files\n";
3522} 3532}
3523 3533
3524sub reload_config { 3534sub reload_config {
3525 warn "reloading config file...\n"; 3535 trace "reloading config file...\n";
3526 3536
3527 open my $fh, "<:utf8", "$CONFDIR/config" 3537 open my $fh, "<:utf8", "$CONFDIR/config"
3528 or return; 3538 or return;
3529 3539
3530 local $/; 3540 local $/;
3541 and die "WARNING: m(un)lockall failed: $!\n"; 3551 and die "WARNING: m(un)lockall failed: $!\n";
3542 }; 3552 };
3543 warn $@ if $@; 3553 warn $@ if $@;
3544 } 3554 }
3545 3555
3546 warn "finished reloading resource files\n"; 3556 trace "finished reloading resource files\n";
3547} 3557}
3548 3558
3549sub pidfile() { 3559sub pidfile() {
3550 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT 3560 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3551 or die "$PIDFILE: $!"; 3561 or die "$PIDFILE: $!";
3564 seek $fh, 0, 0; 3574 seek $fh, 0, 0;
3565 print $fh $$; 3575 print $fh $$;
3566} 3576}
3567 3577
3568sub main_loop { 3578sub main_loop {
3569 warn "EV::loop starting\n"; 3579 trace "EV::loop starting\n";
3570 if (1) { 3580 if (1) {
3571 EV::loop; 3581 EV::loop;
3572 } 3582 }
3573 warn "EV::loop returned\n"; 3583 trace "EV::loop returned\n";
3574 goto &main_loop unless $REALLY_UNLOOP; 3584 goto &main_loop unless $REALLY_UNLOOP;
3575} 3585}
3576 3586
3577sub main { 3587sub main {
3578 cf::init_globals; # initialise logging 3588 cf::init_globals; # initialise logging
3626 3636
3627 (pop @POST_INIT)->(0) while @POST_INIT; 3637 (pop @POST_INIT)->(0) while @POST_INIT;
3628 }; 3638 };
3629 3639
3630 cf::object::thawer::errors_are_fatal 0; 3640 cf::object::thawer::errors_are_fatal 0;
3631 warn "parse errors in files are no longer fatal from this point on.\n"; 3641 info "parse errors in files are no longer fatal from this point on.\n";
3632 3642
3633 main_loop; 3643 main_loop;
3634} 3644}
3635 3645
3636############################################################################# 3646#############################################################################
3677 or return; 3687 or return;
3678 3688
3679 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE 3689 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE
3680 and return; 3690 and return;
3681 3691
3682 warn sprintf "runtime file written (%gs).\n", AE::time - $t0; 3692 trace sprintf "runtime file written (%gs).\n", AE::time - $t0;
3683 3693
3684 1 3694 1
3685} 3695}
3686 3696
3687our $uuid_lock; 3697our $uuid_lock;
3699 or return; 3709 or return;
3700 3710
3701 my $value = uuid_seq uuid_cur; 3711 my $value = uuid_seq uuid_cur;
3702 3712
3703 unless ($value) { 3713 unless ($value) {
3704 warn "cowardly refusing to write zero uuid value!\n"; 3714 info "cowardly refusing to write zero uuid value!\n";
3705 return; 3715 return;
3706 } 3716 }
3707 3717
3708 my $value = uuid_str $value + $uuid_skip; 3718 my $value = uuid_str $value + $uuid_skip;
3709 $uuid_skip = 0; 3719 $uuid_skip = 0;
3719 or return; 3729 or return;
3720 3730
3721 aio_rename "$uuid~", $uuid 3731 aio_rename "$uuid~", $uuid
3722 and return; 3732 and return;
3723 3733
3724 warn "uuid file written ($value).\n"; 3734 trace "uuid file written ($value).\n";
3725 3735
3726 1 3736 1
3727 3737
3728} 3738}
3729 3739
3735} 3745}
3736 3746
3737sub emergency_save() { 3747sub emergency_save() {
3738 my $freeze_guard = cf::freeze_mainloop; 3748 my $freeze_guard = cf::freeze_mainloop;
3739 3749
3740 warn "emergency_perl_save: enter\n"; 3750 info "emergency_perl_save: enter\n";
3741 3751
3742 cf::sync_job { 3752 cf::sync_job {
3743 # this is a trade-off: we want to be very quick here, so 3753 # this is a trade-off: we want to be very quick here, so
3744 # save all maps without fsync, and later call a global sync 3754 # save all maps without fsync, and later call a global sync
3745 # (which in turn might be very very slow) 3755 # (which in turn might be very very slow)
3746 local $USE_FSYNC = 0; 3756 local $USE_FSYNC = 0;
3747 3757
3748 # use a peculiar iteration method to avoid tripping on perl 3758 # use a peculiar iteration method to avoid tripping on perl
3749 # refcount bugs in for. also avoids problems with players 3759 # refcount bugs in for. also avoids problems with players
3750 # and maps saved/destroyed asynchronously. 3760 # and maps saved/destroyed asynchronously.
3751 warn "emergency_perl_save: begin player save\n"; 3761 info "emergency_perl_save: begin player save\n";
3752 for my $login (keys %cf::PLAYER) { 3762 for my $login (keys %cf::PLAYER) {
3753 my $pl = $cf::PLAYER{$login} or next; 3763 my $pl = $cf::PLAYER{$login} or next;
3754 $pl->valid or next; 3764 $pl->valid or next;
3755 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3765 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3756 $pl->save; 3766 $pl->save;
3757 } 3767 }
3758 warn "emergency_perl_save: end player save\n"; 3768 info "emergency_perl_save: end player save\n";
3759 3769
3760 warn "emergency_perl_save: begin map save\n"; 3770 info "emergency_perl_save: begin map save\n";
3761 for my $path (keys %cf::MAP) { 3771 for my $path (keys %cf::MAP) {
3762 my $map = $cf::MAP{$path} or next; 3772 my $map = $cf::MAP{$path} or next;
3763 $map->valid or next; 3773 $map->valid or next;
3764 $map->save; 3774 $map->save;
3765 } 3775 }
3766 warn "emergency_perl_save: end map save\n"; 3776 info "emergency_perl_save: end map save\n";
3767 3777
3768 warn "emergency_perl_save: begin database checkpoint\n"; 3778 info "emergency_perl_save: begin database checkpoint\n";
3769 BDB::db_env_txn_checkpoint $DB_ENV; 3779 BDB::db_env_txn_checkpoint $DB_ENV;
3770 warn "emergency_perl_save: end database checkpoint\n"; 3780 info "emergency_perl_save: end database checkpoint\n";
3771 3781
3772 warn "emergency_perl_save: begin write uuid\n"; 3782 info "emergency_perl_save: begin write uuid\n";
3773 write_uuid_sync 1; 3783 write_uuid_sync 1;
3774 warn "emergency_perl_save: end write uuid\n"; 3784 info "emergency_perl_save: end write uuid\n";
3775 }; 3785 };
3776 3786
3777 warn "emergency_perl_save: starting sync()\n"; 3787 info "emergency_perl_save: starting sync()\n";
3778 IO::AIO::aio_sync sub { 3788 IO::AIO::aio_sync sub {
3779 warn "emergency_perl_save: finished sync()\n"; 3789 info "emergency_perl_save: finished sync()\n";
3780 }; 3790 };
3781 3791
3782 warn "emergency_perl_save: leave\n"; 3792 info "emergency_perl_save: leave\n";
3783} 3793}
3784 3794
3785sub post_cleanup { 3795sub post_cleanup {
3786 my ($make_core) = @_; 3796 my ($make_core) = @_;
3787 3797
3788 warn Carp::longmess "post_cleanup backtrace" 3798 error Carp::longmess "post_cleanup backtrace"
3789 if $make_core; 3799 if $make_core;
3790 3800
3791 my $fh = pidfile; 3801 my $fh = pidfile;
3792 unlink $PIDFILE if <$fh> == $$; 3802 unlink $PIDFILE if <$fh> == $$;
3793} 3803}
3818} 3828}
3819 3829
3820sub do_reload_perl() { 3830sub do_reload_perl() {
3821 # can/must only be called in main 3831 # can/must only be called in main
3822 if (in_main) { 3832 if (in_main) {
3823 warn "can only reload from main coroutine"; 3833 error "can only reload from main coroutine";
3824 return; 3834 return;
3825 } 3835 }
3826 3836
3827 return if $RELOAD++; 3837 return if $RELOAD++;
3828 3838
3829 my $t1 = AE::time; 3839 my $t1 = AE::time;
3830 3840
3831 while ($RELOAD) { 3841 while ($RELOAD) {
3832 warn "reloading..."; 3842 info "reloading...";
3833 3843
3834 warn "entering sync_job"; 3844 trace "entering sync_job";
3835 3845
3836 cf::sync_job { 3846 cf::sync_job {
3837 cf::write_runtime_sync; # external watchdog should not bark 3847 cf::write_runtime_sync; # external watchdog should not bark
3838 cf::emergency_save; 3848 cf::emergency_save;
3839 cf::write_runtime_sync; # external watchdog should not bark 3849 cf::write_runtime_sync; # external watchdog should not bark
3840 3850
3841 warn "syncing database to disk"; 3851 trace "syncing database to disk";
3842 BDB::db_env_txn_checkpoint $DB_ENV; 3852 BDB::db_env_txn_checkpoint $DB_ENV;
3843 3853
3844 # if anything goes wrong in here, we should simply crash as we already saved 3854 # if anything goes wrong in here, we should simply crash as we already saved
3845 3855
3846 warn "flushing outstanding aio requests"; 3856 trace "flushing outstanding aio requests";
3847 while (IO::AIO::nreqs || BDB::nreqs) { 3857 while (IO::AIO::nreqs || BDB::nreqs) {
3848 Coro::EV::timer_once 0.01; # let the sync_job do it's thing 3858 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3849 } 3859 }
3850 3860
3851 warn "cancelling all extension coros"; 3861 trace "cancelling all extension coros";
3852 $_->cancel for values %EXT_CORO; 3862 $_->cancel for values %EXT_CORO;
3853 %EXT_CORO = (); 3863 %EXT_CORO = ();
3854 3864
3855 warn "removing commands"; 3865 trace "removing commands";
3856 %COMMAND = (); 3866 %COMMAND = ();
3857 3867
3858 warn "removing ext/exti commands"; 3868 trace "removing ext/exti commands";
3859 %EXTCMD = (); 3869 %EXTCMD = ();
3860 %EXTICMD = (); 3870 %EXTICMD = ();
3861 3871
3862 warn "unloading/nuking all extensions"; 3872 trace "unloading/nuking all extensions";
3863 for my $pkg (@EXTS) { 3873 for my $pkg (@EXTS) {
3864 warn "... unloading $pkg"; 3874 trace "... unloading $pkg";
3865 3875
3866 if (my $cb = $pkg->can ("unload")) { 3876 if (my $cb = $pkg->can ("unload")) {
3867 eval { 3877 eval {
3868 $cb->($pkg); 3878 $cb->($pkg);
3869 1 3879 1
3870 } or warn "$pkg unloaded, but with errors: $@"; 3880 } or error "$pkg unloaded, but with errors: $@";
3871 } 3881 }
3872 3882
3873 warn "... clearing $pkg"; 3883 trace "... clearing $pkg";
3874 clear_package $pkg; 3884 clear_package $pkg;
3875 } 3885 }
3876 3886
3877 warn "unloading all perl modules loaded from $LIBDIR"; 3887 trace "unloading all perl modules loaded from $LIBDIR";
3878 while (my ($k, $v) = each %INC) { 3888 while (my ($k, $v) = each %INC) {
3879 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; 3889 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
3880 3890
3881 warn "... unloading $k"; 3891 trace "... unloading $k";
3882 delete $INC{$k}; 3892 delete $INC{$k};
3883 3893
3884 $k =~ s/\.pm$//; 3894 $k =~ s/\.pm$//;
3885 $k =~ s/\//::/g; 3895 $k =~ s/\//::/g;
3886 3896
3889 } 3899 }
3890 3900
3891 clear_package $k; 3901 clear_package $k;
3892 } 3902 }
3893 3903
3894 warn "getting rid of safe::, as good as possible"; 3904 trace "getting rid of safe::, as good as possible";
3895 clear_package "safe::$_" 3905 clear_package "safe::$_"
3896 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 3906 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
3897 3907
3898 warn "unloading cf.pm \"a bit\""; 3908 trace "unloading cf.pm \"a bit\"";
3899 delete $INC{"cf.pm"}; 3909 delete $INC{"cf.pm"};
3900 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; 3910 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES;
3901 3911
3902 # don't, removes xs symbols, too, 3912 # don't, removes xs symbols, too,
3903 # and global variables created in xs 3913 # and global variables created in xs
3904 #clear_package __PACKAGE__; 3914 #clear_package __PACKAGE__;
3905 3915
3906 warn "unload completed, starting to reload now"; 3916 info "unload completed, starting to reload now";
3907 3917
3908 warn "reloading cf.pm"; 3918 trace "reloading cf.pm";
3909 require cf; 3919 require cf;
3910 cf::_connect_to_perl_1; 3920 cf::_connect_to_perl_1;
3911 3921
3912 warn "loading config and database again"; 3922 trace "loading config and database again";
3913 cf::reload_config; 3923 cf::reload_config;
3914 3924
3915 warn "loading extensions"; 3925 trace "loading extensions";
3916 cf::load_extensions; 3926 cf::load_extensions;
3917 3927
3918 if ($REATTACH_ON_RELOAD) { 3928 if ($REATTACH_ON_RELOAD) {
3919 warn "reattaching attachments to objects/players"; 3929 trace "reattaching attachments to objects/players";
3920 _global_reattach; # objects, sockets 3930 _global_reattach; # objects, sockets
3921 warn "reattaching attachments to maps"; 3931 trace "reattaching attachments to maps";
3922 reattach $_ for values %MAP; 3932 reattach $_ for values %MAP;
3923 warn "reattaching attachments to players"; 3933 trace "reattaching attachments to players";
3924 reattach $_ for values %PLAYER; 3934 reattach $_ for values %PLAYER;
3925 } 3935 }
3926 3936
3927 warn "running post_init jobs"; 3937 trace "running post_init jobs";
3928 (pop @POST_INIT)->(1) while @POST_INIT; 3938 (pop @POST_INIT)->(1) while @POST_INIT;
3929 3939
3930 warn "leaving sync_job"; 3940 trace "leaving sync_job";
3931 3941
3932 1 3942 1
3933 } or do { 3943 } or do {
3934 warn $@; 3944 error $@;
3935 cf::cleanup "error while reloading, exiting."; 3945 cf::cleanup "error while reloading, exiting.";
3936 }; 3946 };
3937 3947
3938 warn "reloaded"; 3948 info "reloaded";
3939 --$RELOAD; 3949 --$RELOAD;
3940 } 3950 }
3941 3951
3942 $t1 = AE::time - $t1; 3952 $t1 = AE::time - $t1;
3943 warn "reload completed in ${t1}s\n"; 3953 info "reload completed in ${t1}s\n";
3944}; 3954};
3945 3955
3946our $RELOAD_WATCHER; # used only during reload 3956our $RELOAD_WATCHER; # used only during reload
3947 3957
3948sub reload_perl() { 3958sub reload_perl() {
4008 if ($NOW >= $NEXT_RUNTIME_WRITE) { 4018 if ($NOW >= $NEXT_RUNTIME_WRITE) {
4009 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 4019 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
4010 Coro::async_pool { 4020 Coro::async_pool {
4011 $Coro::current->{desc} = "runtime saver"; 4021 $Coro::current->{desc} = "runtime saver";
4012 write_runtime_sync 4022 write_runtime_sync
4013 or warn "ERROR: unable to write runtime file: $!"; 4023 or error "ERROR: unable to write runtime file: $!";
4014 }; 4024 };
4015 } 4025 }
4016 4026
4017 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { 4027 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
4018 $sig->send; 4028 $sig->send;
4026 4036
4027 if (0) { 4037 if (0) {
4028 if ($NEXT_TICK) { 4038 if ($NEXT_TICK) {
4029 my $jitter = $TICK_START - $NEXT_TICK; 4039 my $jitter = $TICK_START - $NEXT_TICK;
4030 $JITTER = $JITTER * 0.75 + $jitter * 0.25; 4040 $JITTER = $JITTER * 0.75 + $jitter * 0.25;
4031 warn "jitter $JITTER\n";#d# 4041 debug "jitter $JITTER\n";#d#
4032 } 4042 }
4033 } 4043 }
4034} 4044}
4035 4045
4036{ 4046{
4046 $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; 4056 $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7;
4047 $DB_ENV->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7; 4057 $DB_ENV->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7;
4048 $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT); 4058 $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT);
4049 $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT); 4059 $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT);
4050 4060
4061 (Coro::async { # async to keep sync_job from complaining
4051 cf::sync_job { 4062 cf::sync_job {
4052 eval { 4063 eval {
4053 BDB::db_env_open 4064 BDB::db_env_open
4054 $DB_ENV, 4065 $DB_ENV,
4055 $BDBDIR, 4066 $BDBDIR,
4056 BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN 4067 BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN
4057 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, 4068 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE,
4058 0666; 4069 0666;
4059 4070
4060 cf::cleanup "db_env_open($BDBDIR): $!" if $!; 4071 cf::cleanup "db_env_open($BDBDIR): $!" if $!;
4072 };
4073
4074 cf::cleanup "db_env_open(db): $@" if $@;
4061 }; 4075 };
4062 4076 })->join;
4063 cf::cleanup "db_env_open(db): $@" if $@;
4064 };
4065 } 4077 }
4066 4078
4067 $BDB_DEADLOCK_WATCHER = EV::periodic 0, 3, 0, sub { 4079 $BDB_DEADLOCK_WATCHER = EV::periodic 0, 3, 0, sub {
4068 BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { }; 4080 BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { };
4069 }; 4081 };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines