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.71 by root, Sun Oct 1 10:59:30 2006 UTC vs.
Revision 1.78 by root, Tue Nov 7 00:15:27 2006 UTC

5use Storable; 5use Storable;
6use Opcode; 6use Opcode;
7use Safe; 7use Safe;
8use Safe::Hole; 8use Safe::Hole;
9 9
10use IO::AIO ();
11use YAML::Syck ();
10use Time::HiRes; 12use Time::HiRes;
11use Event; 13use Event;
12$Event::Eval = 1; # no idea why this is required, but it is 14$Event::Eval = 1; # no idea why this is required, but it is
13 15
16# work around bug in YAML::Syck - bad news for perl6, will it be as broken wrt. unicode?
17$YAML::Syck::ImplicitUnicode = 1;
18
14use strict; 19use strict;
15 20
16_init_vars; 21_init_vars;
17 22
18our %COMMAND = (); 23our %COMMAND = ();
22our $TICK = MAX_TIME * 1e-6; 27our $TICK = MAX_TIME * 1e-6;
23our $TICK_WATCHER; 28our $TICK_WATCHER;
24our $NEXT_TICK; 29our $NEXT_TICK;
25 30
26our %CFG; 31our %CFG;
32
33our $uptime;
34
35$uptime ||= time;
27 36
28############################################################################# 37#############################################################################
29 38
30=head2 GLOBAL VARIABLES 39=head2 GLOBAL VARIABLES
31 40
766sub cf::player::exists($) { 775sub cf::player::exists($) {
767 cf::player::find $_[0] 776 cf::player::find $_[0]
768 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; 777 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
769} 778}
770 779
771=item $player->reply ($npc, $msg[, $flags]) 780=item $object->reply ($npc, $msg[, $flags])
772 781
773Sends a message to the player, as if the npc C<$npc> replied. C<$npc> 782Sends a message to the player, as if the npc C<$npc> replied. C<$npc>
774can be C<undef>. Does the right thing when the player is currently in a 783can be C<undef>. Does the right thing when the player is currently in a
775dialogue with the given NPC character. 784dialogue with the given NPC character.
776 785
961 970
962Immediately write the database to disk I<if it is dirty>. 971Immediately write the database to disk I<if it is dirty>.
963 972
964=cut 973=cut
965 974
975our $DB;
976
966{ 977{
967 my $db;
968 my $path = cf::localdir . "/database.pst"; 978 my $path = cf::localdir . "/database.pst";
969 979
970 sub db_load() { 980 sub db_load() {
971 warn "loading database $path\n";#d# remove later 981 warn "loading database $path\n";#d# remove later
972 $db = stat $path ? Storable::retrieve $path : { }; 982 $DB = stat $path ? Storable::retrieve $path : { };
973 } 983 }
974 984
975 my $pid; 985 my $pid;
976 986
977 sub db_save() { 987 sub db_save() {
978 warn "saving database $path\n";#d# remove later 988 warn "saving database $path\n";#d# remove later
979 waitpid $pid, 0 if $pid; 989 waitpid $pid, 0 if $pid;
980 if (0 == ($pid = fork)) { 990 if (0 == ($pid = fork)) {
981 $db->{_meta}{version} = 1; 991 $DB->{_meta}{version} = 1;
982 Storable::nstore $db, "$path~"; 992 Storable::nstore $DB, "$path~";
983 rename "$path~", $path; 993 rename "$path~", $path;
984 cf::_exit 0 if defined $pid; 994 cf::_exit 0 if defined $pid;
985 } 995 }
986 } 996 }
987 997
1001 $idle->start; 1011 $idle->start;
1002 } 1012 }
1003 1013
1004 sub db_get($;$) { 1014 sub db_get($;$) {
1005 @_ >= 2 1015 @_ >= 2
1006 ? $db->{$_[0]}{$_[1]} 1016 ? $DB->{$_[0]}{$_[1]}
1007 : ($db->{$_[0]} ||= { }) 1017 : ($DB->{$_[0]} ||= { })
1008 } 1018 }
1009 1019
1010 sub db_put($$;$) { 1020 sub db_put($$;$) {
1011 if (@_ >= 3) { 1021 if (@_ >= 3) {
1012 $db->{$_[0]}{$_[1]} = $_[2]; 1022 $DB->{$_[0]}{$_[1]} = $_[2];
1013 } else { 1023 } else {
1014 $db->{$_[0]} = $_[1]; 1024 $DB->{$_[0]} = $_[1];
1015 } 1025 }
1016 db_dirty; 1026 db_dirty;
1017 } 1027 }
1018 1028
1019 attach_global 1029 attach_global
1025} 1035}
1026 1036
1027############################################################################# 1037#############################################################################
1028# the server's main() 1038# the server's main()
1029 1039
1040sub cfg_load {
1041 open my $fh, "<:utf8", cf::confdir . "/config"
1042 or return;
1043
1044 local $/;
1045 *CFG = YAML::Syck::Load <$fh>;
1046}
1047
1030sub main { 1048sub main {
1049 cfg_load;
1031 db_load; 1050 db_load;
1032 load_extensions; 1051 load_extensions;
1033 Event::loop; 1052 Event::loop;
1034} 1053}
1035 1054
1087 1106
1088 # reload cf.pm 1107 # reload cf.pm
1089 $msg->("reloading cf.pm"); 1108 $msg->("reloading cf.pm");
1090 require cf; 1109 require cf;
1091 1110
1092 # load database again 1111 # load config and database again
1112 cf::cfg_load;
1093 cf::db_load; 1113 cf::db_load;
1094 1114
1095 # load extensions 1115 # load extensions
1096 $msg->("load extensions"); 1116 $msg->("load extensions");
1097 cf::load_extensions; 1117 cf::load_extensions;
1126register "<global>", __PACKAGE__; 1146register "<global>", __PACKAGE__;
1127 1147
1128unshift @INC, $LIBDIR; 1148unshift @INC, $LIBDIR;
1129 1149
1130$TICK_WATCHER = Event->timer ( 1150$TICK_WATCHER = Event->timer (
1131 prio => 1, 1151 prio => 1,
1152 async => 1,
1132 at => $NEXT_TICK || 1, 1153 at => $NEXT_TICK || 1,
1133 cb => sub { 1154 cb => sub {
1134 cf::server_tick; # one server iteration 1155 cf::server_tick; # one server iteration
1135 1156
1136 my $NOW = Event::time; 1157 my $NOW = Event::time;
1137 $NEXT_TICK += $TICK; 1158 $NEXT_TICK += $TICK;
1138 1159
1139 # if we are delayed by four ticks, skip them all 1160 # if we are delayed by four ticks or more, skip them all
1140 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; 1161 $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4;
1141 1162
1142 $TICK_WATCHER->at ($NEXT_TICK); 1163 $TICK_WATCHER->at ($NEXT_TICK);
1143 $TICK_WATCHER->start; 1164 $TICK_WATCHER->start;
1144 }, 1165 },
1145); 1166);
1146 1167
1168eval { IO::AIO::max_poll_time $TICK * 0.2 }; #d# remove eval after restart
1169
1170Event->io (fd => IO::AIO::poll_fileno,
1171 poll => 'r',
1172 prio => 5,
1173 cb => \&IO::AIO::poll_cb);
1174
11471 11751
1148 1176

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines