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.503 by root, Sat Jan 30 23:46:39 2010 UTC vs.
Revision 1.519 by root, Fri Apr 16 02:32:25 2010 UTC

1# 1#
2# This file is part of Deliantra, the Roguelike Realtime MMORPG. 2# This file is part of Deliantra, the Roguelike Realtime MMORPG.
3# 3#
4# Copyright (©) 2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4# Copyright (©) 2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5# 5#
6# Deliantra is free software: you can redistribute it and/or modify it under 6# Deliantra is free software: you can redistribute it and/or modify it under
7# the terms of the Affero GNU General Public License as published by the 7# the terms of the Affero GNU General Public License as published by the
8# Free Software Foundation, either version 3 of the License, or (at your 8# Free Software Foundation, either version 3 of the License, or (at your
9# option) any later version. 9# option) any later version.
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
1399 . "\n};\n1"; 1413 . "\n};\n1";
1400 1414
1401 $todo{$base} = \%ext; 1415 $todo{$base} = \%ext;
1402 } 1416 }
1403 1417
1418 my $pass = 0;
1404 my %done; 1419 my %done;
1405 while (%todo) { 1420 while (%todo) {
1406 my $progress; 1421 my $progress;
1407 1422
1423 ++$pass;
1424
1425 ext:
1408 while (my ($k, $v) = each %todo) { 1426 while (my ($k, $v) = each %todo) {
1409 for (split /,\s*/, $v->{meta}{depends}) { 1427 for (split /,\s*/, $v->{meta}{depends}) {
1410 goto skip 1428 next ext
1411 unless exists $done{$_}; 1429 unless exists $done{$_};
1412 } 1430 }
1413 1431
1414 warn "... loading '$k' into '$v->{pkg}'\n"; 1432 warn "... pass $pass, loading '$k' into '$v->{pkg}'\n";
1415 1433
1416 unless (eval $v->{source}) { 1434 my $active = eval $v->{source};
1435
1436 if (length $@) {
1417 my $msg = $@ ? "$v->{path}: $@\n" 1437 warn "$v->{path}: $@\n";
1418 : "$v->{base}: extension inactive.\n";
1419 1438
1420 if (exists $v->{meta}{mandatory}) {
1421 warn $msg;
1422 cf::cleanup "mandatory extension failed to load, exiting."; 1439 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1423 } 1440 if exists $v->{meta}{mandatory};
1424 1441 } else {
1425 warn $msg; 1442 $done{$k} = delete $todo{$k};
1443 push @EXTS, $v->{pkg};
1444 $progress = 1;
1445
1446 warn "$v->{base}: extension inactive.\n"
1447 unless $active;
1426 } 1448 }
1427
1428 $done{$k} = delete $todo{$k};
1429 push @EXTS, $v->{pkg};
1430 $progress = 1;
1431 } 1449 }
1432 1450
1433 skip: 1451 unless ($progress) {
1434 die "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n" 1452 warn "cannot load " . (join ", ", keys %todo) . ": unable to resolve dependencies\n";
1435 unless $progress; 1453
1454 while (my ($k, $v) = each %todo) {
1455 cf::cleanup "mandatory extension '$k' has unresolved dependencies, exiting."
1456 if exists $v->{meta}{mandatory};
1457 }
1458 }
1436 } 1459 }
1437 }; 1460 };
1438} 1461}
1439 1462
1440############################################################################# 1463#############################################################################
1524 $cf::PLAYER{$login} = $pl 1547 $cf::PLAYER{$login} = $pl
1525 } 1548 }
1526 } 1549 }
1527} 1550}
1528 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
1529sub save($) { 1565sub save($) {
1530 my ($pl) = @_; 1566 my ($pl) = @_;
1531 1567
1532 return if $pl->{deny_save}; 1568 return if $pl->{deny_save};
1533 1569
1538 1574
1539 aio_mkdir playerdir $pl, 0770; 1575 aio_mkdir playerdir $pl, 0770;
1540 $pl->{last_save} = $cf::RUNTIME; 1576 $pl->{last_save} = $cf::RUNTIME;
1541 1577
1542 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];
1543 1582
1544 $pl->save_pl ($path); 1583 $pl->save_pl ($path);
1545 cf::cede_to_tick; 1584 cf::cede_to_tick;
1546} 1585}
1547 1586
1760 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
1761 1800
1762 # mit "rum" bekleckern, nicht 1801 # mit "rum" bekleckern, nicht
1763 $self->_create_random_map ( 1802 $self->_create_random_map (
1764 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle}, 1803 $rmp->{wallstyle}, $rmp->{wall_name}, $rmp->{floorstyle}, $rmp->{monsterstyle},
1765 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, 1804 $rmp->{treasurestyle}, $rmp->{layoutstyle}, $rmp->{doorstyle}, $rmp->{decorstyle}, $rmp->{miningstyle},
1766 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map}, 1805 $rmp->{origin_map}, $rmp->{final_map}, $rmp->{exitstyle}, $rmp->{this_map},
1767 $rmp->{exit_on_final_map}, 1806 $rmp->{exit_on_final_map},
1768 $rmp->{xsize}, $rmp->{ysize}, 1807 $rmp->{xsize}, $rmp->{ysize},
1769 $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3}, 1808 $rmp->{expand2x}, $rmp->{layoutoptions1}, $rmp->{layoutoptions2}, $rmp->{layoutoptions3},
1770 $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase}, 1809 $rmp->{symmetry}, $rmp->{difficulty}, $rmp->{difficulty_given}, $rmp->{difficulty_increase},
2084 } 2123 }
2085 2124
2086 $self->post_load; 2125 $self->post_load;
2087} 2126}
2088 2127
2128# customize the map for a given player, i.e.
2129# return the _real_ map. used by e.g. per-player
2130# maps to change the path to ~playername/mappath
2089sub customise_for { 2131sub customise_for {
2090 my ($self, $ob) = @_; 2132 my ($self, $ob) = @_;
2091 2133
2092 return find "~" . $ob->name . "/" . $self->{path} 2134 return find "~" . $ob->name . "/" . $self->{path}
2093 if $self->per_player; 2135 if $self->per_player;
2170 $MAP_PREFETCHER->prio (6); 2212 $MAP_PREFETCHER->prio (6);
2171 2213
2172 () 2214 ()
2173} 2215}
2174 2216
2217# common code, used by both ->save and ->swapout
2175sub save { 2218sub _save {
2176 my ($self) = @_; 2219 my ($self) = @_;
2177
2178 my $lock = cf::lock_acquire "map_data:$self->{path}";
2179 2220
2180 $self->{last_save} = $cf::RUNTIME; 2221 $self->{last_save} = $cf::RUNTIME;
2181 2222
2182 return unless $self->dirty; 2223 return unless $self->dirty;
2183 2224
2203 } else { 2244 } else {
2204 $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);
2205 } 2246 }
2206} 2247}
2207 2248
2249sub save {
2250 my ($self) = @_;
2251
2252 my $lock = cf::lock_acquire "map_data:$self->{path}";
2253
2254 $self->_save;
2255}
2256
2208sub swap_out { 2257sub swap_out {
2209 my ($self) = @_; 2258 my ($self) = @_;
2210 2259
2211 # save first because save cedes
2212 $self->save;
2213
2214 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2260 my $lock = cf::lock_acquire "map_data:$self->{path}";
2215 2261
2216 return if $self->players;
2217 return if $self->in_memory != cf::MAP_ACTIVE; 2262 return if $self->in_memory != cf::MAP_ACTIVE;
2218 return if $self->{deny_save}; 2263 return if $self->{deny_save};
2264 return if $self->players;
2219 2265
2220 $self->in_memory (cf::MAP_SWAPPED); 2266 # first deactivate the map and "unlink" it from the core
2221
2222 $self->deactivate; 2267 $self->deactivate;
2223 $_->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
2224 $self->clear; 2275 $self->clear;
2225} 2276}
2226 2277
2227sub reset_at { 2278sub reset_at {
2228 my ($self) = @_; 2279 my ($self) = @_;
2557 return if UNIVERSAL::isa $self->map, "ext::map_link"; 2608 return if UNIVERSAL::isa $self->map, "ext::map_link";
2558 2609
2559 $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y] 2610 $self->{_link_pos} ||= [$self->map->{path}, $self->x, $self->y]
2560 if $self->map && $self->map->{path} ne "{link}"; 2611 if $self->map && $self->map->{path} ne "{link}";
2561 2612
2562 $self->enter_map ($LINK_MAP || link_map, 10, 10); 2613 $self->enter_map ($LINK_MAP || link_map, 3, 3);
2563} 2614}
2564 2615
2565sub cf::object::player::leave_link { 2616sub cf::object::player::leave_link {
2566 my ($self, $map, $x, $y) = @_; 2617 my ($self, $map, $x, $y) = @_;
2567 2618
3122 3173
3123 $coro 3174 $coro
3124} 3175}
3125 3176
3126cf::client->attach ( 3177cf::client->attach (
3127 on_destroy => sub { 3178 on_client_destroy => sub {
3128 my ($ns) = @_; 3179 my ($ns) = @_;
3129 3180
3130 $_->cancel for values %{ (delete $ns->{_coro}) || {} }; 3181 $_->cancel for values %{ (delete $ns->{_coro}) || {} };
3131 }, 3182 },
3132); 3183);
3501 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3552 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3502 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.";
3503 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3554 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3504 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3555 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3505 3556
3506 cf::init_experience;
3507 cf::init_anim;
3508 cf::init_attackmess;
3509 cf::init_dynamic;
3510
3511 $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
3512 3558
3513 # we must not ever block the main coroutine 3559 # we must not ever block the main coroutine
3514 local $Coro::idle = sub { 3560 local $Coro::idle = sub {
3515 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#
3520 }; 3566 };
3521 3567
3522 evthread_start IO::AIO::poll_fileno; 3568 evthread_start IO::AIO::poll_fileno;
3523 3569
3524 cf::sync_job { 3570 cf::sync_job {
3571 cf::init_experience;
3572 cf::init_anim;
3573 cf::init_attackmess;
3574 cf::init_dynamic;
3575
3525 cf::load_settings; 3576 cf::load_settings;
3526 cf::load_materials; 3577 cf::load_materials;
3527 3578
3528 reload_resources; 3579 reload_resources;
3529 reload_config; 3580 reload_config;
3530 db_init; 3581 db_init;
3531 3582
3532 cf::init_uuid; 3583 cf::init_uuid;
3533 cf::init_signals; 3584 cf::init_signals;
3534 cf::init_commands;
3535 cf::init_skills; 3585 cf::init_skills;
3536 3586
3537 cf::init_beforeplay; 3587 cf::init_beforeplay;
3538 3588
3539 atomic; 3589 atomic;
3546 use POSIX (); 3596 use POSIX ();
3547 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};
3548 3598
3549 (pop @POST_INIT)->(0) while @POST_INIT; 3599 (pop @POST_INIT)->(0) while @POST_INIT;
3550 }; 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";
3551 3604
3552 main_loop; 3605 main_loop;
3553} 3606}
3554 3607
3555############################################################################# 3608#############################################################################
3557 3610
3558# install some emergency cleanup handlers 3611# install some emergency cleanup handlers
3559BEGIN { 3612BEGIN {
3560 our %SIGWATCHER = (); 3613 our %SIGWATCHER = ();
3561 for my $signal (qw(INT HUP TERM)) { 3614 for my $signal (qw(INT HUP TERM)) {
3562 $SIGWATCHER{$signal} = EV::signal $signal, sub { 3615 $SIGWATCHER{$signal} = AE::signal $signal, sub {
3563 cf::cleanup "SIG$signal"; 3616 cf::cleanup "SIG$signal";
3564 }; 3617 };
3565 } 3618 }
3566} 3619}
3567 3620
3568sub write_runtime_sync { 3621sub write_runtime_sync {
3622 my $t0 = AE::time;
3623
3569 # first touch the runtime file to show we are still running: 3624 # first touch the runtime file to show we are still running:
3570 # the fsync below can take a very very long time. 3625 # the fsync below can take a very very long time.
3571 3626
3572 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; 3627 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef;
3573 3628
3574 my $guard = cf::lock_acquire "write_runtime"; 3629 my $guard = cf::lock_acquire "write_runtime";
3575 3630
3576 my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644 3631 my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT | O_TRUNC, 0644
3577 or return; 3632 or return;
3578 3633
3579 my $value = $cf::RUNTIME + 90 + 10; 3634 my $value = $cf::RUNTIME + 90 + 10;
3580 # 10 is the runtime save interval, for a monotonic clock 3635 # 10 is the runtime save interval, for a monotonic clock
3581 # 60 allows for the watchdog to kill the server. 3636 # 60 allows for the watchdog to kill the server.
3594 or return; 3649 or return;
3595 3650
3596 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE 3651 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE
3597 and return; 3652 and return;
3598 3653
3599 warn "runtime file written.\n"; 3654 warn sprintf "runtime file written (%gs).\n", AE::time - $t0;
3600 3655
3601 1 3656 1
3602} 3657}
3603 3658
3604our $uuid_lock; 3659our $uuid_lock;
3742 return; 3797 return;
3743 } 3798 }
3744 3799
3745 return if $RELOAD++; 3800 return if $RELOAD++;
3746 3801
3747 my $t1 = EV::time; 3802 my $t1 = AE::time;
3748 3803
3749 while ($RELOAD) { 3804 while ($RELOAD) {
3750 warn "reloading..."; 3805 warn "reloading...";
3751 3806
3752 warn "entering sync_job"; 3807 warn "entering sync_job";
3855 3910
3856 warn "reloaded"; 3911 warn "reloaded";
3857 --$RELOAD; 3912 --$RELOAD;
3858 } 3913 }
3859 3914
3860 $t1 = EV::time - $t1; 3915 $t1 = AE::time - $t1;
3861 warn "reload completed in ${t1}s\n"; 3916 warn "reload completed in ${t1}s\n";
3862}; 3917};
3863 3918
3864our $RELOAD_WATCHER; # used only during reload 3919our $RELOAD_WATCHER; # used only during reload
3865 3920
3868 # coro crashes during coro_state_free->destroy here. 3923 # coro crashes during coro_state_free->destroy here.
3869 3924
3870 $RELOAD_WATCHER ||= cf::async { 3925 $RELOAD_WATCHER ||= cf::async {
3871 Coro::AIO::aio_wait cache_extensions; 3926 Coro::AIO::aio_wait cache_extensions;
3872 3927
3873 $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { 3928 $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub {
3874 do_reload_perl; 3929 do_reload_perl;
3875 undef $RELOAD_WATCHER; 3930 undef $RELOAD_WATCHER;
3876 }; 3931 };
3877 }; 3932 };
3878} 3933}
3919 return; 3974 return;
3920 } 3975 }
3921 3976
3922 cf::server_tick; # one server iteration 3977 cf::server_tick; # one server iteration
3923 3978
3924 #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#
3925 3980
3926 if ($NOW >= $NEXT_RUNTIME_WRITE) { 3981 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3927 $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.;
3928 Coro::async_pool { 3983 Coro::async_pool {
3929 $Coro::current->{desc} = "runtime saver"; 3984 $Coro::current->{desc} = "runtime saver";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines