* but assumes valid data in array $recipientData, except that
* count($recipientData) is always 2.
*
+ * @param $method Value of __METHOD__ from calling method
+ * @param $line Source code line where this method was called
* @param $socketResource A valid socket resource
* @param $recipientData An array with two elements: 0=IP number, 1=port number
* @return void
* @throws InvalidSocketException If $socketResource is no socket resource
* @throws NoSocketErrorDetectedException If socket_last_error() gives zero back
*/
- protected final function handleSocketError ($socketResource, array $recipientData) {
+ protected final function handleSocketError ($method, $line, $socketResource, array $recipientData) {
// This method handles only socket resources
if (!is_resource($socketResource)) {
// No resource, abort here
/* NOISY-DEBUG: */ $this->debugOutput('TCP-HANDLER: rawData[' . gettype($rawData) . ']=' . strlen($rawData) . ',resource=' . $resource . ',error=' . socket_strerror(socket_last_error($resource)));
// Is it valid?
- if (($rawData === false) || (socket_last_error($resource) > 0)) {
+ if (socket_last_error($resource) == 11) {
+ // Debug message
+ /* NOISY-DEBUG: */ $this->debugOutput('TCP-HANDLER: Ignoring error 11 (Resource temporary unavailable) from socket resource=' . $resource);
+
+ /*
+ * Error code 11 (Resource temporary unavailable) can be safely
+ * ignored on non-blocking sockets. The socket is currently not
+ * sending any data.
+ */
+ socket_clear_error($resource);
+ } elseif (($rawData === false) || (socket_last_error($resource) > 0)) {
// Network error or connection lost
$this->setErrorCode(socket_last_error($resource));
} elseif (empty($rawData)) {
// Set the option to reuse the port
if (!socket_set_option($socketResource, SOL_SOCKET, SO_REUSEADDR, 1)) {
// Handle this socket error with a faked recipientData array
- $this->handleSocketError($socketResource, array('0.0.0.0', '0'));
+ $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
// And throw again
// @TODO Move this to the socket error handler
*/
if (!socket_set_nonblock($socketResource)) {
// Handle this socket error with a faked recipientData array
- $helperInstance->handleSocketError($socketResource, array('0.0.0.0', '0'));
+ $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
// And throw again
throw new SocketOptionException(array($helperInstance, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
// If there was an error, we don't continue here
if ($sentBytes === false) {
// Handle the error with a faked recipientData array
- $this->handleSocketError($socketResource, array('0.0.0.0', '0'));
+ $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
// And throw it
throw new InvalidSocketException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
// Check if there was an error else
if ($socketError > 0) {
// Handle this socket error with a faked recipientData array
- $helperInstance->handleSocketError($socketResource, array('0.0.0.0', '0'));
+ $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
// Then throw again
throw new SocketCreationException(array($helperInstance, gettype($socketResource), $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_SOCKET_CREATION_FAILED);
// Now connect to it
if (!$helperInstance->connectToPeerByRecipientDataArray($recipientData)) {
// Handle socket error
- $helperInstance->handleSocketError($socketResource, $recipientData);
+ $helperInstance->handleSocketError(__METHOD__, __LINE__, $socketResource, $recipientData);
} // END - if
// Okay, that should be it. Return it...
* @return void
*/
public function sendPackage (NodeHelper $nodeInstance) {
- // Sanity check: Is the node in the approx. state? (active)
- $nodeInstance->getStateInstance()->validateNodeStateIsActive();
+ // Sanity check: Is the node in the approx. state? (active/reachable)
+ $nodeInstance->getStateInstance()->validateNodeStateIsActiveOrReachable();
// Compile the template, this inserts the loaded node data into the gaps.
$this->getTemplateInstance()->compileTemplate();
// Check if there was an error else
if ($socketError > 0) {
// Handle this socket error with a faked recipientData array
- $this->handleSocketError($mainSocket, array('0.0.0.0', '0'));
+ $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
/*
// Then throw again
throw new InvalidSocketException(array($this, $mainSocket, $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_INVALID_SOCKET);
// 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'));
+ $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
/*
// Get socket error code for verification
$socketError = socket_last_error($mainSocket);
$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'));
+ $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
/*
// Get socket error code for verification
$socketError = socket_last_error($mainSocket);
$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'));
+ $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
/*
// Get socket error code for verification
$socketError = socket_last_error($mainSocket);
$this->debugOutput('TCP-LISTENER: Setting non-blocking mode.');
if (!socket_set_nonblock($mainSocket)) {
// Handle this socket error with a faked recipientData array
- $this->handleSocketError($mainSocket, array('0.0.0.0', '0'));
+ $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
/*
// Get socket error code for verification
$socketError = socket_last_error($mainSocket);
// @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($newSocket, array('0.0.0.0', '0'));
+ $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0'));
} // END - if
- // Output result (only debugging!)
+ // Output result (only for debugging!)
+ /*
$option = socket_get_option($newSocket, SOL_SOCKET, SO_RCVTIMEO);
$this->debugOutput('SO_RCVTIMEO[' . gettype($option) . ']=' . print_r($option, true));
+ */
// 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'));
+ $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0'));
+ } // END - if
+
+ // Set non-blocking
+ if (!socket_set_nonblock($newSocket)) {
+ // Handle this socket error with a faked recipientData array
+ $this->handleSocketError(__METHOD__, __LINE__, $newSocket, array('0.0.0.0', '0'));
} // END - if
// Add it to the peers
$this->debugOutput('UDP-LISTENER: Binding to address ' . $this->getListenAddress() . ':' . $this->getListenPort());
if (!socket_bind($mainSocket, $this->getListenAddress(), $this->getListenPort())) {
// Handle the socket error with a faked recipientData array
- $this->handleSocketError($mainSocket, array('0.0.0.0', '0'));
+ $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
/*
// Get socket error code for verification
$socketError = socket_last_error($mainSocket);
$this->debugOutput('UDP-LISTENER: Setting non-blocking mode.');
if (!socket_set_nonblock($mainSocket)) {
// Handle the socket error with a faked recipientData array
- $this->handleSocketError($mainSocket, array('0.0.0.0', '0'));
+ $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
/*
// Get socket error code for verification
$socketError = socket_last_error($socket);
$this->debugOutput('UDP-LISTENER: Setting re-use address option.');
if (!socket_set_option($mainSocket, SOL_SOCKET, SO_REUSEADDR, 1)) {
// Handle the socket error with a faked recipientData array
- $this->handleSocketError($mainSocket, array('0.0.0.0', '0'));
+ $this->handleSocketError(__METHOD__, __LINE__, $mainSocket, array('0.0.0.0', '0'));
/*
// Get socket error code for verification
$socketError = socket_last_error($mainSocket);
// Is it without any errors?
if ($errorCode > 0) {
// Handle the socket error with a faked recipientData array
- $this->handleSocketError($socketResource, array('0.0.0.0', '0'));
+ $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
/*
// Get error message
$errorMessage = socket_strerror($errorCode);
// Try to determine the peer's IP number
if (!socket_getpeername($socketResource, $peerName)) {
// Handle the socket error with a faked recipientData array
- $this->handleSocketError($socketResource, array('0.0.0.0', '0'));
+ $this->handleSocketError(__METHOD__, __LINE__, $socketResource, array('0.0.0.0', '0'));
/*
// Get last error
$lastError = socket_last_error($socketResource);
// Try to get the "peer"'s name
if (!socket_getpeername($socket, $peerIp)) {
// Handle the socket error with given package data
- $this->handleSocketError($socket, explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
+ $this->handleSocketError(__METHOD__, __LINE__, $socket, explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
} // END - if
// If the "peer" IP and recipient is same, use it
* exception if it is every other state.
*
* @return void
- * @throws InvalidStateException If the state is not 'active'
+ * @throws InvalidStateException If the state is not 'active' and not 'announced'
*/
public function validateNodeStateIsActiveOrAnnounced () {
// Just compare it...
throw new InvalidStateException($this, self::EXCEPTION_INVALID_STATE);
} // END - if
}
+
+ /**
+ * Validates whether the state is 'active' or 'reachable' or throws an
+ * exception if it is every other state.
+ *
+ * @return void
+ * @throws InvalidStateException If the state is not 'active' and not 'reachable'
+ */
+ public function validateNodeStateIsActiveOrReachable () {
+ // Just compare it...
+ if ((!$this instanceof NodeActiveState) && (!$this instanceof NodeReachableState)) {
+ // Throw the exception
+ throw new InvalidStateException($this, self::EXCEPTION_INVALID_STATE);
+ } // END - if
+ }
}
// [EOF]