acceptConnection( $this->initFD );
// check for maximum amount of connections if( $this->maxClients > 0 ){ if( $this->clients > $this->maxClients ){ $this->sendDebugMessage( "Too many connections." );
if( method_exists( $this, "onConnectionRefused" ) ) $this->onConnectionRefused( $newClient );
$this->closeConnection( $newClient ); } }
if( --$ready <= 0 ) continue; }
// check all clients for incoming data for( $i = 0; $i < count( $this->clientFD ); $i++ ){ if( !isset( $this->clientFD[$i] ) ) continue;
if( in_array( $this->clientFD[$i], $readFDs ) ){ $data = $this->readFromSocket( $i );
// empty data => connection was closed if( !$data ){ $this->sendDebugMessage( "Connection closed by peer" ); $this->closeConnection( $i ); }else{ $this->sendDebugMessage( "Received ".trim( $data )." from ".$i );
if( method_exists( $this, "onReceiveData" ) ) $this->onReceiveData( $i, $data ); } } } } }
/** * read from a socket * * @access private * @param integer $clientId internal id of the client to read from * @return string $data data that was read */ function readFromSocket( $clientId ){ // start with empty string $data = "";
// read data from socket while( $buf = socket_read( $this->clientFD[$clientId], $this->readBufferSize ) ){ $data .= $buf;
$endString = substr( $buf, - strlen( $this->readEndCharacter ) ); if( $endString == $this->readEndCharacter ) break; if( $buf == NULL ) break; }
if( $buf === false ) $this->sendDebugMessage( "Could not read from client ".$clientId." ( ".$this->getLastSocketError( $this->clientFD[$clientId] )." )." );
return $data; }
/** * accept a new connection * * @access public * @param resource &$socket socket that received the new connection * @return int $clientID internal ID of the client */ function acceptConnection( &$socket ){ for( |