do non blocking connects
[spider.git] / perl / ExtMsg.pm
index 58fb619f89925642561631e85dc6867e9b08b3ff..8772955350f78c72381c37a3d2b4dcf95210c74e 100644 (file)
@@ -29,12 +29,12 @@ sub enqueue
 {
        my ($conn, $msg) = @_;
        unless ($msg =~ /^[ABZ]/) {
-               if ($msg =~ /^E[-\w]+\|([01])/) {
+               if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
                        $conn->{echo} = $1;
                        if ($1) {
-                               $conn->send_raw("\xFF\xFC\x01");
+#                              $conn->send_raw("\xFF\xFC\x01");
                        } else {
-                               $conn->send_raw("\xFF\xFB\x01");
+#                              $conn->send_raw("\xFF\xFB\x01");
                        }
                } else {
                        $msg =~ s/^[-\w]+\|//;
@@ -57,46 +57,56 @@ sub dequeue
 {
        my $conn = shift;
        my $msg;
-       
-       while ($msg = shift @{$conn->{inqueue}}){
-               dbg('connect', $msg) unless $conn->{state} eq 'C';
-               
-               $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
-               $msg =~ s/[\x00-\x08\x0a-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
 
-               if ($conn->{state} eq 'C') {
-                       &{$conn->{rproc}}($conn, "I$conn->{call}|$msg", $!);
-                       $! = 0;
-               } elsif ($conn->{state} eq 'WL' ) {
-                       $msg = uc $msg;
-                       if (is_callsign($msg)) {
-                               _send_file($conn, "$main::data/connected");
-                               $conn->{call} = $msg;
-                               &{$conn->{rproc}}($conn, "A$conn->{call}|telnet");
-                               $conn->{state} = 'C';
-                       } else {
-                               $conn->send_now("Sorry $msg is an invalid callsign");
-                               $conn->disconnect;
-                       }
-               } elsif ($conn->{state} eq 'WC') {
-                       if (exists $conn->{cmd} && @{$conn->{cmd}}) {
-                               $conn->_docmd($msg);
-                               if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
-                                       $conn->{state} = 'C';
-                                       &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
-                                       delete $conn->{cmd};
-                                       $conn->{timeout}->del_timer if $conn->{timeout};
-                               }
-                       }
+       if ($conn->{state} eq 'WC') {
+               if (exists $conn->{cmd}) {
+                       if (@{$conn->{cmd}}) {
+                               dbg('connect', $conn->{msg});
+                               $conn->_docmd($conn->{msg});
+                       } 
                }
-       }
-       if ($conn->{msg} && $conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}}) {
-               $conn->_docmd($conn->{msg});
                if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
                        $conn->{state} = 'C';
                        &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
                        delete $conn->{cmd};
-                       $conn->{timeout}->del_timer if $conn->{timeout};
+                       $conn->{timeout}->del if $conn->{timeout};
+               }
+       } elsif ($conn->{msg} =~ /\n/) {
+               my @lines = split /\r?\n/, $conn->{msg};
+               if ($conn->{msg} =~ /\n$/) {
+                       delete $conn->{msg};
+               } else {
+                       $conn->{msg} = pop @lines;
+               }
+               while (defined ($msg = shift @lines)) {
+                       dbg('connect', $msg) unless $conn->{state} eq 'C';
+               
+                       $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
+                       $msg =~ s/[\x00-\x08\x0a-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
+                       
+                       if ($conn->{state} eq 'C') {
+                               &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
+                       } elsif ($conn->{state} eq 'WL' ) {
+                               $msg = uc $msg;
+                               if (is_callsign($msg)) {
+                                       &{$conn->{rproc}}($conn, "A$msg|telnet");
+                                       _send_file($conn, "$main::data/connected");
+                                       $conn->{state} = 'C';
+                               } else {
+                                       $conn->send_now("Sorry $msg is an invalid callsign");
+                                       $conn->disconnect;
+                               }
+                       } elsif ($conn->{state} eq 'WC') {
+                               if (exists $conn->{cmd} && @{$conn->{cmd}}) {
+                                       $conn->_docmd($msg);
+                                       if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
+                                               $conn->{state} = 'C';
+                                               &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
+                                               delete $conn->{cmd};
+                                               $conn->{timeout}->del if $conn->{timeout};
+                                       }
+                               }
+                       }
                }
        }
 }
@@ -107,7 +117,11 @@ sub new_client {
     my $conn = $server_conn->new($server_conn->{rproc});
        $conn->{sock} = $sock;
 
-    my $rproc = &{$server_conn->{rproc}} ($conn, $sock->peerhost(), $sock->peerport());
+    my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost} = $sock->peerhost(), $conn->{peerport} = $sock->peerport());
+       if ($eproc) {
+               $conn->{eproc} = $eproc;
+        set_event_handler ($sock, "error" => $eproc);
+       }
     if ($rproc) {
         $conn->{rproc} = $rproc;
         my $callback = sub {$conn->_rcv};
@@ -116,9 +130,9 @@ sub new_client {
                $conn->{state} = 'WL';
 #              $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
 #              $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
-               $conn->send_raw("\xFF\xFC\x01");
+#              $conn->send_raw("\xFF\xFC\x01");
                _send_file($conn, "$main::data/issue");
-               $conn->send_raw("Login: ");
+               $conn->send_raw("login: ");
     } else { 
         $conn->disconnect();
     }
@@ -129,12 +143,11 @@ sub start_connect
        my $call = shift;
        my $fn = shift;
        my $conn = ExtMsg->new(\&main::rec); 
-       $conn->{call} = $call;
+       $conn->conns($call);
        
        my $f = new IO::File $fn;
        push @{$conn->{cmd}}, <$f>;
        $f->close;
-       push @main::outstanding_connects, {call => $call, conn => $conn};
        $conn->_dotimeout($deftimeout);
        $conn->_docmd;
 }
@@ -169,9 +182,6 @@ sub _docmd
                }
                last if $conn->{state} eq 'E';
        }
-       unless (exists $conn->{cmd} && @{$conn->{cmd}}) {
-               @main::outstanding_connects = grep {$_->{call} ne $conn->{call}} @main::outstanding_connects;
-       }
 }
 
 sub _doconnect
@@ -212,8 +222,8 @@ sub _dotimeout
        my $conn = shift;
        my $val = shift;
        dbg('connect', "timeout set to $val");
-       $conn->{timeout}->del_timer if $conn->{timeout};
-       $conn->{timeout} = ExtMsg->new_timer($val, sub{ _timeout($conn); });
+       my $old = $conn->{timeout}->del if $conn->{timeout};
+       $conn->{timeout} = Timer->new($val, sub{ &_timeout($conn) });
        $conn->{timeval} = $val;
 }
 
@@ -259,7 +269,6 @@ sub _timeout
        my $conn = shift;
        dbg('connect', "timed out after $conn->{timeval} seconds");
        $conn->disconnect;
-       @main::outstanding_connects = grep {$_->{call} ne $conn->{call}} @main::outstanding_connects;
 }
 
 # handle callsign and connection type firtling
@@ -273,7 +282,7 @@ sub _doclient
        $conn->{state} = 'C';
        &{$conn->{rproc}}($conn, "O$conn->{call}|telnet");
        delete $conn->{cmd};
-       $conn->{timeout}->del_timer if $conn->{timeout};
+       $conn->{timeout}->del if $conn->{timeout};
 }
 
 sub _send_file
@@ -286,10 +295,9 @@ sub _send_file
                if ($f) {
                        while (<$f>) {
                                chomp;
-                               $conn->send_later($_);
+                               $conn->send_raw($_ . $conn->{lineend});
                        }
                        $f->close;
                }
        }
-       $! = undef;
 }