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.466 by root, Thu Jan 8 03:03:24 2009 UTC vs.
Revision 1.483 by root, Mon Oct 12 04:02:17 2009 UTC

72 72
73# make sure c-lzf reinitialises itself 73# make sure c-lzf reinitialises itself
74Compress::LZF::set_serializer "Storable", "Storable::net_mstore", "Storable::mretrieve"; 74Compress::LZF::set_serializer "Storable", "Storable::net_mstore", "Storable::mretrieve";
75Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later 75Compress::LZF::sfreeze_cr { }; # prime Compress::LZF so it does not use require later
76 76
77# strictly for debugging
78$SIG{QUIT} = sub { Carp::cluck "SIGQUIT" };
79
77sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload 80sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
78 81
79our %COMMAND = (); 82our %COMMAND = ();
80our %COMMAND_TIME = (); 83our %COMMAND_TIME = ();
81 84
85our %EXT_CORO = (); # coroutines bound to extensions 88our %EXT_CORO = (); # coroutines bound to extensions
86our %EXT_MAP = (); # pluggable maps 89our %EXT_MAP = (); # pluggable maps
87 90
88our $RELOAD; # number of reloads so far, non-zero while in reload 91our $RELOAD; # number of reloads so far, non-zero while in reload
89our @EVENT; 92our @EVENT;
93our @REFLECT; # set by XS
94our %REFLECT; # set by us
90 95
91our $CONFDIR = confdir; 96our $CONFDIR = confdir;
92our $DATADIR = datadir; 97our $DATADIR = datadir;
93our $LIBDIR = "$DATADIR/ext"; 98our $LIBDIR = "$DATADIR/ext";
94our $PODDIR = "$DATADIR/pod"; 99our $PODDIR = "$DATADIR/pod";
105our %RESOURCE; 110our %RESOURCE;
106 111
107our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) 112our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!)
108our $NEXT_RUNTIME_WRITE; # when should the runtime file be written 113our $NEXT_RUNTIME_WRITE; # when should the runtime file be written
109our $NEXT_TICK; 114our $NEXT_TICK;
110our $USE_FSYNC = 1; # use fsync to write maps - default off 115our $USE_FSYNC = 1; # use fsync to write maps - default on
111 116
112our $BDB_DEADLOCK_WATCHER; 117our $BDB_DEADLOCK_WATCHER;
113our $BDB_CHECKPOINT_WATCHER; 118our $BDB_CHECKPOINT_WATCHER;
114our $BDB_TRICKLE_WATCHER; 119our $BDB_TRICKLE_WATCHER;
115our $DB_ENV; 120our $DB_ENV;
116 121
117our @EXTRA_MODULES = qw(pod mapscript); 122our @EXTRA_MODULES = qw(pod match mapscript);
118 123
119our %CFG; 124our %CFG;
120 125
121our $UPTIME; $UPTIME ||= time; 126our $UPTIME; $UPTIME ||= time;
122our $RUNTIME; 127our $RUNTIME;
134our $JITTER; # average jitter 139our $JITTER; # average jitter
135our $TICK_START; # for load detecting purposes 140our $TICK_START; # for load detecting purposes
136 141
137our @POST_INIT; 142our @POST_INIT;
138 143
139our $REATTACH_ON_RELOAD; # ste to true to force object reattach on reload (slow) 144our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow)
145our $REALLY_UNLOOP; # never set to true, please :)
140 146
141binmode STDOUT; 147binmode STDOUT;
142binmode STDERR; 148binmode STDERR;
143 149
144# read virtual server time, if available 150# read virtual server time, if available
146 open my $fh, "<", $RUNTIMEFILE 152 open my $fh, "<", $RUNTIMEFILE
147 or die "unable to read $RUNTIMEFILE file: $!"; 153 or die "unable to read $RUNTIMEFILE file: $!";
148 $RUNTIME = <$fh> + 0.; 154 $RUNTIME = <$fh> + 0.;
149} 155}
150 156
157eval "sub TICK() { $TICK } 1" or die;
158
151mkdir $_ 159mkdir $_
152 for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; 160 for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR;
153 161
154our $EMERGENCY_POSITION; 162our $EMERGENCY_POSITION;
155 163
156sub cf::map::normalise; 164sub cf::map::normalise;
165
166#############################################################################
167
168%REFLECT = ();
169for (@REFLECT) {
170 my $reflect = JSON::XS::decode_json $_;
171 $REFLECT{$reflect->{class}} = $reflect;
172}
173
174# this is decidedly evil
175$REFLECT{object}{flags} = { map +($_ => undef), grep $_, map /^FLAG_([A-Z0-9_]+)$/ && lc $1, keys %{"cf::"} };
157 176
158############################################################################# 177#############################################################################
159 178
160=head2 GLOBAL VARIABLES 179=head2 GLOBAL VARIABLES
161 180
212 231
213This array contains the results of the last C<invoke ()> call. When 232This array contains the results of the last C<invoke ()> call. When
214C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of 233C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of
215that call. 234that call.
216 235
236=item %cf::REFLECT
237
238Contains, for each (C++) class name, a hash reference with information
239about object members (methods, scalars, arrays and flags) and other
240metadata, which is useful for introspection.
241
217=back 242=back
218 243
219=cut 244=cut
220 245
221BEGIN { 246$Coro::State::WARNHOOK = sub {
222 *CORE::GLOBAL::warn = sub {
223 my $msg = join "", @_; 247 my $msg = join "", @_;
224 248
225 $msg .= "\n" 249 $msg .= "\n"
226 unless $msg =~ /\n$/; 250 unless $msg =~ /\n$/;
227 251
228 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; 252 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge;
229 253
230 LOG llevError, $msg; 254 LOG llevError, $msg;
231 }; 255};
232}
233 256
234$Coro::State::DIEHOOK = sub { 257$Coro::State::DIEHOOK = sub {
235 return unless $^S eq 0; # "eq", not "==" 258 return unless $^S eq 0; # "eq", not "=="
259
260 warn Carp::longmess $_[0];
236 261
237 if ($Coro::current == $Coro::main) {#d# 262 if ($Coro::current == $Coro::main) {#d#
238 warn "DIEHOOK called in main context, Coro bug?\n";#d# 263 warn "DIEHOOK called in main context, Coro bug?\n";#d#
239 return;#d# 264 return;#d#
240 }#d# 265 }#d#
241 266
242 # kill coroutine otherwise 267 # kill coroutine otherwise
243 warn Carp::longmess $_[0];
244 Coro::terminate 268 Coro::terminate
245}; 269};
246
247$SIG{__DIE__} = sub { }; #d#?
248 270
249@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; 271@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable';
250@safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; 272@safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable';
251@safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; 273@safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable';
252@safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; 274@safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable';
1149 $decname, length $$rdata, scalar @$objs; 1171 $decname, length $$rdata, scalar @$objs;
1150 1172
1151 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { 1173 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) {
1152 aio_chmod $fh, SAVE_MODE; 1174 aio_chmod $fh, SAVE_MODE;
1153 aio_write $fh, 0, (length $$rdata), $$rdata, 0; 1175 aio_write $fh, 0, (length $$rdata), $$rdata, 0;
1154 aio_fsync $fh if $cf::USE_FSYNC; 1176 if ($cf::USE_FSYNC) {
1177 aio_sync_file_range $fh, 0, 0, IO::AIO::SYNC_FILE_RANGE_WAIT_BEFORE | IO::AIO::SYNC_FILE_RANGE_WRITE | IO::AIO::SYNC_FILE_RANGE_WAIT_AFTER;
1178 aio_fsync $fh;
1179 }
1155 aio_close $fh; 1180 aio_close $fh;
1156 1181
1157 if (@$objs) { 1182 if (@$objs) {
1158 if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { 1183 if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) {
1159 aio_chmod $fh, SAVE_MODE; 1184 aio_chmod $fh, SAVE_MODE;
1160 my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; 1185 my $data = Coro::Storable::nfreeze { version => 1, objs => $objs };
1161 aio_write $fh, 0, (length $data), $data, 0; 1186 aio_write $fh, 0, (length $data), $data, 0;
1162 aio_fsync $fh if $cf::USE_FSYNC; 1187 if ($cf::USE_FSYNC) {
1188 aio_sync_file_range $fh, 0, 0, IO::AIO::SYNC_FILE_RANGE_WAIT_BEFORE | IO::AIO::SYNC_FILE_RANGE_WRITE | IO::AIO::SYNC_FILE_RANGE_WAIT_AFTER;
1189 aio_fsync $fh;
1190 }
1163 aio_close $fh; 1191 aio_close $fh;
1164 aio_rename "$filename.pst~", "$filename.pst"; 1192 aio_rename "$filename.pst~", "$filename.pst";
1165 } 1193 }
1166 } else { 1194 } else {
1167 aio_unlink "$filename.pst"; 1195 aio_unlink "$filename.pst";
1318 1346
1319# "readahead" all extensions 1347# "readahead" all extensions
1320sub cache_extensions { 1348sub cache_extensions {
1321 my $grp = IO::AIO::aio_group; 1349 my $grp = IO::AIO::aio_group;
1322 1350
1323 add $grp IO::AIO::aio_readdir $LIBDIR, sub { 1351 add $grp IO::AIO::aio_readdirx $LIBDIR, IO::AIO::READDIR_STAT_ORDER, sub {
1324 for (grep /\.ext$/, @{$_[0]}) { 1352 for (grep /\.ext$/, @{$_[0]}) {
1325 add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; 1353 add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data;
1326 } 1354 }
1327 }; 1355 };
1328 1356
2211 2239
2212 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2240 my $lock = cf::lock_acquire "map_data:$self->{path}";
2213 2241
2214 return if $self->players; 2242 return if $self->players;
2215 2243
2216 warn "resetting map ", $self->path; 2244 warn "resetting map ", $self->path, "\n";
2217 2245
2218 $self->in_memory (cf::MAP_SWAPPED); 2246 $self->in_memory (cf::MAP_SWAPPED);
2219 2247
2220 # need to save uniques path 2248 # need to save uniques path
2221 unless ($self->{deny_save}) { 2249 unless ($self->{deny_save}) {
2386 2414
2387our $SAY_CHANNEL = { 2415our $SAY_CHANNEL = {
2388 id => "say", 2416 id => "say",
2389 title => "Map", 2417 title => "Map",
2390 reply => "say ", 2418 reply => "say ",
2391 tooltip => "Things said to and replied from npcs near you and other players on the same map only.", 2419 tooltip => "Things said to and replied from NPCs near you and other players on the same map only.",
2392}; 2420};
2393 2421
2394our $CHAT_CHANNEL = { 2422our $CHAT_CHANNEL = {
2395 id => "chat", 2423 id => "chat",
2396 title => "Chat", 2424 title => "Chat",
2522 2550
2523 $map->load; 2551 $map->load;
2524 $map->load_neighbours; 2552 $map->load_neighbours;
2525 2553
2526 return unless $self->contr->active; 2554 return unless $self->contr->active;
2527 $self->flag (cf::FLAG_DEBUG, 0);#d# temp
2528 $self->activate_recursive;
2529 2555
2530 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext 2556 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext
2531 $self->enter_map ($map, $x, $y); 2557 $self->enter_map ($map, $x, $y);
2558
2559 # only activate afterwards, to support waiting in hooks
2560 $self->activate_recursive;
2532} 2561}
2533 2562
2534=item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) 2563=item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]])
2535 2564
2536Moves the player to the given map-path and coordinates by first freezing 2565Moves the player to the given map-path and coordinates by first freezing
2783 id => "infobox", 2812 id => "infobox",
2784 title => "Skills", 2813 title => "Skills",
2785 reply => undef, 2814 reply => undef,
2786 tooltip => "Shows your experience per skill and item power", 2815 tooltip => "Shows your experience per skill and item power",
2787 }, 2816 },
2817 "c/shopitems" => {
2818 id => "infobox",
2819 title => "Shop Items",
2820 reply => undef,
2821 tooltip => "Shows the items currently for sale in this shop",
2822 },
2788 "c/resistances" => { 2823 "c/resistances" => {
2789 id => "infobox", 2824 id => "infobox",
2790 title => "Resistances", 2825 title => "Resistances",
2791 reply => undef, 2826 reply => undef,
2792 tooltip => "Shows your resistances", 2827 tooltip => "Shows your resistances",
2794 "c/pets" => { 2829 "c/pets" => {
2795 id => "infobox", 2830 id => "infobox",
2796 title => "Pets", 2831 title => "Pets",
2797 reply => undef, 2832 reply => undef,
2798 tooltip => "Shows information abotu your pets/a specific pet", 2833 tooltip => "Shows information abotu your pets/a specific pet",
2834 },
2835 "c/perceiveself" => {
2836 id => "infobox",
2837 title => "Perceive Self",
2838 reply => undef,
2839 tooltip => "You gained detailed knowledge about yourself",
2799 }, 2840 },
2800 "c/uptime" => { 2841 "c/uptime" => {
2801 id => "infobox", 2842 id => "infobox",
2802 title => "Uptime", 2843 title => "Uptime",
2803 reply => undef, 2844 reply => undef,
3380 3421
3381 seek $fh, 0, 0; 3422 seek $fh, 0, 0;
3382 print $fh $$; 3423 print $fh $$;
3383} 3424}
3384 3425
3426sub main_loop {
3427 warn "EV::loop starting\n";
3428 if (1) {
3429 EV::loop;
3430 }
3431 warn "EV::loop returned\n";
3432 goto &main_loop unless $REALLY_UNLOOP;
3433}
3434
3385sub main { 3435sub main {
3386 cf::init_globals; # initialise logging 3436 cf::init_globals; # initialise logging
3387 3437
3388 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3438 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3389 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3439 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3427 load_extensions; 3477 load_extensions;
3428 3478
3429 utime time, time, $RUNTIMEFILE; 3479 utime time, time, $RUNTIMEFILE;
3430 3480
3431 # no (long-running) fork's whatsoever before this point(!) 3481 # no (long-running) fork's whatsoever before this point(!)
3482 use POSIX ();
3432 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; 3483 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3433 3484
3434 (pop @POST_INIT)->(0) while @POST_INIT; 3485 (pop @POST_INIT)->(0) while @POST_INIT;
3435 }; 3486 };
3436 3487
3437 EV::loop; 3488 main_loop;
3438} 3489}
3439 3490
3440############################################################################# 3491#############################################################################
3441# initialisation and cleanup 3492# initialisation and cleanup
3442 3493
3708 3759
3709 warn "unload completed, starting to reload now"; 3760 warn "unload completed, starting to reload now";
3710 3761
3711 warn "reloading cf.pm"; 3762 warn "reloading cf.pm";
3712 require cf; 3763 require cf;
3713 cf::_connect_to_perl; # nominally unnecessary, but cannot hurt 3764 cf::_connect_to_perl_1;
3714 3765
3715 warn "loading config and database again"; 3766 warn "loading config and database again";
3716 cf::reload_config; 3767 cf::reload_config;
3717 3768
3718 warn "loading extensions"; 3769 warn "loading extensions";
3925 LOG llevInfo, "[ABT] [suppressed]\n"; 3976 LOG llevInfo, "[ABT] [suppressed]\n";
3926 } 3977 }
3927} 3978}
3928 3979
3929# load additional modules 3980# load additional modules
3930eval "use cf::$_" for @EXTRA_MODULES; 3981require "cf/$_.pm" for @EXTRA_MODULES;
3982cf::_connect_to_perl_2;
3931 3983
3932END { cf::emergency_save } 3984END { cf::emergency_save }
3933 3985
39341 39861
3935 3987

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines