--- deliantra/Deliantra-Client/DC.pm 2006/11/07 22:41:27 1.126 +++ deliantra/Deliantra-Client/DC.pm 2006/11/16 19:42:44 1.127 @@ -40,6 +40,12 @@ use Scalar::Util (); use Storable (); # finally +BEGIN { + use Crossfire::Protocol::Base (); + *to_json = \&Crossfire::Protocol::Base::to_json; + *from_json = \&Crossfire::Protocol::Base::from_json; +} + =item guard { BLOCK } Returns an object that executes the given block as soon as it is destroyed. @@ -71,8 +77,8 @@ ($fh1, $fh2) } -sub background(&) { - my ($cb) = @_; +sub background(&;&) { + my ($bg, $cb) = @_; my ($fh_r, $fh_w) = CFPlus::socketpipe; @@ -88,7 +94,7 @@ $| = 1; - eval { $cb->() }; + eval { $bg->() }; if ($@) { my $msg = $@; @@ -110,20 +116,34 @@ my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub { unless (sysread $fh_r, $buffer, 4096, length $buffer) { undef $w; - $buffer .= "done\n"; + $cb->(); + return; } while ($buffer =~ s/^(.*)\n//) { my $line = $1; $line =~ s/\s+$//; utf8::decode $line; - ::message ({ - markup => "editor($pid): " . CFPlus::asxml $line, - }); + if ($line =~ /^\x{e877}json_msg (.*)$/s) { + $cb->(from_json $1); + } else { + ::message ({ + markup => "background($pid): " . CFPlus::asxml $line, + }); + } } }); } +sub background_msg { + my ($msg) = @_; + + $msg = "\x{e877}json_msg " . to_json $msg; + $msg =~ s/\n//g; + utf8::encode $msg; + print $msg, "\n"; +} + package CFPlus::Database; our @ISA = BerkeleyDB::Btree::; @@ -164,12 +184,6 @@ die "FATAL: can't find required file $_[0]\n"; } -BEGIN { - use Crossfire::Protocol::Base (); - *to_json = \&Crossfire::Protocol::Base::to_json; - *from_json = \&Crossfire::Protocol::Base::from_json; -} - sub read_cfg { my ($file) = @_; @@ -219,6 +233,28 @@ $ENV{http_proxy} = $proxy; } +sub lwp_useragent { + require LWP::UserAgent; + + CFPlus::set_proxy; + + my $ua = LWP::UserAgent->new ( + agent => "cfplus $VERSION", + keep_alive => 1, + env_proxy => 1, + timeout => 30, + ); +} + +sub lwp_check($) { + my ($res) = @_; + + $res->is_error + and die $res->status_line; + + $res +} + our $DB_ENV; our $DB_STATE;