X-Git-Url: http://gb7djk.dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FMsg.pm;h=83c82be6e1a70bcb04433187b1df6ca020d3046d;hb=74db48555c9a548e2ef49815234af3fc2b8ecd81;hp=f2bb29768169b02b953887dcc6395a42d7b7175d;hpb=c02dc2d8b544b9d682c9a068ba13bdedf40ed3bf;p=spider.git diff --git a/perl/Msg.pm b/perl/Msg.pm index f2bb2976..83c82be6 100644 --- a/perl/Msg.pm +++ b/perl/Msg.pm @@ -123,7 +123,7 @@ sub new $noconns++; - dbg("Connection created ($noconns)") if isdbg('connll'); + dbg("$class Connection $conn->{cnum} created (total $noconns)") if isdbg('connll'); return bless $conn, $class; } @@ -170,11 +170,11 @@ sub conns if (ref $pkg) { $call = $pkg->{call} unless $call; return undef unless $call; - dbg("changing $pkg->{call} to $call") if isdbg('connll') && exists $pkg->{call} && $call ne $pkg->{call}; + dbg((ref $pkg) . " changing $pkg->{call} to $call") if isdbg('connll') && exists $pkg->{call} && $call ne $pkg->{call}; delete $conns{$pkg->{call}} if exists $pkg->{call} && exists $conns{$pkg->{call}} && $pkg->{call} ne $call; $pkg->{call} = $call; $ref = $conns{$call} = $pkg; - dbg("Connection $pkg->{cnum} $call stored") if isdbg('connll'); + dbg((ref $pkg) . " Connection $pkg->{cnum} $call stored") if isdbg('connll'); } else { $ref = $conns{$call}; } @@ -221,26 +221,35 @@ sub connect { $conn->{peerhost} = $to_host; $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; - # does the host resolve? - my $ip = gethostbyname($to_host); - return undef unless $ip; + dbg((ref $conn) . " connecting $conn->{cnum} to $to_host:$to_port") if isdbg('connll'); - my $r = connect($sock, pack_sockaddr_in($to_port, $ip)); - return undef unless $r || _err_will_block($!); + 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; + + 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 +# $conn->{peerhost} = $sock->peerhost; # for consistency + + dbg((ref $conn) . " connected $conn->{cnum} to $to_host:$to_port") if isdbg('connll'); if ($conn->{rproc}) { my $callback = sub {$conn->_rcv}; @@ -315,7 +324,7 @@ sub disconnect delete $conns{$call} if $ref && $ref == $conn; } $call ||= 'unallocated'; - dbg("Connection $conn->{cnum} $call disconnected") if isdbg('connll'); + dbg((ref $conn) . " Connection $conn->{cnum} $call disconnected") if isdbg('connll'); # get rid of any references for (keys %$conn) { @@ -326,7 +335,7 @@ sub disconnect if (defined($sock)) { set_event_handler ($sock, read => undef, write => undef, error => undef); - shutdown($sock, 3); + shutdown($sock, 2); close($sock); } @@ -455,8 +464,8 @@ sub new_server { my $self = $pkg->new($login_proc); $self->{sock} = $io_socket->new ( - LocalAddr => "$my_host:$my_port", -# LocalPort => $my_port, + LocalAddr => $my_host, + LocalPort => $my_port, Listen => SOMAXCONN, Proto => 'tcp', Reuse => 1); @@ -495,15 +504,17 @@ sub nolinger sub dequeue { my $conn = shift; - - if ($conn->{msg} =~ /\n/) { - my @lines = split /\r?\n/, $conn->{msg}; - if ($conn->{msg} =~ /\n$/) { + return if $conn->{disconnecting}; + + if ($conn->{msg} =~ /\cJ/) { + my @lines = split /\cM?\cJ/, $conn->{msg}; + if ($conn->{msg} =~ /\cM?\cJ$/) { delete $conn->{msg}; } else { $conn->{msg} = pop @lines; } for (@lines) { + last if $conn->{disconnecting}; &{$conn->{rproc}}($conn, defined $_ ? $_ : ''); } } @@ -712,8 +723,8 @@ sub DESTROY my $call = $conn->{call} || 'unallocated'; my $host = $conn->{peerhost} || ''; my $port = $conn->{peerport} || ''; - dbg("Connection $conn->{cnum} $call [$host $port] being destroyed") if isdbg('connll'); $noconns--; + dbg((ref $conn) . " Connection $conn->{cnum} $call [$host $port] being destroyed (total $noconns)") if isdbg('connll'); } 1;