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.450 by root, Mon Sep 22 01:33:09 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 = ();
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
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;
126 132
127binmode STDOUT; 133binmode STDOUT;
128binmode STDERR; 134binmode STDERR;
129 135
130# read virtual server time, if available 136# read virtual server time, if available
131unless ($RUNTIME || !-e "$LOCALDIR/runtime") { 137unless ($RUNTIME || !-e $RUNTIMEFILE) {
132 open my $fh, "<", "$LOCALDIR/runtime" 138 open my $fh, "<", $RUNTIMEFILE
133 or die "unable to read runtime file: $!"; 139 or die "unable to read $RUNTIMEFILE file: $!";
134 $RUNTIME = <$fh> + 0.; 140 $RUNTIME = <$fh> + 0.;
135} 141}
136 142
137mkdir $_ 143mkdir $_
138 for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; 144 for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR;
727 733
728############################################################################# 734#############################################################################
729 735
730=head2 ATTACHABLE OBJECTS 736=head2 ATTACHABLE OBJECTS
731 737
732Many objects in crossfire are so-called attachable objects. That means you can 738Many objects in deliantra are so-called attachable objects. That means you can
733attach callbacks/event handlers (a collection of which is called an "attachment") 739attach callbacks/event handlers (a collection of which is called an "attachment")
734to it. All such attachable objects support the following methods. 740to it. All such attachable objects support the following methods.
735 741
736In the following description, CLASS can be any of C<global>, C<object> 742In 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 743C<player>, C<client> or C<map> (i.e. the attachable objects in
787=item cf::CLASS::attachment $name, ... 793=item cf::CLASS::attachment $name, ...
788 794
789Register an attachment by C<$name> through which attachable objects of the 795Register an attachment by C<$name> through which attachable objects of the
790given CLASS can refer to this attachment. 796given CLASS can refer to this attachment.
791 797
792Some classes such as crossfire maps and objects can specify attachments 798Some classes such as deliantra maps and objects can specify attachments
793that are attached at load/instantiate time, thus the need for a name. 799that are attached at load/instantiate time, thus the need for a name.
794 800
795These calls expect any number of the following handler/hook descriptions: 801These calls expect any number of the following handler/hook descriptions:
796 802
797=over 4 803=over 4
1385 1391
1386=back 1392=back
1387 1393
1388=head2 CORE EXTENSIONS 1394=head2 CORE EXTENSIONS
1389 1395
1390Functions and methods that extend core crossfire objects. 1396Functions and methods that extend core deliantra objects.
1391 1397
1392=cut 1398=cut
1393 1399
1394package cf::player; 1400package cf::player;
1395 1401
1619 } 1625 }
1620 1626
1621 \@paths 1627 \@paths
1622} 1628}
1623 1629
1624=item $protocol_xml = $player->expand_cfpod ($crossfire_pod) 1630=item $protocol_xml = $player->expand_cfpod ($cfpod)
1625 1631
1626Expand crossfire pod fragments into protocol xml. 1632Expand 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 1633
1719=item $player->ext_reply ($msgid, @msg) 1634=item $player->ext_reply ($msgid, @msg)
1720 1635
1721Sends an ext reply to the player. 1636Sends an ext reply to the player.
1722 1637
2536 2451
2537=item $player_object->enter_link 2452=item $player_object->enter_link
2538 2453
2539Freezes the player and moves him/her to a special map (C<{link}>). 2454Freezes the player and moves him/her to a special map (C<{link}>).
2540 2455
2541The player should be reasonably safe there for short amounts of time. You 2456The player should be reasonably safe there for short amounts of time (e.g.
2542I<MUST> call C<leave_link> as soon as possible, though. 2457for loading a map). You I<MUST> call C<leave_link> as soon as possible,
2458though, as the palyer cannot control the character while it is on the link
2459map.
2543 2460
2544Will never block. 2461Will never block.
2545 2462
2546=item $player_object->leave_link ($map, $x, $y) 2463=item $player_object->leave_link ($map, $x, $y)
2547 2464
2605 2522
2606 $map->load; 2523 $map->load;
2607 $map->load_neighbours; 2524 $map->load_neighbours;
2608 2525
2609 return unless $self->contr->active; 2526 return unless $self->contr->active;
2527 $self->flag (cf::FLAG_DEBUG, 0);#d# temp
2610 $self->activate_recursive; 2528 $self->activate_recursive;
2611 2529
2612 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext 2530 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext
2613 $self->enter_map ($map, $x, $y); 2531 $self->enter_map ($map, $x, $y);
2614} 2532}
2777 $self->contr->savebed ($map, $x, $y) 2695 $self->contr->savebed ($map, $x, $y)
2778 if $exit->flag (cf::FLAG_DAMNED); 2696 if $exit->flag (cf::FLAG_DAMNED);
2779 2697
2780 1 2698 1
2781 }) { 2699 }) {
2782 $self->message ("Something went wrong deep within the crossfire server. " 2700 $self->message ("Something went wrong deep within the deliantra server. "
2783 . "I'll try to bring you back to the map you were before. " 2701 . "I'll try to bring you back to the map you were before. "
2784 . "Please report this to the dungeon master!", 2702 . "Please report this to the dungeon master!",
2785 cf::NDI_UNIQUE | cf::NDI_RED); 2703 cf::NDI_UNIQUE | cf::NDI_RED);
2786 2704
2787 warn "ERROR in enter_exit: $@"; 2705 warn "ERROR in enter_exit: $@";
2853 id => "infobox", 2771 id => "infobox",
2854 title => "Body Parts", 2772 title => "Body Parts",
2855 reply => undef, 2773 reply => undef,
2856 tooltip => "Shows which body parts you posess and are available", 2774 tooltip => "Shows which body parts you posess and are available",
2857 }, 2775 },
2776 "c/skills" => {
2777 id => "infobox",
2778 title => "Skills",
2779 reply => undef,
2780 tooltip => "Shows your experience per skill and item power",
2781 },
2858 "c/uptime" => { 2782 "c/uptime" => {
2859 id => "infobox", 2783 id => "infobox",
2860 title => "Uptime", 2784 title => "Uptime",
2861 reply => undef, 2785 reply => undef,
2862 tooltip => "How long the server has been running since last restart", 2786 tooltip => "How long the server has been running since last restart",
2876); 2800);
2877 2801
2878sub cf::client::send_msg { 2802sub cf::client::send_msg {
2879 my ($self, $channel, $msg, $color, @extra) = @_; 2803 my ($self, $channel, $msg, $color, @extra) = @_;
2880 2804
2881 $msg = $self->pl->expand_cfpod ($msg); 2805 $msg = $self->pl->expand_cfpod ($msg)
2806 unless $color & cf::NDI_VERBATIM;
2882 2807
2883 $color &= cf::NDI_CLIENT_MASK; # just in case... 2808 $color &= cf::NDI_CLIENT_MASK; # just in case...
2884 2809
2885 # check predefined channels, for the benefit of C 2810 # check predefined channels, for the benefit of C
2886 if ($CHANNEL{$channel}) { 2811 if ($CHANNEL{$channel}) {
3444 }; 3369 };
3445 warn $@ if $@; 3370 warn $@ if $@;
3446 } 3371 }
3447} 3372}
3448 3373
3374sub pidfile() {
3375 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3376 or die "$PIDFILE: $!";
3377 flock $fh, &Fcntl::LOCK_EX
3378 or die "$PIDFILE: flock: $!";
3379 $fh
3380}
3381
3382# make sure only one server instance is running at any one time
3383sub atomic {
3384 my $fh = pidfile;
3385
3386 my $pid = <$fh>;
3387 kill 9, $pid if $pid > 0;
3388
3389 seek $fh, 0, 0;
3390 print $fh $$;
3391}
3392
3449sub main { 3393sub main {
3394 atomic;
3395
3450 # we must not ever block the main coroutine 3396 # we must not ever block the main coroutine
3451 local $Coro::idle = sub { 3397 local $Coro::idle = sub {
3452 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# 3398 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
3453 (async { 3399 (async {
3454 $Coro::current->{desc} = "IDLE BUG HANDLER"; 3400 $Coro::current->{desc} = "IDLE BUG HANDLER";
3463 load_extensions; 3409 load_extensions;
3464 3410
3465 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3411 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3466 } 3412 }
3467 3413
3414 utime time, time, $RUNTIMEFILE;
3415
3416 # no (long-running) fork's whatsoever before this point(!)
3417 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3418
3468 EV::loop; 3419 EV::loop;
3469} 3420}
3470 3421
3471############################################################################# 3422#############################################################################
3472# initialisation and cleanup 3423# initialisation and cleanup
3480 }; 3431 };
3481 } 3432 }
3482} 3433}
3483 3434
3484sub write_runtime_sync { 3435sub write_runtime_sync {
3485 my $runtime = "$LOCALDIR/runtime";
3486
3487 # first touch the runtime file to show we are still running: 3436 # first touch the runtime file to show we are still running:
3488 # the fsync below can take a very very long time. 3437 # the fsync below can take a very very long time.
3489 3438
3490 IO::AIO::aio_utime $runtime, undef, undef; 3439 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef;
3491 3440
3492 my $guard = cf::lock_acquire "write_runtime"; 3441 my $guard = cf::lock_acquire "write_runtime";
3493 3442
3494 my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 3443 my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644
3495 or return; 3444 or return;
3496 3445
3497 my $value = $cf::RUNTIME + 90 + 10; 3446 my $value = $cf::RUNTIME + 90 + 10;
3498 # 10 is the runtime save interval, for a monotonic clock 3447 # 10 is the runtime save interval, for a monotonic clock
3499 # 60 allows for the watchdog to kill the server. 3448 # 60 allows for the watchdog to kill the server.
3509 aio_utime $fh, undef, undef; 3458 aio_utime $fh, undef, undef;
3510 3459
3511 close $fh 3460 close $fh
3512 or return; 3461 or return;
3513 3462
3514 aio_rename "$runtime~", $runtime 3463 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE
3515 and return; 3464 and return;
3516 3465
3517 warn "runtime file written.\n"; 3466 warn "runtime file written.\n";
3518 3467
3519 1 3468 1
3603sub post_cleanup { 3552sub post_cleanup {
3604 my ($make_core) = @_; 3553 my ($make_core) = @_;
3605 3554
3606 warn Carp::longmess "post_cleanup backtrace" 3555 warn Carp::longmess "post_cleanup backtrace"
3607 if $make_core; 3556 if $make_core;
3557
3558 my $fh = pidfile;
3559 unlink $PIDFILE if <$fh> == $$;
3608} 3560}
3609 3561
3610# a safer delete_package, copied from Symbol 3562# a safer delete_package, copied from Symbol
3611sub clear_package($) { 3563sub clear_package($) {
3612 my $pkg = shift; 3564 my $pkg = shift;
3893 $msg =~ s/\n//; 3845 $msg =~ s/\n//;
3894 3846
3895 # limit the # of concurrent backtraces 3847 # limit the # of concurrent backtraces
3896 if ($_log_backtrace < 2) { 3848 if ($_log_backtrace < 2) {
3897 ++$_log_backtrace; 3849 ++$_log_backtrace;
3850 my $perl_bt = Carp::longmess $msg;
3898 async { 3851 async {
3899 $Coro::current->{desc} = "abt $msg"; 3852 $Coro::current->{desc} = "abt $msg";
3900 3853
3901 my @bt = fork_call { 3854 my @bt = fork_call {
3902 @addr = map { sprintf "%x", $_ } @addr; 3855 @addr = map { sprintf "%x", $_ } @addr;
3913 } 3866 }
3914 3867
3915 @funcs 3868 @funcs
3916 }; 3869 };
3917 3870
3918 LOG llevInfo, "[ABT] $msg\n"; 3871 LOG llevInfo, "[ABT] $perl_bt\n";
3872 LOG llevInfo, "[ABT] --- C backtrace follows ---\n";
3919 LOG llevInfo, "[ABT] $_\n" for @bt; 3873 LOG llevInfo, "[ABT] $_\n" for @bt;
3920 --$_log_backtrace; 3874 --$_log_backtrace;
3921 }; 3875 };
3922 } else { 3876 } else {
3923 LOG llevInfo, "[ABT] $msg\n"; 3877 LOG llevInfo, "[ABT] $msg\n";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines