… | |
… | |
16 | |
16 | |
17 | use strict; |
17 | use strict; |
18 | use utf8; |
18 | use utf8; |
19 | |
19 | |
20 | use Carp (); |
20 | use Carp (); |
21 | use AnyEvent (); |
|
|
22 | use Storable (); |
21 | use Storable (); |
23 | use Config; |
22 | use Config; |
|
|
23 | use Event (); |
24 | |
24 | |
25 | use CFPlus; |
25 | use CFPlus; |
26 | |
26 | |
27 | our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; |
27 | our $DB_HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version-$Config{archname}"; |
28 | |
28 | |
29 | sub path_of($) { |
29 | sub path_of_res($) { |
|
|
30 | utf8::downgrade $_[0]; # bug in unpack "H*" |
30 | "$DB_HOME/res-data-$_[0]" |
31 | "$DB_HOME/res-data-" . unpack "H*", $_[0] |
31 | } |
32 | } |
32 | |
33 | |
33 | sub sync { |
34 | sub sync { |
34 | # for debugging |
35 | # for debugging |
35 | #CFPlus::DB::Server::req (sync => sub { }); |
36 | #CFPlus::DB::Server::req (sync => sub { }); |
… | |
… | |
52 | CFPlus::DB::Server::req (unlink => @_); |
53 | CFPlus::DB::Server::req (unlink => @_); |
53 | } |
54 | } |
54 | |
55 | |
55 | sub write_file($$$) { |
56 | sub write_file($$$) { |
56 | CFPlus::DB::Server::req (write_file => @_); |
57 | CFPlus::DB::Server::req (write_file => @_); |
|
|
58 | } |
|
|
59 | |
|
|
60 | sub prefetch_file($$$) { |
|
|
61 | CFPlus::DB::Server::req (prefetch_file => @_); |
57 | } |
62 | } |
58 | |
63 | |
59 | our $tilemap; |
64 | our $tilemap; |
60 | |
65 | |
61 | sub get_tile_id_sync($) { |
66 | sub get_tile_id_sync($) { |
… | |
… | |
120 | -Flags => DB_CREATE | DB_UPGRADE, |
125 | -Flags => DB_CREATE | DB_UPGRADE, |
121 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
126 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
122 | } |
127 | } |
123 | } |
128 | } |
124 | |
129 | |
125 | our $SYNC_INTERVAL = 60; |
|
|
126 | |
|
|
127 | our %CB; |
130 | our %CB; |
128 | our $FH; |
131 | our $FH; |
129 | our $ID = "aaa0"; |
132 | our $ID = "aaa0"; |
130 | our ($fh_r_watcher, $fh_w_watcher); |
133 | our ($fh_r_watcher, $fh_w_watcher); |
131 | our $sync_timer; |
134 | our $sync_timer; |
132 | our $write_buf; |
135 | our $write_buf; |
133 | our $read_buf; |
136 | our $read_buf; |
134 | |
137 | |
|
|
138 | our $SYNC = Event->idle (min => 5, max => 60, parked => 1, cb => sub { |
|
|
139 | CFPlus::DB::Server::req (sync => sub { }); |
|
|
140 | $_[0]->w->stop; |
|
|
141 | }); |
|
|
142 | |
135 | sub fh_write { |
143 | sub fh_write { |
136 | my $len = syswrite $FH, $write_buf; |
144 | my $len = syswrite $FH, $write_buf; |
137 | |
145 | |
138 | substr $write_buf, 0, $len, ""; |
146 | substr $write_buf, 0, $len, ""; |
139 | |
147 | |
140 | undef $fh_w_watcher |
148 | $fh_w_watcher->stop |
141 | unless length $write_buf; |
149 | unless length $write_buf; |
142 | } |
150 | } |
143 | |
151 | |
144 | sub fh_read { |
152 | sub fh_read { |
145 | my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
153 | my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
… | |
… | |
181 | |
189 | |
182 | my $id = ++$ID; |
190 | my $id = ++$ID; |
183 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
191 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
184 | $CB{$id} = $cb; |
192 | $CB{$id} = $cb; |
185 | |
193 | |
186 | $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); |
194 | $fh_w_watcher->start; |
187 | } |
195 | $SYNC->start; |
188 | |
|
|
189 | sub sync_tick { |
|
|
190 | req "sync", sub { }; |
|
|
191 | $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick); |
|
|
192 | } |
196 | } |
193 | |
197 | |
194 | sub do_sync { |
198 | sub do_sync { |
195 | $DB_ENV->txn_checkpoint (0, 0, 0); |
199 | $DB_ENV->txn_checkpoint (0, 0, 0); |
196 | () |
200 | () |
… | |
… | |
271 | sub do_write_file { |
275 | sub do_write_file { |
272 | my ($file, $data) = @_; |
276 | my ($file, $data) = @_; |
273 | |
277 | |
274 | utf8::downgrade $file; |
278 | utf8::downgrade $file; |
275 | utf8::downgrade $data; |
279 | utf8::downgrade $data; |
276 | open my $fh, ">:raw", CFPlus::DB::path_of $file |
280 | open my $fh, ">:raw", $file |
277 | or return; |
281 | or return; |
278 | print $fh $data; |
282 | print $fh $data; |
279 | close $fh; |
283 | close $fh; |
|
|
284 | |
|
|
285 | 1 |
|
|
286 | } |
|
|
287 | |
|
|
288 | sub do_prefetch_file { |
|
|
289 | my ($file, $size) = @_; |
|
|
290 | |
|
|
291 | utf8::downgrade $file; |
|
|
292 | open my $fh, "<:raw", $file |
|
|
293 | or return; |
|
|
294 | sysread $fh, my $buf, $size; |
280 | |
295 | |
281 | 1 |
296 | 1 |
282 | } |
297 | } |
283 | |
298 | |
284 | sub run { |
299 | sub run { |
… | |
… | |
332 | close $fh; |
347 | close $fh; |
333 | CFPlus::fh_nonblocking $FH, 1; |
348 | CFPlus::fh_nonblocking $FH, 1; |
334 | |
349 | |
335 | $CB{die} = sub { die shift }; |
350 | $CB{die} = sub { die shift }; |
336 | |
351 | |
337 | $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); |
352 | $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read); |
338 | |
353 | $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write); |
339 | sync_tick; |
354 | $SYNC->start; |
340 | } |
355 | } |
341 | |
356 | |
342 | sub stop { |
357 | sub stop { |
343 | close $FH; |
358 | close $FH; |
344 | } |
359 | } |