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.443 by root, Sun Aug 31 10:05:26 2008 UTC vs.
Revision 1.456 by root, Tue Sep 30 04:00:35 2008 UTC

63 63
64# configure various modules to our taste 64# configure various modules to our taste
65# 65#
66$Storable::canonical = 1; # reduce rsync transfers 66$Storable::canonical = 1; # reduce rsync transfers
67Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator 67Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator
68
69$Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority
70
71# make sure c-lzf reinitialises itself
72Compress::LZF::set_serializer "Storable", "Storable::net_mstore", "Storable::mretrieve";
68Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later 73Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later
69
70$Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority
71 74
72sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload 75sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
73 76
74our %COMMAND = (); 77our %COMMAND = ();
75our %COMMAND_TIME = (); 78our %COMMAND_TIME = ();
78our %EXTCMD = (); 81our %EXTCMD = ();
79our %EXTICMD = (); 82our %EXTICMD = ();
80our %EXT_CORO = (); # coroutines bound to extensions 83our %EXT_CORO = (); # coroutines bound to extensions
81our %EXT_MAP = (); # pluggable maps 84our %EXT_MAP = (); # pluggable maps
82 85
83our $RELOAD; # number of reloads so far 86our $RELOAD; # number of reloads so far, non-zero while in reload
84our @EVENT; 87our @EVENT;
85 88
86our $CONFDIR = confdir; 89our $CONFDIR = confdir;
87our $DATADIR = datadir; 90our $DATADIR = datadir;
88our $LIBDIR = "$DATADIR/ext"; 91our $LIBDIR = "$DATADIR/ext";
89our $PODDIR = "$DATADIR/pod"; 92our $PODDIR = "$DATADIR/pod";
90our $MAPDIR = "$DATADIR/" . mapdir; 93our $MAPDIR = "$DATADIR/" . mapdir;
91our $LOCALDIR = localdir; 94our $LOCALDIR = localdir;
92our $TMPDIR = "$LOCALDIR/" . tmpdir; 95our $TMPDIR = "$LOCALDIR/" . tmpdir;
93our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; 96our $UNIQUEDIR = "$LOCALDIR/" . uniquedir;
94our $PLAYERDIR = "$LOCALDIR/" . playerdir; 97our $PLAYERDIR = "$LOCALDIR/" . playerdir;
95our $RANDOMDIR = "$LOCALDIR/random"; 98our $RANDOMDIR = "$LOCALDIR/random";
96our $BDBDIR = "$LOCALDIR/db"; 99our $BDBDIR = "$LOCALDIR/db";
100our $PIDFILE = "$LOCALDIR/pid";
101our $RUNTIMEFILE = "$LOCALDIR/runtime";
102
97our %RESOURCE; 103our %RESOURCE;
98 104
99our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) 105our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!)
100our $NEXT_RUNTIME_WRITE; # when should the runtime file be written 106our $NEXT_RUNTIME_WRITE; # when should the runtime file be written
101our $NEXT_TICK; 107our $NEXT_TICK;
122our $LOAD; # a number between 0 (idle) and 1 (too many objects) 128our $LOAD; # a number between 0 (idle) and 1 (too many objects)
123our $LOADAVG; # same thing, but with alpha-smoothing 129our $LOADAVG; # same thing, but with alpha-smoothing
124our $JITTER; # average jitter 130our $JITTER; # average jitter
125our $TICK_START; # for load detecting purposes 131our $TICK_START; # for load detecting purposes
126 132
133our @POST_INIT;
134
127binmode STDOUT; 135binmode STDOUT;
128binmode STDERR; 136binmode STDERR;
129 137
130# read virtual server time, if available 138# read virtual server time, if available
131unless ($RUNTIME || !-e "$LOCALDIR/runtime") { 139unless ($RUNTIME || !-e $RUNTIMEFILE) {
132 open my $fh, "<", "$LOCALDIR/runtime" 140 open my $fh, "<", $RUNTIMEFILE
133 or die "unable to read runtime file: $!"; 141 or die "unable to read $RUNTIMEFILE file: $!";
134 $RUNTIME = <$fh> + 0.; 142 $RUNTIME = <$fh> + 0.;
135} 143}
136 144
137mkdir $_ 145mkdir $_
138 for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; 146 for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR;
299our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max 307our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max
300 308
301sub encode_json($) { $json_coder->encode ($_[0]) } 309sub encode_json($) { $json_coder->encode ($_[0]) }
302sub decode_json($) { $json_coder->decode ($_[0]) } 310sub decode_json($) { $json_coder->decode ($_[0]) }
303 311
312=item cf::post_init { BLOCK }
313
314Execute the given codeblock, I<after> all extensions have been (re-)loaded,
315but I<before> the server starts ticking again.
316
317The cdoeblock will have a single boolean argument to indicate whether this
318is a reload or not.
319
320=cut
321
322sub post_init(&) {
323 push @POST_INIT, shift;
324}
325
304=item cf::lock_wait $string 326=item cf::lock_wait $string
305 327
306Wait until the given lock is available. See cf::lock_acquire. 328Wait until the given lock is available. See cf::lock_acquire.
307 329
308=item my $lock = cf::lock_acquire $string 330=item my $lock = cf::lock_acquire $string
335 return;#d# 357 return;#d#
336 }#d# 358 }#d#
337 359
338 # wait for lock, if any 360 # wait for lock, if any
339 while ($LOCK{$key}) { 361 while ($LOCK{$key}) {
362 #local $Coro::current->{desc} = "$Coro::current->{desc} <waiting for lock $key>";
340 push @{ $LOCK{$key} }, $Coro::current; 363 push @{ $LOCK{$key} }, $Coro::current;
341 Coro::schedule; 364 Coro::schedule;
342 } 365 }
343} 366}
344 367
727 750
728############################################################################# 751#############################################################################
729 752
730=head2 ATTACHABLE OBJECTS 753=head2 ATTACHABLE OBJECTS
731 754
732Many objects in crossfire are so-called attachable objects. That means you can 755Many objects in deliantra are so-called attachable objects. That means you can
733attach callbacks/event handlers (a collection of which is called an "attachment") 756attach callbacks/event handlers (a collection of which is called an "attachment")
734to it. All such attachable objects support the following methods. 757to it. All such attachable objects support the following methods.
735 758
736In the following description, CLASS can be any of C<global>, C<object> 759In the following description, CLASS can be any of C<global>, C<object>
737C<player>, C<client> or C<map> (i.e. the attachable objects in 760C<player>, C<client> or C<map> (i.e. the attachable objects in
787=item cf::CLASS::attachment $name, ... 810=item cf::CLASS::attachment $name, ...
788 811
789Register an attachment by C<$name> through which attachable objects of the 812Register an attachment by C<$name> through which attachable objects of the
790given CLASS can refer to this attachment. 813given CLASS can refer to this attachment.
791 814
792Some classes such as crossfire maps and objects can specify attachments 815Some classes such as deliantra maps and objects can specify attachments
793that are attached at load/instantiate time, thus the need for a name. 816that are attached at load/instantiate time, thus the need for a name.
794 817
795These calls expect any number of the following handler/hook descriptions: 818These calls expect any number of the following handler/hook descriptions:
796 819
797=over 4 820=over 4
1164 } else { 1187 } else {
1165 aio_unlink "$filename.pst"; 1188 aio_unlink "$filename.pst";
1166 } 1189 }
1167 1190
1168 aio_rename "$filename~", $filename; 1191 aio_rename "$filename~", $filename;
1192
1193 $filename =~ s%/[^/]+$%%;
1194 aio_pathsync $filename;
1169 } else { 1195 } else {
1170 warn "FATAL: $filename~: $!\n"; 1196 warn "FATAL: $filename~: $!\n";
1171 } 1197 }
1172 } else { 1198 } else {
1173 aio_unlink $filename; 1199 aio_unlink $filename;
1385 1411
1386=back 1412=back
1387 1413
1388=head2 CORE EXTENSIONS 1414=head2 CORE EXTENSIONS
1389 1415
1390Functions and methods that extend core crossfire objects. 1416Functions and methods that extend core deliantra objects.
1391 1417
1392=cut 1418=cut
1393 1419
1394package cf::player; 1420package cf::player;
1395 1421
1438 1464
1439sub exists($) { 1465sub exists($) {
1440 my ($login) = @_; 1466 my ($login) = @_;
1441 1467
1442 $cf::PLAYER{$login} 1468 $cf::PLAYER{$login}
1443 or cf::sync_job { !aio_stat path $login } 1469 or !aio_stat path $login
1444} 1470}
1445 1471
1446sub find($) { 1472sub find($) {
1447 return $cf::PLAYER{$_[0]} || do { 1473 return $cf::PLAYER{$_[0]} || do {
1448 my $login = $_[0]; 1474 my $login = $_[0];
1619 } 1645 }
1620 1646
1621 \@paths 1647 \@paths
1622} 1648}
1623 1649
1624=item $protocol_xml = $player->expand_cfpod ($crossfire_pod) 1650=item $protocol_xml = $player->expand_cfpod ($cfpod)
1625 1651
1626Expand crossfire pod fragments into protocol xml. 1652Expand deliantra pod fragments into protocol xml.
1627
1628=cut
1629
1630sub expand_cfpod {
1631 my ($self, $pod) = @_;
1632
1633 my @nest = [qr<\G$>, undef, ""];
1634 my $xml;
1635
1636 for ($pod) {
1637 while () {
1638 if (/\G( (?: [^BCEGHITUZ&>\n\ ]+ | [BCEGHITUZ](?!<) | \ (?!>) )+ )/xgcs) {
1639 $xml .= $1;
1640 } elsif (/\G\n(?=\S)/xgcs) {
1641 $xml .= " ";
1642 } elsif (/\G\n/xgcs) {
1643 $xml .= "\n";
1644 } elsif (/\G ([BCEGHITUZ]) (< (?: <+\ | (?!<) ) )/xgcs) {
1645 my ($code, $delim) = ($1, scalar reverse $2);
1646 $delim =~ y/</>/; # delim now contains the stop sequence
1647 $delim = qr{\G\Q$delim};
1648
1649 my $cb;
1650
1651 if ($code eq "B") {
1652 $cb = sub { "<b>$_[0]</b>" };
1653 } elsif ($code eq "C") {
1654 $cb = sub { "<tt>$_[0]</tt>" };
1655 } elsif ($code eq "E") {
1656 $cb = sub { warn "E<$_[0]>\n";"&$_[0];" };
1657 } elsif ($code eq "G") {
1658 $cb = sub {
1659 my ($male, $female) = split /\|/, $_[0];
1660 $self->gender ? $female : $male
1661 };
1662 } elsif ($code eq "H") {
1663 $cb = sub {
1664 (
1665 "<fg name=\"lightblue\">[$_[0] (Use hintmode to suppress hints)]</fg>",
1666 "<fg name=\"lightblue\">[Hint suppressed, see hintmode]</fg>",
1667 "",
1668 )[$self->{hintmode}];
1669 };
1670 } elsif ($code eq "I") {
1671 $cb = sub { "<i>$_[0]</i>" };
1672 } elsif ($code eq "T") {
1673 $cb = sub { "<big><b>$_[0]</b></big>" };
1674 } elsif ($code eq "U") {
1675 $cb = sub { "<u>$_[0]</u>" };
1676 } elsif ($code eq "Z") {
1677 $cb = sub { };
1678 } else {
1679 die "FATAL error in expand_cfpod";
1680 }
1681
1682 push @nest, [$delim, $cb, $xml];
1683 undef $xml;
1684
1685 } elsif ($_ =~ /$nest[-1][0]/gcs) {
1686 my $nest = pop @nest;
1687
1688 if ($nest->[1]) {
1689 $xml = $nest->[2] . $nest->[1]->($xml);
1690 } else {
1691 last;
1692 }
1693 } elsif (/\G</xgcs) {
1694 $xml .= "&lt;";
1695 } elsif (/\G&/xgcs) {
1696 $xml .= "&amp;";
1697 } elsif (/\G>/xgcs) {
1698 $xml .= ">";
1699 } else {
1700 if ($pod =~ /\G(.+)/xgcs) {
1701 warn "parse error (at $1)($nest[-1][0]) while expanding cfpod:\n$pod";
1702 last;
1703 } else {
1704 warn "parse error (unclosed interior sequence at end of cfpod) while expanding cfpod:\n$pod";
1705 return "<b>Sorry, the server encountered an internal error when formatting this message, please report this.</b>";
1706 }
1707 }
1708 }
1709 }
1710
1711 $xml
1712}
1713
1714sub hintmode {
1715 $_[0]{hintmode} = $_[1] if @_ > 1;
1716 $_[0]{hintmode}
1717}
1718 1653
1719=item $player->ext_reply ($msgid, @msg) 1654=item $player->ext_reply ($msgid, @msg)
1720 1655
1721Sends an ext reply to the player. 1656Sends an ext reply to the player.
1722 1657
2536 2471
2537=item $player_object->enter_link 2472=item $player_object->enter_link
2538 2473
2539Freezes the player and moves him/her to a special map (C<{link}>). 2474Freezes the player and moves him/her to a special map (C<{link}>).
2540 2475
2541The player should be reasonably safe there for short amounts of time. You 2476The player should be reasonably safe there for short amounts of time (e.g.
2542I<MUST> call C<leave_link> as soon as possible, though. 2477for loading a map). You I<MUST> call C<leave_link> as soon as possible,
2478though, as the palyer cannot control the character while it is on the link
2479map.
2543 2480
2544Will never block. 2481Will never block.
2545 2482
2546=item $player_object->leave_link ($map, $x, $y) 2483=item $player_object->leave_link ($map, $x, $y)
2547 2484
2605 2542
2606 $map->load; 2543 $map->load;
2607 $map->load_neighbours; 2544 $map->load_neighbours;
2608 2545
2609 return unless $self->contr->active; 2546 return unless $self->contr->active;
2547 $self->flag (cf::FLAG_DEBUG, 0);#d# temp
2610 $self->activate_recursive; 2548 $self->activate_recursive;
2611 2549
2612 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext 2550 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext
2613 $self->enter_map ($map, $x, $y); 2551 $self->enter_map ($map, $x, $y);
2614} 2552}
2777 $self->contr->savebed ($map, $x, $y) 2715 $self->contr->savebed ($map, $x, $y)
2778 if $exit->flag (cf::FLAG_DAMNED); 2716 if $exit->flag (cf::FLAG_DAMNED);
2779 2717
2780 1 2718 1
2781 }) { 2719 }) {
2782 $self->message ("Something went wrong deep within the crossfire server. " 2720 $self->message ("Something went wrong deep within the deliantra server. "
2783 . "I'll try to bring you back to the map you were before. " 2721 . "I'll try to bring you back to the map you were before. "
2784 . "Please report this to the dungeon master!", 2722 . "Please report this to the dungeon master!",
2785 cf::NDI_UNIQUE | cf::NDI_RED); 2723 cf::NDI_UNIQUE | cf::NDI_RED);
2786 2724
2787 warn "ERROR in enter_exit: $@"; 2725 warn "ERROR in enter_exit: $@";
2853 id => "infobox", 2791 id => "infobox",
2854 title => "Body Parts", 2792 title => "Body Parts",
2855 reply => undef, 2793 reply => undef,
2856 tooltip => "Shows which body parts you posess and are available", 2794 tooltip => "Shows which body parts you posess and are available",
2857 }, 2795 },
2796 "c/skills" => {
2797 id => "infobox",
2798 title => "Skills",
2799 reply => undef,
2800 tooltip => "Shows your experience per skill and item power",
2801 },
2858 "c/uptime" => { 2802 "c/uptime" => {
2859 id => "infobox", 2803 id => "infobox",
2860 title => "Uptime", 2804 title => "Uptime",
2861 reply => undef, 2805 reply => undef,
2862 tooltip => "How long the server has been running since last restart", 2806 tooltip => "How long the server has been running since last restart",
2876); 2820);
2877 2821
2878sub cf::client::send_msg { 2822sub cf::client::send_msg {
2879 my ($self, $channel, $msg, $color, @extra) = @_; 2823 my ($self, $channel, $msg, $color, @extra) = @_;
2880 2824
2881 $msg = $self->pl->expand_cfpod ($msg); 2825 $msg = $self->pl->expand_cfpod ($msg)
2826 unless $color & cf::NDI_VERBATIM;
2882 2827
2883 $color &= cf::NDI_CLIENT_MASK; # just in case... 2828 $color &= cf::NDI_CLIENT_MASK; # just in case...
2884 2829
2885 # check predefined channels, for the benefit of C 2830 # check predefined channels, for the benefit of C
2886 if ($CHANNEL{$channel}) { 2831 if ($CHANNEL{$channel}) {
3415 reload_treasures; 3360 reload_treasures;
3416 3361
3417 warn "finished reloading resource files\n"; 3362 warn "finished reloading resource files\n";
3418} 3363}
3419 3364
3420sub init {
3421 my $guard = freeze_mainloop;
3422
3423 evthread_start IO::AIO::poll_fileno;
3424
3425 reload_resources;
3426}
3427
3428sub reload_config { 3365sub reload_config {
3429 open my $fh, "<:utf8", "$CONFDIR/config" 3366 open my $fh, "<:utf8", "$CONFDIR/config"
3430 or return; 3367 or return;
3431 3368
3432 local $/; 3369 local $/;
3444 }; 3381 };
3445 warn $@ if $@; 3382 warn $@ if $@;
3446 } 3383 }
3447} 3384}
3448 3385
3386sub pidfile() {
3387 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3388 or die "$PIDFILE: $!";
3389 flock $fh, &Fcntl::LOCK_EX
3390 or die "$PIDFILE: flock: $!";
3391 $fh
3392}
3393
3394# make sure only one server instance is running at any one time
3395sub atomic {
3396 my $fh = pidfile;
3397
3398 my $pid = <$fh>;
3399 kill 9, $pid if $pid > 0;
3400
3401 seek $fh, 0, 0;
3402 print $fh $$;
3403}
3404
3449sub main { 3405sub main {
3406 cf::init_globals; # initialise logging
3407
3408 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3409 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3410 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3411 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3412
3413 cf::init_experience;
3414 cf::init_anim;
3415 cf::init_attackmess;
3416 cf::init_dynamic;
3417 cf::init_block;
3418
3419 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3420
3450 # we must not ever block the main coroutine 3421 # we must not ever block the main coroutine
3451 local $Coro::idle = sub { 3422 local $Coro::idle = sub {
3452 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# 3423 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
3453 (async { 3424 (async {
3454 $Coro::current->{desc} = "IDLE BUG HANDLER"; 3425 $Coro::current->{desc} = "IDLE BUG HANDLER";
3455 EV::loop EV::LOOP_ONESHOT; 3426 EV::loop EV::LOOP_ONESHOT;
3456 })->prio (Coro::PRIO_MAX); 3427 })->prio (Coro::PRIO_MAX);
3457 }; 3428 };
3458 3429
3459 { 3430 evthread_start IO::AIO::poll_fileno;
3460 my $guard = freeze_mainloop; 3431
3432 cf::sync_job {
3433 reload_resources;
3461 reload_config; 3434 reload_config;
3462 db_init; 3435 db_init;
3436
3437 cf::load_settings;
3438 cf::load_materials;
3439 cf::init_uuid;
3440 cf::init_signals;
3441 cf::init_commands;
3442 cf::init_skills;
3443
3444 cf::init_beforeplay;
3445
3446 atomic;
3447
3463 load_extensions; 3448 load_extensions;
3464 3449
3465 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3450 utime time, time, $RUNTIMEFILE;
3451
3452 # no (long-running) fork's whatsoever before this point(!)
3453 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3454
3455 (pop @POST_INIT)->(0) while @POST_INIT;
3466 } 3456 };
3467 3457
3468 EV::loop; 3458 EV::loop;
3469} 3459}
3470 3460
3471############################################################################# 3461#############################################################################
3480 }; 3470 };
3481 } 3471 }
3482} 3472}
3483 3473
3484sub write_runtime_sync { 3474sub write_runtime_sync {
3485 my $runtime = "$LOCALDIR/runtime";
3486
3487 # first touch the runtime file to show we are still running: 3475 # first touch the runtime file to show we are still running:
3488 # the fsync below can take a very very long time. 3476 # the fsync below can take a very very long time.
3489 3477
3490 IO::AIO::aio_utime $runtime, undef, undef; 3478 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef;
3491 3479
3492 my $guard = cf::lock_acquire "write_runtime"; 3480 my $guard = cf::lock_acquire "write_runtime";
3493 3481
3494 my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 3482 my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644
3495 or return; 3483 or return;
3496 3484
3497 my $value = $cf::RUNTIME + 90 + 10; 3485 my $value = $cf::RUNTIME + 90 + 10;
3498 # 10 is the runtime save interval, for a monotonic clock 3486 # 10 is the runtime save interval, for a monotonic clock
3499 # 60 allows for the watchdog to kill the server. 3487 # 60 allows for the watchdog to kill the server.
3509 aio_utime $fh, undef, undef; 3497 aio_utime $fh, undef, undef;
3510 3498
3511 close $fh 3499 close $fh
3512 or return; 3500 or return;
3513 3501
3514 aio_rename "$runtime~", $runtime 3502 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE
3515 and return; 3503 and return;
3516 3504
3517 warn "runtime file written.\n"; 3505 warn "runtime file written.\n";
3518 3506
3519 1 3507 1
3531 my $uuid = "$LOCALDIR/uuid"; 3519 my $uuid = "$LOCALDIR/uuid";
3532 3520
3533 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 3521 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644
3534 or return; 3522 or return;
3535 3523
3536 my $value = uuid_str $uuid_skip + uuid_seq uuid_cur; 3524 my $value = uuid_seq uuid_cur;
3525
3526 unless ($value) {
3527 warn "cowardly refusing to write zero uuid value!\n";
3528 return;
3529 }
3530
3531 my $value = uuid_str $value + $uuid_skip;
3537 $uuid_skip = 0; 3532 $uuid_skip = 0;
3538 3533
3539 (aio_write $fh, 0, (length $value), $value, 0) <= 0 3534 (aio_write $fh, 0, (length $value), $value, 0) <= 0
3540 and return; 3535 and return;
3541 3536
3603sub post_cleanup { 3598sub post_cleanup {
3604 my ($make_core) = @_; 3599 my ($make_core) = @_;
3605 3600
3606 warn Carp::longmess "post_cleanup backtrace" 3601 warn Carp::longmess "post_cleanup backtrace"
3607 if $make_core; 3602 if $make_core;
3603
3604 my $fh = pidfile;
3605 unlink $PIDFILE if <$fh> == $$;
3608} 3606}
3609 3607
3610# a safer delete_package, copied from Symbol 3608# a safer delete_package, copied from Symbol
3611sub clear_package($) { 3609sub clear_package($) {
3612 my $pkg = shift; 3610 my $pkg = shift;
3628 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; 3626 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
3629 for my $name (keys %$leaf_symtab) { 3627 for my $name (keys %$leaf_symtab) {
3630 _gv_clear *{"$pkg$name"}; 3628 _gv_clear *{"$pkg$name"};
3631# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; 3629# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"};
3632 } 3630 }
3633 warn "cleared package #$pkg\n";#d# 3631 warn "cleared package $pkg\n";#d#
3634} 3632}
3635
3636our $RELOAD; # how many times to reload
3637 3633
3638sub do_reload_perl() { 3634sub do_reload_perl() {
3639 # can/must only be called in main 3635 # can/must only be called in main
3640 if ($Coro::current != $Coro::main) { 3636 if ($Coro::current != $Coro::main) {
3641 warn "can only reload from main coroutine"; 3637 warn "can only reload from main coroutine";
3736 warn "reattaching attachments to maps"; 3732 warn "reattaching attachments to maps";
3737 reattach $_ for values %MAP; 3733 reattach $_ for values %MAP;
3738 warn "reattaching attachments to players"; 3734 warn "reattaching attachments to players";
3739 reattach $_ for values %PLAYER; 3735 reattach $_ for values %PLAYER;
3740 3736
3737 warn "running post_load";
3738 (pop @POST_INIT)->(1) while @POST_INIT;
3739
3741 warn "leaving sync_job"; 3740 warn "leaving sync_job";
3742 3741
3743 1 3742 1
3744 } or do { 3743 } or do {
3745 warn $@; 3744 warn $@;
3755 3754
3756sub reload_perl() { 3755sub reload_perl() {
3757 # doing reload synchronously and two reloads happen back-to-back, 3756 # doing reload synchronously and two reloads happen back-to-back,
3758 # coro crashes during coro_state_free->destroy here. 3757 # coro crashes during coro_state_free->destroy here.
3759 3758
3760 $RELOAD_WATCHER ||= EV::timer 0, 0, sub { 3759 $RELOAD_WATCHER ||= EV::timer $TICK * 1.5, 0, sub {
3761 do_reload_perl; 3760 do_reload_perl;
3762 undef $RELOAD_WATCHER; 3761 undef $RELOAD_WATCHER;
3763 }; 3762 };
3764} 3763}
3765 3764
3893 $msg =~ s/\n//; 3892 $msg =~ s/\n//;
3894 3893
3895 # limit the # of concurrent backtraces 3894 # limit the # of concurrent backtraces
3896 if ($_log_backtrace < 2) { 3895 if ($_log_backtrace < 2) {
3897 ++$_log_backtrace; 3896 ++$_log_backtrace;
3897 my $perl_bt = Carp::longmess $msg;
3898 async { 3898 async {
3899 $Coro::current->{desc} = "abt $msg"; 3899 $Coro::current->{desc} = "abt $msg";
3900 3900
3901 my @bt = fork_call { 3901 my @bt = fork_call {
3902 @addr = map { sprintf "%x", $_ } @addr; 3902 @addr = map { sprintf "%x", $_ } @addr;
3913 } 3913 }
3914 3914
3915 @funcs 3915 @funcs
3916 }; 3916 };
3917 3917
3918 LOG llevInfo, "[ABT] $msg\n"; 3918 LOG llevInfo, "[ABT] $perl_bt\n";
3919 LOG llevInfo, "[ABT] --- C backtrace follows ---\n";
3919 LOG llevInfo, "[ABT] $_\n" for @bt; 3920 LOG llevInfo, "[ABT] $_\n" for @bt;
3920 --$_log_backtrace; 3921 --$_log_backtrace;
3921 }; 3922 };
3922 } else { 3923 } else {
3923 LOG llevInfo, "[ABT] $msg\n"; 3924 LOG llevInfo, "[ABT] $msg\n";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines