--- deliantra/Deliantra-Client/DC/DB.pm 2007/07/13 05:31:31 1.10 +++ deliantra/Deliantra-Client/DC/DB.pm 2007/07/29 19:32:29 1.14 @@ -18,16 +18,17 @@ use utf8; use Carp (); -use AnyEvent (); use Storable (); use Config; +use Event (); use CFPlus; our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; -sub path_of($) { - "$DB_HOME/res-data-$_[0]" +sub path_of_res($) { + utf8::downgrade $_[0]; # bug in unpack "H*" + "$DB_HOME/res-data-" . unpack "H*", $_[0] } sub sync { @@ -56,6 +57,10 @@ CFPlus::DB::Server::req (write_file => @_); } +sub prefetch_file($$$) { + CFPlus::DB::Server::req (prefetch_file => @_); +} + our $tilemap; sub get_tile_id_sync($) { @@ -122,8 +127,6 @@ } } -our $SYNC_INTERVAL = 60; - our %CB; our $FH; our $ID = "aaa0"; @@ -132,12 +135,17 @@ our $write_buf; our $read_buf; +our $SYNC = Event->idle (min => 5, max => 60, parked => 1, cb => sub { + CFPlus::DB::Server::req (sync => sub { }); + $_[0]->w->stop; +}); + sub fh_write { my $len = syswrite $FH, $write_buf; substr $write_buf, 0, $len, ""; - undef $fh_w_watcher + $fh_w_watcher->stop unless length $write_buf; } @@ -183,12 +191,8 @@ $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; $CB{$id} = $cb; - $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); -} - -sub sync_tick { - req "sync", sub { }; - $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick); + $fh_w_watcher->start; + $SYNC->start; } sub do_sync { @@ -273,7 +277,7 @@ utf8::downgrade $file; utf8::downgrade $data; - open my $fh, ">:raw", CFPlus::DB::path_of $file + open my $fh, ">:raw", $file or return; print $fh $data; close $fh; @@ -281,6 +285,17 @@ 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; @@ -334,9 +349,9 @@ $CB{die} = sub { die shift }; - $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); - - sync_tick; + $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read); + $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write); + $SYNC->start; } sub stop {