… | |
… | |
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 | |
… | |
… | |
125 | -Flags => DB_CREATE | DB_UPGRADE, |
125 | -Flags => DB_CREATE | DB_UPGRADE, |
126 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
126 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error" |
127 | } |
127 | } |
128 | } |
128 | } |
129 | |
129 | |
130 | our $SYNC_INTERVAL = 60; |
|
|
131 | |
|
|
132 | our %CB; |
130 | our %CB; |
133 | our $FH; |
131 | our $FH; |
134 | our $ID = "aaa0"; |
132 | our $ID = "aaa0"; |
135 | our ($fh_r_watcher, $fh_w_watcher); |
133 | our ($fh_r_watcher, $fh_w_watcher); |
136 | our $sync_timer; |
134 | our $sync_timer; |
137 | our $write_buf; |
135 | our $write_buf; |
138 | our $read_buf; |
136 | our $read_buf; |
139 | |
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 | |
140 | sub fh_write { |
143 | sub fh_write { |
141 | my $len = syswrite $FH, $write_buf; |
144 | my $len = syswrite $FH, $write_buf; |
142 | |
145 | |
143 | substr $write_buf, 0, $len, ""; |
146 | substr $write_buf, 0, $len, ""; |
144 | |
147 | |
145 | undef $fh_w_watcher |
148 | $fh_w_watcher->stop |
146 | unless length $write_buf; |
149 | unless length $write_buf; |
147 | } |
150 | } |
148 | |
151 | |
149 | sub fh_read { |
152 | sub fh_read { |
150 | my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
153 | my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
… | |
… | |
186 | |
189 | |
187 | my $id = ++$ID; |
190 | my $id = ++$ID; |
188 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
191 | $write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
189 | $CB{$id} = $cb; |
192 | $CB{$id} = $cb; |
190 | |
193 | |
191 | $fh_w_watcher = AnyEvent->io (fh => $FH, poll => 'w', cb => \&fh_write); |
194 | $fh_w_watcher->start; |
192 | } |
195 | $SYNC->start; |
193 | |
|
|
194 | sub sync_tick { |
|
|
195 | req "sync", sub { }; |
|
|
196 | $sync_timer = AnyEvent->timer (after => $SYNC_INTERVAL, cb => \&sync_tick); |
|
|
197 | } |
196 | } |
198 | |
197 | |
199 | sub do_sync { |
198 | sub do_sync { |
200 | $DB_ENV->txn_checkpoint (0, 0, 0); |
199 | $DB_ENV->txn_checkpoint (0, 0, 0); |
201 | () |
200 | () |
… | |
… | |
348 | close $fh; |
347 | close $fh; |
349 | CFPlus::fh_nonblocking $FH, 1; |
348 | CFPlus::fh_nonblocking $FH, 1; |
350 | |
349 | |
351 | $CB{die} = sub { die shift }; |
350 | $CB{die} = sub { die shift }; |
352 | |
351 | |
353 | $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); |
354 | |
353 | $fh_w_watcher = Event->io (fd => $FH, poll => 'w', nice => -1, parked => 1, cb => \&fh_write); |
355 | sync_tick; |
354 | $SYNC->start; |
356 | } |
355 | } |
357 | |
356 | |
358 | sub stop { |
357 | sub stop { |
359 | close $FH; |
358 | close $FH; |
360 | } |
359 | } |