sockets = array(); } /** * Closes all connections. * * @return void */ public function __destruct() { unset($this->sockets); } /** * Returns the amount of active sockets. * * @return int */ public function countSockets() { return count($this->sockets); } /** * Adds a socket to the pool. * * @param ressource $addSocket * @return void */ public function addSocket($addSocket) { array_push($this->sockets, $addSocket); } /** * Removes the given socket from the pool. * The socket will be shutdown. * * @param ressource $removeSocket * @return void */ public function removeSocket($removeSocket) { foreach($this->sockets AS $key=>$socket) { if($socket !== $removeSocket) { continue; } else { unset($socket); unset($this->sockets[$key]); break; } } } /** * Creates a new TcpSocket and adds it to the pool. * * @throws \JPT\SocketFramework\Exception\SocketException * @param boolean $IPv6 will determine whether the socket uses IPv4 or IPv6. * @return ressource */ public function createTcpSocket($IPv6 = FALSE) { $domain = ($IPv6) ? \AF_INET6 : \AF_INET; $socket = socket_create($domain, \SOCK_STREAM, \SOL_TCP); socket_set_block($socket); if($socket === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("socket_create() failed!", 1290273709); $this->addSocket($socket); return $socket; } /** * Returns an array of sockets one can read from. * * @param array $read * @param array $write * @param array $except * @param int $timeout * @throws \JPT\SocketFramework\Exception\SocketException * @return array */ public function select($read, $write, $except, $timeout = NULL) { $n = socket_select($read, $write, $except, $timeout); if($n === FALSE) throw new \JPT\SocketFramework\Exception\SocketException("socket_select() failed! - Error: " . socket_strerror(socket_last_error()), 1290273693); if($n === 0) return array("read" => array(), "write" => array(), "except" => array()); return array("read" => $read, "write" => $write, "except" => $except); } } ?>