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.121 by root, Fri Sep 29 00:56:05 2006 UTC vs.
Revision 1.131 by root, Tue Dec 5 00:52:56 2006 UTC

15package CFPlus; 15package CFPlus;
16 16
17use Carp (); 17use Carp ();
18 18
19BEGIN { 19BEGIN {
20 $VERSION = '0.52'; 20 $VERSION = '0.97';
21 21
22 use XSLoader; 22 use XSLoader;
23 XSLoader::load "CFPlus", $VERSION; 23 XSLoader::load "CFPlus", $VERSION;
24}
25
26BEGIN {
27 $SIG{__DIE__} = sub {
28 return if CFPlus::in_destruct;
29 #CFPlus::fatal $_[0];#d#
30 CFPlus::error Carp::longmess $_[0];#d#
31 die;#d#
32 };
33} 24}
34 25
35use utf8; 26use utf8;
36 27
37use AnyEvent (); 28use AnyEvent ();
38use BerkeleyDB; 29use BerkeleyDB;
39use Pod::POM (); 30use Pod::POM ();
40use Scalar::Util (); 31use Scalar::Util ();
41use Storable (); # finally 32use Storable (); # finally
42 33
34BEGIN {
35 use Crossfire::Protocol::Base ();
36 *to_json = \&Crossfire::Protocol::Base::to_json;
37 *from_json = \&Crossfire::Protocol::Base::from_json;
38}
39
43=item guard { BLOCK } 40=item guard { BLOCK }
44 41
45Returns an object that executes the given block as soon as it is destroyed. 42Returns an object that executes the given block as soon as it is destroyed.
46 43
47=cut 44=cut
60 s/&/&/g; 57 s/&/&/g;
61 s/>/>/g; 58 s/>/>/g;
62 s/</&lt;/g; 59 s/</&lt;/g;
63 60
64 $_ 61 $_
62}
63
64sub socketpipe() {
65 socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC
66 or die "cannot establish bidiretcional pipe: $!\n";
67
68 ($fh1, $fh2)
69}
70
71sub background(&;&) {
72 my ($bg, $cb) = @_;
73
74 my ($fh_r, $fh_w) = CFPlus::socketpipe;
75
76 my $pid = fork;
77
78 if (defined $pid && !$pid) {
79 local $SIG{__DIE__};
80
81 open STDOUT, ">&", $fh_w;
82 open STDERR, ">&", $fh_w;
83 close $fh_r;
84 close $fh_w;
85
86 $| = 1;
87
88 eval { $bg->() };
89
90 if ($@) {
91 my $msg = $@;
92 $msg =~ s/\n+/\n/;
93 warn "FATAL: $msg";
94 CFPlus::_exit 1;
95 }
96
97 # win32 is fucked up, of course. exit will clean stuff up,
98 # which destroys our database etc. _exit will exit ALL
99 # forked processes, because of the dreaded fork emulation.
100 CFPlus::_exit 0;
101 }
102
103 close $fh_w;
104
105 my $buffer;
106
107 my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub {
108 unless (sysread $fh_r, $buffer, 4096, length $buffer) {
109 undef $w;
110 $cb->();
111 return;
112 }
113
114 while ($buffer =~ s/^(.*)\n//) {
115 my $line = $1;
116 $line =~ s/\s+$//;
117 utf8::decode $line;
118 if ($line =~ /^\x{e877}json_msg (.*)$/s) {
119 $cb->(from_json $1);
120 } else {
121 ::message ({
122 markup => "background($pid): " . CFPlus::asxml $line,
123 });
124 }
125 }
126 });
127}
128
129sub background_msg {
130 my ($msg) = @_;
131
132 $msg = "\x{e877}json_msg " . to_json $msg;
133 $msg =~ s/\n//g;
134 utf8::encode $msg;
135 print $msg, "\n";
65} 136}
66 137
67package CFPlus::Database; 138package CFPlus::Database;
68 139
69our @ISA = BerkeleyDB::Btree::; 140our @ISA = BerkeleyDB::Btree::;
100 $path = "$_/CFPlus/resources/$_[0]"; 171 $path = "$_/CFPlus/resources/$_[0]";
101 return $path if -r $path; 172 return $path if -r $path;
102 } 173 }
103 174
104 die "FATAL: can't find required file $_[0]\n"; 175 die "FATAL: can't find required file $_[0]\n";
105}
106
107BEGIN {
108 use Crossfire::Protocol::Base ();
109 *to_json = \&Crossfire::Protocol::Base::to_json;
110 *from_json = \&Crossfire::Protocol::Base::from_json;
111} 176}
112 177
113sub read_cfg { 178sub read_cfg {
114 my ($file) = @_; 179 my ($file) = @_;
115 180
138 open my $fh, ">:utf8", $file 203 open my $fh, ">:utf8", $file
139 or return; 204 or return;
140 print $fh to_json $::CFG; 205 print $fh to_json $::CFG;
141} 206}
142 207
208sub http_proxy {
209 my @proxy = win32_proxy_info;
210
211 if (@proxy) {
212 "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0]
213 } elsif (exists $ENV{http_proxy}) {
214 $ENV{http_proxy}
215 } else {
216 ()
217 }
218}
219
220sub set_proxy {
221 my $proxy = http_proxy
222 or return;
223
224 $ENV{http_proxy} = $proxy;
225}
226
227sub lwp_useragent {
228 require LWP::UserAgent;
229
230 CFPlus::set_proxy;
231
232 my $ua = LWP::UserAgent->new (
233 agent => "cfplus $VERSION",
234 keep_alive => 1,
235 env_proxy => 1,
236 timeout => 30,
237 );
238}
239
240sub lwp_check($) {
241 my ($res) = @_;
242
243 $res->is_error
244 and die $res->status_line;
245
246 $res
247}
248
143our $DB_ENV; 249our $DB_ENV;
144our $DB_STATE; 250our $DB_STATE;
145 251
146sub db_table($) { 252sub db_table($) {
147 my ($table) = @_; 253 my ($table) = @_;
159} 265}
160 266
161{ 267{
162 use strict; 268 use strict;
163 269
164 mkdir "$Crossfire::VARDIR/cfplus", 0777; 270 my $HOME = "$Crossfire::VARDIR/cfplus-$BerkeleyDB::db_version";
271
272 mkdir $HOME, 0777;
165 my $recover = $BerkeleyDB::db_version >= 4.4 273 my $recover = $BerkeleyDB::db_version >= 4.4
166 ? eval "DB_REGISTER | DB_RECOVER" 274 ? eval "DB_REGISTER | DB_RECOVER"
167 : 0; 275 : 0;
168 276
169 $DB_ENV = new BerkeleyDB::Env 277 $DB_ENV = new BerkeleyDB::Env
170 -Home => "$Crossfire::VARDIR/cfplus", 278 -Home => $HOME,
171 -Cachesize => 1_000_000, 279 -Cachesize => 1_000_000,
172 -ErrFile => "$Crossfire::VARDIR/cfplus/errorlog.txt", 280 -ErrFile => "$Crossfire::VARDIR/cfplus/errorlog.txt",
173# -ErrPrefix => "DATABASE", 281# -ErrPrefix => "DATABASE",
174 -Verbose => 1, 282 -Verbose => 1,
175 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover, 283 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN | $recover,
176 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE, 284 -SetFlags => DB_AUTO_COMMIT | DB_LOG_AUTOREMOVE,
177 or die "unable to create/open database home $Crossfire::VARDIR/cfplus: $BerkeleyDB::Error"; 285 or die "unable to create/open database home $HOME: $BerkeleyDB::Error";
178 286
179 $DB_STATE = db_table "state"; 287 $DB_STATE = db_table "state";
180} 288}
181 289
182package CFPlus::Layout; 290package CFPlus::Layout;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines