From d2c5c51d5789ec62a66411e24420a5cb70ccecd7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 4 Nov 2011 19:30:52 +0000 Subject: [PATCH] Some minor improvements: - $socket renamed to $socketResource - InvalidSocketException enhanced to accept a socket resource at index 1 --- .../socket/class_InvalidSocketException.php | 32 +++++++++++++------ .../socket/class_SocketShutdownException.php | 2 +- .../main/console/class_ConsoleTools.php | 12 +++---- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/inc/classes/exceptions/socket/class_InvalidSocketException.php b/inc/classes/exceptions/socket/class_InvalidSocketException.php index 27ccb5d5..448d2cbd 100644 --- a/inc/classes/exceptions/socket/class_InvalidSocketException.php +++ b/inc/classes/exceptions/socket/class_InvalidSocketException.php @@ -26,18 +26,32 @@ class InvalidSocketException extends FrameworkException { /** * A Constructor for this exception * - * @param $messageArray Error message array - * @param $code Error code + * @param $messageArray Error message array + * @param $code Error code * @return void */ public function __construct (array $messageData, $code) { - // Construct the message - $message = sprintf("[%s:] Invalid socket, type=%s, errno=%s, errstr=%s", - $messageData[0]->__toString(), - $messageData[1], - $messageData[2], - $messageData[3] - ); + // Is it a resource? + if (is_resource($message[1])) { + // Get last error + $lastError = socket_last_error($messageData[1]); + + // Construct the message + $message = sprintf("[%s:] Invalid socket, type=%s, errno=%s, errstr=%s", + $messageData[0]->__toString(), + gettype($messageData[1]), + $lastError, + socket_strerror($lastError) + ); + } else { + // Construct the message + $message = sprintf("[%s:] Invalid socket, type=%s, errno=%s, errstr=%s", + $messageData[0]->__toString(), + $messageData[1], + $messageData[2], + $messageData[3] + ); + } // Call parent exception constructor parent::__construct($message, $code); diff --git a/inc/classes/exceptions/socket/class_SocketShutdownException.php b/inc/classes/exceptions/socket/class_SocketShutdownException.php index 9311677a..256001b1 100644 --- a/inc/classes/exceptions/socket/class_SocketShutdownException.php +++ b/inc/classes/exceptions/socket/class_SocketShutdownException.php @@ -26,7 +26,7 @@ class SocketShutdownException extends FrameworkException { /** * A Constructor for this exception * - * @param $helperInstance A ConnectionHelper instance + * @param $helperInstance An instance of a ConnectionHelper class * @param $code Error code * @return void */ diff --git a/inc/classes/main/console/class_ConsoleTools.php b/inc/classes/main/console/class_ConsoleTools.php index ab7df3da..825f5fa9 100644 --- a/inc/classes/main/console/class_ConsoleTools.php +++ b/inc/classes/main/console/class_ConsoleTools.php @@ -153,12 +153,12 @@ class ConsoleTools extends BaseFrameworkSystem { // First get a socket // @TODO Add some DNS caching here - $socket = fsockopen('188.138.90.169', 80, $errorNo, $errorStr, 5); + $socketResource = fsockopen('188.138.90.169', 80, $errorNo, $errorStr, 5); // Check if there was an error else if ($errorNo > 0) { // Then throw again - throw new InvalidSocketException(array($helperInstance, gettype($socket), $errorNo, $errorStr), BaseListener::EXCEPTION_INVALID_SOCKET); + throw new InvalidSocketException(array($helperInstance, gettype($socketResource), $errorNo, $errorStr), BaseListener::EXCEPTION_INVALID_SOCKET); } // END - if // Prepare the GET request @@ -169,18 +169,18 @@ class ConsoleTools extends BaseFrameworkSystem { $request .= self::HTTP_EOL; // Send it to the socket - fwrite($socket, $request); + fwrite($socketResource, $request); // Init IP (this will always be the last line) $externalAddress = 'invalid'; // And read the reply - while (!feof($socket)) { - $externalAddress = fgets($socket, 128); + while (!feof($socketResource)) { + $externalAddress = fgets($socketResource, 128); } // END - while // Close socket - fclose($socket); + fclose($socketResource); // Return determined external IP return $externalAddress; -- 2.39.5