/**
* 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);
/**
* A Constructor for this exception
*
- * @param $helperInstance A ConnectionHelper instance
+ * @param $helperInstance An instance of a ConnectionHelper class
* @param $code Error code
* @return void
*/
// 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
$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;