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.445 by root, Wed Sep 10 18:18:10 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";
125our $LOAD; # a number between 0 (idle) and 1 (too many objects) 128our $LOAD; # a number between 0 (idle) and 1 (too many objects)
126our $LOADAVG; # same thing, but with alpha-smoothing 129our $LOADAVG; # same thing, but with alpha-smoothing
127our $JITTER; # average jitter 130our $JITTER; # average jitter
128our $TICK_START; # for load detecting purposes 131our $TICK_START; # for load detecting purposes
129 132
133our @POST_INIT;
134
130binmode STDOUT; 135binmode STDOUT;
131binmode STDERR; 136binmode STDERR;
132 137
133# read virtual server time, if available 138# read virtual server time, if available
134unless ($RUNTIME || !-e $RUNTIMEFILE) { 139unless ($RUNTIME || !-e $RUNTIMEFILE) {
302our $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
303 308
304sub encode_json($) { $json_coder->encode ($_[0]) } 309sub encode_json($) { $json_coder->encode ($_[0]) }
305sub decode_json($) { $json_coder->decode ($_[0]) } 310sub decode_json($) { $json_coder->decode ($_[0]) }
306 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
307=item cf::lock_wait $string 326=item cf::lock_wait $string
308 327
309Wait until the given lock is available. See cf::lock_acquire. 328Wait until the given lock is available. See cf::lock_acquire.
310 329
311=item my $lock = cf::lock_acquire $string 330=item my $lock = cf::lock_acquire $string
338 return;#d# 357 return;#d#
339 }#d# 358 }#d#
340 359
341 # wait for lock, if any 360 # wait for lock, if any
342 while ($LOCK{$key}) { 361 while ($LOCK{$key}) {
362 #local $Coro::current->{desc} = "$Coro::current->{desc} <waiting for lock $key>";
343 push @{ $LOCK{$key} }, $Coro::current; 363 push @{ $LOCK{$key} }, $Coro::current;
344 Coro::schedule; 364 Coro::schedule;
345 } 365 }
346} 366}
347 367
730 750
731############################################################################# 751#############################################################################
732 752
733=head2 ATTACHABLE OBJECTS 753=head2 ATTACHABLE OBJECTS
734 754
735Many 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
736attach callbacks/event handlers (a collection of which is called an "attachment") 756attach callbacks/event handlers (a collection of which is called an "attachment")
737to it. All such attachable objects support the following methods. 757to it. All such attachable objects support the following methods.
738 758
739In 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>
740C<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
790=item cf::CLASS::attachment $name, ... 810=item cf::CLASS::attachment $name, ...
791 811
792Register an attachment by C<$name> through which attachable objects of the 812Register an attachment by C<$name> through which attachable objects of the
793given CLASS can refer to this attachment. 813given CLASS can refer to this attachment.
794 814
795Some classes such as crossfire maps and objects can specify attachments 815Some classes such as deliantra maps and objects can specify attachments
796that 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.
797 817
798These calls expect any number of the following handler/hook descriptions: 818These calls expect any number of the following handler/hook descriptions:
799 819
800=over 4 820=over 4
1167 } else { 1187 } else {
1168 aio_unlink "$filename.pst"; 1188 aio_unlink "$filename.pst";
1169 } 1189 }
1170 1190
1171 aio_rename "$filename~", $filename; 1191 aio_rename "$filename~", $filename;
1192
1193 $filename =~ s%/[^/]+$%%;
1194 aio_pathsync $filename;
1172 } else { 1195 } else {
1173 warn "FATAL: $filename~: $!\n"; 1196 warn "FATAL: $filename~: $!\n";
1174 } 1197 }
1175 } else { 1198 } else {
1176 aio_unlink $filename; 1199 aio_unlink $filename;
1388 1411
1389=back 1412=back
1390 1413
1391=head2 CORE EXTENSIONS 1414=head2 CORE EXTENSIONS
1392 1415
1393Functions and methods that extend core crossfire objects. 1416Functions and methods that extend core deliantra objects.
1394 1417
1395=cut 1418=cut
1396 1419
1397package cf::player; 1420package cf::player;
1398 1421
1441 1464
1442sub exists($) { 1465sub exists($) {
1443 my ($login) = @_; 1466 my ($login) = @_;
1444 1467
1445 $cf::PLAYER{$login} 1468 $cf::PLAYER{$login}
1446 or cf::sync_job { !aio_stat path $login } 1469 or !aio_stat path $login
1447} 1470}
1448 1471
1449sub find($) { 1472sub find($) {
1450 return $cf::PLAYER{$_[0]} || do { 1473 return $cf::PLAYER{$_[0]} || do {
1451 my $login = $_[0]; 1474 my $login = $_[0];
1622 } 1645 }
1623 1646
1624 \@paths 1647 \@paths
1625} 1648}
1626 1649
1627=item $protocol_xml = $player->expand_cfpod ($crossfire_pod) 1650=item $protocol_xml = $player->expand_cfpod ($cfpod)
1628 1651
1629Expand crossfire pod fragments into protocol xml. 1652Expand deliantra pod fragments into protocol xml.
1630 1653
1631=item $player->ext_reply ($msgid, @msg) 1654=item $player->ext_reply ($msgid, @msg)
1632 1655
1633Sends an ext reply to the player. 1656Sends an ext reply to the player.
1634 1657
2448 2471
2449=item $player_object->enter_link 2472=item $player_object->enter_link
2450 2473
2451Freezes 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}>).
2452 2475
2453The 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.
2454I<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.
2455 2480
2456Will never block. 2481Will never block.
2457 2482
2458=item $player_object->leave_link ($map, $x, $y) 2483=item $player_object->leave_link ($map, $x, $y)
2459 2484
2517 2542
2518 $map->load; 2543 $map->load;
2519 $map->load_neighbours; 2544 $map->load_neighbours;
2520 2545
2521 return unless $self->contr->active; 2546 return unless $self->contr->active;
2547 $self->flag (cf::FLAG_DEBUG, 0);#d# temp
2522 $self->activate_recursive; 2548 $self->activate_recursive;
2523 2549
2524 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext 2550 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext
2525 $self->enter_map ($map, $x, $y); 2551 $self->enter_map ($map, $x, $y);
2526} 2552}
2689 $self->contr->savebed ($map, $x, $y) 2715 $self->contr->savebed ($map, $x, $y)
2690 if $exit->flag (cf::FLAG_DAMNED); 2716 if $exit->flag (cf::FLAG_DAMNED);
2691 2717
2692 1 2718 1
2693 }) { 2719 }) {
2694 $self->message ("Something went wrong deep within the crossfire server. " 2720 $self->message ("Something went wrong deep within the deliantra server. "
2695 . "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. "
2696 . "Please report this to the dungeon master!", 2722 . "Please report this to the dungeon master!",
2697 cf::NDI_UNIQUE | cf::NDI_RED); 2723 cf::NDI_UNIQUE | cf::NDI_RED);
2698 2724
2699 warn "ERROR in enter_exit: $@"; 2725 warn "ERROR in enter_exit: $@";
2765 id => "infobox", 2791 id => "infobox",
2766 title => "Body Parts", 2792 title => "Body Parts",
2767 reply => undef, 2793 reply => undef,
2768 tooltip => "Shows which body parts you posess and are available", 2794 tooltip => "Shows which body parts you posess and are available",
2769 }, 2795 },
2796 "c/skills" => {
2797 id => "infobox",
2798 title => "Skills",
2799 reply => undef,
2800 tooltip => "Shows your experience per skill and item power",
2801 },
2770 "c/uptime" => { 2802 "c/uptime" => {
2771 id => "infobox", 2803 id => "infobox",
2772 title => "Uptime", 2804 title => "Uptime",
2773 reply => undef, 2805 reply => undef,
2774 tooltip => "How long the server has been running since last restart", 2806 tooltip => "How long the server has been running since last restart",
2788); 2820);
2789 2821
2790sub cf::client::send_msg { 2822sub cf::client::send_msg {
2791 my ($self, $channel, $msg, $color, @extra) = @_; 2823 my ($self, $channel, $msg, $color, @extra) = @_;
2792 2824
2793 $msg = $self->pl->expand_cfpod ($msg); 2825 $msg = $self->pl->expand_cfpod ($msg)
2826 unless $color & cf::NDI_VERBATIM;
2794 2827
2795 $color &= cf::NDI_CLIENT_MASK; # just in case... 2828 $color &= cf::NDI_CLIENT_MASK; # just in case...
2796 2829
2797 # check predefined channels, for the benefit of C 2830 # check predefined channels, for the benefit of C
2798 if ($CHANNEL{$channel}) { 2831 if ($CHANNEL{$channel}) {
3327 reload_treasures; 3360 reload_treasures;
3328 3361
3329 warn "finished reloading resource files\n"; 3362 warn "finished reloading resource files\n";
3330} 3363}
3331 3364
3332sub init {
3333 my $guard = freeze_mainloop;
3334
3335 evthread_start IO::AIO::poll_fileno;
3336
3337 reload_resources;
3338}
3339
3340sub reload_config { 3365sub reload_config {
3341 open my $fh, "<:utf8", "$CONFDIR/config" 3366 open my $fh, "<:utf8", "$CONFDIR/config"
3342 or return; 3367 or return;
3343 3368
3344 local $/; 3369 local $/;
3376 seek $fh, 0, 0; 3401 seek $fh, 0, 0;
3377 print $fh $$; 3402 print $fh $$;
3378} 3403}
3379 3404
3380sub main { 3405sub main {
3381 atomic; 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
3382 3420
3383 # we must not ever block the main coroutine 3421 # we must not ever block the main coroutine
3384 local $Coro::idle = sub { 3422 local $Coro::idle = sub {
3385 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#
3386 (async { 3424 (async {
3387 $Coro::current->{desc} = "IDLE BUG HANDLER"; 3425 $Coro::current->{desc} = "IDLE BUG HANDLER";
3388 EV::loop EV::LOOP_ONESHOT; 3426 EV::loop EV::LOOP_ONESHOT;
3389 })->prio (Coro::PRIO_MAX); 3427 })->prio (Coro::PRIO_MAX);
3390 }; 3428 };
3391 3429
3392 { 3430 evthread_start IO::AIO::poll_fileno;
3393 my $guard = freeze_mainloop; 3431
3432 cf::sync_job {
3433 reload_resources;
3394 reload_config; 3434 reload_config;
3395 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
3396 load_extensions; 3448 load_extensions;
3397 3449
3398 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3399 }
3400
3401 utime time, time, $RUNTIMEFILE; 3450 utime time, time, $RUNTIMEFILE;
3402 3451
3403 # no (long-running) fork's whatsoever before this point(!) 3452 # no (long-running) fork's whatsoever before this point(!)
3404 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; 3453 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3454
3455 (pop @POST_INIT)->(0) while @POST_INIT;
3456 };
3405 3457
3406 EV::loop; 3458 EV::loop;
3407} 3459}
3408 3460
3409############################################################################# 3461#############################################################################
3467 my $uuid = "$LOCALDIR/uuid"; 3519 my $uuid = "$LOCALDIR/uuid";
3468 3520
3469 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 3521 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644
3470 or return; 3522 or return;
3471 3523
3472 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;
3473 $uuid_skip = 0; 3532 $uuid_skip = 0;
3474 3533
3475 (aio_write $fh, 0, (length $value), $value, 0) <= 0 3534 (aio_write $fh, 0, (length $value), $value, 0) <= 0
3476 and return; 3535 and return;
3477 3536
3567 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; 3626 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
3568 for my $name (keys %$leaf_symtab) { 3627 for my $name (keys %$leaf_symtab) {
3569 _gv_clear *{"$pkg$name"}; 3628 _gv_clear *{"$pkg$name"};
3570# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"}; 3629# use PApp::Util; PApp::Util::sv_dump *{"$pkg$name"};
3571 } 3630 }
3572 warn "cleared package #$pkg\n";#d# 3631 warn "cleared package $pkg\n";#d#
3573} 3632}
3574
3575our $RELOAD; # how many times to reload
3576 3633
3577sub do_reload_perl() { 3634sub do_reload_perl() {
3578 # can/must only be called in main 3635 # can/must only be called in main
3579 if ($Coro::current != $Coro::main) { 3636 if ($Coro::current != $Coro::main) {
3580 warn "can only reload from main coroutine"; 3637 warn "can only reload from main coroutine";
3675 warn "reattaching attachments to maps"; 3732 warn "reattaching attachments to maps";
3676 reattach $_ for values %MAP; 3733 reattach $_ for values %MAP;
3677 warn "reattaching attachments to players"; 3734 warn "reattaching attachments to players";
3678 reattach $_ for values %PLAYER; 3735 reattach $_ for values %PLAYER;
3679 3736
3737 warn "running post_load";
3738 (pop @POST_INIT)->(1) while @POST_INIT;
3739
3680 warn "leaving sync_job"; 3740 warn "leaving sync_job";
3681 3741
3682 1 3742 1
3683 } or do { 3743 } or do {
3684 warn $@; 3744 warn $@;
3694 3754
3695sub reload_perl() { 3755sub reload_perl() {
3696 # doing reload synchronously and two reloads happen back-to-back, 3756 # doing reload synchronously and two reloads happen back-to-back,
3697 # coro crashes during coro_state_free->destroy here. 3757 # coro crashes during coro_state_free->destroy here.
3698 3758
3699 $RELOAD_WATCHER ||= EV::timer 0, 0, sub { 3759 $RELOAD_WATCHER ||= EV::timer $TICK * 1.5, 0, sub {
3700 do_reload_perl; 3760 do_reload_perl;
3701 undef $RELOAD_WATCHER; 3761 undef $RELOAD_WATCHER;
3702 }; 3762 };
3703} 3763}
3704 3764
3832 $msg =~ s/\n//; 3892 $msg =~ s/\n//;
3833 3893
3834 # limit the # of concurrent backtraces 3894 # limit the # of concurrent backtraces
3835 if ($_log_backtrace < 2) { 3895 if ($_log_backtrace < 2) {
3836 ++$_log_backtrace; 3896 ++$_log_backtrace;
3897 my $perl_bt = Carp::longmess $msg;
3837 async { 3898 async {
3838 $Coro::current->{desc} = "abt $msg"; 3899 $Coro::current->{desc} = "abt $msg";
3839 3900
3840 my @bt = fork_call { 3901 my @bt = fork_call {
3841 @addr = map { sprintf "%x", $_ } @addr; 3902 @addr = map { sprintf "%x", $_ } @addr;
3852 } 3913 }
3853 3914
3854 @funcs 3915 @funcs
3855 }; 3916 };
3856 3917
3857 LOG llevInfo, "[ABT] $msg\n"; 3918 LOG llevInfo, "[ABT] $perl_bt\n";
3919 LOG llevInfo, "[ABT] --- C backtrace follows ---\n";
3858 LOG llevInfo, "[ABT] $_\n" for @bt; 3920 LOG llevInfo, "[ABT] $_\n" for @bt;
3859 --$_log_backtrace; 3921 --$_log_backtrace;
3860 }; 3922 };
3861 } else { 3923 } else {
3862 LOG llevInfo, "[ABT] $msg\n"; 3924 LOG llevInfo, "[ABT] $msg\n";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines