start of AnyEvent conversion
[spider.git] / perl / Msg.pm
index c9b58a482cb19398d9e94cc3cf471e3f69bb801d..5aebab650caac475f3b05600ff2a2e7e3d46f35b 100644 (file)
@@ -14,7 +14,6 @@ use strict;
 
 use DXUtil;
 
-use IO::Select;
 use DXDebug;
 use Timer;
 
@@ -193,10 +192,19 @@ sub pid_gone
        }
 }
 
+sub ax25
+{
+       my $conn = shift;
+       return $conn->{csort} eq 'ax25';
+}
+
 sub peerhost
 {
-       my $self = shift;
-       return $self->{peerhost};
+       my $conn = shift;
+       $conn->{peerhost} ||= 'ax25' if $conn->ax25;
+       $conn->{peerhost} ||= $conn->{sock}->peerhost if $conn->{sock} && $conn->{sock}->isa('IO::Socket::INET');
+       $conn->{peerhost} ||= 'UNKNOWN';
+       return $conn->{peerhost};
 }
 
 #-----------------------------------------------------------------
@@ -213,22 +221,27 @@ sub connect {
        $conn->{peerport} = $to_port;
        $conn->{sort} = 'Outgoing';
        
-    # Create a new internet socket
-    my $sock = $io_socket->new();
-    return undef unless $sock;
-       
-       my $proto = getprotobyname('tcp');
-       $sock->socket(AF_INET, SOCK_STREAM, $proto) or return undef;
-       
-       blocking($sock, 0);
-       $conn->{blocking} = 0;
+       my $sock;
+       if ($blocking_supported) {
+               $sock = $io_socket->new(PeerAddr => $to_host, PeerPort => $to_port, Proto => 'tcp', Blocking =>0) or return undef;
+       } else {
+               # Create a new internet socket
+               $sock = $io_socket->new();
+               return undef unless $sock;
 
-       # does the host resolve?
-       my $ip = gethostbyname($to_host);
-       return undef unless $ip;
-       
-       my $r = connect($sock, pack_sockaddr_in($to_port, $ip));
-       return undef unless $r || _err_will_block($!);
+               my $proto = getprotobyname('tcp');
+               $sock->socket(AF_INET, SOCK_STREAM, $proto) or return undef;
+
+               blocking($sock, 0);
+               $conn->{blocking} = 0;
+
+               # does the host resolve?
+               my $ip = gethostbyname($to_host);
+               return undef unless $ip;
+
+               my $r = connect($sock, pack_sockaddr_in($to_port, $ip));
+               return undef unless $r || _err_will_block($!);
+       }
        
        $conn->{sock} = $sock;
        $conn->{peerhost} = $sock->peerhost;    # for consistency
@@ -317,7 +330,7 @@ sub disconnect
 
        if (defined($sock)) {
                set_event_handler ($sock, read => undef, write => undef, error => undef);
-               shutdown($sock, 3);
+               shutdown($sock, 2);
                close($sock);
        }