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.502 by root, Sat Jan 23 20:24:50 2010 UTC vs.
Revision 1.515 by root, Thu Apr 15 04:56:47 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;
3122 3164
3123 $coro 3165 $coro
3124} 3166}
3125 3167
3126cf::client->attach ( 3168cf::client->attach (
3127 on_destroy => sub { 3169 on_client_destroy => sub {
3128 my ($ns) = @_; 3170 my ($ns) = @_;
3129 3171
3130 $_->cancel for values %{ (delete $ns->{_coro}) || {} }; 3172 $_->cancel for values %{ (delete $ns->{_coro}) || {} };
3131 }, 3173 },
3132); 3174);
3501 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3543 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3502 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.";
3503 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3545 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3504 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3546 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3505 3547
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 3548 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3512 3549
3513 # we must not ever block the main coroutine 3550 # we must not ever block the main coroutine
3514 local $Coro::idle = sub { 3551 local $Coro::idle = sub {
3515 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#
3520 }; 3557 };
3521 3558
3522 evthread_start IO::AIO::poll_fileno; 3559 evthread_start IO::AIO::poll_fileno;
3523 3560
3524 cf::sync_job { 3561 cf::sync_job {
3562 cf::init_experience;
3563 cf::init_anim;
3564 cf::init_attackmess;
3565 cf::init_dynamic;
3566
3525 cf::load_settings; 3567 cf::load_settings;
3526 cf::load_materials; 3568 cf::load_materials;
3527 3569
3528 reload_resources; 3570 reload_resources;
3529 reload_config; 3571 reload_config;
3530 db_init; 3572 db_init;
3531 3573
3532 cf::init_uuid; 3574 cf::init_uuid;
3533 cf::init_signals; 3575 cf::init_signals;
3534 cf::init_commands;
3535 cf::init_skills; 3576 cf::init_skills;
3536 3577
3537 cf::init_beforeplay; 3578 cf::init_beforeplay;
3538 3579
3539 atomic; 3580 atomic;
3557 3598
3558# install some emergency cleanup handlers 3599# install some emergency cleanup handlers
3559BEGIN { 3600BEGIN {
3560 our %SIGWATCHER = (); 3601 our %SIGWATCHER = ();
3561 for my $signal (qw(INT HUP TERM)) { 3602 for my $signal (qw(INT HUP TERM)) {
3562 $SIGWATCHER{$signal} = EV::signal $signal, sub { 3603 $SIGWATCHER{$signal} = AE::signal $signal, sub {
3563 cf::cleanup "SIG$signal"; 3604 cf::cleanup "SIG$signal";
3564 }; 3605 };
3565 } 3606 }
3566} 3607}
3567 3608
3568sub write_runtime_sync { 3609sub write_runtime_sync {
3610 my $t0 = AE::time;
3611
3569 # first touch the runtime file to show we are still running: 3612 # first touch the runtime file to show we are still running:
3570 # the fsync below can take a very very long time. 3613 # the fsync below can take a very very long time.
3571 3614
3572 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef; 3615 IO::AIO::aio_utime $RUNTIMEFILE, undef, undef;
3573 3616
3574 my $guard = cf::lock_acquire "write_runtime"; 3617 my $guard = cf::lock_acquire "write_runtime";
3575 3618
3576 my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT, 0644 3619 my $fh = aio_open "$RUNTIMEFILE~", O_WRONLY | O_CREAT | O_TRUNC, 0644
3577 or return; 3620 or return;
3578 3621
3579 my $value = $cf::RUNTIME + 90 + 10; 3622 my $value = $cf::RUNTIME + 90 + 10;
3580 # 10 is the runtime save interval, for a monotonic clock 3623 # 10 is the runtime save interval, for a monotonic clock
3581 # 60 allows for the watchdog to kill the server. 3624 # 60 allows for the watchdog to kill the server.
3594 or return; 3637 or return;
3595 3638
3596 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE 3639 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE
3597 and return; 3640 and return;
3598 3641
3599 warn "runtime file written.\n"; 3642 warn sprintf "runtime file written (%gs).\n", AE::time - $t0;
3600 3643
3601 1 3644 1
3602} 3645}
3603 3646
3604our $uuid_lock; 3647our $uuid_lock;
3742 return; 3785 return;
3743 } 3786 }
3744 3787
3745 return if $RELOAD++; 3788 return if $RELOAD++;
3746 3789
3747 my $t1 = EV::time; 3790 my $t1 = AE::time;
3748 3791
3749 while ($RELOAD) { 3792 while ($RELOAD) {
3750 warn "reloading..."; 3793 warn "reloading...";
3751 3794
3752 warn "entering sync_job"; 3795 warn "entering sync_job";
3855 3898
3856 warn "reloaded"; 3899 warn "reloaded";
3857 --$RELOAD; 3900 --$RELOAD;
3858 } 3901 }
3859 3902
3860 $t1 = EV::time - $t1; 3903 $t1 = AE::time - $t1;
3861 warn "reload completed in ${t1}s\n"; 3904 warn "reload completed in ${t1}s\n";
3862}; 3905};
3863 3906
3864our $RELOAD_WATCHER; # used only during reload 3907our $RELOAD_WATCHER; # used only during reload
3865 3908
3868 # coro crashes during coro_state_free->destroy here. 3911 # coro crashes during coro_state_free->destroy here.
3869 3912
3870 $RELOAD_WATCHER ||= cf::async { 3913 $RELOAD_WATCHER ||= cf::async {
3871 Coro::AIO::aio_wait cache_extensions; 3914 Coro::AIO::aio_wait cache_extensions;
3872 3915
3873 $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { 3916 $RELOAD_WATCHER = AE::timer $TICK * 1.5, 0, sub {
3874 do_reload_perl; 3917 do_reload_perl;
3875 undef $RELOAD_WATCHER; 3918 undef $RELOAD_WATCHER;
3876 }; 3919 };
3877 }; 3920 };
3878} 3921}
3919 return; 3962 return;
3920 } 3963 }
3921 3964
3922 cf::server_tick; # one server iteration 3965 cf::server_tick; # one server iteration
3923 3966
3924 #for(1..3e6){} EV::now_update; $NOW=EV::now; # generate load #d# 3967 #for(1..3e6){} AE::now_update; $NOW=AE::now; # generate load #d#
3925 3968
3926 if ($NOW >= $NEXT_RUNTIME_WRITE) { 3969 if ($NOW >= $NEXT_RUNTIME_WRITE) {
3927 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 3970 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
3928 Coro::async_pool { 3971 Coro::async_pool {
3929 $Coro::current->{desc} = "runtime saver"; 3972 $Coro::current->{desc} = "runtime saver";
3952} 3995}
3953 3996
3954{ 3997{
3955 # configure BDB 3998 # configure BDB
3956 3999
3957 BDB::min_parallel 8; 4000 BDB::min_parallel 16;
3958 BDB::max_poll_reqs $TICK * 0.1; 4001 BDB::max_poll_reqs $TICK * 0.1;
3959 $AnyEvent::BDB::WATCHER->priority (1); 4002 $AnyEvent::BDB::WATCHER->priority (1);
3960 4003
3961 unless ($DB_ENV) { 4004 unless ($DB_ENV) {
3962 $DB_ENV = BDB::db_env_create; 4005 $DB_ENV = BDB::db_env_create;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines