* - 'tcp' for TCP/IPv4 connections
* - 'file' for Unix* file-based sockets
*/
- private $socketProtocol = 'invalid';
+ private $socketProtocol = StorableSocket::SOCKET_PROTOCOL_INVALID;
/**
* Protected constructor
* Checks whether the stored socket resource is a server socket
*
* @return $isServerSocket Whether the stored socket resource is a server socket
+ * @throws LogicException If SOCKET_ARRAY_INDEX_FILE is not set in packageData
*/
public function isServerSocketResource () {
// Trace message
$peerAddress = '0.0.0.0';
$peerPort = '0';
+ // Is socket file?
+ if ($this->getSocketProtocol() == StorableSocket::SOCKET_PROTOCOL_FILE) {
+ // Get package data
+ $packageData = $this->getPackageData();
+
+ // Is it there?
+ if (!isset($packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE])) {
+ // Is not set
+ throw new LogicException(sprintf('packageData[%s] is not set.', StorableSocket::SOCKET_ARRAY_INDEX_FILE));
+ } // END - if
+
+ // Set file as peer address
+ $peerAddress = $packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE];
+ } // END - if
+
// Check it
$isServerSocket = (($this->isValidSocket()) && ($this->getSocketPeerName($peerAddress, $peerPort) === FALSE));
return;
// Clear any previous errors
- socket_clear_error($this->getSocketResource());
+ $this->clearLastSocketError();
// Call the shutdown function on the currently set socket
if (!socket_shutdown($this->getSocketResource())) {
} // END - if
} // END - if
- // Try to make blocking IO for socket_close()
+ // Try to make blocking I/O for socket_close()
socket_set_block($this->getSocketResource());
// Drop all data (don't sent any on socket closure)
// Init package data
$packageData = array(
- $socketFile,
- '0',
+ StorableSocket::SOCKET_ARRAY_INDEX_FILE => $socketFile,
+ '__fake_port' => '0',
);
// Init main socket
// Get container from it
// @TODO Somehow handle $infoInstance to this factory
- $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, 'file', $packageData, NULL));
+ $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, StorableSocket::SOCKET_PROTOCOL_FILE, $packageData, NULL));
// Debug message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('SOCKET-FILE_LISTENER: socketInstance[]=%s', gettype($socketInstance)));
throw new InvalidSocketException(array($listenerInstance, $socketInstance->getSocketResource()), self::EXCEPTION_INVALID_SOCKET);
} // END - if
- // Get socket error code for verification
- $socketError = $socketInstance->getLastSocketErrorCode();
-
// Check if there was an error else
- if ($socketError > 0) {
+ if ($socketInstance->getLastSocketErrorCode() > 0) {
// Handle this socket error with a faked recipientData array
- $socketInstance->handleSocketError(__METHOD__, __LINE__, array('null', '0'));
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData));
} // END - if
// Is the file there?
- if ((FrameworkBootstrap::isReachableFilePath($packageData[0])) && (file_exists($packageData[0]))) {
+ if ((FrameworkBootstrap::isReachableFilePath($packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE])) && (file_exists($packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE]))) {
// Old socket found
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: WARNING: Old socket at ' . $packageData[0] . ' found. Will not start.');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: WARNING: Old socket at ' . $packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE] . ' found. Will not start.');
// Shutdown this socket
$socketInstance->shutdownSocket();
} // END - if
// Debug message
- self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Binding to ' . $packageData[0] . ' ...');
+ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Binding to ' . $packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE] . ' ...');
// Try to bind to it
- if (!$socketInstance->bindSocketTo($packageData[0])) {
+ if (!$socketInstance->bindSocketTo($packageData[StorableSocket::SOCKET_ARRAY_INDEX_FILE])) {
// Handle error here
- $socketInstance->handleSocketError(__METHOD__, __LINE__, $packageData);
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData));
} // END - if
// Start listen for connections
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Listening for connections.');
if (!$socketInstance->listenOnSocket()) {
// Handle this socket error with a faked recipientData array
- $socketInstance->handleSocketError(__METHOD__, __LINE__, $packageData);
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData));
} // END - if
// Allow non-blocking I/O
self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('SOCKET-FILE-LISTENER: Setting non-blocking mode.');
if (!$socketInstance->enableSocketNonBlocking()) {
// Handle this socket error with a faked recipientData array
- $socketInstance->handleSocketError(__METHOD__, __LINE__, $packageData);
+ $socketInstance->handleSocketError(__METHOD__, __LINE__, array_values($packageData));
} // END - if
// Return socket instance
// Construct container class, this won't be reached if an exception is thrown
// @TODO Somehow handle $infoInstance to this factory
- $socketInstance = ObjectFactory::createObjectByConfiguredName('socket_container_class', array($socketResource, 'tcp', $packageData, NULL));
+ $socketInstance = ObjectFactory::createObjectByConfiguredName('socket_container_class', array($socketResource, StorableSocket::SOCKET_PROTOCOL_TCP, $packageData, NULL));
// Is the socket resource valid?
if (!$socketInstance->isValidSocket()) {
throw new SocketCreationException(array($factoryInstance, $socketInstance->getSocketResource()), StorableSocket::EXCEPTION_SOCKET_CREATION_FAILED);
} // END - if
- // Get socket error code for verification
- $socketError = $socketInstance->getLastSocketErrorCode();
-
// Check if there was an error else
- if ($socketError > 0) {
+ if ($socketInstance->getLastSocketErrorCode() > 0) {
// Handle this socket error with a faked recipientData array
$helperInstance->handleSocketError(__METHOD__, __LINE__, $socketInstance, array('0.0.0.0', '0'));
} // END - if
);
// Create socket instance
- $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, 'file', $packageData, NULL));
+ $socketInstance = self::createObjectByConfiguredName('socket_container_class', array($socketResource, StorableSocket::SOCKET_PROTOCOL_TCP, $packageData, NULL));
// Is the socket resource valid?
if (!$socketInstance->isValidSocket()) {
throw new InvalidSocketException(array($listenerInstance, $socketInstance), self::EXCEPTION_INVALID_SOCKET);
} // END - if
- // Get socket error code for verification
- $socketError = $socketInstance->getLastSocketErrorCode();
-
// Check if there was an error else
- if ($socketError > 0) {
+ if ($socketInstance->getLastSocketErrorCode() > 0) {
// Handle this socket error with a faked recipientData array
$socketInstance->handleSocketError(__METHOD__, __LINE__, array('0.0.0.0', '0'));
} // END - if