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.452 by root, Tue Sep 23 00:24:52 2008 UTC vs.
Revision 1.456 by root, Tue Sep 30 04:00:35 2008 UTC

128our $LOAD; # a number between 0 (idle) and 1 (too many objects) 128our $LOAD; # a number between 0 (idle) and 1 (too many objects)
129our $LOADAVG; # same thing, but with alpha-smoothing 129our $LOADAVG; # same thing, but with alpha-smoothing
130our $JITTER; # average jitter 130our $JITTER; # average jitter
131our $TICK_START; # for load detecting purposes 131our $TICK_START; # for load detecting purposes
132 132
133our @POST_INIT;
134
133binmode STDOUT; 135binmode STDOUT;
134binmode STDERR; 136binmode STDERR;
135 137
136# read virtual server time, if available 138# read virtual server time, if available
137unless ($RUNTIME || !-e $RUNTIMEFILE) { 139unless ($RUNTIME || !-e $RUNTIMEFILE) {
304 306
305our $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
306 308
307sub encode_json($) { $json_coder->encode ($_[0]) } 309sub encode_json($) { $json_coder->encode ($_[0]) }
308sub decode_json($) { $json_coder->decode ($_[0]) } 310sub decode_json($) { $json_coder->decode ($_[0]) }
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}
309 325
310=item cf::lock_wait $string 326=item cf::lock_wait $string
311 327
312Wait until the given lock is available. See cf::lock_acquire. 328Wait until the given lock is available. See cf::lock_acquire.
313 329
1171 } else { 1187 } else {
1172 aio_unlink "$filename.pst"; 1188 aio_unlink "$filename.pst";
1173 } 1189 }
1174 1190
1175 aio_rename "$filename~", $filename; 1191 aio_rename "$filename~", $filename;
1192
1193 $filename =~ s%/[^/]+$%%;
1194 aio_pathsync $filename;
1176 } else { 1195 } else {
1177 warn "FATAL: $filename~: $!\n"; 1196 warn "FATAL: $filename~: $!\n";
1178 } 1197 }
1179 } else { 1198 } else {
1180 aio_unlink $filename; 1199 aio_unlink $filename;
3341 reload_treasures; 3360 reload_treasures;
3342 3361
3343 warn "finished reloading resource files\n"; 3362 warn "finished reloading resource files\n";
3344} 3363}
3345 3364
3346sub init {
3347 my $guard = freeze_mainloop;
3348
3349 evthread_start IO::AIO::poll_fileno;
3350
3351 reload_resources;
3352}
3353
3354sub reload_config { 3365sub reload_config {
3355 open my $fh, "<:utf8", "$CONFDIR/config" 3366 open my $fh, "<:utf8", "$CONFDIR/config"
3356 or return; 3367 or return;
3357 3368
3358 local $/; 3369 local $/;
3390 seek $fh, 0, 0; 3401 seek $fh, 0, 0;
3391 print $fh $$; 3402 print $fh $$;
3392} 3403}
3393 3404
3394sub main { 3405sub main {
3395 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
3396 3420
3397 # we must not ever block the main coroutine 3421 # we must not ever block the main coroutine
3398 local $Coro::idle = sub { 3422 local $Coro::idle = sub {
3399 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#
3400 (async { 3424 (async {
3401 $Coro::current->{desc} = "IDLE BUG HANDLER"; 3425 $Coro::current->{desc} = "IDLE BUG HANDLER";
3402 EV::loop EV::LOOP_ONESHOT; 3426 EV::loop EV::LOOP_ONESHOT;
3403 })->prio (Coro::PRIO_MAX); 3427 })->prio (Coro::PRIO_MAX);
3404 }; 3428 };
3405 3429
3406 { 3430 evthread_start IO::AIO::poll_fileno;
3407 my $guard = freeze_mainloop; 3431
3432 cf::sync_job {
3433 reload_resources;
3408 reload_config; 3434 reload_config;
3409 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
3410 load_extensions; 3448 load_extensions;
3411 3449
3412 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3413 }
3414
3415 utime time, time, $RUNTIMEFILE; 3450 utime time, time, $RUNTIMEFILE;
3416 3451
3417 # no (long-running) fork's whatsoever before this point(!) 3452 # no (long-running) fork's whatsoever before this point(!)
3418 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 };
3419 3457
3420 EV::loop; 3458 EV::loop;
3421} 3459}
3422 3460
3423############################################################################# 3461#############################################################################
3481 my $uuid = "$LOCALDIR/uuid"; 3519 my $uuid = "$LOCALDIR/uuid";
3482 3520
3483 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 3521 my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644
3484 or return; 3522 or return;
3485 3523
3486 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;
3487 $uuid_skip = 0; 3532 $uuid_skip = 0;
3488 3533
3489 (aio_write $fh, 0, (length $value), $value, 0) <= 0 3534 (aio_write $fh, 0, (length $value), $value, 0) <= 0
3490 and return; 3535 and return;
3491 3536
3687 warn "reattaching attachments to maps"; 3732 warn "reattaching attachments to maps";
3688 reattach $_ for values %MAP; 3733 reattach $_ for values %MAP;
3689 warn "reattaching attachments to players"; 3734 warn "reattaching attachments to players";
3690 reattach $_ for values %PLAYER; 3735 reattach $_ for values %PLAYER;
3691 3736
3737 warn "running post_load";
3738 (pop @POST_INIT)->(1) while @POST_INIT;
3739
3692 warn "leaving sync_job"; 3740 warn "leaving sync_job";
3693 3741
3694 1 3742 1
3695 } or do { 3743 } or do {
3696 warn $@; 3744 warn $@;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines