fix error message on disconnect from peer
[spider.git] / perl / Msg.pm
index ea2747dc1f158de86ca15512eb8506814495ea0e..3fa0e6762166816f86d31ebaaf2d2849b45a5ef9 100644 (file)
@@ -61,6 +61,14 @@ sub set_error
        $conn->{eproc} = $callback;
 }
 
+sub set_on_eof
+{
+       my $conn = shift;
+       my $callback = shift;
+       $conn->{sock}->on_eof($callback);
+       $conn->{sock}->on_error($callback);
+}
+
 sub set_rproc
 {
        my $conn = shift;
@@ -123,7 +131,9 @@ sub connect {
 
     # Create a connection end-point object
     my $conn = $pkg;
-       $conn = $pkg->new($rproc);
+       unless (ref $pkg) {
+               $conn = $pkg->new($rproc);
+       }
        $conn->{peerhost} = $to_host;
        $conn->{peerport} = $to_port;
        $conn->{sort} = 'Outgoing';
@@ -132,7 +142,7 @@ sub connect {
 
                connect => [$to_host, $to_port],
 
-#              on_connect => sub {my $h = shift; $conn->{peerhost} = $h->handle->peername;},
+               on_connect => sub {my $h = shift; $conn->{peerhost} = shift;},
 
                on_eof => sub {$conn->disconnect},
 
@@ -224,8 +234,14 @@ sub disconnect
                }
        }
 
-       if (defined($sock)) {
+       if (ref $sock && $sock->isa('AnyEvent::Handle') && exists $sock->{fh}) {
+               shutdown($sock->{fh}, 2);
                $sock->destroy;
+       } else {
+               my $s;
+               $s = "already destroyed" unless exists $sock->{fh};
+               $s ||= ref $sock || $sock || "undefined";
+               dbg("Msg::disconnect trying to disconnect a $s socket") if isdbg('chan');
        }
        
        unless ($main::is_win) {
@@ -242,14 +258,18 @@ sub _send_stuff
        while (@$rq) {
                my $data = shift @$rq;
                my $lth = length $data;
+               my $call = $conn->{call} || 'none';
                if (isdbg('raw')) {
-                       my $call = $conn->{call} || 'none';
                        if (isdbg('raw')) {
                                dbgdump('raw', "$call send $lth: ", $lth);
                        }
                }
-               $sock->push_write($data);
-               $total_out = $lth;
+               if (defined $sock && !$sock->destroyed) {
+                       $sock->push_write($data);
+                       $total_out = $lth;
+               } else {
+                       dbg("_send_stuff $call ending data ignored: $data");
+               }
        }
 }
 
@@ -285,7 +305,7 @@ sub _err_will_block {
 sub close_on_empty
 {
        my $conn = shift;
-       $conn->{sock}->push_shutdown;
+       $conn->{sock}->on_drain(sub {$conn->disconnect;});
 }
 
 #-----------------------------------------------------------------
@@ -452,6 +472,12 @@ sub set_event_handler
        dbg("Msg::set_event_handler called from ${pkg}::${fn} line $line doing $s");
 }
 
+sub echo
+{
+       my $conn = shift;
+       return defined $_[0] ? $conn->{echo} = $_[0] : $_[0];
+}
+
 sub DESTROY
 {
        my $conn = shift;