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.547 by root, Sun May 9 22:51:13 2010 UTC vs.
Revision 1.557 by root, Tue Oct 5 21:40:05 2010 UTC

357our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max 357our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max
358 358
359sub encode_json($) { $json_coder->encode ($_[0]) } 359sub encode_json($) { $json_coder->encode ($_[0]) }
360sub decode_json($) { $json_coder->decode ($_[0]) } 360sub decode_json($) { $json_coder->decode ($_[0]) }
361 361
362=item $ref = cf::yaml_load $scalar
363
364Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks).
365
366=cut
367
368sub fork_call(&@);
369
370sub yaml_load($) {
371 fork_call { YAML::XS::Load $_[0] } @_
372}
373
362=item cf::post_init { BLOCK } 374=item cf::post_init { BLOCK }
363 375
364Execute the given codeblock, I<after> all extensions have been (re-)loaded, 376Execute the given codeblock, I<after> all extensions have been (re-)loaded,
365but I<before> the server starts ticking again. 377but I<before> the server starts ticking again.
366 378
367The cdoeblock will have a single boolean argument to indicate whether this 379The codeblock will have a single boolean argument to indicate whether this
368is a reload or not. 380is a reload or not.
369 381
370=cut 382=cut
371 383
372sub post_init(&) { 384sub post_init(&) {
373 push @POST_INIT, shift; 385 push @POST_INIT, shift;
386}
387
388sub _post_init {
389 trace "running post_init jobs";
390
391 # run them in parallel...
392
393 my @join;
394
395 while () {
396 push @join, map &Coro::async ($_, 0), @POST_INIT;
397 @POST_INIT = ();
398
399 @join or last;
400
401 (pop @join)->join;
402 }
374} 403}
375 404
376=item cf::lock_wait $string 405=item cf::lock_wait $string
377 406
378Wait until the given lock is available. See cf::lock_acquire. 407Wait until the given lock is available. See cf::lock_acquire.
583 $EXT_CORO{$coro+0} = $coro; 612 $EXT_CORO{$coro+0} = $coro;
584 613
585 $coro 614 $coro
586} 615}
587 616
588=item fork_call { }, $args 617=item fork_call { }, @args
589 618
590Executes the given code block with the given arguments in a seperate 619Executes the given code block with the given arguments in a seperate
591process, returning the results. Everything must be serialisable with 620process, returning the results. Everything must be serialisable with
592Coro::Storable. May, of course, block. Note that the executed sub may 621Coro::Storable. May, of course, block. Note that the executed sub may
593never block itself or use any form of event handling. 622never block itself or use any form of event handling.
606 # TODO: investigate and fix (likely this will be rather laborious) 635 # TODO: investigate and fix (likely this will be rather laborious)
607 636
608 my @res = Coro::Util::fork_eval { 637 my @res = Coro::Util::fork_eval {
609 cf::post_fork; 638 cf::post_fork;
610 &$cb 639 &$cb
611 }, @args; 640 } @args;
612 641
613 wantarray ? @res : $res[-1] 642 wantarray ? @res : $res[-1]
614} 643}
615 644
616sub objinfo { 645sub objinfo {
1650 my $name = $pl->ob->name; 1679 my $name = $pl->ob->name;
1651 1680
1652 $pl->{deny_save} = 1; 1681 $pl->{deny_save} = 1;
1653 $pl->password ("*"); # this should lock out the player until we have nuked the dir 1682 $pl->password ("*"); # this should lock out the player until we have nuked the dir
1654 1683
1655 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; 1684 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns;
1656 $pl->deactivate; 1685 $pl->deactivate;
1657 1686
1658 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; 1687 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy;
1688 $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns;
1659 ext::highscore::check ($pl->ob); 1689 ext::highscore::check ($pl->ob);
1660 1690
1661 $pl->invoke (cf::EVENT_PLAYER_QUIT);
1662 $pl->ns->destroy if $pl->ns; 1691 $pl->ns->destroy if $pl->ns;
1663 1692
1664 my $path = playerdir $pl; 1693 my $path = playerdir $pl;
1665 my $temp = "$path~$cf::RUNTIME~deleting~"; 1694 my $temp = "$path~$cf::RUNTIME~deleting~";
1666 aio_rename $path, $temp; 1695 aio_rename $path, $temp;
1832sub generate_random_map { 1861sub generate_random_map {
1833 my ($self, $rmp) = @_; 1862 my ($self, $rmp) = @_;
1834 1863
1835 my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM 1864 my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM
1836 1865
1837 # mit "rum" bekleckern, nicht
1838 $self->_create_random_map ( 1866 $self->_create_random_map ($rmp);
1839 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
1840 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle},
1841 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
1842 $rmp->{exit_on_final_map},
1843 $rmp->{xsize}, $rmp->{ysize},
1844 $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3},
1845 $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase},
1846 $rmp->{dungeon_level}, $rmp->{dungeon_depth}, $rmp->{decoroptions}, $rmp->{orientation},
1847 $rmp->{origin_y}, $rmp->{origin_x}, $rmp->{random_seed}, $rmp->{total_map_hp},
1848 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used},
1849 (cf::region::find $rmp->{region}), $rmp->{custom}
1850 )
1851} 1867}
1852 1868
1853=item cf::map->register ($regex, $prio) 1869=item cf::map->register ($regex, $prio)
1854 1870
1855Register a handler for the map path matching the given regex at the 1871Register a handler for the map path matching the given regex at the
1906 $base =~ s{[^/]+/?$}{}; 1922 $base =~ s{[^/]+/?$}{};
1907 $path = "$base/$path"; 1923 $path = "$base/$path";
1908 } 1924 }
1909 1925
1910 for ($path) { 1926 for ($path) {
1911 redo if s{//}{/};
1912 redo if s{/\.?/}{/}; 1927 redo if s{/\.?/}{/};
1913 redo if s{/[^/]+/\.\./}{/}; 1928 redo if s{/[^/]+/\.\./}{/};
1914 } 1929 }
1915 1930
1916 $path 1931 $path
2158 2173
2159 $self->in_memory (cf::MAP_ACTIVE); 2174 $self->in_memory (cf::MAP_ACTIVE);
2160 } 2175 }
2161 2176
2162 $self->post_load; 2177 $self->post_load;
2178
2179 1
2163} 2180}
2164 2181
2165# customize the map for a given player, i.e. 2182# customize the map for a given player, i.e.
2166# return the _real_ map. used by e.g. per-player 2183# return the _real_ map. used by e.g. per-player
2167# maps to change the path to ~playername/mappath 2184# maps to change the path to ~playername/mappath
3409 3426
3410 cf::face::set_visibility $idx, $info->{visibility}; 3427 cf::face::set_visibility $idx, $info->{visibility};
3411 cf::face::set_magicmap $idx, $info->{magicmap}; 3428 cf::face::set_magicmap $idx, $info->{magicmap};
3412 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32}; 3429 cf::face::set_data $idx, 0, $info->{data32}, $info->{hash32};
3413 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64}; 3430 cf::face::set_data $idx, 1, $info->{data64}, $info->{hash64};
3431 cf::face::set_data $idx, 2, "-", chr $idx & 255;
3414 3432
3415 cf::cede_to_tick; 3433 cf::cede_to_tick;
3416 } 3434 }
3417 3435
3418 while (my ($face, $info) = each %$faces) { 3436 while (my ($face, $info) = each %$faces) {
3554 3572
3555 0 < aio_load "$CONFDIR/config", my $config 3573 0 < aio_load "$CONFDIR/config", my $config
3556 or die "$CONFDIR/config: $!"; 3574 or die "$CONFDIR/config: $!";
3557 3575
3558 utf8::decode $config; 3576 utf8::decode $config;
3559 3577 *CFG = yaml_load $config;
3560 cf::get_slot 0.1, 10, "reload_config"; # yaml might be slow...
3561 *CFG = YAML::XS::Load $config;
3562 3578
3563 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; 3579 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3564 3580
3565 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3581 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset};
3566 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; 3582 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3650 3666
3651 # no (long-running) fork's whatsoever before this point(!) 3667 # no (long-running) fork's whatsoever before this point(!)
3652 use POSIX (); 3668 use POSIX ();
3653 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; 3669 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3654 3670
3655 (pop @POST_INIT)->(0) while @POST_INIT; 3671 cf::_post_init 0;
3656 }; 3672 };
3657 3673
3658 cf::object::thawer::errors_are_fatal 0; 3674 cf::object::thawer::errors_are_fatal 0;
3659 info "parse errors in files are no longer fatal from this point on.\n"; 3675 info "parse errors in files are no longer fatal from this point on.\n";
3660 3676
3883 3899
3884 my $t1 = AE::time; 3900 my $t1 = AE::time;
3885 3901
3886 while ($RELOAD) { 3902 while ($RELOAD) {
3887 cf::get_slot 0.1, -1, "reload_perl"; 3903 cf::get_slot 0.1, -1, "reload_perl";
3888 info "reloading..."; 3904 info "perl_reload: reloading...";
3889 3905
3890 trace "entering sync_job"; 3906 trace "perl_reload: entering sync_job";
3891 3907
3892 cf::sync_job { 3908 cf::sync_job {
3893 #cf::emergency_save; 3909 #cf::emergency_save;
3894 3910
3895 trace "cancelling all extension coros"; 3911 trace "perl_reload: cancelling all extension coros";
3896 $_->cancel for values %EXT_CORO; 3912 $_->cancel for values %EXT_CORO;
3897 %EXT_CORO = (); 3913 %EXT_CORO = ();
3898 3914
3899 trace "removing commands"; 3915 trace "perl_reload: removing commands";
3900 %COMMAND = (); 3916 %COMMAND = ();
3901 3917
3902 trace "removing ext/exti commands"; 3918 trace "perl_reload: removing ext/exti commands";
3903 %EXTCMD = (); 3919 %EXTCMD = ();
3904 %EXTICMD = (); 3920 %EXTICMD = ();
3905 3921
3906 trace "unloading/nuking all extensions"; 3922 trace "perl_reload: unloading/nuking all extensions";
3907 for my $pkg (@EXTS) { 3923 for my $pkg (@EXTS) {
3908 trace "... unloading $pkg"; 3924 trace "... unloading $pkg";
3909 3925
3910 if (my $cb = $pkg->can ("unload")) { 3926 if (my $cb = $pkg->can ("unload")) {
3911 eval { 3927 eval {
3916 3932
3917 trace "... clearing $pkg"; 3933 trace "... clearing $pkg";
3918 clear_package $pkg; 3934 clear_package $pkg;
3919 } 3935 }
3920 3936
3921 trace "unloading all perl modules loaded from $LIBDIR"; 3937 trace "perl_reload: unloading all perl modules loaded from $LIBDIR";
3922 while (my ($k, $v) = each %INC) { 3938 while (my ($k, $v) = each %INC) {
3923 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; 3939 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
3924 3940
3925 trace "... unloading $k"; 3941 trace "... unloading $k";
3926 delete $INC{$k}; 3942 delete $INC{$k};
3933 } 3949 }
3934 3950
3935 clear_package $k; 3951 clear_package $k;
3936 } 3952 }
3937 3953
3938 trace "getting rid of safe::, as good as possible"; 3954 trace "perl_reload: getting rid of safe::, as good as possible";
3939 clear_package "safe::$_" 3955 clear_package "safe::$_"
3940 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 3956 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
3941 3957
3942 trace "unloading cf.pm \"a bit\""; 3958 trace "perl_reload: unloading cf.pm \"a bit\"";
3943 delete $INC{"cf.pm"}; 3959 delete $INC{"cf.pm"};
3944 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; 3960 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES;
3945 3961
3946 # don't, removes xs symbols, too, 3962 # don't, removes xs symbols, too,
3947 # and global variables created in xs 3963 # and global variables created in xs
3948 #clear_package __PACKAGE__; 3964 #clear_package __PACKAGE__;
3949 3965
3950 info "unload completed, starting to reload now"; 3966 info "perl_reload: unload completed, starting to reload now";
3951 3967
3952 trace "reloading cf.pm"; 3968 trace "perl_reload: reloading cf.pm";
3953 require cf; 3969 require cf;
3954 cf::_connect_to_perl_1; 3970 cf::_connect_to_perl_1;
3955 3971
3956 trace "loading config and database again"; 3972 trace "perl_reload: loading config and database again";
3957 cf::reload_config; 3973 cf::reload_config;
3958 3974
3959 trace "loading extensions"; 3975 trace "perl_reload: loading extensions";
3960 cf::load_extensions; 3976 cf::load_extensions;
3961 3977
3962 if ($REATTACH_ON_RELOAD) { 3978 if ($REATTACH_ON_RELOAD) {
3963 trace "reattaching attachments to objects/players"; 3979 trace "perl_reload: reattaching attachments to objects/players";
3964 _global_reattach; # objects, sockets 3980 _global_reattach; # objects, sockets
3965 trace "reattaching attachments to maps"; 3981 trace "perl_reload: reattaching attachments to maps";
3966 reattach $_ for values %MAP; 3982 reattach $_ for values %MAP;
3967 trace "reattaching attachments to players"; 3983 trace "perl_reload: reattaching attachments to players";
3968 reattach $_ for values %PLAYER; 3984 reattach $_ for values %PLAYER;
3969 } 3985 }
3970 3986
3971 trace "running post_init jobs"; 3987 cf::_post_init 1;
3972 (pop @POST_INIT)->(1) while @POST_INIT;
3973 3988
3974 trace "leaving sync_job"; 3989 trace "perl_reload: leaving sync_job";
3975 3990
3976 1 3991 1
3977 } or do { 3992 } or do {
3978 error $@; 3993 error $@;
3979 cf::cleanup "error while reloading, exiting."; 3994 cf::cleanup "perl_reload: error, exiting.";
3980 }; 3995 };
3981 3996
3982 info "reloaded";
3983 --$RELOAD; 3997 --$RELOAD;
3984 } 3998 }
3985 3999
3986 $t1 = AE::time - $t1; 4000 $t1 = AE::time - $t1;
3987 info "reload completed in ${t1}s\n"; 4001 info "perl_reload: completed in ${t1}s\n";
3988}; 4002};
3989 4003
3990our $RELOAD_WATCHER; # used only during reload 4004our $RELOAD_WATCHER; # used only during reload
3991 4005
3992sub reload_perl() { 4006sub reload_perl() {
4125 IO::AIO::min_parallel 8; 4139 IO::AIO::min_parallel 8;
4126 IO::AIO::max_poll_time $TICK * 0.1; 4140 IO::AIO::max_poll_time $TICK * 0.1;
4127 undef $AnyEvent::AIO::WATCHER; 4141 undef $AnyEvent::AIO::WATCHER;
4128} 4142}
4129 4143
4130my $_log_backtrace; 4144our $_log_backtrace;
4145our $_log_backtrace_last;
4131 4146
4132sub _log_backtrace { 4147sub _log_backtrace {
4133 my ($msg, @addr) = @_; 4148 my ($msg, @addr) = @_;
4134 4149
4135 $msg =~ s/\n//; 4150 $msg =~ s/\n$//;
4136 4151
4152 if ($_log_backtrace_last eq $msg) {
4153 LOG llevInfo, "[ABT] $msg\n";
4154 LOG llevInfo, "[ABT] [duplicate, suppressed]\n";
4137 # limit the # of concurrent backtraces 4155 # limit the # of concurrent backtraces
4138 if ($_log_backtrace < 2) { 4156 } elsif ($_log_backtrace < 2) {
4157 $_log_backtrace_last = $msg;
4139 ++$_log_backtrace; 4158 ++$_log_backtrace;
4140 my $perl_bt = Carp::longmess $msg; 4159 my $perl_bt = Carp::longmess $msg;
4141 async { 4160 async {
4142 $Coro::current->{desc} = "abt $msg"; 4161 $Coro::current->{desc} = "abt $msg";
4143 4162
4163 LOG llevInfo, "[ABT] $_\n" for @bt; 4182 LOG llevInfo, "[ABT] $_\n" for @bt;
4164 --$_log_backtrace; 4183 --$_log_backtrace;
4165 }; 4184 };
4166 } else { 4185 } else {
4167 LOG llevInfo, "[ABT] $msg\n"; 4186 LOG llevInfo, "[ABT] $msg\n";
4168 LOG llevInfo, "[ABT] [suppressed]\n"; 4187 LOG llevInfo, "[ABT] [overload, suppressed]\n";
4169 } 4188 }
4170} 4189}
4171 4190
4172# load additional modules 4191# load additional modules
4173require "cf/$_.pm" for @EXTRA_MODULES; 4192require "cf/$_.pm" for @EXTRA_MODULES;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines