… | |
… | |
970 | |
970 | |
971 | Immediately write the database to disk I<if it is dirty>. |
971 | Immediately write the database to disk I<if it is dirty>. |
972 | |
972 | |
973 | =cut |
973 | =cut |
974 | |
974 | |
|
|
975 | our $DB; |
|
|
976 | |
975 | { |
977 | { |
976 | my $db; |
|
|
977 | my $path = cf::localdir . "/database.pst"; |
978 | my $path = cf::localdir . "/database.pst"; |
978 | |
979 | |
979 | sub db_load() { |
980 | sub db_load() { |
980 | warn "loading database $path\n";#d# remove later |
981 | warn "loading database $path\n";#d# remove later |
981 | $db = stat $path ? Storable::retrieve $path : { }; |
982 | $DB = stat $path ? Storable::retrieve $path : { }; |
982 | } |
983 | } |
983 | |
984 | |
984 | my $pid; |
985 | my $pid; |
985 | |
986 | |
986 | sub db_save() { |
987 | sub db_save() { |
987 | warn "saving database $path\n";#d# remove later |
988 | warn "saving database $path\n";#d# remove later |
988 | waitpid $pid, 0 if $pid; |
989 | waitpid $pid, 0 if $pid; |
989 | if (0 == ($pid = fork)) { |
990 | if (0 == ($pid = fork)) { |
990 | $db->{_meta}{version} = 1; |
991 | $DB->{_meta}{version} = 1; |
991 | Storable::nstore $db, "$path~"; |
992 | Storable::nstore $DB, "$path~"; |
992 | rename "$path~", $path; |
993 | rename "$path~", $path; |
993 | cf::_exit 0 if defined $pid; |
994 | cf::_exit 0 if defined $pid; |
994 | } |
995 | } |
995 | } |
996 | } |
996 | |
997 | |
… | |
… | |
1010 | $idle->start; |
1011 | $idle->start; |
1011 | } |
1012 | } |
1012 | |
1013 | |
1013 | sub db_get($;$) { |
1014 | sub db_get($;$) { |
1014 | @_ >= 2 |
1015 | @_ >= 2 |
1015 | ? $db->{$_[0]}{$_[1]} |
1016 | ? $DB->{$_[0]}{$_[1]} |
1016 | : ($db->{$_[0]} ||= { }) |
1017 | : ($DB->{$_[0]} ||= { }) |
1017 | } |
1018 | } |
1018 | |
1019 | |
1019 | sub db_put($$;$) { |
1020 | sub db_put($$;$) { |
1020 | if (@_ >= 3) { |
1021 | if (@_ >= 3) { |
1021 | $db->{$_[0]}{$_[1]} = $_[2]; |
1022 | $DB->{$_[0]}{$_[1]} = $_[2]; |
1022 | } else { |
1023 | } else { |
1023 | $db->{$_[0]} = $_[1]; |
1024 | $DB->{$_[0]} = $_[1]; |
1024 | } |
1025 | } |
1025 | db_dirty; |
1026 | db_dirty; |
1026 | } |
1027 | } |
1027 | |
1028 | |
1028 | attach_global |
1029 | attach_global |
… | |
… | |
1154 | cf::server_tick; # one server iteration |
1155 | cf::server_tick; # one server iteration |
1155 | |
1156 | |
1156 | my $NOW = Event::time; |
1157 | my $NOW = Event::time; |
1157 | $NEXT_TICK += $TICK; |
1158 | $NEXT_TICK += $TICK; |
1158 | |
1159 | |
1159 | # if we are delayed by four ticks, skip them all |
1160 | # if we are delayed by four ticks or more, skip them all |
1160 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1161 | $NEXT_TICK = $NOW if $NOW >= $NEXT_TICK + $TICK * 4; |
1161 | |
1162 | |
1162 | $TICK_WATCHER->at ($NEXT_TICK); |
1163 | $TICK_WATCHER->at ($NEXT_TICK); |
1163 | $TICK_WATCHER->start; |
1164 | $TICK_WATCHER->start; |
1164 | }, |
1165 | }, |
1165 | ); |
1166 | ); |
1166 | |
1167 | |
1167 | IO::AIO::max_poll_time $TICK * 0.2; |
1168 | eval { IO::AIO::max_poll_time $TICK * 0.2 }; #d# remove eval after restart |
1168 | |
1169 | |
1169 | Event->io (fd => IO::AIO::poll_fileno, |
1170 | Event->io (fd => IO::AIO::poll_fileno, |
1170 | poll => 'r', |
1171 | poll => 'r', |
1171 | prio => 5, |
1172 | prio => 5, |
1172 | cb => \&IO::AIO::poll_cb); |
1173 | cb => \&IO::AIO::poll_cb); |