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.441 by root, Sat Aug 30 05:19:03 2008 UTC vs.
Revision 1.454 by root, Thu Sep 25 12:14:32 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
1385 1408
1386=back 1409=back
1387 1410
1388=head2 CORE EXTENSIONS 1411=head2 CORE EXTENSIONS
1389 1412
1390Functions and methods that extend core crossfire objects. 1413Functions and methods that extend core deliantra objects.
1391 1414
1392=cut 1415=cut
1393 1416
1394package cf::player; 1417package cf::player;
1395 1418
1438 1461
1439sub exists($) { 1462sub exists($) {
1440 my ($login) = @_; 1463 my ($login) = @_;
1441 1464
1442 $cf::PLAYER{$login} 1465 $cf::PLAYER{$login}
1443 or cf::sync_job { !aio_stat path $login } 1466 or !aio_stat path $login
1444} 1467}
1445 1468
1446sub find($) { 1469sub find($) {
1447 return $cf::PLAYER{$_[0]} || do { 1470 return $cf::PLAYER{$_[0]} || do {
1448 my $login = $_[0]; 1471 my $login = $_[0];
1523 my ($pl) = @_; 1546 my ($pl) = @_;
1524 1547
1525 my $name = $pl->ob->name; 1548 my $name = $pl->ob->name;
1526 1549
1527 $pl->{deny_save} = 1; 1550 $pl->{deny_save} = 1;
1528 $pl->password ("*"); # this should lock out the player until we nuked the dir 1551 $pl->password ("*"); # this should lock out the player until we have nuked the dir
1529 1552
1530 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; 1553 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active;
1531 $pl->deactivate; 1554 $pl->deactivate;
1532 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; 1555 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy;
1533 $pl->ob->check_score; 1556 $pl->ob->check_score;
1619 } 1642 }
1620 1643
1621 \@paths 1644 \@paths
1622} 1645}
1623 1646
1624=item $protocol_xml = $player->expand_cfpod ($crossfire_pod) 1647=item $protocol_xml = $player->expand_cfpod ($cfpod)
1625 1648
1626Expand crossfire pod fragments into protocol xml. 1649Expand deliantra pod fragments into protocol xml.
1627
1628=cut
1629
1630use re 'eval';
1631
1632my $group;
1633my $interior; $interior = qr{
1634 # match a pod interior sequence sans C<< >>
1635 (?:
1636 \ (.*?)\ (?{ $group = $^N })
1637 | < (??{$interior}) >
1638 )
1639}x;
1640
1641sub expand_cfpod {
1642 my ($self, $pod) = @_;
1643
1644 my $xml;
1645
1646 while () {
1647 if ($pod =~ /\G( (?: [^BCGHITU]+ | .(?!<) )+ )/xgcs) {
1648 $group = $1;
1649
1650 $group =~ s/&/&amp;/g;
1651 $group =~ s/</&lt;/g;
1652
1653 $xml .= $group;
1654 } elsif ($pod =~ m%\G
1655 ([BCGHITU])
1656 <
1657 (?:
1658 ([^<>]*) (?{ $group = $^N })
1659 | < $interior >
1660 )
1661 >
1662 %gcsx
1663 ) {
1664 my ($code, $data) = ($1, $group);
1665
1666 if ($code eq "B") {
1667 $xml .= "<b>" . expand_cfpod ($self, $data) . "</b>";
1668 } elsif ($code eq "I") {
1669 $xml .= "<i>" . expand_cfpod ($self, $data) . "</i>";
1670 } elsif ($code eq "U") {
1671 $xml .= "<u>" . expand_cfpod ($self, $data) . "</u>";
1672 } elsif ($code eq "C") {
1673 $xml .= "<tt>" . expand_cfpod ($self, $data) . "</tt>";
1674 } elsif ($code eq "T") {
1675 $xml .= "<big><b>" . expand_cfpod ($self, $data) . "</b></big>";
1676 } elsif ($code eq "G") {
1677 my ($male, $female) = split /\|/, $data;
1678 $data = $self->gender ? $female : $male;
1679 $xml .= expand_cfpod ($self, $data);
1680 } elsif ($code eq "H") {
1681 $xml .= ("<fg name=\"lightblue\">[" . expand_cfpod ($self, $data) . " (Use hintmode to suppress hints)]</fg>",
1682 "<fg name=\"lightblue\">[Hint suppressed, see hintmode]</fg>",
1683 "")
1684 [$self->{hintmode}];
1685 } else {
1686 $xml .= "error processing '$code($data)' directive";
1687 }
1688 } else {
1689 if ($pod =~ /\G(.+)/) {
1690 warn "parse error while expanding $pod (at $1)";
1691 }
1692 last;
1693 }
1694 }
1695
1696 for ($xml) {
1697 # create single paragraphs (very hackish)
1698 s/(?<=\S)\n(?=\w)/ /g;
1699
1700 # compress some whitespace
1701 s/\s+\n/\n/g; # ws line-ends
1702 s/\n\n+/\n/g; # double lines
1703 s/^\n+//; # beginning lines
1704 s/\n+$//; # ending lines
1705 }
1706
1707 $xml
1708}
1709
1710no re 'eval';
1711
1712sub hintmode {
1713 $_[0]{hintmode} = $_[1] if @_ > 1;
1714 $_[0]{hintmode}
1715}
1716 1650
1717=item $player->ext_reply ($msgid, @msg) 1651=item $player->ext_reply ($msgid, @msg)
1718 1652
1719Sends an ext reply to the player. 1653Sends an ext reply to the player.
1720 1654
2534 2468
2535=item $player_object->enter_link 2469=item $player_object->enter_link
2536 2470
2537Freezes the player and moves him/her to a special map (C<{link}>). 2471Freezes the player and moves him/her to a special map (C<{link}>).
2538 2472
2539The player should be reasonably safe there for short amounts of time. You 2473The player should be reasonably safe there for short amounts of time (e.g.
2540I<MUST> call C<leave_link> as soon as possible, though. 2474for loading a map). You I<MUST> call C<leave_link> as soon as possible,
2475though, as the palyer cannot control the character while it is on the link
2476map.
2541 2477
2542Will never block. 2478Will never block.
2543 2479
2544=item $player_object->leave_link ($map, $x, $y) 2480=item $player_object->leave_link ($map, $x, $y)
2545 2481
2603 2539
2604 $map->load; 2540 $map->load;
2605 $map->load_neighbours; 2541 $map->load_neighbours;
2606 2542
2607 return unless $self->contr->active; 2543 return unless $self->contr->active;
2544 $self->flag (cf::FLAG_DEBUG, 0);#d# temp
2608 $self->activate_recursive; 2545 $self->activate_recursive;
2609 2546
2610 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext 2547 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext
2611 $self->enter_map ($map, $x, $y); 2548 $self->enter_map ($map, $x, $y);
2612} 2549}
2775 $self->contr->savebed ($map, $x, $y) 2712 $self->contr->savebed ($map, $x, $y)
2776 if $exit->flag (cf::FLAG_DAMNED); 2713 if $exit->flag (cf::FLAG_DAMNED);
2777 2714
2778 1 2715 1
2779 }) { 2716 }) {
2780 $self->message ("Something went wrong deep within the crossfire server. " 2717 $self->message ("Something went wrong deep within the deliantra server. "
2781 . "I'll try to bring you back to the map you were before. " 2718 . "I'll try to bring you back to the map you were before. "
2782 . "Please report this to the dungeon master!", 2719 . "Please report this to the dungeon master!",
2783 cf::NDI_UNIQUE | cf::NDI_RED); 2720 cf::NDI_UNIQUE | cf::NDI_RED);
2784 2721
2785 warn "ERROR in enter_exit: $@"; 2722 warn "ERROR in enter_exit: $@";
2851 id => "infobox", 2788 id => "infobox",
2852 title => "Body Parts", 2789 title => "Body Parts",
2853 reply => undef, 2790 reply => undef,
2854 tooltip => "Shows which body parts you posess and are available", 2791 tooltip => "Shows which body parts you posess and are available",
2855 }, 2792 },
2793 "c/skills" => {
2794 id => "infobox",
2795 title => "Skills",
2796 reply => undef,
2797 tooltip => "Shows your experience per skill and item power",
2798 },
2856 "c/uptime" => { 2799 "c/uptime" => {
2857 id => "infobox", 2800 id => "infobox",
2858 title => "Uptime", 2801 title => "Uptime",
2859 reply => undef, 2802 reply => undef,
2860 tooltip => "How long the server has been running since last restart", 2803 tooltip => "How long the server has been running since last restart",
2874); 2817);
2875 2818
2876sub cf::client::send_msg { 2819sub cf::client::send_msg {
2877 my ($self, $channel, $msg, $color, @extra) = @_; 2820 my ($self, $channel, $msg, $color, @extra) = @_;
2878 2821
2879 $msg = $self->pl->expand_cfpod ($msg); 2822 $msg = $self->pl->expand_cfpod ($msg)
2823 unless $color & cf::NDI_VERBATIM;
2880 2824
2881 $color &= cf::NDI_CLIENT_MASK; # just in case... 2825 $color &= cf::NDI_CLIENT_MASK; # just in case...
2882 2826
2883 # check predefined channels, for the benefit of C 2827 # check predefined channels, for the benefit of C
2884 if ($CHANNEL{$channel}) { 2828 if ($CHANNEL{$channel}) {
2905 if ($self->can_msg) { 2849 if ($self->can_msg) {
2906 # default colour, mask it out 2850 # default colour, mask it out
2907 $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF) 2851 $color &= ~(cf::NDI_COLOR_MASK | cf::NDI_DEF)
2908 if $color & cf::NDI_DEF; 2852 if $color & cf::NDI_DEF;
2909 2853
2910 $self->send_packet ("msg " . $self->{json_coder}->encode ( 2854 my $pkt = "msg "
2855 . $self->{json_coder}->encode (
2911 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra])); 2856 [$color & cf::NDI_CLIENT_MASK, $channel, $msg, @extra]
2857 );
2858
2859 # try lzf for large packets
2860 $pkt = "lzf " . Compress::LZF::compress $pkt
2861 if 1024 <= length $pkt and $self->{can_lzf};
2862
2863 # split very large packets
2864 if (8192 < length $pkt and $self->{can_lzf}) {
2865 $self->send_packet ("frag $_") for unpack "(a8192)*", $pkt;
2866 $pkt = "frag";
2867 }
2868
2869 $self->send_packet ($pkt);
2912 } else { 2870 } else {
2913 if ($color >= 0) { 2871 if ($color >= 0) {
2914 # replace some tags by gcfclient-compatible ones 2872 # replace some tags by gcfclient-compatible ones
2915 for ($msg) { 2873 for ($msg) {
2916 1 while 2874 1 while
3399 reload_treasures; 3357 reload_treasures;
3400 3358
3401 warn "finished reloading resource files\n"; 3359 warn "finished reloading resource files\n";
3402} 3360}
3403 3361
3404sub init {
3405 my $guard = freeze_mainloop;
3406
3407 evthread_start IO::AIO::poll_fileno;
3408
3409 reload_resources;
3410}
3411
3412sub reload_config { 3362sub reload_config {
3413 open my $fh, "<:utf8", "$CONFDIR/config" 3363 open my $fh, "<:utf8", "$CONFDIR/config"
3414 or return; 3364 or return;
3415 3365
3416 local $/; 3366 local $/;
3428 }; 3378 };
3429 warn $@ if $@; 3379 warn $@ if $@;
3430 } 3380 }
3431} 3381}
3432 3382
3383sub pidfile() {
3384 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3385 or die "$PIDFILE: $!";
3386 flock $fh, &Fcntl::LOCK_EX
3387 or die "$PIDFILE: flock: $!";
3388 $fh
3389}
3390
3391# make sure only one server instance is running at any one time
3392sub atomic {
3393 my $fh = pidfile;
3394
3395 my $pid = <$fh>;
3396 kill 9, $pid if $pid > 0;
3397
3398 seek $fh, 0, 0;
3399 print $fh $$;
3400}
3401
3433sub main { 3402sub main {
3403 cf::init_globals; # initialise logging
3404
3405 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3406 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3407 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3408 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3409
3410 cf::init_experience;
3411 cf::init_anim;
3412 cf::init_attackmess;
3413 cf::init_dynamic;
3414 cf::init_block;
3415
3416 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3417
3434 # we must not ever block the main coroutine 3418 # we must not ever block the main coroutine
3435 local $Coro::idle = sub { 3419 local $Coro::idle = sub {
3436 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# 3420 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
3437 (async { 3421 (async {
3438 $Coro::current->{desc} = "IDLE BUG HANDLER"; 3422 $Coro::current->{desc} = "IDLE BUG HANDLER";
3439 EV::loop EV::LOOP_ONESHOT; 3423 EV::loop EV::LOOP_ONESHOT;
3440 })->prio (Coro::PRIO_MAX); 3424 })->prio (Coro::PRIO_MAX);
3441 }; 3425 };
3442 3426
3443 { 3427 evthread_start IO::AIO::poll_fileno;
3444 my $guard = freeze_mainloop; 3428
3429 cf::sync_job {
3430 reload_resources;
3445 reload_config; 3431 reload_config;
3446 db_init; 3432 db_init;
3433
3434 cf::load_settings;
3435 cf::load_materials;
3436 cf::init_uuid;
3437 cf::init_signals;
3438 cf::init_commands;
3439 cf::init_skills;
3440
3441 cf::init_beforeplay;
3442
3443 atomic;
3444
3447 load_extensions; 3445 load_extensions;
3448 3446
3449 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3447 utime time, time, $RUNTIMEFILE;
3448
3449 # no (long-running) fork's whatsoever before this point(!)
3450 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3451
3452 (pop @POST_INIT)->(0) while @POST_INIT;
3450 } 3453 };
3451 3454
3452 EV::loop; 3455 EV::loop;
3453} 3456}
3454 3457
3455############################################################################# 3458#############################################################################
3464 }; 3467 };
3465 } 3468 }
3466} 3469}
3467 3470
3468sub write_runtime_sync { 3471sub write_runtime_sync {
3469 my $runtime = "$LOCALDIR/runtime";
3470
3471 # first touch the runtime file to show we are still running: 3472 # first touch the runtime file to show we are still running:
3472 # the fsync below can take a very very long time. 3473 # the fsync below can take a very very long time.
3473 3474
3474 IO::AIO::aio_utime $runtime, undef, undef; 3475 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef;
3475 3476
3476 my $guard = cf::lock_acquire "write_runtime"; 3477 my $guard = cf::lock_acquire "write_runtime";
3477 3478
3478 my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 3479 my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644
3479 or return; 3480 or return;
3480 3481
3481 my $value = $cf::RUNTIME + 90 + 10; 3482 my $value = $cf::RUNTIME + 90 + 10;
3482 # 10 is the runtime save interval, for a monotonic clock 3483 # 10 is the runtime save interval, for a monotonic clock
3483 # 60 allows for the watchdog to kill the server. 3484 # 60 allows for the watchdog to kill the server.
3493 aio_utime $fh, undef, undef; 3494 aio_utime $fh, undef, undef;
3494 3495
3495 close $fh 3496 close $fh
3496 or return; 3497 or return;
3497 3498
3498 aio_rename "$runtime~", $runtime 3499 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE
3499 and return; 3500 and return;
3500 3501
3501 warn "runtime file written.\n"; 3502 warn "runtime file written.\n";
3502 3503
3503 1 3504 1
3515 my $uuid = "$LOCALDIR/uuid"; 3516 my $uuid = "$LOCALDIR/uuid";
3516 3517
3517 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 3518 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644
3518 or return; 3519 or return;
3519 3520
3520 my $value = uuid_str $uuid_skip + uuid_seq uuid_cur; 3521 my $value = uuid_seq uuid_cur;
3522
3523 if ($value < 1024) {
3524 warn "cowardly refusing to write low uuid value ($value)!\n";
3525 return;
3526 }
3527
3528 my $value = uuid_str $value + $uuid_skip;
3521 $uuid_skip = 0; 3529 $uuid_skip = 0;
3522 3530
3523 (aio_write $fh, 0, (length $value), $value, 0) <= 0 3531 (aio_write $fh, 0, (length $value), $value, 0) <= 0
3524 and return; 3532 and return;
3525 3533
3587sub post_cleanup { 3595sub post_cleanup {
3588 my ($make_core) = @_; 3596 my ($make_core) = @_;
3589 3597
3590 warn Carp::longmess "post_cleanup backtrace" 3598 warn Carp::longmess "post_cleanup backtrace"
3591 if $make_core; 3599 if $make_core;
3600
3601 my $fh = pidfile;
3602 unlink $PIDFILE if <$fh> == $$;
3592} 3603}
3593 3604
3594# a safer delete_package, copied from Symbol 3605# a safer delete_package, copied from Symbol
3595sub clear_package($) { 3606sub clear_package($) {
3596 my $pkg = shift; 3607 my $pkg = shift;
3612 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; 3623 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
3613 for my $name (keys %$leaf_symtab) { 3624 for my $name (keys %$leaf_symtab) {
3614 _gv_clear *{"$pkg$name"}; 3625 _gv_clear *{"$pkg$name"};
3615# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; 3626# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"};
3616 } 3627 }
3617 warn "cleared package #$pkg\n";#d# 3628 warn "cleared package $pkg\n";#d#
3618} 3629}
3619
3620our $RELOAD; # how many times to reload
3621 3630
3622sub do_reload_perl() { 3631sub do_reload_perl() {
3623 # can/must only be called in main 3632 # can/must only be called in main
3624 if ($Coro::current != $Coro::main) { 3633 if ($Coro::current != $Coro::main) {
3625 warn "can only reload from main coroutine"; 3634 warn "can only reload from main coroutine";
3720 warn "reattaching attachments to maps"; 3729 warn "reattaching attachments to maps";
3721 reattach $_ for values %MAP; 3730 reattach $_ for values %MAP;
3722 warn "reattaching attachments to players"; 3731 warn "reattaching attachments to players";
3723 reattach $_ for values %PLAYER; 3732 reattach $_ for values %PLAYER;
3724 3733
3734 warn "running post_load";
3735 (pop @POST_INIT)->(1) while @POST_INIT;
3736
3725 warn "leaving sync_job"; 3737 warn "leaving sync_job";
3726 3738
3727 1 3739 1
3728 } or do { 3740 } or do {
3729 warn $@; 3741 warn $@;
3739 3751
3740sub reload_perl() { 3752sub reload_perl() {
3741 # doing reload synchronously and two reloads happen back-to-back, 3753 # doing reload synchronously and two reloads happen back-to-back,
3742 # coro crashes during coro_state_free->destroy here. 3754 # coro crashes during coro_state_free->destroy here.
3743 3755
3744 $RELOAD_WATCHER ||= EV::timer 0, 0, sub { 3756 $RELOAD_WATCHER ||= EV::timer $TICK * 1.5, 0, sub {
3745 do_reload_perl; 3757 do_reload_perl;
3746 undef $RELOAD_WATCHER; 3758 undef $RELOAD_WATCHER;
3747 }; 3759 };
3748} 3760}
3749 3761
3877 $msg =~ s/\n//; 3889 $msg =~ s/\n//;
3878 3890
3879 # limit the # of concurrent backtraces 3891 # limit the # of concurrent backtraces
3880 if ($_log_backtrace < 2) { 3892 if ($_log_backtrace < 2) {
3881 ++$_log_backtrace; 3893 ++$_log_backtrace;
3894 my $perl_bt = Carp::longmess $msg;
3882 async { 3895 async {
3883 $Coro::current->{desc} = "abt $msg"; 3896 $Coro::current->{desc} = "abt $msg";
3884 3897
3885 my @bt = fork_call { 3898 my @bt = fork_call {
3886 @addr = map { sprintf "%x", $_ } @addr; 3899 @addr = map { sprintf "%x", $_ } @addr;
3897 } 3910 }
3898 3911
3899 @funcs 3912 @funcs
3900 }; 3913 };
3901 3914
3902 LOG llevInfo, "[ABT] $msg\n"; 3915 LOG llevInfo, "[ABT] $perl_bt\n";
3916 LOG llevInfo, "[ABT] --- C backtrace follows ---\n";
3903 LOG llevInfo, "[ABT] $_\n" for @bt; 3917 LOG llevInfo, "[ABT] $_\n" for @bt;
3904 --$_log_backtrace; 3918 --$_log_backtrace;
3905 }; 3919 };
3906 } else { 3920 } else {
3907 LOG llevInfo, "[ABT] $msg\n"; 3921 LOG llevInfo, "[ABT] $msg\n";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines