--- deliantra/Deliantra-Client/DC/DB.pm 2007/07/05 10:15:17 1.8 +++ deliantra/Deliantra-Client/DC/DB.pm 2007/07/29 04:14:45 1.13 @@ -1,6 +1,6 @@ =head1 NAME -CFPlus::DB - async. database access for cfplus +CFPlus::DB - async. database and filesystem access for cfplus =head1 SYNOPSIS @@ -20,15 +20,27 @@ use Carp (); use AnyEvent (); use Storable (); +use Config; use CFPlus; +our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; + +sub path_of_res($) { + utf8::downgrade $_[0]; # bug in unpack "H*" + "$DB_HOME/res-data-" . unpack "H*", $_[0] +} + sub sync { # for debugging #CFPlus::DB::Server::req (sync => sub { }); CFPlus::DB::Server::sync (); } +sub exists($$$) { + CFPlus::DB::Server::req (exists => @_); +} + sub get($$$) { CFPlus::DB::Server::req (get => @_); } @@ -37,6 +49,18 @@ CFPlus::DB::Server::req (put => @_); } +sub unlink($$) { + CFPlus::DB::Server::req (unlink => @_); +} + +sub write_file($$$) { + CFPlus::DB::Server::req (write_file => @_); +} + +sub prefetch_file($$$) { + CFPlus::DB::Server::req (prefetch_file => @_); +} + our $tilemap; sub get_tile_id_sync($) { @@ -62,9 +86,7 @@ use Fcntl; use BerkeleyDB; -use Config; -our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; our $DB_ENV; our $DB_STATE; our %DB_TABLE; @@ -179,6 +201,16 @@ () } +sub do_exists { + my ($db, $key) = @_; + + utf8::downgrade $key; + my $data; + (table $db)->db_get ($key, $data) == 0 + ? length $data + : () +} + sub do_get { my ($db, $key) = @_; @@ -237,6 +269,34 @@ die "maximum number of transaction retries reached - database problems?"; } +sub do_unlink { + unlink $_[0]; +} + +sub do_write_file { + my ($file, $data) = @_; + + utf8::downgrade $file; + utf8::downgrade $data; + open my $fh, ">:raw", $file + or return; + print $fh $data; + close $fh; + + 1 +} + +sub do_prefetch_file { + my ($file, $size) = @_; + + utf8::downgrade $file; + open my $fh, "<:raw", $file + or return; + sysread $fh, my $buf, $size; + + 1 +} + sub run { ($FH, my $fh) = CFPlus::socketpipe; @@ -290,7 +350,7 @@ $CB{die} = sub { die shift }; - $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); + $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', nice => 1, cb => \&fh_read); sync_tick; }