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.126 by root, Tue Nov 7 22:41:27 2006 UTC vs.
Revision 1.127 by root, Thu Nov 16 19:42:44 2006 UTC

38use BerkeleyDB; 38use BerkeleyDB;
39use Pod::POM (); 39use Pod::POM ();
40use Scalar::Util (); 40use Scalar::Util ();
41use Storable (); # finally 41use Storable (); # finally
42 42
43BEGIN {
44 use Crossfire::Protocol::Base ();
45 *to_json = \&Crossfire::Protocol::Base::to_json;
46 *from_json = \&Crossfire::Protocol::Base::from_json;
47}
48
43=item guard { BLOCK } 49=item guard { BLOCK }
44 50
45Returns an object that executes the given block as soon as it is destroyed. 51Returns an object that executes the given block as soon as it is destroyed.
46 52
47=cut 53=cut
69 or die "cannot establish bidiretcional pipe: $!\n"; 75 or die "cannot establish bidiretcional pipe: $!\n";
70 76
71 ($fh1, $fh2) 77 ($fh1, $fh2)
72} 78}
73 79
74sub background(&) { 80sub background(&;&) {
75 my ($cb) = @_; 81 my ($bg, $cb) = @_;
76 82
77 my ($fh_r, $fh_w) = CFPlus::socketpipe; 83 my ($fh_r, $fh_w) = CFPlus::socketpipe;
78 84
79 my $pid = fork; 85 my $pid = fork;
80 86
86 close $fh_r; 92 close $fh_r;
87 close $fh_w; 93 close $fh_w;
88 94
89 $| = 1; 95 $| = 1;
90 96
91 eval { $cb->() }; 97 eval { $bg->() };
92 98
93 if ($@) { 99 if ($@) {
94 my $msg = $@; 100 my $msg = $@;
95 $msg =~ s/\n+/\n/; 101 $msg =~ s/\n+/\n/;
96 warn "FATAL: $msg"; 102 warn "FATAL: $msg";
108 my $buffer; 114 my $buffer;
109 115
110 my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub { 116 my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub {
111 unless (sysread $fh_r, $buffer, 4096, length $buffer) { 117 unless (sysread $fh_r, $buffer, 4096, length $buffer) {
112 undef $w; 118 undef $w;
113 $buffer .= "done\n"; 119 $cb->();
120 return;
114 } 121 }
115 122
116 while ($buffer =~ s/^(.*)\n//) { 123 while ($buffer =~ s/^(.*)\n//) {
117 my $line = $1; 124 my $line = $1;
118 $line =~ s/\s+$//; 125 $line =~ s/\s+$//;
119 utf8::decode $line; 126 utf8::decode $line;
127 if ($line =~ /^\x{e877}json_msg (.*)$/s) {
128 $cb->(from_json $1);
129 } else {
120 ::message ({ 130 ::message ({
121 markup => "editor($pid): " . CFPlus::asxml $line, 131 markup => "background($pid): " . CFPlus::asxml $line,
132 });
122 }); 133 }
123 } 134 }
124 }); 135 });
136}
137
138sub background_msg {
139 my ($msg) = @_;
140
141 $msg = "\x{e877}json_msg " . to_json $msg;
142 $msg =~ s/\n//g;
143 utf8::encode $msg;
144 print $msg, "\n";
125} 145}
126 146
127package CFPlus::Database; 147package CFPlus::Database;
128 148
129our @ISA = BerkeleyDB::Btree::; 149our @ISA = BerkeleyDB::Btree::;
160 $path = "$_/CFPlus/resources/$_[0]"; 180 $path = "$_/CFPlus/resources/$_[0]";
161 return $path if -r $path; 181 return $path if -r $path;
162 } 182 }
163 183
164 die "FATAL: can't find required file $_[0]\n"; 184 die "FATAL: can't find required file $_[0]\n";
165}
166
167BEGIN {
168 use Crossfire::Protocol::Base ();
169 *to_json = \&Crossfire::Protocol::Base::to_json;
170 *from_json = \&Crossfire::Protocol::Base::from_json;
171} 185}
172 186
173sub read_cfg { 187sub read_cfg {
174 my ($file) = @_; 188 my ($file) = @_;
175 189
215sub set_proxy { 229sub set_proxy {
216 my $proxy = http_proxy 230 my $proxy = http_proxy
217 or return; 231 or return;
218 232
219 $ENV{http_proxy} = $proxy; 233 $ENV{http_proxy} = $proxy;
234}
235
236sub lwp_useragent {
237 require LWP::UserAgent;
238
239 CFPlus::set_proxy;
240
241 my $ua = LWP::UserAgent->new (
242 agent => "cfplus $VERSION",
243 keep_alive => 1,
244 env_proxy => 1,
245 timeout => 30,
246 );
247}
248
249sub lwp_check($) {
250 my ($res) = @_;
251
252 $res->is_error
253 and die $res->status_line;
254
255 $res
220} 256}
221 257
222our $DB_ENV; 258our $DB_ENV;
223our $DB_STATE; 259our $DB_STATE;
224 260

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines