ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC.pm (file contents):
Revision 1.136 by root, Sat Dec 9 02:21:24 2006 UTC vs.
Revision 1.157 by root, Sat Nov 3 12:08:43 2007 UTC

15package CFPlus; 15package CFPlus;
16 16
17use Carp (); 17use Carp ();
18 18
19BEGIN { 19BEGIN {
20 $VERSION = '0.97'; 20 $VERSION = '0.9955';
21 21
22 use XSLoader; 22 use XSLoader;
23 XSLoader::load "CFPlus", $VERSION; 23 XSLoader::load "CFPlus", $VERSION;
24} 24}
25 25
26use utf8; 26use utf8;
27 27
28use AnyEvent (); 28use AnyEvent ();
29use BerkeleyDB;
30use Pod::POM (); 29use Pod::POM ();
31use Scalar::Util ();
32use File::Path (); 30use File::Path ();
33use Storable (); # finally 31use Storable (); # finally
34 32use Fcntl ();
35BEGIN { 33use JSON::XS qw(to_json from_json);
36 use Crossfire::Protocol::Base ();
37 *to_json = \&Crossfire::Protocol::Base::to_json;
38 *from_json = \&Crossfire::Protocol::Base::from_json;
39}
40 34
41=item guard { BLOCK } 35=item guard { BLOCK }
42 36
43Returns an object that executes the given block as soon as it is destroyed. 37Returns an object that executes the given block as soon as it is destroyed.
44 38
72 $_ 66 $_
73} 67}
74 68
75sub socketpipe() { 69sub socketpipe() {
76 socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC 70 socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC
77 or die "cannot establish bidiretcional pipe: $!\n"; 71 or die "cannot establish bidirectional pipe: $!\n";
78 72
79 ($fh1, $fh2) 73 ($fh1, $fh2)
80} 74}
81 75
82sub background(&;&) { 76sub background(&;&) {
125 while ($buffer =~ s/^(.*)\n//) { 119 while ($buffer =~ s/^(.*)\n//) {
126 my $line = $1; 120 my $line = $1;
127 $line =~ s/\s+$//; 121 $line =~ s/\s+$//;
128 utf8::decode $line; 122 utf8::decode $line;
129 if ($line =~ /^\x{e877}json_msg (.*)$/s) { 123 if ($line =~ /^\x{e877}json_msg (.*)$/s) {
130 $cb->(from_json $1); 124 $cb->(JSON::XS->new->allow_nonref->decode ($1));
131 } else { 125 } else {
132 ::message ({ 126 ::message ({
133 markup => "background($pid): " . CFPlus::asxml $line, 127 markup => "background($pid): " . CFPlus::asxml $line,
134 }); 128 });
135 } 129 }
138} 132}
139 133
140sub background_msg { 134sub background_msg {
141 my ($msg) = @_; 135 my ($msg) = @_;
142 136
143 $msg = "\x{e877}json_msg " . to_json $msg; 137 $msg = "\x{e877}json_msg " . JSON::XS->new->allow_nonref->encode ($msg);
144 $msg =~ s/\n//g; 138 $msg =~ s/\n//g;
145 utf8::encode $msg; 139 utf8::encode $msg;
146 print $msg, "\n"; 140 print $msg, "\n";
147}
148
149package CFPlus::Database;
150
151our @ISA = BerkeleyDB::Btree::;
152
153sub get($$) {
154 my $data;
155
156 $_[0]->db_get ($_[1], $data) == 0
157 ? $data
158 : ()
159}
160
161my %DB_SYNC;
162
163sub put($$$) {
164 my ($db, $key, $data) = @_;
165
166 my $hkey = $db + 0;
167 Scalar::Util::weaken $db;
168 $DB_SYNC{$hkey} ||= AnyEvent->timer (after => 5, cb => sub {
169 delete $DB_SYNC{$hkey};
170 $db->db_sync if $db;
171 });
172
173 $db->db_put ($key => $data)
174} 141}
175 142
176package CFPlus; 143package CFPlus;
177 144
178sub find_rcfile($) { 145sub find_rcfile($) {
255 and die $res->status_line; 222 and die $res->status_line;
256 223
257 $res 224 $res
258} 225}
259 226
260our $DB_ENV; 227sub fh_nonblocking($$) {
261our $DB_STATE;
262
263sub db_table($) {
264 my ($table) = @_; 228 my ($fh, $nb) = @_;
265 229
266 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; 230 if ($^O eq "MSWin32") {
267 231 $nb = (! ! $nb) + 0;
268 new CFPlus::Database 232 ioctl $fh, 0x8004667e, \$nb; # FIONBIO
269 -Env => $DB_ENV, 233 } else {
270 -Filename => $table, 234 fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0;
271# -Filename => "database",
272# -Subname => $table,
273 -Property => DB_CHKSUM,
274 -Flags => DB_CREATE | DB_UPGRADE,
275 or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"
276}
277
278our $DB_HOME = "$Crossfire::VARDIR/cfplus";
279
280sub open_db {
281 use strict;
282
283 mkdir $DB_HOME, 0777;
284 my $recover = $BerkeleyDB::db_version >= 4.4
285 ? eval "DB_REGISTER | DB_RECOVER"
286 : 0;
287
288 $DB_ENV = new BerkeleyDB::Env
289 -Home => $DB_HOME,
290 -Cachesize => 1_000_000,
291 -ErrFile => "$DB_HOME/errorlog.txt",
292# -ErrPrefix => "DATABASE",
293 -Verbose => 1,
294 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover,
295 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE,
296 or die "unable to create/open database home $DB_HOME: $BerkeleyDB::Error";
297
298 $DB_STATE = db_table "state";
299
300 1 235 }
301}
302 236
303unless (eval { open_db }) {
304 File::Path::rmtree $DB_HOME;
305 open_db;
306} 237}
307 238
308package CFPlus::Layout; 239package CFPlus::Layout;
309 240
310$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub { 241$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines