--- googlecode.com/svn/trunk/Meteor/Connection.pm 2006/11/20 17:59:30 3 +++ googlecode.com/svn/trunk/Meteor/Connection.pm 2009/01/19 11:19:41 64 @@ -52,8 +52,8 @@ my $rVecRef=shift; my $wVecRef=shift; my $eVecRef=shift; - - map {$_->addHandleBits($rVecRef,$wVecRef,$eVecRef)} @Connections; + my @cons=@Connections; + map {$_->addHandleBits($rVecRef,$wVecRef,$eVecRef) if(defined($_)) } @cons; } sub checkAllHandleBits { @@ -63,7 +63,8 @@ my $wVec=shift; my $eVec=shift; - map {$_->checkHandleBits($rVec,$wVec,$eVec)} @Connections; + my @cons=@Connections; + map {$_->checkHandleBits($rVec,$wVec,$eVec) if(defined($_)) } @cons; } sub connectionCount { @@ -76,6 +77,82 @@ map { $_->close(); } @cons; } +sub listConnections { + my $class=shift; + my $list=''; + foreach my $conn (@Connections) { + $list .= $conn->{'socketFN'}.' '.$conn->{'ip'}.' '.$conn->{'type'}; + if (exists($conn->{'subscriberID'})) { + $list .= ' '.$conn->{'subscriberID'}; + } + $list .= "$::CRLF"; + } + $list; +} + +sub describeConnWithFileNum { + my $class=shift; + my $filenum=shift; + foreach my $conn (@Connections) { + if ($conn->{'socketFN'}==$filenum) { + my $ret = ""; + if (exists($conn->{'socketFN'})) { + $ret .= "socketFN: ".$conn->{'socketFN'}."$::CRLF"; + } + if (exists($conn->{'connectionStart'})) { + $ret .= "connectionStart: ".$conn->{'connectionStart'}."$::CRLF"; + } + if (exists($conn->{'writeBuffer'})) { + $ret .= "writeBuffer: ".$conn->{'writeBuffer'}."$::CRLF"; + } + if (exists($conn->{'readBuffer'})) { + $ret .= "readBuffer: ".$conn->{'readBuffer'}."$::CRLF"; + } + if (exists($conn->{'bytesWritten'})) { + $ret .= "bytesWritten: ".$conn->{'bytesWritten'}."$::CRLF"; + } + if (exists($conn->{'type'})) { + $ret .= "type: ".$conn->{'type'}."$::CRLF"; + } + if (exists($conn->{'mode'})) { + $ret .= "mode: ".$conn->{'mode'}."$::CRLF"; + } + if (exists($conn->{'ip'})) { + $ret .= "ip: ".$conn->{'ip'}."$::CRLF"; + } + if (exists($conn->{'headerBuffer'})) { + $ret .= "headerBuffer: ".$conn->{'headerBuffer'}."$::CRLF"; + } + if (exists($conn->{'messageCount'})) { + $ret .= "messageCount: ".$conn->{'messageCount'}."$::CRLF"; + } + if (exists($conn->{'connectionStart'})) { + $ret .= "age: ".(time-$conn->{'connectionStart'})."$::CRLF"; + } + if (exists($conn->{'connectionTimeLimit'})) { + $ret .= "connectionTimeLimit: ".$conn->{'connectionTimeLimit'}."$::CRLF"; + } + if (exists($conn->{'useragent'})) { + $ret .= "useragent: ".$conn->{'useragent'}."$::CRLF"; + } + if (exists($conn->{'subscriberID'})) { + $ret .= "subscriberID: ".$conn->{'subscriberID'}."$::CRLF"; + } + return $ret; + } + } + return -1; +} + +sub destroyBadRequests { + foreach my $conn (@Connections) { + if (time-$conn->{'connectionStart'} > 30 && !$conn->{'autoClose'} && !exists($conn->{'subscriberID'}) && $conn->{'type'} eq 'Meteor::Subscriber') { + &::syslog('debug',"Closing misbehaving subscriber %s",$conn->{'socketFN'}); + $conn->close(); + } + } +} + ############################################################################### # Factory methods ############################################################################### @@ -96,20 +173,26 @@ # my $self=shift->new(); + $::Statistics->{'total_requests'}++; + my $server=shift; my $socket=$server->conSocket(); $self->{'socket'}=$socket; $self->{'socketFN'}=$socket->fileno(); + $self->{'connectionStart'}=time; $socket->setNonBlocking(); $self->{'writeBuffer'}=''; $self->{'readBuffer'}=''; + $self->{'bytesWritten'}=0; + $self->{'type'}=ref($self); + $self->{'ip'}=$socket->{'connection'}->{'remoteIP'}; push(@Connections,$self); - &::syslog('debug',"New %s for %s",ref($self),$socket->{'connection'}->{'remoteIP'}); + &::syslog('debug',"New %s for %s using file number %s",ref($self),$self->{'ip'},$self->{'socketFN'}); $self; } @@ -180,6 +263,7 @@ my $bytesRead=sysread($socket->{'handle'},$buffer,$MAX_READ_SIZE); if(defined($bytesRead) && $bytesRead>0) { + $::Statistics->{'total_inbound_bytes'}+=$bytesRead; $self->{'readBuffer'}.=$buffer; while($self->{'readBuffer'}=~s/^([^\r\n]*)\r?\n//) { @@ -190,7 +274,7 @@ { # Connection closed $self->{'remoteClosed'}=1; - $self->close(); + $self->close(1, 'remoteClosed'); return; } @@ -200,7 +284,7 @@ { &::syslog('notice',"Connection closed: $!"); $self->{'remoteClosed'}=1; - $self->close(); + $self->close(1, 'remoteClosed'); return; } @@ -218,11 +302,13 @@ if(defined($bytesWritten) && $bytesWritten>0) { + $::Statistics->{'total_outbound_bytes'}+=$bytesWritten; + $self->{'bytesWritten'}+=$bytesWritten; $self->{'writeBuffer'}=substr($self->{'writeBuffer'},$bytesWritten); if(length($self->{'writeBuffer'})==0) { delete($self->{'writeBufferTimestamp'}); - $self->close() if(exists($self->{'autoClose'})); + $self->close(1) if(exists($self->{'autoClose'})); } else { @@ -235,7 +321,7 @@ { &::syslog('notice',"Connection closed: $!"); $self->{'remoteClosed'}=1; - $self->close(); + $self->close(1, 'remoteClosed'); return; } @@ -276,7 +362,8 @@ # Remove connection from list of connections # my $idx=undef; - for(my $i=0;$i{'socket'}->{'connection'}->{'remoteIP'}); + + $self->didClose(); +} + +sub didClose { } 1; -############################################################################EOF \ No newline at end of file +############################################################################EOF