… | |
… | |
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_res($) { |
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 { }); |
… | |
… | |
56 | CFPlus::DB::Server::req (write_file => @_); |
57 | CFPlus::DB::Server::req (write_file => @_); |
57 | } |
58 | } |
58 | |
59 | |
59 | sub prefetch_file($$$) { |
60 | sub prefetch_file($$$) { |
60 | CFPlus::DB::Server::req (prefetch_file => @_); |
61 | CFPlus::DB::Server::req (prefetch_file => @_); |
|
|
62 | } |
|
|
63 | |
|
|
64 | sub logprint($$$) { |
|
|
65 | CFPlus::DB::Server::req (logprint => @_); |
61 | } |
66 | } |
62 | |
67 | |
63 | our $tilemap; |
68 | our $tilemap; |
64 | |
69 | |
65 | sub get_tile_id_sync($) { |
70 | sub get_tile_id_sync($) { |
… | |
… | |
124 | -Flags => DB_CREATE | DB_UPGRADE, |
129 | -Flags => DB_CREATE | DB_UPGRADE, |
125 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
130 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
126 | } |
131 | } |
127 | } |
132 | } |
128 | |
133 | |
129 | our $SYNC_INTERVAL = 60; |
|
|
130 | |
|
|
131 | our %CB; |
134 | our %CB; |
132 | our $FH; |
135 | our $FH; |
133 | our $ID = "aaa0"; |
136 | our $ID = "aaa0"; |
134 | our ($fh_r_watcher, $fh_w_watcher); |
137 | our ($fh_r_watcher, $fh_w_watcher); |
135 | our $sync_timer; |
138 | our $sync_timer; |
136 | our $write_buf; |
139 | our $write_buf; |
137 | our $read_buf; |
140 | our $read_buf; |
138 | |
141 | |
|
|
142 | our $SYNC = Event->idle (min => 5, max => 60, parked => 1, cb => sub { |
|
|
143 | CFPlus::DB::Server::req (sync => sub { }); |
|
|
144 | $_[0]->w->stop; |
|
|
145 | }); |
|
|
146 | |
139 | sub fh_write { |
147 | sub fh_write { |
140 | my $len = syswrite $FH, $write_buf; |
148 | my $len = syswrite $FH, $write_buf; |
141 | |
149 | |
142 | substr $write_buf, 0, $len, ""; |
150 | substr $write_buf, 0, $len, ""; |
143 | |
151 | |
144 | undef $fh_w_watcher |
152 | $fh_w_watcher->stop |
145 | unless length $write_buf; |
153 | unless length $write_buf; |
146 | } |
154 | } |
147 | |
155 | |
148 | sub fh_read { |
156 | sub fh_read { |
149 | my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
157 | my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
… | |
… | |
185 | |
193 | |
186 | my $id = ++$ID; |
194 | my $id = ++$ID; |
187 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
195 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
188 | $CB{$id} = $cb; |
196 | $CB{$id} = $cb; |
189 | |
197 | |
190 | $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); |
198 | $fh_w_watcher->start; |
191 | } |
199 | $SYNC->start; |
192 | |
|
|
193 | sub sync_tick { |
|
|
194 | req "sync", sub { }; |
|
|
195 | $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick); |
|
|
196 | } |
200 | } |
197 | |
201 | |
198 | sub do_sync { |
202 | sub do_sync { |
199 | $DB_ENV->txn_checkpoint (0, 0, 0); |
203 | $DB_ENV->txn_checkpoint (0, 0, 0); |
200 | () |
204 | () |
… | |
… | |
294 | sysread $fh, my $buf, $size; |
298 | sysread $fh, my $buf, $size; |
295 | |
299 | |
296 | 1 |
300 | 1 |
297 | } |
301 | } |
298 | |
302 | |
|
|
303 | our %LOG_FH; |
|
|
304 | |
|
|
305 | sub do_logprint { |
|
|
306 | my ($path, $line) = @_; |
|
|
307 | |
|
|
308 | $LOG_FH{$path} ||= do { |
|
|
309 | open my $fh, ">>:utf8", $path |
|
|
310 | or warn "Couldn't open logfile $path: $!"; |
|
|
311 | |
|
|
312 | $fh->autoflush (1); |
|
|
313 | |
|
|
314 | $fh |
|
|
315 | }; |
|
|
316 | |
|
|
317 | my ($sec, $min, $hour, $mday, $mon, $year) = localtime time; |
|
|
318 | |
|
|
319 | my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", |
|
|
320 | $year + 1900, $mon + 1, $mday, $hour, $min, $sec; |
|
|
321 | |
|
|
322 | print { $LOG_FH{$path} } "$ts $line\n" |
|
|
323 | } |
|
|
324 | |
299 | sub run { |
325 | sub run { |
300 | ($FH, my $fh) = CFPlus::socketpipe; |
326 | ($FH, my $fh) = CFPlus::socketpipe; |
301 | |
327 | |
302 | my $oldfh = select $FH; $| = 1; select $oldfh; |
328 | my $oldfh = select $FH; $| = 1; select $oldfh; |
303 | my $oldfh = select $fh; $| = 1; select $oldfh; |
329 | my $oldfh = select $fh; $| = 1; select $oldfh; |
304 | |
330 | |
305 | my $pid = fork; |
331 | my $pid = fork; |
306 | |
332 | |
307 | if (defined $pid && !$pid) { |
333 | if (defined $pid && !$pid) { |
|
|
334 | local $SIG{QUIT}; |
308 | local $SIG{__DIE__}; |
335 | local $SIG{__DIE__}; |
|
|
336 | local $SIG{__WARN__}; |
309 | eval { |
337 | eval { |
310 | close $FH; |
338 | close $FH; |
311 | |
339 | |
312 | unless (eval { open_db }) { |
340 | unless (eval { open_db }) { |
313 | eval { File::Path::rmtree $DB_HOME }; |
341 | eval { File::Path::rmtree $DB_HOME }; |
… | |
… | |
347 | close $fh; |
375 | close $fh; |
348 | CFPlus::fh_nonblocking $FH, 1; |
376 | CFPlus::fh_nonblocking $FH, 1; |
349 | |
377 | |
350 | $CB{die} = sub { die shift }; |
378 | $CB{die} = sub { die shift }; |
351 | |
379 | |
352 | $fh_r_watcher = AnyEvent->io (fh => $FH, poll => 'r', cb => \&fh_read); |
380 | $fh_r_watcher = Event->io (fd => $FH, poll => 'r', nice => 1, cb => \&fh_read); |
353 | |
381 | $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write); |
354 | sync_tick; |
382 | $SYNC->start; |
355 | } |
383 | } |
356 | |
384 | |
357 | sub stop { |
385 | sub stop { |
358 | close $FH; |
386 | close $FH; |
359 | } |
387 | } |