fix console.pl so it ends gracefully
[spider.git] / perl / Msg.pm
index ea2747dc1f158de86ca15512eb8506814495ea0e..dcc3c812d1ac77763c3e41bd47439a17a4589885 100644 (file)
@@ -61,6 +61,13 @@ sub set_error
        $conn->{eproc} = $callback;
 }
 
+sub set_eof
+{
+       my $conn = shift;
+       my $callback = shift;
+       $conn->{sock}->on_eof(sub {$callback});
+}
+
 sub set_rproc
 {
        my $conn = shift;
@@ -123,7 +130,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';
@@ -225,6 +234,7 @@ sub disconnect
        }
 
        if (defined($sock)) {
+               shutdown($sock->{fh}, 2);
                $sock->destroy;
        }
        
@@ -242,14 +252,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 +299,7 @@ sub _err_will_block {
 sub close_on_empty
 {
        my $conn = shift;
-       $conn->{sock}->push_shutdown;
+       $conn->{sock}->on_drain(sub {$conn->disconnect;});
 }
 
 #-----------------------------------------------------------------