- push (@{$conn->{outqueue}}, defined $_[0] ? $_[0] : '');
-}
-
-sub _send {
- my ($conn, $flush) = @_;
- my $sock = $conn->{sock};
- return unless defined($sock);
- my $rq = $conn->{outqueue};
-
- # If $flush is set, set the socket to blocking, and send all
- # messages in the queue - return only if there's an error
- # If $flush is 0 (deferred mode) make the socket non-blocking, and
- # return to the event loop only after every message, or if it
- # is likely to block in the middle of a message.
-
- if ($conn->{blocking} != $flush) {
- blocking($sock, $flush);
- $conn->{blocking} = $flush;
- }
- my $offset = (exists $conn->{send_offset}) ? $conn->{send_offset} : 0;
-
- while (@$rq) {
- my $msg = $rq->[0];
- my $mlth = length($msg);
- my $bytes_to_write = $mlth - $offset;
- my $bytes_written = 0;
- confess("Negative Length! msg: '$msg' lth: $mlth offset: $offset") if $bytes_to_write < 0;
- while ($bytes_to_write > 0) {
- $bytes_written = syswrite ($sock, $msg,
- $bytes_to_write, $offset);
- if (!defined($bytes_written)) {
- if (_err_will_block($!)) {
- # Should happen only in deferred mode. Record how
- # much we have already sent.
- $conn->{send_offset} = $offset;
- # Event handler should already be set, so we will
- # be called back eventually, and will resume sending
- return 1;
- } else { # Uh, oh
- &{$conn->{eproc}}($conn, $!) if exists $conn->{eproc};
- $conn->disconnect;
- return 0; # fail. Message remains in queue ..
- }
- } elsif (isdbg('raw')) {
- my $call = $conn->{call} || 'none';
- dbgdump('raw', "$call send $bytes_written: ", $msg);
- }
- $total_out += $bytes_written;
- $offset += $bytes_written;
- $bytes_to_write -= $bytes_written;
- }
- delete $conn->{send_offset};
- $offset = 0;
- shift @$rq;
- #last unless $flush; # Go back to select and wait
- # for it to fire again.
- }
- # Call me back if queue has not been drained.
- unless (@$rq) {
- set_event_handler ($sock, write => undef);
- if (exists $conn->{close_on_empty}) {
- &{$conn->{eproc}}($conn, undef) if exists $conn->{eproc};
- $conn->disconnect;
- }
- }
- 1; # Success