… | |
… | |
183 | connect $txn->{fh}, ... |
183 | connect $txn->{fh}, ... |
184 | and !$!{EWOULDBLOCK} |
184 | and !$!{EWOULDBLOCK} |
185 | and !$!{EINPROGRESS} |
185 | and !$!{EINPROGRESS} |
186 | and Carp::croak "unable to connect: $!\n"; |
186 | and Carp::croak "unable to connect: $!\n"; |
187 | |
187 | |
188 | Then it creates a write-watcher which gets called wehnever an error occurs |
188 | Then it creates a write-watcher which gets called whenever an error occurs |
189 | or the connection succeeds: |
189 | or the connection succeeds: |
190 | |
190 | |
191 | $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'w', cb => sub { $txn->fh_ready_w }); |
191 | $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'w', cb => sub { $txn->fh_ready_w }); |
192 | |
192 | |
193 | And returns this transaction object. The C<fh_ready_w> callback gets |
193 | And returns this transaction object. The C<fh_ready_w> callback gets |
… | |
… | |
210 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
210 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
211 | |
211 | |
212 | if (end-of-file or data complete) { |
212 | if (end-of-file or data complete) { |
213 | $txn->{result} = $txn->{buf}; |
213 | $txn->{result} = $txn->{buf}; |
214 | $txn->{finished}->broadcast; |
214 | $txn->{finished}->broadcast; |
|
|
215 | $txb->{cb}->($txn) of $txn->{cb}; # also call callback |
215 | } |
216 | } |
216 | |
217 | |
217 | The C<result> method, finally, just waits for the finished signal (if the |
218 | The C<result> method, finally, just waits for the finished signal (if the |
218 | request was already finished, it doesn't wait, of course, and returns the |
219 | request was already finished, it doesn't wait, of course, and returns the |
219 | data: |
220 | data: |
220 | |
221 | |
221 | $txn->{finished}->wait; |
222 | $txn->{finished}->wait; |
222 | return $txn->{buf}; |
223 | return $txn->{result}; |
223 | |
224 | |
224 | The actual code goes further and collects all errors (C<die>s, exceptions) |
225 | The actual code goes further and collects all errors (C<die>s, exceptions) |
225 | that occured during request processing. The C<result> method detects |
226 | that occured during request processing. The C<result> method detects |
226 | wether an exception as thrown (it is stored inside the $txn object) |
227 | wether an exception as thrown (it is stored inside the $txn object) |
227 | and just throws the exception, which means connection errors and other |
228 | and just throws the exception, which means connection errors and other |