*/
} // END - if
- /*
- * "Bind" the socket to the given address, on given port so this means
- * that all connections on this port are now our resposibility to
- * send/recv data, disconnect, etc..
- */
- $this->debugOutput('TCP-LISTENER: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort());
- if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) {
+ // Set the option to reuse the port
+ if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) {
// Handle this socket error with a faked recipientData array
$this->handleSocketError($mainSocket, array('0.0.0.0', '0'));
/*
*/
} // END - if
- // Start listen for connections
- $this->debugOutput('TCP-LISTENER: Listening for connections.');
- if (!socket_listen($mainSocket)) {
+ /*
+ * "Bind" the socket to the given address, on given port so this means
+ * that all connections on this port are now our resposibility to
+ * send/recv data, disconnect, etc..
+ */
+ $this->debugOutput('TCP-LISTENER: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort());
+ if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) {
// Handle this socket error with a faked recipientData array
$this->handleSocketError($mainSocket, array('0.0.0.0', '0'));
/*
*/
} // END - if
- // Set the option to reuse the port
- if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) {
+ // Start listen for connections
+ $this->debugOutput('TCP-LISTENER: Listening for connections.');
+ if (!socket_listen($mainSocket)) {
// Handle this socket error with a faked recipientData array
$this->handleSocketError($mainSocket, array('0.0.0.0', '0'));
/*
// Do we have changed peers?
if (in_array($this->getSocketResource(), $readers)) {
- // Then accept it
+ /*
+ * Then accept it, if this socket is set to non-blocking IO and the
+ * connection is NOT sending any data, socket_read() may throw
+ * error 11 (Resource temporary unavailable). This really nasty
+ * because if you have blocking IO socket_read() will wait and wait
+ * and wait ...
+ */
$newSocket = socket_accept($this->getSocketResource());
- //* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: newSocket=' . $newSocket);
-
- // We want non-blocking here, too
- if (!socket_set_nonblock($newSocket)) {
+ /* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: newSocket=' . $newSocket);
+
+ // Array for timeout settings
+ $options = array(
+ // Seconds
+ 'sec' => $this->getConfigInstance()->getConfigEntry('tcp_socket_accept_wait_sec'),
+ // Milliseconds
+ 'usec' => $this->getConfigInstance()->getConfigEntry('tcp_socket_accept_wait_usec')
+ );
+
+ // Set timeout to configured seconds
+ // @TODO Does this work on Windozer boxes???
+ if (!socket_set_option($newSocket, SOL_SOCKET, SO_RCVTIMEO, $options)) {
// Handle this socket error with a faked recipientData array
- $this->handleSocketError($mainSocket, array('0.0.0.0', '0'));
- /*
- // Get socket error code for verification
- $socketError = socket_last_error($newSocket);
-
- // Get error message
- $errorMessage = socket_strerror($socketError);
+ $this->handleSocketError($newSocket, array('0.0.0.0', '0'));
+ } // END - if
- // Shutdown this socket
- $this->shutdownSocket($newSocket);
+ // Output result (only debugging!)
+ $option = socket_get_option($newSocket, SOL_SOCKET, SO_RCVTIMEO);
+ $this->debugOutput('SO_RCVTIMEO[' . gettype($option) . ']=' . print_r($option, true));
- // And throw the exception
- throw new InvalidSocketException(array($this, $newSocket, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
- */
+ // Enable SO_OOBINLINE
+ if (!socket_set_option($newSocket, SOL_SOCKET, SO_OOBINLINE ,1)) {
+ // Handle this socket error with a faked recipientData array
+ $this->handleSocketError($newSocket, array('0.0.0.0', '0'));
} // END - if
// Add it to the peers
// Handle it here, if not main socket
if ($currentSocket != $this->getSocketResource()) {
// ... or else it will raise warnings like 'Transport endpoint is not connected'
- //* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: currentSocket=' . $currentSocket . ',server=' . $this->getSocketResource());
+ /* NOISY-DEBUG: */ $this->debugOutput('TCP-LISTENER: currentSocket=' . $currentSocket . ',server=' . $this->getSocketResource());
$this->getHandlerInstance()->processRawDataFromResource($currentSocket);
} // END - if