fix disconnect from peer
[spider.git] / perl / DXChannel.pm
index 958fe61860b3cb69122df0101fe8741c6afcc805..584a541c1244e44a8b3f1ae90e22b09a8b72e793 100644 (file)
@@ -147,11 +147,16 @@ sub alloc
 {
        my ($pkg, $call, $conn, $user) = @_;
        my $self = {};
-  
+
        die "trying to create a duplicate channel for $call" if $channels{$call};
+       bless $self, $pkg;
+
        $self->{call} = $call;
        $self->{priv} = 0;
-       $self->{conn} = $conn if defined $conn; # if this isn't defined then it must be a list
+       if (defined $conn && ref $conn) { # if this isn't defined then it must be a list
+               $self->{conn} = $conn;
+               $conn->set_on_eof(sub {$self->disconnect});
+       }
        if (defined $user) {
                $self->{user} = $user;
                $self->{lang} = $user->lang;
@@ -177,7 +182,6 @@ sub alloc
 
        $count++;
        dbg("DXChannel $self->{call} created ($count)") if isdbg('chan');
-       bless $self, $pkg; 
        return $channels{$call} = $self;
 }
 
@@ -202,7 +206,9 @@ sub rebless
 {
        my $self = shift;
        my $class = shift;
-       return $channels{$self->{call}} = bless $self, $class;
+       my $new = bless $self, $class;
+       $new->{conn}->on_eof(sub {$new->disconnect});
+       return $channels{$self->{call}} = $new;
 }
 
 sub rec        
@@ -386,9 +392,9 @@ sub send_now
 #              chomp;
         my @lines = split /\n/;
                for (@lines) {
+                       dbg("-> $sort $call $_") if $sort ne 'L' && isdbg('chan');
                        $conn->send_now("$sort$call|$_");
                        # debug log it, but not if it is a log message
-                       dbg("-> $sort $call $_") if $sort ne 'L' && isdbg('chan');
                }
        }
        $self->{t} = time;
@@ -410,9 +416,9 @@ sub send_later
 #              chomp;
         my @lines = split /\n/;
                for (@lines) {
+                       dbg("-> $sort $call $_") if $sort ne 'L' && isdbg('chan');
                        $conn->send_later("$sort$call|$_");
                        # debug log it, but not if it is a log message
-                       dbg("-> $sort $call $_") if $sort ne 'L' && isdbg('chan');
                }
        }
        $self->{t} = time;
@@ -432,8 +438,8 @@ sub send                                            # this is always later and always data
                for (ref $l ? @$l : $l) {
                        my @lines = split /\n/;
                        for (@lines) {
-                               $conn->send_later("D$call|$_");
                                dbg("-> D $call $_") if isdbg('chan');
+                               $conn->send_later("D$call|$_");
                        }
                }
        }
@@ -500,7 +506,7 @@ sub disconnect
        my $user = $self->{user};
        
        $user->close() if defined $user;
-       $self->{conn}->disconnect if $self->{conn};
+       $self->{conn}->close_on_empty if $self->{conn};
        $self->del();
 }