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.519 by root, Fri Apr 16 02:32:25 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},
2182 $MAP_PREFETCHER->prio (6); 2212 $MAP_PREFETCHER->prio (6);
2183 2213
2184 () 2214 ()
2185} 2215}
2186 2216
2217# common code, used by both ->save and ->swapout
2187sub save { 2218sub _save {
2188 my ($self) = @_; 2219 my ($self) = @_;
2189
2190 my $lock = cf::lock_acquire "map_data:$self->{path}";
2191 2220
2192 $self->{last_save} = $cf::RUNTIME; 2221 $self->{last_save} = $cf::RUNTIME;
2193 2222
2194 return unless $self->dirty; 2223 return unless $self->dirty;
2195 2224
2215 } else { 2244 } else {
2216 $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES); 2245 $self->_save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS | cf::IO_UNIQUES);
2217 } 2246 }
2218} 2247}
2219 2248
2249sub save {
2250 my ($self) = @_;
2251
2252 my $lock = cf::lock_acquire "map_data:$self->{path}";
2253
2254 $self->_save;
2255}
2256
2220sub swap_out { 2257sub swap_out {
2221 my ($self) = @_; 2258 my ($self) = @_;
2222 2259
2223 # save first because save cedes
2224 $self->save;
2225
2226 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2260 my $lock = cf::lock_acquire "map_data:$self->{path}";
2227 2261
2228 return if $self->players;
2229 return if $self->in_memory != cf::MAP_ACTIVE; 2262 return if $self->in_memory != cf::MAP_ACTIVE;
2230 return if $self->{deny_save}; 2263 return if $self->{deny_save};
2264 return if $self->players;
2231 2265
2232 $self->in_memory (cf::MAP_SWAPPED); 2266 # first deactivate the map and "unlink" it from the core
2233
2234 $self->deactivate; 2267 $self->deactivate;
2235 $_->clear_links_to ($self) for values %cf::MAP; 2268 $_->clear_links_to ($self) for values %cf::MAP;
2269 $self->in_memory (cf::MAP_SWAPPED);
2270
2271 # then atomically save
2272 $self->_save;
2273
2274 # then free the map
2236 $self->clear; 2275 $self->clear;
2237} 2276}
2238 2277
2239sub reset_at { 2278sub reset_at {
2240 my ($self) = @_; 2279 my ($self) = @_;
2569 return if UNIVERSAL::isa $self->map, "ext::map_link"; 2608 return if UNIVERSAL::isa $self->map, "ext::map_link";
2570 2609
2571 $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] 2610 $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y]
2572 if $self->map && $self->map->{path} ne "{link}"; 2611 if $self->map && $self->map->{path} ne "{link}";
2573 2612
2574 $self->enter_map ($LINK_MAP || link_map, 10, 10); 2613 $self->enter_map ($LINK_MAP || link_map, 3, 3);
2575} 2614}
2576 2615
2577sub cf::object::player::leave_link { 2616sub cf::object::player::leave_link {
2578 my ($self, $map, $x, $y) = @_; 2617 my ($self, $map, $x, $y) = @_;
2579 2618
3134 3173
3135 $coro 3174 $coro
3136} 3175}
3137 3176
3138cf::client->attach ( 3177cf::client->attach (
3139 on_destroy => sub { 3178 on_client_destroy => sub {
3140 my ($ns) = @_; 3179 my ($ns) = @_;
3141 3180
3142 $_->cancel for values %{ (delete $ns->{_coro}) || {} }; 3181 $_->cancel for values %{ (delete $ns->{_coro}) || {} };
3143 }, 3182 },
3144); 3183);
3513 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3552 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3514 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3553 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3515 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3554 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3516 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3555 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3517 3556
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 3557 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3524 3558
3525 # we must not ever block the main coroutine 3559 # we must not ever block the main coroutine
3526 local $Coro::idle = sub { 3560 local $Coro::idle = sub {
3527 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d# 3561 Carp::cluck "FATAL: Coro::idle was called, major BUG, use cf::sync_job!\n";#d#
3532 }; 3566 };
3533 3567
3534 evthread_start IO::AIO::poll_fileno; 3568 evthread_start IO::AIO::poll_fileno;
3535 3569
3536 cf::sync_job { 3570 cf::sync_job {
3571 cf::init_experience;
3572 cf::init_anim;
3573 cf::init_attackmess;
3574 cf::init_dynamic;
3575
3537 cf::load_settings; 3576 cf::load_settings;
3538 cf::load_materials; 3577 cf::load_materials;
3539 3578
3540 reload_resources; 3579 reload_resources;
3541 reload_config; 3580 reload_config;
3542 db_init; 3581 db_init;
3543 3582
3544 cf::init_uuid; 3583 cf::init_uuid;
3545 cf::init_signals; 3584 cf::init_signals;
3546 cf::init_commands;
3547 cf::init_skills; 3585 cf::init_skills;
3548 3586
3549 cf::init_beforeplay; 3587 cf::init_beforeplay;
3550 3588
3551 atomic; 3589 atomic;
3558 use POSIX (); 3596 use POSIX ();
3559 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; 3597 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3560 3598
3561 (pop @POST_INIT)->(0) while @POST_INIT; 3599 (pop @POST_INIT)->(0) while @POST_INIT;
3562 }; 3600 };
3601
3602 cf::object::thawer::errors_are_fatal 0;
3603 warn "parse errors in files are no longer fatal from this point on.\n";
3563 3604
3564 main_loop; 3605 main_loop;
3565} 3606}
3566 3607
3567############################################################################# 3608#############################################################################
3569 3610
3570# install some emergency cleanup handlers 3611# install some emergency cleanup handlers
3571BEGIN { 3612BEGIN {
3572 our %SIGWATCHER = (); 3613 our %SIGWATCHER = ();
3573 for my $signal (qw(INT HUP TERM)) { 3614 for my $signal (qw(INT HUP TERM)) {
3574 $SIGWATCHER{$signal} = EV::signal $signal, sub { 3615 $SIGWATCHER{$signal} = AE::signal $signal, sub {
3575 cf::cleanup "SIG$signal"; 3616 cf::cleanup "SIG$signal";
3576 }; 3617 };
3577 } 3618 }
3578} 3619}
3579 3620
3580sub write_runtime_sync { 3621sub write_runtime_sync {
3581 my $t0 = EV::time; 3622 my $t0 = AE::time;
3582 3623
3583 # first touch the runtime file to show we are still running: 3624 # first touch the runtime file to show we are still running:
3584 # the fsync below can take a very very long time. 3625 # the fsync below can take a very very long time.
3585 3626
3586 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; 3627 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef;
3608 or return; 3649 or return;
3609 3650
3610 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE 3651 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE
3611 and return; 3652 and return;
3612 3653
3613 warn sprintf "runtime file written (%gs).\n", EV::time - $t0; 3654 warn sprintf "runtime file written (%gs).\n", AE::time - $t0;
3614 3655
3615 1 3656 1
3616} 3657}
3617 3658
3618our $uuid_lock; 3659our $uuid_lock;
3756 return; 3797 return;
3757 } 3798 }
3758 3799
3759 return if $RELOAD++; 3800 return if $RELOAD++;
3760 3801
3761 my $t1 = EV::time; 3802 my $t1 = AE::time;
3762 3803
3763 while ($RELOAD) { 3804 while ($RELOAD) {
3764 warn "reloading..."; 3805 warn "reloading...";
3765 3806
3766 warn "entering sync_job"; 3807 warn "entering sync_job";
3869 3910
3870 warn "reloaded"; 3911 warn "reloaded";
3871 --$RELOAD; 3912 --$RELOAD;
3872 } 3913 }
3873 3914
3874 $t1 = EV::time - $t1; 3915 $t1 = AE::time - $t1;
3875 warn "reload completed in ${t1}s\n"; 3916 warn "reload completed in ${t1}s\n";
3876}; 3917};
3877 3918
3878our $RELOAD_WATCHER; # used only during reload 3919our $RELOAD_WATCHER; # used only during reload
3879 3920
3882 # coro crashes during coro_state_free->destroy here. 3923 # coro crashes during coro_state_free->destroy here.
3883 3924
3884 $RELOAD_WATCHER ||= cf::async { 3925 $RELOAD_WATCHER ||= cf::async {
3885 Coro::AIO::aio_wait cache_extensions; 3926 Coro::AIO::aio_wait cache_extensions;
3886 3927
3887 $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { 3928 $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub {
3888 do_reload_perl; 3929 do_reload_perl;
3889 undef $RELOAD_WATCHER; 3930 undef $RELOAD_WATCHER;
3890 }; 3931 };
3891 }; 3932 };
3892} 3933}
3933 return; 3974 return;
3934 } 3975 }
3935 3976
3936 cf::server_tick; # one server iteration 3977 cf::server_tick; # one server iteration
3937 3978
3938 #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d# 3979 #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d#
3939 3980
3940 if ($NOW >= $NEXT_RUNTIME_WRITE) { 3981 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3941 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 3982 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
3942 Coro::async_pool { 3983 Coro::async_pool {
3943 $Coro::current->{desc} = "runtime saver"; 3984 $Coro::current->{desc} = "runtime saver";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines