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.337 by root, Tue Aug 21 00:54:01 2007 UTC vs.
Revision 1.345 by root, Tue Aug 28 19:30:11 2007 UTC

21use Coro::Semaphore; 21use Coro::Semaphore;
22use Coro::AIO; 22use Coro::AIO;
23use Coro::Storable; 23use Coro::Storable;
24use Coro::Util (); 24use Coro::Util ();
25 25
26use JSON::XS 1.4 (); 26use JSON::XS ();
27use BDB (); 27use BDB ();
28use Data::Dumper; 28use Data::Dumper;
29use Digest::MD5; 29use Digest::MD5;
30use Fcntl; 30use Fcntl;
31use YAML::Syck (); 31use YAML::Syck ();
1149 my ($type, $reply, @payload) = 1149 my ($type, $reply, @payload) =
1150 "ARRAY" eq ref $msg 1150 "ARRAY" eq ref $msg
1151 ? @$msg 1151 ? @$msg
1152 : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove 1152 : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove
1153 1153
1154 my @reply;
1155
1154 if (my $cb = $EXTCMD{$type}) { 1156 if (my $cb = $EXTCMD{$type}) {
1155 my @reply = $cb->($pl, @payload); 1157 @reply = $cb->($pl, @payload);
1156
1157 $pl->ext_reply ($reply, @reply)
1158 if $reply;
1159 } 1158 }
1159
1160 $pl->ext_reply ($reply, @reply)
1161 if $reply;
1162
1160 } else { 1163 } else {
1161 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 1164 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
1162 } 1165 }
1163 1166
1164 cf::override; 1167 cf::override;
1846 local $self->{deny_reset} = 1; # loading can take a long time 1849 local $self->{deny_reset} = 1; # loading can take a long time
1847 1850
1848 my $path = $self->{path}; 1851 my $path = $self->{path};
1849 1852
1850 { 1853 {
1854 my $guard1 = cf::lock_acquire "map_data:$path";
1851 my $guard = cf::lock_acquire "map_load:$path"; 1855 my $guard2 = cf::lock_acquire "map_load:$path";
1852 1856
1853 return if $self->in_memory != cf::MAP_SWAPPED; 1857 return if $self->in_memory != cf::MAP_SWAPPED;
1854 1858
1855 $self->in_memory (cf::MAP_LOADING); 1859 $self->in_memory (cf::MAP_LOADING);
1856 1860
1944} 1948}
1945 1949
1946sub do_load_sync { 1950sub do_load_sync {
1947 my ($map) = @_; 1951 my ($map) = @_;
1948 1952
1953 cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync"
1954 if $Coro::current == $Coro::main;
1955
1949 cf::sync_job { $map->load }; 1956 cf::sync_job { $map->load };
1950} 1957}
1951 1958
1952our %MAP_PREFETCH; 1959our %MAP_PREFETCH;
1953our $MAP_PREFETCHER = undef; 1960our $MAP_PREFETCHER = undef;
1954 1961
1955sub find_async { 1962sub find_async {
1956 my ($path, $origin) = @_; 1963 my ($path, $origin, $load) = @_;
1957 1964
1958 $path = normalise $path, $origin && $origin->{path}; 1965 $path = normalise $path, $origin && $origin->{path};
1959 1966
1960 if (my $map = $cf::MAP{$path}) { 1967 if (my $map = $cf::MAP{$path}) {
1961 return $map if $map->in_memory == cf::MAP_IN_MEMORY; 1968 return $map if !$load || $map->in_memory == cf::MAP_IN_MEMORY;
1962 } 1969 }
1963 1970
1964 undef $MAP_PREFETCH{$path}; 1971 $MAP_PREFETCH{$path} |= $load;
1972
1965 $MAP_PREFETCHER ||= cf::async { 1973 $MAP_PREFETCHER ||= cf::async {
1966 while (%MAP_PREFETCH) { 1974 while (%MAP_PREFETCH) {
1967 for my $path (keys %MAP_PREFETCH) { 1975 while (my ($k, $v) = each %MAP_PREFETCH) {
1968 if (my $map = find $path) { 1976 if (my $map = find $k) {
1969 $map->load; 1977 $map->load if $v;
1970 } 1978 }
1971 1979
1972 delete $MAP_PREFETCH{$path}; 1980 delete $MAP_PREFETCH{$k};
1973 } 1981 }
1974 } 1982 }
1975 undef $MAP_PREFETCHER; 1983 undef $MAP_PREFETCHER;
1976 }; 1984 };
1977 $MAP_PREFETCHER->prio (6); 1985 $MAP_PREFETCHER->prio (6);
1980} 1988}
1981 1989
1982sub save { 1990sub save {
1983 my ($self) = @_; 1991 my ($self) = @_;
1984 1992
1985 my $lock = cf::lock_acquire "map_data:" . $self->path; 1993 my $lock = cf::lock_acquire "map_data:$self->{path}";
1986 1994
1987 $self->{last_save} = $cf::RUNTIME; 1995 $self->{last_save} = $cf::RUNTIME;
1988 1996
1989 return unless $self->dirty; 1997 return unless $self->dirty;
1990 1998
2013 my ($self) = @_; 2021 my ($self) = @_;
2014 2022
2015 # save first because save cedes 2023 # save first because save cedes
2016 $self->save; 2024 $self->save;
2017 2025
2018 my $lock = cf::lock_acquire "map_data:" . $self->path; 2026 my $lock = cf::lock_acquire "map_data:$self->{path}";
2019 2027
2020 return if $self->players; 2028 return if $self->players;
2021 return if $self->in_memory != cf::MAP_IN_MEMORY; 2029 return if $self->in_memory != cf::MAP_IN_MEMORY;
2022 return if $self->{deny_save}; 2030 return if $self->{deny_save};
2023 2031
2551=cut 2559=cut
2552 2560
2553sub cf::client::ext_msg($$@) { 2561sub cf::client::ext_msg($$@) {
2554 my ($self, $type, @msg) = @_; 2562 my ($self, $type, @msg) = @_;
2555 2563
2556 my $extcmd = $self->extcmd;
2557
2558 if ($extcmd == 2) { 2564 if ($self->extcmd == 2) {
2559 $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg])); 2565 $self->send_packet ("ext " . $self->{json_coder}->encode ([$type, @msg]));
2560 } elsif ($extcmd == 1) { # TODO: remove 2566 } elsif ($self->extcmd == 1) { # TODO: remove
2561 push @msg, msgtype => "event_$type"; 2567 push @msg, msgtype => "event_$type";
2562 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); 2568 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2563 } 2569 }
2564} 2570}
2565 2571
2572sub cf::client::ext_reply($$@) { 2578sub cf::client::ext_reply($$@) {
2573 my ($self, $id, @msg) = @_; 2579 my ($self, $id, @msg) = @_;
2574 2580
2575 if ($self->extcmd == 2) { 2581 if ($self->extcmd == 2) {
2576 $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg])); 2582 $self->send_packet ("ext " . $self->{json_coder}->encode (["reply-$id", @msg]));
2577 } elsif ($self->ns->extcmd == 1) { 2583 } elsif ($self->extcmd == 1) {
2578 #TODO: version 1, remove 2584 #TODO: version 1, remove
2579 unshift @msg, msgtype => "reply", msgid => $id; 2585 unshift @msg, msgtype => "reply", msgid => $id;
2580 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg})); 2586 $self->send_packet ("ext " . $self->{json_coder}->encode ({@msg}));
2581 } 2587 }
2582} 2588}
2646 my ($type, $reply, @payload) = 2652 my ($type, $reply, @payload) =
2647 "ARRAY" eq ref $msg 2653 "ARRAY" eq ref $msg
2648 ? @$msg 2654 ? @$msg
2649 : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove 2655 : ($msg->{msgtype}, $msg->{msgid}, %$msg); # TODO: version 1, remove
2650 2656
2657 my @reply;
2658
2651 if (my $cb = $EXTICMD{$type}) { 2659 if (my $cb = $EXTICMD{$type}) {
2652 my @reply = $cb->($ns, @payload); 2660 @reply = $cb->($ns, @payload);
2653
2654 $ns->ext_reply ($reply, @reply)
2655 if $reply;
2656 } 2661 }
2662
2663 $ns->ext_reply ($reply, @reply)
2664 if $reply;
2665
2657 } else { 2666 } else {
2658 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 2667 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
2659 } 2668 }
2660 2669
2661 cf::override; 2670 cf::override;
2722 2731
2723The following functions and methods are available within a safe environment: 2732The following functions and methods are available within a safe environment:
2724 2733
2725 cf::object 2734 cf::object
2726 contr pay_amount pay_player map x y force_find force_add 2735 contr pay_amount pay_player map x y force_find force_add
2727 insert remove name archname title slaying race 2736 insert remove name archname title slaying race decrease_ob_nr
2728 2737
2729 cf::object::player 2738 cf::object::player
2730 player 2739 player
2731 2740
2732 cf::player 2741 cf::player
2737 2746
2738=cut 2747=cut
2739 2748
2740for ( 2749for (
2741 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y 2750 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y
2742 insert remove inv name archname title slaying race)], 2751 insert remove inv name archname title slaying race
2752 decrease_ob_nr)],
2743 ["cf::object::player" => qw(player)], 2753 ["cf::object::player" => qw(player)],
2744 ["cf::player" => qw(peaceful)], 2754 ["cf::player" => qw(peaceful)],
2745 ["cf::map" => qw(trigger)], 2755 ["cf::map" => qw(trigger)],
2746) { 2756) {
2747 no strict 'refs'; 2757 no strict 'refs';
2823# the server's init and main functions 2833# the server's init and main functions
2824 2834
2825sub load_facedata($) { 2835sub load_facedata($) {
2826 my ($path) = @_; 2836 my ($path) = @_;
2827 2837
2828 my $enc = JSON::XS->new->utf8->canonical; 2838 my $enc = JSON::XS->new->utf8->canonical->relaxed;
2829 2839
2830 warn "loading facedata from $path\n"; 2840 warn "loading facedata from $path\n";
2831 2841
2832 my $facedata; 2842 my $facedata;
2833 0 < aio_load $path, $facedata 2843 0 < aio_load $path, $facedata
2988 2998
2989sub init { 2999sub init {
2990 reload_resources; 3000 reload_resources;
2991} 3001}
2992 3002
2993sub cfg_load { 3003sub reload_config {
2994 open my $fh, "<:utf8", "$CONFDIR/config" 3004 open my $fh, "<:utf8", "$CONFDIR/config"
2995 or return; 3005 or return;
2996 3006
2997 local $/; 3007 local $/;
2998 *CFG = YAML::Syck::Load <$fh>; 3008 *CFG = YAML::Syck::Load <$fh>;
3018 (async { 3028 (async {
3019 Event::one_event; 3029 Event::one_event;
3020 })->prio (Coro::PRIO_MAX); 3030 })->prio (Coro::PRIO_MAX);
3021 }; 3031 };
3022 3032
3023 cfg_load; 3033 reload_config;
3024 db_init; 3034 db_init;
3025 load_extensions; 3035 load_extensions;
3026 3036
3027 $TICK_WATCHER->start; 3037 $TICK_WATCHER->start;
3028 Event::loop; 3038 Event::loop;
3221 warn "reloading cf.pm"; 3231 warn "reloading cf.pm";
3222 require cf; 3232 require cf;
3223 cf::_connect_to_perl; # nominally unnecessary, but cannot hurt 3233 cf::_connect_to_perl; # nominally unnecessary, but cannot hurt
3224 3234
3225 warn "loading config and database again"; 3235 warn "loading config and database again";
3226 cf::cfg_load; 3236 cf::reload_config;
3227 3237
3228 warn "loading extensions"; 3238 warn "loading extensions";
3229 cf::load_extensions; 3239 cf::load_extensions;
3230 3240
3231 warn "reattaching attachments to objects/players"; 3241 warn "reattaching attachments to objects/players";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines