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.507 by root, Sat Mar 20 02:24:38 2010 UTC vs.
Revision 1.517 by root, Thu Apr 15 06:07:51 2010 UTC

108our $PIDFILE = "$LOCALDIR/pid"; 108our $PIDFILE = "$LOCALDIR/pid";
109our $RUNTIMEFILE = "$LOCALDIR/runtime"; 109our $RUNTIMEFILE = "$LOCALDIR/runtime";
110 110
111our %RESOURCE; 111our %RESOURCE;
112 112
113our $OUTPUT_RATE_MIN = 4000;
114our $OUTPUT_RATE_MAX = 100000;
115
113our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!) 116our $TICK = MAX_TIME * 1e-6; # this is a CONSTANT(!)
114our $NEXT_RUNTIME_WRITE; # when should the runtime file be written 117our $NEXT_RUNTIME_WRITE; # when should the runtime file be written
115our $NEXT_TICK; 118our $NEXT_TICK;
116our $USE_FSYNC = 1; # use fsync to write maps - default on 119our $USE_FSYNC = 1; # use fsync to write maps - default on
117 120
396} 399}
397 400
398=item cf::periodic $interval, $cb 401=item cf::periodic $interval, $cb
399 402
400Like EV::periodic, but randomly selects a starting point so that the actions 403Like EV::periodic, but randomly selects a starting point so that the actions
401get spread over timer. 404get spread over time.
402 405
403=cut 406=cut
404 407
405sub periodic($$) { 408sub periodic($$) {
406 my ($interval, $cb) = @_; 409 my ($interval, $cb) = @_;
503 506
504sub sync_job(&) { 507sub sync_job(&) {
505 my ($job) = @_; 508 my ($job) = @_;
506 509
507 if ($Coro::current == $Coro::main) { 510 if ($Coro::current == $Coro::main) {
508 my $time = EV::time; 511 my $time = AE::time;
509 512
510 # this is the main coro, too bad, we have to block 513 # this is the main coro, too bad, we have to block
511 # till the operation succeeds, freezing the server :/ 514 # till the operation succeeds, freezing the server :/
512 515
513 LOG llevError, Carp::longmess "sync job";#d# 516 LOG llevError, Carp::longmess "sync job";#d#
530 } else { 533 } else {
531 EV::loop EV::LOOP_ONESHOT; 534 EV::loop EV::LOOP_ONESHOT;
532 } 535 }
533 } 536 }
534 537
535 my $time = EV::time - $time; 538 my $time = AE::time - $time;
536 539
537 $TICK_START += $time; # do not account sync jobs to server load 540 $TICK_START += $time; # do not account sync jobs to server load
538 541
539 wantarray ? @res : $res[0] 542 wantarray ? @res : $res[0]
540 } else { 543 } else {
584 reset_signals; 587 reset_signals;
585 &$cb 588 &$cb
586 }, @args; 589 }, @args;
587 590
588 wantarray ? @res : $res[-1] 591 wantarray ? @res : $res[-1]
592}
593
594sub objinfo {
595 (
596 "counter value" => cf::object::object_count,
597 "objects created" => cf::object::create_count,
598 "objects destroyed" => cf::object::destroy_count,
599 "freelist size" => cf::object::free_count,
600 "allocated objects" => cf::object::objects_size,
601 "active objects" => cf::object::actives_size,
602 )
589} 603}
590 604
591=item $coin = coin_from_name $name 605=item $coin = coin_from_name $name
592 606
593=cut 607=cut
1310} 1324}
1311 1325
1312use File::Glob (); 1326use File::Glob ();
1313 1327
1314cf::player->attach ( 1328cf::player->attach (
1315 on_command => sub { 1329 on_unknown_command => sub {
1316 my ($pl, $name, $params) = @_; 1330 my ($pl, $name, $params) = @_;
1317 1331
1318 my $cb = $COMMAND{$name} 1332 my $cb = $COMMAND{$name}
1319 or return; 1333 or return;
1320 1334
1533 $cf::PLAYER{$login} = $pl 1547 $cf::PLAYER{$login} = $pl
1534 } 1548 }
1535 } 1549 }
1536} 1550}
1537 1551
1552cf::player->attach (
1553 on_load => sub {
1554 my ($pl, $path) = @_;
1555
1556 # restore slots saved in save, below
1557 my $slots = delete $pl->{_slots};
1558
1559 $pl->ob->current_weapon ($slots->[0]);
1560 $pl->combat_ob ($slots->[1]);
1561 $pl->ranged_ob ($slots->[2]);
1562 },
1563);
1564
1538sub save($) { 1565sub save($) {
1539 my ($pl) = @_; 1566 my ($pl) = @_;
1540 1567
1541 return if $pl->{deny_save}; 1568 return if $pl->{deny_save};
1542 1569
1547 1574
1548 aio_mkdir playerdir $pl, 0770; 1575 aio_mkdir playerdir $pl, 0770;
1549 $pl->{last_save} = $cf::RUNTIME; 1576 $pl->{last_save} = $cf::RUNTIME;
1550 1577
1551 cf::get_slot 0.01; 1578 cf::get_slot 0.01;
1579
1580 # save slots, to be restored later
1581 local $pl->{_slots} = [$pl->ob->current_weapon, $pl->combat_ob, $pl->ranged_ob];
1552 1582
1553 $pl->save_pl ($path); 1583 $pl->save_pl ($path);
1554 cf::cede_to_tick; 1584 cf::cede_to_tick;
1555} 1585}
1556 1586
1769 my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM 1799 my $lock = cf::lock_acquire "generate_random_map"; # the random map generator is NOT reentrant ATM
1770 1800
1771 # mit "rum" bekleckern, nicht 1801 # mit "rum" bekleckern, nicht
1772 $self->_create_random_map ( 1802 $self->_create_random_map (
1773 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, 1803 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
1774 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, 1804 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle},
1775 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, 1805 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
1776 $rmp->{exit_on_final_map}, 1806 $rmp->{exit_on_final_map},
1777 $rmp->{xsize}, $rmp->{ysize}, 1807 $rmp->{xsize}, $rmp->{ysize},
1778 $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, 1808 $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3},
1779 $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, 1809 $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase},
3134 3164
3135 $coro 3165 $coro
3136} 3166}
3137 3167
3138cf::client->attach ( 3168cf::client->attach (
3139 on_destroy => sub { 3169 on_client_destroy => sub {
3140 my ($ns) = @_; 3170 my ($ns) = @_;
3141 3171
3142 $_->cancel for values %{ (delete $ns->{_coro}) || {} }; 3172 $_->cancel for values %{ (delete $ns->{_coro}) || {} };
3143 }, 3173 },
3144); 3174);
3513 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3543 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3514 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3544 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3515 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3545 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3516 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3546 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3517 3547
3518 cf::init_experience;
3519 cf::init_anim;
3520 cf::init_attackmess;
3521 cf::init_dynamic;
3522
3523 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3548 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3524 3549
3525 # we must not ever block the main coroutine 3550 # we must not ever block the main coroutine
3526 local $Coro::idle = sub { 3551 local $Coro::idle = sub {
3527 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# 3552 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
3532 }; 3557 };
3533 3558
3534 evthread_start IO::AIO::poll_fileno; 3559 evthread_start IO::AIO::poll_fileno;
3535 3560
3536 cf::sync_job { 3561 cf::sync_job {
3562 cf::init_experience;
3563 cf::init_anim;
3564 cf::init_attackmess;
3565 cf::init_dynamic;
3566
3537 cf::load_settings; 3567 cf::load_settings;
3538 cf::load_materials; 3568 cf::load_materials;
3539 3569
3540 reload_resources; 3570 reload_resources;
3541 reload_config; 3571 reload_config;
3542 db_init; 3572 db_init;
3543 3573
3544 cf::init_uuid; 3574 cf::init_uuid;
3545 cf::init_signals; 3575 cf::init_signals;
3546 cf::init_commands;
3547 cf::init_skills; 3576 cf::init_skills;
3548 3577
3549 cf::init_beforeplay; 3578 cf::init_beforeplay;
3550 3579
3551 atomic; 3580 atomic;
3558 use POSIX (); 3587 use POSIX ();
3559 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; 3588 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3560 3589
3561 (pop @POST_INIT)->(0) while @POST_INIT; 3590 (pop @POST_INIT)->(0) while @POST_INIT;
3562 }; 3591 };
3592
3593 cf::object::thawer::errors_are_fatal 0;
3594 warn "parse errors in files are no longer fatal from this point on.\n";
3563 3595
3564 main_loop; 3596 main_loop;
3565} 3597}
3566 3598
3567############################################################################# 3599#############################################################################
3569 3601
3570# install some emergency cleanup handlers 3602# install some emergency cleanup handlers
3571BEGIN { 3603BEGIN {
3572 our %SIGWATCHER = (); 3604 our %SIGWATCHER = ();
3573 for my $signal (qw(INT HUP TERM)) { 3605 for my $signal (qw(INT HUP TERM)) {
3574 $SIGWATCHER{$signal} = EV::signal $signal, sub { 3606 $SIGWATCHER{$signal} = AE::signal $signal, sub {
3575 cf::cleanup "SIG$signal"; 3607 cf::cleanup "SIG$signal";
3576 }; 3608 };
3577 } 3609 }
3578} 3610}
3579 3611
3580sub write_runtime_sync { 3612sub write_runtime_sync {
3581 my $t0 = EV::time; 3613 my $t0 = AE::time;
3582 3614
3583 # first touch the runtime file to show we are still running: 3615 # first touch the runtime file to show we are still running:
3584 # the fsync below can take a very very long time. 3616 # the fsync below can take a very very long time.
3585 3617
3586 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; 3618 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef;
3608 or return; 3640 or return;
3609 3641
3610 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE 3642 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE
3611 and return; 3643 and return;
3612 3644
3613 warn sprintf "runtime file written (%gs).\n", EV::time - $t0; 3645 warn sprintf "runtime file written (%gs).\n", AE::time - $t0;
3614 3646
3615 1 3647 1
3616} 3648}
3617 3649
3618our $uuid_lock; 3650our $uuid_lock;
3756 return; 3788 return;
3757 } 3789 }
3758 3790
3759 return if $RELOAD++; 3791 return if $RELOAD++;
3760 3792
3761 my $t1 = EV::time; 3793 my $t1 = AE::time;
3762 3794
3763 while ($RELOAD) { 3795 while ($RELOAD) {
3764 warn "reloading..."; 3796 warn "reloading...";
3765 3797
3766 warn "entering sync_job"; 3798 warn "entering sync_job";
3869 3901
3870 warn "reloaded"; 3902 warn "reloaded";
3871 --$RELOAD; 3903 --$RELOAD;
3872 } 3904 }
3873 3905
3874 $t1 = EV::time - $t1; 3906 $t1 = AE::time - $t1;
3875 warn "reload completed in ${t1}s\n"; 3907 warn "reload completed in ${t1}s\n";
3876}; 3908};
3877 3909
3878our $RELOAD_WATCHER; # used only during reload 3910our $RELOAD_WATCHER; # used only during reload
3879 3911
3882 # coro crashes during coro_state_free->destroy here. 3914 # coro crashes during coro_state_free->destroy here.
3883 3915
3884 $RELOAD_WATCHER ||= cf::async { 3916 $RELOAD_WATCHER ||= cf::async {
3885 Coro::AIO::aio_wait cache_extensions; 3917 Coro::AIO::aio_wait cache_extensions;
3886 3918
3887 $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { 3919 $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub {
3888 do_reload_perl; 3920 do_reload_perl;
3889 undef $RELOAD_WATCHER; 3921 undef $RELOAD_WATCHER;
3890 }; 3922 };
3891 }; 3923 };
3892} 3924}
3933 return; 3965 return;
3934 } 3966 }
3935 3967
3936 cf::server_tick; # one server iteration 3968 cf::server_tick; # one server iteration
3937 3969
3938 #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d# 3970 #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d#
3939 3971
3940 if ($NOW >= $NEXT_RUNTIME_WRITE) { 3972 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3941 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 3973 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
3942 Coro::async_pool { 3974 Coro::async_pool {
3943 $Coro::current->{desc} = "runtime saver"; 3975 $Coro::current->{desc} = "runtime saver";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines