… | |
… | |
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 |
67 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
67 | Coro::State::cctx_stacksize 256000; # 1-2MB stack, for deep recursions in maze generator |
68 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
|
|
69 | |
68 | |
70 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
69 | $Coro::main->prio (Coro::PRIO_MAX); # run main coroutine ("the server") with very high priority |
|
|
70 | |
|
|
71 | { |
|
|
72 | # very ugly, but ensure we acquire the storable lock |
|
|
73 | |
|
|
74 | sub net_mstore { |
|
|
75 | my $guard = Coro::Storable::guard; |
|
|
76 | &Storable::net_mstore |
|
|
77 | } |
|
|
78 | |
|
|
79 | sub mretrieve { |
|
|
80 | my $guard = Coro::Storable::guard; |
|
|
81 | &Storable::mretrieve |
|
|
82 | } |
|
|
83 | |
|
|
84 | Compress::LZF::set_serializer "Coro::Storable", "cf::net_mstore", "cf::mretrieve"; |
|
|
85 | Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later |
|
|
86 | } |
71 | |
87 | |
72 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
88 | sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload |
73 | |
89 | |
74 | our %COMMAND = (); |
90 | our %COMMAND = (); |
75 | our %COMMAND_TIME = (); |
91 | our %COMMAND_TIME = (); |
… | |
… | |
81 | our %EXT_MAP = (); # pluggable maps |
97 | our %EXT_MAP = (); # pluggable maps |
82 | |
98 | |
83 | our $RELOAD; # number of reloads so far |
99 | our $RELOAD; # number of reloads so far |
84 | our @EVENT; |
100 | our @EVENT; |
85 | |
101 | |
86 | our $CONFDIR = confdir; |
102 | our $CONFDIR = confdir; |
87 | our $DATADIR = datadir; |
103 | our $DATADIR = datadir; |
88 | our $LIBDIR = "$DATADIR/ext"; |
104 | our $LIBDIR = "$DATADIR/ext"; |
89 | our $PODDIR = "$DATADIR/pod"; |
105 | our $PODDIR = "$DATADIR/pod"; |
90 | our $MAPDIR = "$DATADIR/" . mapdir; |
106 | our $MAPDIR = "$DATADIR/" . mapdir; |
91 | our $LOCALDIR = localdir; |
107 | our $LOCALDIR = localdir; |
92 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
108 | our $TMPDIR = "$LOCALDIR/" . tmpdir; |
93 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
109 | our $UNIQUEDIR = "$LOCALDIR/" . uniquedir; |
94 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
110 | our $PLAYERDIR = "$LOCALDIR/" . playerdir; |
95 | our $RANDOMDIR = "$LOCALDIR/random"; |
111 | our $RANDOMDIR = "$LOCALDIR/random"; |
96 | our $BDBDIR = "$LOCALDIR/db"; |
112 | our $BDBDIR = "$LOCALDIR/db"; |
|
|
113 | our $PIDFILE = "$LOCALDIR/pid"; |
|
|
114 | our $RUNTIMEFILE = "$LOCALDIR/runtime"; |
|
|
115 | |
97 | our %RESOURCE; |
116 | our %RESOURCE; |
98 | |
117 | |
99 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
118 | our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) |
100 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
119 | our $NEXT_RUNTIME_WRITE; # when should the runtime file be written |
101 | our $NEXT_TICK; |
120 | our $NEXT_TICK; |
… | |
… | |
126 | |
145 | |
127 | binmode STDOUT; |
146 | binmode STDOUT; |
128 | binmode STDERR; |
147 | binmode STDERR; |
129 | |
148 | |
130 | # read virtual server time, if available |
149 | # read virtual server time, if available |
131 | unless ($RUNTIME || !-e "$LOCALDIR/runtime") { |
150 | unless ($RUNTIME || !-e $RUNTIMEFILE) { |
132 | open my $fh, "<", "$LOCALDIR/runtime" |
151 | open my $fh, "<", $RUNTIMEFILE |
133 | or die "unable to read runtime file: $!"; |
152 | or die "unable to read $RUNTIMEFILE file: $!"; |
134 | $RUNTIME = <$fh> + 0.; |
153 | $RUNTIME = <$fh> + 0.; |
135 | } |
154 | } |
136 | |
155 | |
137 | mkdir $_ |
156 | mkdir $_ |
138 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
157 | for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; |
… | |
… | |
727 | |
746 | |
728 | ############################################################################# |
747 | ############################################################################# |
729 | |
748 | |
730 | =head2 ATTACHABLE OBJECTS |
749 | =head2 ATTACHABLE OBJECTS |
731 | |
750 | |
732 | Many objects in crossfire are so-called attachable objects. That means you can |
751 | Many objects in deliantra are so-called attachable objects. That means you can |
733 | attach callbacks/event handlers (a collection of which is called an "attachment") |
752 | attach callbacks/event handlers (a collection of which is called an "attachment") |
734 | to it. All such attachable objects support the following methods. |
753 | to it. All such attachable objects support the following methods. |
735 | |
754 | |
736 | In the following description, CLASS can be any of C<global>, C<object> |
755 | In the following description, CLASS can be any of C<global>, C<object> |
737 | C<player>, C<client> or C<map> (i.e. the attachable objects in |
756 | C<player>, C<client> or C<map> (i.e. the attachable objects in |
… | |
… | |
787 | =item cf::CLASS::attachment $name, ... |
806 | =item cf::CLASS::attachment $name, ... |
788 | |
807 | |
789 | Register an attachment by C<$name> through which attachable objects of the |
808 | Register an attachment by C<$name> through which attachable objects of the |
790 | given CLASS can refer to this attachment. |
809 | given CLASS can refer to this attachment. |
791 | |
810 | |
792 | Some classes such as crossfire maps and objects can specify attachments |
811 | Some classes such as deliantra maps and objects can specify attachments |
793 | that are attached at load/instantiate time, thus the need for a name. |
812 | that are attached at load/instantiate time, thus the need for a name. |
794 | |
813 | |
795 | These calls expect any number of the following handler/hook descriptions: |
814 | These calls expect any number of the following handler/hook descriptions: |
796 | |
815 | |
797 | =over 4 |
816 | =over 4 |
… | |
… | |
1385 | |
1404 | |
1386 | =back |
1405 | =back |
1387 | |
1406 | |
1388 | =head2 CORE EXTENSIONS |
1407 | =head2 CORE EXTENSIONS |
1389 | |
1408 | |
1390 | Functions and methods that extend core crossfire objects. |
1409 | Functions and methods that extend core deliantra objects. |
1391 | |
1410 | |
1392 | =cut |
1411 | =cut |
1393 | |
1412 | |
1394 | package cf::player; |
1413 | package cf::player; |
1395 | |
1414 | |
… | |
… | |
1619 | } |
1638 | } |
1620 | |
1639 | |
1621 | \@paths |
1640 | \@paths |
1622 | } |
1641 | } |
1623 | |
1642 | |
1624 | =item $protocol_xml = $player->expand_cfpod ($crossfire_pod) |
1643 | =item $protocol_xml = $player->expand_cfpod ($cfpod) |
1625 | |
1644 | |
1626 | Expand crossfire pod fragments into protocol xml. |
1645 | Expand deliantra pod fragments into protocol xml. |
1627 | |
1646 | |
1628 | =item $player->ext_reply ($msgid, @msg) |
1647 | =item $player->ext_reply ($msgid, @msg) |
1629 | |
1648 | |
1630 | Sends an ext reply to the player. |
1649 | Sends an ext reply to the player. |
1631 | |
1650 | |
… | |
… | |
2445 | |
2464 | |
2446 | =item $player_object->enter_link |
2465 | =item $player_object->enter_link |
2447 | |
2466 | |
2448 | Freezes the player and moves him/her to a special map (C<{link}>). |
2467 | Freezes the player and moves him/her to a special map (C<{link}>). |
2449 | |
2468 | |
2450 | The player should be reasonably safe there for short amounts of time. You |
2469 | The player should be reasonably safe there for short amounts of time (e.g. |
2451 | I<MUST> call C<leave_link> as soon as possible, though. |
2470 | for loading a map). You I<MUST> call C<leave_link> as soon as possible, |
|
|
2471 | though, as the palyer cannot control the character while it is on the link |
|
|
2472 | map. |
2452 | |
2473 | |
2453 | Will never block. |
2474 | Will never block. |
2454 | |
2475 | |
2455 | =item $player_object->leave_link ($map, $x, $y) |
2476 | =item $player_object->leave_link ($map, $x, $y) |
2456 | |
2477 | |
… | |
… | |
2514 | |
2535 | |
2515 | $map->load; |
2536 | $map->load; |
2516 | $map->load_neighbours; |
2537 | $map->load_neighbours; |
2517 | |
2538 | |
2518 | return unless $self->contr->active; |
2539 | return unless $self->contr->active; |
|
|
2540 | $self->flag (cf::FLAG_DEBUG, 0);#d# temp |
2519 | $self->activate_recursive; |
2541 | $self->activate_recursive; |
2520 | |
2542 | |
2521 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2543 | local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext |
2522 | $self->enter_map ($map, $x, $y); |
2544 | $self->enter_map ($map, $x, $y); |
2523 | } |
2545 | } |
… | |
… | |
2686 | $self->contr->savebed ($map, $x, $y) |
2708 | $self->contr->savebed ($map, $x, $y) |
2687 | if $exit->flag (cf::FLAG_DAMNED); |
2709 | if $exit->flag (cf::FLAG_DAMNED); |
2688 | |
2710 | |
2689 | 1 |
2711 | 1 |
2690 | }) { |
2712 | }) { |
2691 | $self->message ("Something went wrong deep within the crossfire server. " |
2713 | $self->message ("Something went wrong deep within the deliantra server. " |
2692 | . "I'll try to bring you back to the map you were before. " |
2714 | . "I'll try to bring you back to the map you were before. " |
2693 | . "Please report this to the dungeon master!", |
2715 | . "Please report this to the dungeon master!", |
2694 | cf::NDI_UNIQUE | cf::NDI_RED); |
2716 | cf::NDI_UNIQUE | cf::NDI_RED); |
2695 | |
2717 | |
2696 | warn "ERROR in enter_exit: $@"; |
2718 | warn "ERROR in enter_exit: $@"; |
… | |
… | |
2785 | ); |
2807 | ); |
2786 | |
2808 | |
2787 | sub cf::client::send_msg { |
2809 | sub cf::client::send_msg { |
2788 | my ($self, $channel, $msg, $color, @extra) = @_; |
2810 | my ($self, $channel, $msg, $color, @extra) = @_; |
2789 | |
2811 | |
2790 | $msg = $self->pl->expand_cfpod ($msg); |
2812 | $msg = $self->pl->expand_cfpod ($msg) |
|
|
2813 | unless $color & cf::NDI_VERBATIM; |
2791 | |
2814 | |
2792 | $color &= cf::NDI_CLIENT_MASK; # just in case... |
2815 | $color &= cf::NDI_CLIENT_MASK; # just in case... |
2793 | |
2816 | |
2794 | # check predefined channels, for the benefit of C |
2817 | # check predefined channels, for the benefit of C |
2795 | if ($CHANNEL{$channel}) { |
2818 | if ($CHANNEL{$channel}) { |
… | |
… | |
3353 | }; |
3376 | }; |
3354 | warn $@ if $@; |
3377 | warn $@ if $@; |
3355 | } |
3378 | } |
3356 | } |
3379 | } |
3357 | |
3380 | |
|
|
3381 | sub pidfile() { |
|
|
3382 | sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT |
|
|
3383 | or die "$PIDFILE: $!"; |
|
|
3384 | flock $fh, &Fcntl::LOCK_EX |
|
|
3385 | or die "$PIDFILE: flock: $!"; |
|
|
3386 | $fh |
|
|
3387 | } |
|
|
3388 | |
|
|
3389 | # make sure only one server instance is running at any one time |
|
|
3390 | sub atomic { |
|
|
3391 | my $fh = pidfile; |
|
|
3392 | |
|
|
3393 | my $pid = <$fh>; |
|
|
3394 | kill 9, $pid if $pid > 0; |
|
|
3395 | |
|
|
3396 | seek $fh, 0, 0; |
|
|
3397 | print $fh $$; |
|
|
3398 | } |
|
|
3399 | |
3358 | sub main { |
3400 | sub main { |
|
|
3401 | atomic; |
|
|
3402 | |
3359 | # we must not ever block the main coroutine |
3403 | # we must not ever block the main coroutine |
3360 | local $Coro::idle = sub { |
3404 | local $Coro::idle = sub { |
3361 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
3405 | Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# |
3362 | (async { |
3406 | (async { |
3363 | $Coro::current->{desc} = "IDLE BUG HANDLER"; |
3407 | $Coro::current->{desc} = "IDLE BUG HANDLER"; |
… | |
… | |
3372 | load_extensions; |
3416 | load_extensions; |
3373 | |
3417 | |
3374 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3418 | $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority |
3375 | } |
3419 | } |
3376 | |
3420 | |
|
|
3421 | utime time, time, $RUNTIMEFILE; |
|
|
3422 | |
|
|
3423 | # no (long-running) fork's whatsoever before this point(!) |
|
|
3424 | POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; |
|
|
3425 | |
3377 | EV::loop; |
3426 | EV::loop; |
3378 | } |
3427 | } |
3379 | |
3428 | |
3380 | ############################################################################# |
3429 | ############################################################################# |
3381 | # initialisation and cleanup |
3430 | # initialisation and cleanup |
… | |
… | |
3389 | }; |
3438 | }; |
3390 | } |
3439 | } |
3391 | } |
3440 | } |
3392 | |
3441 | |
3393 | sub write_runtime_sync { |
3442 | sub write_runtime_sync { |
3394 | my $runtime = "$LOCALDIR/runtime"; |
|
|
3395 | |
|
|
3396 | # first touch the runtime file to show we are still running: |
3443 | # first touch the runtime file to show we are still running: |
3397 | # the fsync below can take a very very long time. |
3444 | # the fsync below can take a very very long time. |
3398 | |
3445 | |
3399 | IO::AIO::aio_utime $runtime, undef, undef; |
3446 | IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; |
3400 | |
3447 | |
3401 | my $guard = cf::lock_acquire "write_runtime"; |
3448 | my $guard = cf::lock_acquire "write_runtime"; |
3402 | |
3449 | |
3403 | my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 |
3450 | my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644 |
3404 | or return; |
3451 | or return; |
3405 | |
3452 | |
3406 | my $value = $cf::RUNTIME + 90 + 10; |
3453 | my $value = $cf::RUNTIME + 90 + 10; |
3407 | # 10 is the runtime save interval, for a monotonic clock |
3454 | # 10 is the runtime save interval, for a monotonic clock |
3408 | # 60 allows for the watchdog to kill the server. |
3455 | # 60 allows for the watchdog to kill the server. |
… | |
… | |
3418 | aio_utime $fh, undef, undef; |
3465 | aio_utime $fh, undef, undef; |
3419 | |
3466 | |
3420 | close $fh |
3467 | close $fh |
3421 | or return; |
3468 | or return; |
3422 | |
3469 | |
3423 | aio_rename "$runtime~", $runtime |
3470 | aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE |
3424 | and return; |
3471 | and return; |
3425 | |
3472 | |
3426 | warn "runtime file written.\n"; |
3473 | warn "runtime file written.\n"; |
3427 | |
3474 | |
3428 | 1 |
3475 | 1 |
… | |
… | |
3512 | sub post_cleanup { |
3559 | sub post_cleanup { |
3513 | my ($make_core) = @_; |
3560 | my ($make_core) = @_; |
3514 | |
3561 | |
3515 | warn Carp::longmess "post_cleanup backtrace" |
3562 | warn Carp::longmess "post_cleanup backtrace" |
3516 | if $make_core; |
3563 | if $make_core; |
|
|
3564 | |
|
|
3565 | my $fh = pidfile; |
|
|
3566 | unlink $PIDFILE if <$fh> == $$; |
3517 | } |
3567 | } |
3518 | |
3568 | |
3519 | # a safer delete_package, copied from Symbol |
3569 | # a safer delete_package, copied from Symbol |
3520 | sub clear_package($) { |
3570 | sub clear_package($) { |
3521 | my $pkg = shift; |
3571 | my $pkg = shift; |
… | |
… | |
3802 | $msg =~ s/\n//; |
3852 | $msg =~ s/\n//; |
3803 | |
3853 | |
3804 | # limit the # of concurrent backtraces |
3854 | # limit the # of concurrent backtraces |
3805 | if ($_log_backtrace < 2) { |
3855 | if ($_log_backtrace < 2) { |
3806 | ++$_log_backtrace; |
3856 | ++$_log_backtrace; |
|
|
3857 | my $perl_bt = Carp::longmess $msg; |
3807 | async { |
3858 | async { |
3808 | $Coro::current->{desc} = "abt $msg"; |
3859 | $Coro::current->{desc} = "abt $msg"; |
3809 | |
3860 | |
3810 | my @bt = fork_call { |
3861 | my @bt = fork_call { |
3811 | @addr = map { sprintf "%x", $_ } @addr; |
3862 | @addr = map { sprintf "%x", $_ } @addr; |
… | |
… | |
3822 | } |
3873 | } |
3823 | |
3874 | |
3824 | @funcs |
3875 | @funcs |
3825 | }; |
3876 | }; |
3826 | |
3877 | |
3827 | LOG llevInfo, "[ABT] $msg\n"; |
3878 | LOG llevInfo, "[ABT] $perl_bt\n"; |
|
|
3879 | LOG llevInfo, "[ABT] --- C backtrace follows ---\n"; |
3828 | LOG llevInfo, "[ABT] $_\n" for @bt; |
3880 | LOG llevInfo, "[ABT] $_\n" for @bt; |
3829 | --$_log_backtrace; |
3881 | --$_log_backtrace; |
3830 | }; |
3882 | }; |
3831 | } else { |
3883 | } else { |
3832 | LOG llevInfo, "[ABT] $msg\n"; |
3884 | LOG llevInfo, "[ABT] $msg\n"; |