* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class BaseConnectionHelper extends BaseHubHelper implements Registerable, ProtocolHandler {
+ // Exception codes
+ const EXCEPTION_UNSUPPORTED_ERROR_HANDLER = 0x900;
+
/**
* Protocol used
*/
// Call parent constructor
parent::__construct($className);
- // Register this connection helper
- Registry::getRegistry()->addInstance('connection', $this);
-
// Initialize output stream
$streamInstance = ObjectFactory::createObjectByConfiguredName('node_raw_data_output_stream_class');
// Init state which sets the state to 'init'
$this->initState();
+
+ // Register this connection helper
+ Registry::getRegistry()->addInstance('connection', $this);
}
/**
- * Getter for real class name
+ * Getter for real class name, overwrites generic method and is final
*
* @return $class Name of this class
*/
- public function __toString () {
+ public final function __toString () {
// Class name representation
$class = self::getConnectionClassName($this->getAddress(), $this->getPort(), parent::__toString());
$rawData .= $dataStream;
} // END - while
- // Nothing to sent is bad news!
+ // Nothing to sent is bad news, so assert on it
assert(strlen($rawData) > 0);
// Encode the raw data with our output-stream
// If there was an error, we don't continue here
if ($sentBytes === false) {
- // Get socket error code for verification
- $socketError = socket_last_error($socketResource);
-
- // Get error message
- $errorMessage = socket_strerror($socketError);
-
- // Shutdown this socket
- $this->shutdownSocket($socketResource);
+ // Handle the error with a faked recipientData array
+ $this->handleSocketError($socketResource, array('0.0.0.0', '0'));
// And throw it
- throw new InvalidSocketException(array($this, gettype($socketResource), $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
} elseif (($sentBytes == 0) && (strlen($encodedData) > 0)) {
// Nothing sent means we are done
//* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: All sent! (' . __LINE__ . ')');
break;
}
- // The difference between sent bytes and length of raw data should not be below zero
+ // The difference between sent bytes and length of raw data should not go below zero
assert((strlen($encodedData) - $sentBytes) >= 0);
// Add total sent bytes
/* NOISY-DEBUG: */ $this->debugOutput('CONNECTION: ' . $this->__toString() . ',shuttedDown=' . intval($this->shuttedDown));
return $this->shuttedDown;
}
+
+ // ************************************************************************
+ // Socket error handler call-back methods
+ // ************************************************************************
+
+ /**
+ * Handles socket error 'connection timed out', but does not clear it for
+ * later debugging purposes.
+ *
+ * @param $socketResource A valid socket resource
+ * @throws SocketConnectionException The connection attempts fails with a time-out
+ */
+ private function socketErrorConnectionTimedOutHandler ($socketResource) {
+ // Get socket error code for verification
+ $socketError = socket_last_error($socketResource);
+
+ // Get error message
+ $errorMessage = socket_strerror($socketError);
+
+ // Shutdown this socket
+ $this->shutdownSocket($socketResource);
+
+ // Throw it again
+ throw new SocketConnectionException(array($this, $socketResource, $socketError, $errorMessage), BaseListener::EXCEPTION_INVALID_SOCKET);
+ }
}
// [EOF]