+
+ /**
+ * "Getter" for a valid socket resource from given packae data.
+ *
+ * @param $packageData Raw package data
+ * @param $connectionType Type of connection, can be 'incoming', 'outgoing', 'server' or default
+ * @return $socketResource Socket resource
+ * @throws InvalidConnectionTypeException If the provided connection type is not valid
+ */
+ public function getSocketFromPackageData (array $packageData, $connectionType = NULL) {
+ // Default is no socket
+ $socketResource = FALSE;
+
+ // Temporary resolve recipient field
+ $recipientIpArray = explode(':', HubTools::resolveSessionId($packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
+
+ // Make sure it is a valid ip:port array (2 elements)
+ assert(count($recipientIpArray) == 2);
+
+ // Debug message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: Checking ' . count($this->getAllSockets()) . ' socket(s),recipientIpArray[0]=' . $recipientIpArray[0] . ',recipientIpArray[1]=' . $recipientIpArray[1] . ' ...');
+
+ // Default is all sockets
+ $sockets = $this->getAllSockets();
+
+ // Is connection type set?
+ if ((is_string($connectionType)) && ($this->isValidConnectionType($connectionType))) {
+ // Then get a list of this type
+ $sockets = $this->getSocketsByConnectionType($connectionType);
+ } elseif (is_string($connectionType)) {
+ // Is not a valid connection type!
+ throw new InvalidConnectionTypeException(array($this, $connectionType), self::EXCEPTION_INVALID_CONNECTION_TYPE);
+ }
+
+ // Get all sockets and check them, skip the server socket
+ foreach ($sockets as $socketArray) {
+ // Is this a server socket?
+ if ($socketArray[self::SOCKET_ARRAY_RESOURCE] === $this->getListenerInstance()->getSocketResource()) {
+ // Skip 'server' sockets (local socket)
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: Skipping server socket ' . $socketArray[self::SOCKET_ARRAY_RESOURCE] . ' ...');
+ continue;
+ } // END - if
+
+ // Try to get the "peer"'s name
+ if (!socket_getpeername($socketArray[self::SOCKET_ARRAY_RESOURCE], $peerIp)) {
+ // Handle the socket error with given package data
+ $this->handleSocketError(__METHOD__, __LINE__, $socketArray[self::SOCKET_ARRAY_RESOURCE], explode(':', $packageData[NetworkPackage::PACKAGE_DATA_RECIPIENT]));
+ } // END - if
+
+ // Get
+ // If the "peer" IP and recipient is same, use it
+ if ($peerIp == $recipientIpArray[0]) {
+ // IPs match, so take the socket and quit this loop
+ $socketResource = $socketArray[self::SOCKET_ARRAY_RESOURCE];
+
+ // Debug message
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: peerIp=' . $peerIp . ' matches with recipient IP address. Taking socket=' . $socketArray[self::SOCKET_ARRAY_RESOURCE] . ',type=' . $socketArray[self::SOCKET_ARRAY_CONN_TYPE]);
+ break;
+ } // END - if
+ } // END - foreach
+
+ // Return the determined socket resource
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('POOL[' . __LINE__ . ']: socketResource[' . gettype($socketResource) . ']=' . $socketResource);
+ return $socketResource;
+ }