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.465 by root, Thu Jan 8 00:54:55 2009 UTC vs.
Revision 1.477 by root, Wed Sep 2 22:52:36 2009 UTC

21 21
22package cf; 22package cf;
23 23
24use 5.10.0; 24use 5.10.0;
25use utf8; 25use utf8;
26use strict "vars", "subs"; 26use strict qw(vars subs);
27 27
28use Symbol; 28use Symbol;
29use List::Util; 29use List::Util;
30use Socket; 30use Socket;
31use EV; 31use EV;
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
105our %RESOURCE; 108our %RESOURCE;
106 109
107our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) 110our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!)
108our $NEXT_RUNTIME_WRITE; # when should the runtime file be written 111our $NEXT_RUNTIME_WRITE; # when should the runtime file be written
109our $NEXT_TICK; 112our $NEXT_TICK;
110our $USE_FSYNC = 1; # use fsync to write maps - default off 113our $USE_FSYNC = 1; # use fsync to write maps - default on
111 114
112our $BDB_DEADLOCK_WATCHER; 115our $BDB_DEADLOCK_WATCHER;
113our $BDB_CHECKPOINT_WATCHER; 116our $BDB_CHECKPOINT_WATCHER;
114our $BDB_TRICKLE_WATCHER; 117our $BDB_TRICKLE_WATCHER;
115our $DB_ENV; 118our $DB_ENV;
116 119
120our @EXTRA_MODULES = qw(pod mapscript);
121
117our %CFG; 122our %CFG;
118 123
119our $UPTIME; $UPTIME ||= time; 124our $UPTIME; $UPTIME ||= time;
120our $RUNTIME; 125our $RUNTIME;
121our $NOW; 126our $NOW;
132our $JITTER; # average jitter 137our $JITTER; # average jitter
133our $TICK_START; # for load detecting purposes 138our $TICK_START; # for load detecting purposes
134 139
135our @POST_INIT; 140our @POST_INIT;
136 141
137our $REATTACH_ON_RELOAD; # ste to true to force object reattach on reload (slow) 142our $REATTACH_ON_RELOAD; # set to true to force object reattach on reload (slow)
143our $REALLY_UNLOOP; # never set to true, please :)
138 144
139binmode STDOUT; 145binmode STDOUT;
140binmode STDERR; 146binmode STDERR;
141 147
142# read virtual server time, if available 148# read virtual server time, if available
144 open my $fh, "<", $RUNTIMEFILE 150 open my $fh, "<", $RUNTIMEFILE
145 or die "unable to read $RUNTIMEFILE file: $!"; 151 or die "unable to read $RUNTIMEFILE file: $!";
146 $RUNTIME = <$fh> + 0.; 152 $RUNTIME = <$fh> + 0.;
147} 153}
148 154
155eval "sub TICK() { $TICK } 1" or die;
156
149mkdir $_ 157mkdir $_
150 for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR; 158 for $LOCALDIR, $TMPDIR, $UNIQUEDIR, $PLAYERDIR, $RANDOMDIR, $BDBDIR;
151 159
152our $EMERGENCY_POSITION; 160our $EMERGENCY_POSITION;
153 161
214 222
215=back 223=back
216 224
217=cut 225=cut
218 226
219BEGIN { 227$Coro::State::WARNHOOK = sub {
220 *CORE::GLOBAL::warn = sub {
221 my $msg = join "", @_; 228 my $msg = join "", @_;
222 229
223 $msg .= "\n" 230 $msg .= "\n"
224 unless $msg =~ /\n$/; 231 unless $msg =~ /\n$/;
225 232
226 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; 233 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge;
227 234
228 LOG llevError, $msg; 235 LOG llevError, $msg;
229 }; 236};
230}
231 237
232$Coro::State::DIEHOOK = sub { 238$Coro::State::DIEHOOK = sub {
233 return unless $^S eq 0; # "eq", not "==" 239 return unless $^S eq 0; # "eq", not "=="
240
241 warn Carp::longmess $_[0];
234 242
235 if ($Coro::current == $Coro::main) {#d# 243 if ($Coro::current == $Coro::main) {#d#
236 warn "DIEHOOK called in main context, Coro bug?\n";#d# 244 warn "DIEHOOK called in main context, Coro bug?\n";#d#
237 return;#d# 245 return;#d#
238 }#d# 246 }#d#
239 247
240 # kill coroutine otherwise 248 # kill coroutine otherwise
241 warn Carp::longmess $_[0];
242 Coro::terminate 249 Coro::terminate
243}; 250};
244
245$SIG{__DIE__} = sub { }; #d#?
246 251
247@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; 252@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable';
248@safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable'; 253@safe::cf::object::ISA = @cf::object::ISA = 'cf::attachable';
249@safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable'; 254@safe::cf::player::ISA = @cf::player::ISA = 'cf::attachable';
250@safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable'; 255@safe::cf::client::ISA = @cf::client::ISA = 'cf::attachable';
1147 $decname, length $$rdata, scalar @$objs; 1152 $decname, length $$rdata, scalar @$objs;
1148 1153
1149 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { 1154 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) {
1150 aio_chmod $fh, SAVE_MODE; 1155 aio_chmod $fh, SAVE_MODE;
1151 aio_write $fh, 0, (length $$rdata), $$rdata, 0; 1156 aio_write $fh, 0, (length $$rdata), $$rdata, 0;
1152 aio_fsync $fh if $cf::USE_FSYNC; 1157 if ($cf::USE_FSYNC) {
1158 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;
1159 aio_fsync $fh;
1160 }
1153 aio_close $fh; 1161 aio_close $fh;
1154 1162
1155 if (@$objs) { 1163 if (@$objs) {
1156 if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) { 1164 if (my $fh = aio_open "$filename.pst~", O_WRONLY | O_CREAT, 0600) {
1157 aio_chmod $fh, SAVE_MODE; 1165 aio_chmod $fh, SAVE_MODE;
1158 my $data = Coro::Storable::nfreeze { version => 1, objs => $objs }; 1166 my $data = Coro::Storable::nfreeze { version => 1, objs => $objs };
1159 aio_write $fh, 0, (length $data), $data, 0; 1167 aio_write $fh, 0, (length $data), $data, 0;
1160 aio_fsync $fh if $cf::USE_FSYNC; 1168 if ($cf::USE_FSYNC) {
1169 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;
1170 aio_fsync $fh;
1171 }
1161 aio_close $fh; 1172 aio_close $fh;
1162 aio_rename "$filename.pst~", "$filename.pst"; 1173 aio_rename "$filename.pst~", "$filename.pst";
1163 } 1174 }
1164 } else { 1175 } else {
1165 aio_unlink "$filename.pst"; 1176 aio_unlink "$filename.pst";
1316 1327
1317# "readahead" all extensions 1328# "readahead" all extensions
1318sub cache_extensions { 1329sub cache_extensions {
1319 my $grp = IO::AIO::aio_group; 1330 my $grp = IO::AIO::aio_group;
1320 1331
1321 add $grp IO::AIO::aio_readdir $LIBDIR, sub { 1332 add $grp IO::AIO::aio_readdirx $LIBDIR, IO::AIO::READDIR_STAT_ORDER, sub {
1322 for (grep /\.ext$/, @{$_[0]}) { 1333 for (grep /\.ext$/, @{$_[0]}) {
1323 add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; 1334 add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data;
1324 } 1335 }
1325 }; 1336 };
1326 1337
2384 2395
2385our $SAY_CHANNEL = { 2396our $SAY_CHANNEL = {
2386 id => "say", 2397 id => "say",
2387 title => "Map", 2398 title => "Map",
2388 reply => "say ", 2399 reply => "say ",
2389 tooltip => "Things said to and replied from npcs near you and other players on the same map only.", 2400 tooltip => "Things said to and replied from NPCs near you and other players on the same map only.",
2390}; 2401};
2391 2402
2392our $CHAT_CHANNEL = { 2403our $CHAT_CHANNEL = {
2393 id => "chat", 2404 id => "chat",
2394 title => "Chat", 2405 title => "Chat",
2520 2531
2521 $map->load; 2532 $map->load;
2522 $map->load_neighbours; 2533 $map->load_neighbours;
2523 2534
2524 return unless $self->contr->active; 2535 return unless $self->contr->active;
2525 $self->flag (cf::FLAG_DEBUG, 0);#d# temp
2526 $self->activate_recursive;
2527 2536
2528 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext 2537 local $self->{_prev_pos} = $link_pos; # ugly hack for rent.ext
2529 $self->enter_map ($map, $x, $y); 2538 $self->enter_map ($map, $x, $y);
2539
2540 # only activate afterwards, to support waiting in hooks
2541 $self->activate_recursive;
2530} 2542}
2531 2543
2532=item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]]) 2544=item $player_object->goto ($path, $x, $y[, $check->($map)[, $done->()]])
2533 2545
2534Moves the player to the given map-path and coordinates by first freezing 2546Moves the player to the given map-path and coordinates by first freezing
2781 id => "infobox", 2793 id => "infobox",
2782 title => "Skills", 2794 title => "Skills",
2783 reply => undef, 2795 reply => undef,
2784 tooltip => "Shows your experience per skill and item power", 2796 tooltip => "Shows your experience per skill and item power",
2785 }, 2797 },
2798 "c/shopitems" => {
2799 id => "infobox",
2800 title => "Shop Items",
2801 reply => undef,
2802 tooltip => "Shows the items currently for sale in this shop",
2803 },
2786 "c/resistances" => { 2804 "c/resistances" => {
2787 id => "infobox", 2805 id => "infobox",
2788 title => "Resistances", 2806 title => "Resistances",
2789 reply => undef, 2807 reply => undef,
2790 tooltip => "Shows your resistances", 2808 tooltip => "Shows your resistances",
2792 "c/pets" => { 2810 "c/pets" => {
2793 id => "infobox", 2811 id => "infobox",
2794 title => "Pets", 2812 title => "Pets",
2795 reply => undef, 2813 reply => undef,
2796 tooltip => "Shows information abotu your pets/a specific pet", 2814 tooltip => "Shows information abotu your pets/a specific pet",
2815 },
2816 "c/perceiveself" => {
2817 id => "infobox",
2818 title => "Perceive Self",
2819 reply => undef,
2820 tooltip => "You gained detailed knowledge about yourself",
2797 }, 2821 },
2798 "c/uptime" => { 2822 "c/uptime" => {
2799 id => "infobox", 2823 id => "infobox",
2800 title => "Uptime", 2824 title => "Uptime",
2801 reply => undef, 2825 reply => undef,
3052The following functions and methods are available within a safe environment: 3076The following functions and methods are available within a safe environment:
3053 3077
3054 cf::object 3078 cf::object
3055 contr pay_amount pay_player map x y force_find force_add destroy 3079 contr pay_amount pay_player map x y force_find force_add destroy
3056 insert remove name archname title slaying race decrease split 3080 insert remove name archname title slaying race decrease split
3081 value
3057 3082
3058 cf::object::player 3083 cf::object::player
3059 player 3084 player
3060 3085
3061 cf::player 3086 cf::player
3067=cut 3092=cut
3068 3093
3069for ( 3094for (
3070 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y 3095 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y
3071 insert remove inv nrof name archname title slaying race 3096 insert remove inv nrof name archname title slaying race
3072 decrease split destroy change_exp)], 3097 decrease split destroy change_exp value msg lore send_msg)],
3073 ["cf::object::player" => qw(player)], 3098 ["cf::object::player" => qw(player)],
3074 ["cf::player" => qw(peaceful)], 3099 ["cf::player" => qw(peaceful send_msg)],
3075 ["cf::map" => qw(trigger)], 3100 ["cf::map" => qw(trigger)],
3076) { 3101) {
3077 no strict 'refs'; 3102 no strict 'refs';
3078 my ($pkg, @funs) = @$_; 3103 my ($pkg, @funs) = @$_;
3079 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 3104 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
3096 my ($code, %vars) = @_; 3121 my ($code, %vars) = @_;
3097 3122
3098 my $qcode = $code; 3123 my $qcode = $code;
3099 $qcode =~ s/"/‟/g; # not allowed in #line filenames 3124 $qcode =~ s/"/‟/g; # not allowed in #line filenames
3100 $qcode =~ s/\n/\\n/g; 3125 $qcode =~ s/\n/\\n/g;
3126
3127 %vars = (_dummy => 0) unless %vars;
3101 3128
3102 local $_; 3129 local $_;
3103 local @safe::cf::_safe_eval_args = values %vars; 3130 local @safe::cf::_safe_eval_args = values %vars;
3104 3131
3105 my $eval = 3132 my $eval =
3373 my $pid = <$fh>; 3400 my $pid = <$fh>;
3374 kill 9, $pid if $pid > 0; 3401 kill 9, $pid if $pid > 0;
3375 3402
3376 seek $fh, 0, 0; 3403 seek $fh, 0, 0;
3377 print $fh $$; 3404 print $fh $$;
3405}
3406
3407sub main_loop {
3408 warn "EV::loop starting\n";
3409 if (1) {
3410 EV::loop;
3411 }
3412 warn "EV::loop returned\n";
3413 goto &main_loop unless $REALLY_UNLOOP;
3378} 3414}
3379 3415
3380sub main { 3416sub main {
3381 cf::init_globals; # initialise logging 3417 cf::init_globals; # initialise logging
3382 3418
3422 load_extensions; 3458 load_extensions;
3423 3459
3424 utime time, time, $RUNTIMEFILE; 3460 utime time, time, $RUNTIMEFILE;
3425 3461
3426 # no (long-running) fork's whatsoever before this point(!) 3462 # no (long-running) fork's whatsoever before this point(!)
3463 use POSIX ();
3427 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; 3464 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3428 3465
3429 (pop @POST_INIT)->(0) while @POST_INIT; 3466 (pop @POST_INIT)->(0) while @POST_INIT;
3430 }; 3467 };
3431 3468
3432 EV::loop; 3469 main_loop;
3433} 3470}
3434 3471
3435############################################################################# 3472#############################################################################
3436# initialisation and cleanup 3473# initialisation and cleanup
3437 3474
3693 clear_package "safe::$_" 3730 clear_package "safe::$_"
3694 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 3731 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
3695 3732
3696 warn "unloading cf.pm \"a bit\""; 3733 warn "unloading cf.pm \"a bit\"";
3697 delete $INC{"cf.pm"}; 3734 delete $INC{"cf.pm"};
3698 delete $INC{"cf/pod.pm"}; 3735 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES;
3699 3736
3700 # don't, removes xs symbols, too, 3737 # don't, removes xs symbols, too,
3701 # and global variables created in xs 3738 # and global variables created in xs
3702 #clear_package __PACKAGE__; 3739 #clear_package __PACKAGE__;
3703 3740
3920 LOG llevInfo, "[ABT] [suppressed]\n"; 3957 LOG llevInfo, "[ABT] [suppressed]\n";
3921 } 3958 }
3922} 3959}
3923 3960
3924# load additional modules 3961# load additional modules
3925use cf::pod; 3962require "cf/$_.pm" for @EXTRA_MODULES;
3926 3963
3927END { cf::emergency_save } 3964END { cf::emergency_save }
3928 3965
39291 39661
3930 3967

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines