// Check it
$isServerSocket = (($this->isValidSocket()) && ($this->getSocketPeerName($peerAddress, $peerPort) === FALSE));
+ // Debug message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: isServerSocket=%d', strtoupper($this->getSocketProtocol()), intval($isServerSocket)));
+
// Need to clear the error here if it is a resource
if ($isServerSocket === true) {
// Clear the error
} // END - if
// Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf(strtoupper($this->getSocketProtocol()) . '-SOCKET: peerAddress=%s,peerPort=%d', $peerAddress, $peerPort));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: peerAddress=%s,peerPort=%d', strtoupper($this->getSocketProtocol()), $peerAddress, $peerPort));
// Check peer name, it must be empty
- $isServerSocket = (($isServerSocket) && (empty($peerAddress)));
+ // @TODO: $isServerSocket = (($isServerSocket) && ($peerAddress === '0.0.0.0') && ($peerPort === '0'));
// Trace message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf(strtoupper($this->getSocketProtocol()) . '-SOCKET: isServerSocket=%d - EXIT!', intval($isServerSocket)));
* @throws InvalidSocketException If socket is invalid
*/
public function getSocketPeerName (&$peerAddress, &$peerPort) {
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: peerAddress=%s,peerPort=%d - CALLED!', strtoupper($this->getSocketProtocol()), $peerAddress, $peerPort));
+
// Should be valid socket
if (!$this->isValidSocket()) {
// Throw exception
* support socket_getpeername():
* http://de2.php.net/manual/en/function.socket-getpeername.php#35656
*/
- if ($this->getSocketType() == 'udp') {
+ if ($this->getSocketProtocol() == 'udp') {
// UDP is WIP:
$this->partialStub('UDP sockets are unfinished.');
} else {
$result = socket_getpeername($this->getSocketResource(), $peerAddress, $peerPort);
}
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: peerAddress=%s,peerPort=%d,result[%s]=%d - EXIT!', strtoupper($this->getSocketProtocol()), $peerAddress, $peerPort, gettype($result), intval($result)));
+
// Return result
return $result;
}
return $this->socketProtocol;
}
- /**
- * Some "getter" for socket type of stored socket
- *
- * @return $socketType Socket type
- * @throws InvalidSocketException If socket is invalid
- */
- public function getSocketType () {
- // Should be valid socket
- if (!$this->isValidSocket()) {
- // Throw exception
- throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
- } // END - if
-
- // Get socket protocol first
- $socketProtocol = $this->getSocketProtocol();
-
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf(strtoupper($this->getSocketProtocol()) . '-SOCKET: socketProtocol[%s]=%s', gettype($socketProtocol), $socketProtocol));
-
- // Get socket option
- $socketType = socket_get_option($this->getSocketResource(), getprotobyname($socketProtocol), SO_TYPE);
-
- // Debug message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf(strtoupper($this->getSocketProtocol()) . '-SOCKET: socketType[%s]=%s', gettype($socketType), $socketType));
-
- // Is it valid?
- if ($socketType === FALSE) {
- // Something went wrong
- $this->handleSocketError(__METHOD__, __LINE__, array('null', '0'));
- } // END - if
- }
-
/**
* Validates stored stocket
*
}
/**
- * Getter for last socket error
+ * Getter for last socket error code
*
- * @return $lastSocketError Last socket error
+ * @return $errorCode Last socket error
* @throws InvalidSocketException If socket is invalid
*/
- public function getLastSocketError () {
+ public function getLastSocketErrorCode () {
// Should be valid socket
if (!$this->isValidSocket()) {
// Throw exception
/**
* Getter for last socket error message
*
- * @return $lastSocketErrorMessage Last socket error message
+ * @return $errorMessage Last socket error message
* @throws InvalidSocketException If socket is invalid
*/
public function getLastSocketErrorMessage () {
$socketResource = $this->getSocketResource();
// Get error code and message
- $message = socket_strerror(socket_last_error($socketResource));
+ $errorMessage = socket_strerror(socket_last_error($socketResource));
// Return it
- return $message;
+ return $errorMessage;
}
/**
*/
public function doShutdown () {
// Debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HELPER: Shutting down socket ' . $this->getSocketResource());
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Shutting down socket ' . $this->getSocketResource());
$this->partialStub('Please rewrite this method.');
return;
*/
public final function handleSocketError ($method, $line, array $socketData) {
// Trace message
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: Handling socket errorCode=%d - CALLED!', $this->getLastSocketError()));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HUB-SYSTEM: Handling socket errorCode=%d - CALLED!', $this->getLastSocketErrorCode()));
// This method handles only socket resources
if (!$this->isValidSocket()) {
assert(isset($socketData[1]));
// Get error code for first validation (0 is not an error)
- $errorCode = $this->getLastSocketError();
+ $errorCode = $this->getLastSocketErrorCode();
// If the error code is zero, someone called this method without an error
if ($errorCode == 0) {
*/
protected function handleSocketErrorPermissionDenied (array $socketData) {
// Get socket error code for verification
- $socketError = $this->getLastSocketError();
+ $socketError = $this->getLastSocketErrorCode();
// Get error message
$errorMessage = $this->getLastSocketErrorMessage();
*/
protected function handleSocketErrorConnectionTimedOut (array $socketData) {
// Get socket error code for verification
- $socketError = $this->getLastSocketError();
+ $socketError = $this->getLastSocketErrorCode();
// Get error message
$errorMessage = $this->getLastSocketErrorMessage();
*/
protected function handleSocketErrorResourceUnavailable (array $socketData) {
// Get socket error code for verification
- $socketError = $this->getLastSocketError();
+ $socketError = $this->getLastSocketErrorCode();
// Get error message
$errorMessage = $this->getLastSocketErrorMessage();
*/
protected function handleSocketErrorConnectionRefused (array $socketData) {
// Get socket error code for verification
- $socketError = $this->getLastSocketError();
+ $socketError = $this->getLastSocketErrorCode();
// Get error message
$errorMessage = $this->getLastSocketErrorMessage();
*/
protected function handleSocketErrorNoRouteToHost (array $socketData) {
// Get socket error code for verification
- $socketError = $this->getLastSocketError();
+ $socketError = $this->getLastSocketErrorCode();
// Get error message
$errorMessage = $this->getLastSocketErrorMessage();
*/
protected function handleSocketErrorOperationAlreadyProgress (array $socketData) {
// Get socket error code for verification
- $socketError = $this->getLastSocketError();
+ $socketError = $this->getLastSocketErrorCode();
// Get error message
$errorMessage = $this->getLastSocketErrorMessage();
*/
protected function handleSocketErrorConnectionResetByPeer (array $socketData) {
// Get socket error code for verification
- $socketError = $this->getLastSocketError();
+ $socketError = $this->getLastSocketErrorCode();
// Get error message
$errorMessage = $this->getLastSocketErrorMessage();
*/
protected function handleSocketErrorOperationNotSupported (array $socketData) {
// Get socket error code for verification
- $socketError = $this->getLastSocketError();
+ $socketError = $this->getLastSocketErrorCode();
// Get error message
$errorMessage = $this->getLastSocketErrorMessage();
* @return void
*/
protected function handleSocketErrorOperationInProgress (array $socketData) {
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONNECTION-HELPER: Operation is now in progress, this is usual for non-blocking connections and is no bug.');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-HELPER: Operation is now in progress, this is usual for non-blocking connections and is no bug.');
}
/************************************************************************
$this->socketProtocol = $socketProtocol;
}
+ /**
+ * Clears last socket error
+ *
+ * @return void
+ * @throws InvalidSocketException If stored socket is invalid
+ * @throws BadMethodCallException If no socket error was reported but method called
+ */
+ private function clearLastSocketError () {
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Clearing socket error - CALLED!');
+
+ // Should be valid socket
+ if (!$this->isValidSocket()) {
+ // Throw exception
+ throw new InvalidSocketException(array($this, $this->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
+ } elseif ($this->getLastSocketErrorCode() === 0) {
+ // Nothing to clear
+ throw new BadMethodCallException(sprintf('Socket "%s" has no error reported, but method is called.', $this->getSocketResource()));
+ }
+
+ // Clear last error
+ socket_clear_error($this->getSocketResource());
+
+ // Trace message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(strtoupper($this->getSocketProtocol()) . '-SOCKET: Error cleared - EXIT!');
+ }
+
}