I am attempting to create an abstract PHP class that will allow some developer to create their own custom server using PHP. I am able to start the server, bind the socket, and start listening. The only issue I have is how to handle multiple connections for reading and writing. From what I understand there are multiple ways of doing this so please point me in the right direction if my approach is wrong.
With the code above, I read up on someone's else handling of connections and began my attempt. After the above attempt I got lost.
Please let me know if the above source is not enough to provide help.
Thanks
Code:
protected function _listen ( )
{
// Make arrays.
$read = array( $this->_daemon );
$write = array( );
$except = array( );
// Get connection sockets.
if ( ( $ret = @socket_select( $read,
$write,
$except,
0 ) ) === false )
{
// Log warning.
$this->_log( "Unable to select sockets",
self::WARNING,
$this->_daemon );
}
// No sockets?
if ( $ret === 0 )
{
// Skip cycle.
return;
}
// Check for connecting clients.
if ( in_array( $this->_daemon, $read ) )
{
// Acquire client socket.
$this->_clients []= socket_accept( $this->_daemon );
// Get client index.
$index = array_pop( array_keys( $this->_clients ) );
// Get peer information.
socket_getpeername( $this->_clients[ $index ],
$peer_addr,
$peer_port );
// Very verbose?
if ( $this->verbose >= self::HIGH )
{
// Log connection.
$this->_log( "Client connected on {$peer_addr} " .
"port {$peer_port}.",
self::NOTICE );
}
// Find daemon in read.
$dindex = array_search( $this->_daemon, $read );
// Remove daemon from read.
unset( $read[ $dindex ] );
// Loop through each client.
foreach ( $read as $socket )
{
// Get message.
$msg = "Connection: Keep-Alive";
socket_write( $this->_clients[ $index ],
$msg,
strlen( $msg ) );
$msg = "This is a test.\n";
socket_write( $this->_clients[ $index ],
$msg,
strlen( $msg ) );
}
}
}
With the code above, I read up on someone's else handling of connections and began my attempt. After the above attempt I got lost.
Please let me know if the above source is not enough to provide help.
Thanks