$bufferSize = $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE];
// Is some data still pending or sent all out?
- /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: bufferSize=%d,socketBuffer[%s]=%d', strtoupper($this->getSocketProtocol()), NetworkPackageHandler::RAW_INDEX_DIFF, $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF]));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: bufferSize=%d,socketBuffer[%s]=%d', strtoupper($this->getSocketProtocol()), $bufferSize, NetworkPackageHandler::RAW_INDEX_DIFF, $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF]));
if ($socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF] >= 0) {
// Reduce buffer size by difference
$bufferSize = $socketBuffer[NetworkPackageHandler::RAW_INDEX_BUFFER_SIZE] - $socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF];
use \InvalidArgumentException;
use \Iterator;
use \LogicException;
+use \OutOfBoundsException;
use \UnexpectedValueException;
/**
* Sends out encoded data to a socket
*
* @return void
+ * @throws BadMethodCallException If this method was unexpectedly called
+ * @throws OutOfBoundsException If an important array element was not found
+ * @throws UnexpectedValueException If an instance of StorableSocket was expected but not provided
+ * @throws LogicException If the socket is somehow invalid but this method was called
*/
public function sendEncodedData () {
// Make sure there is pending encoded data
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('NETWORK-PACKAGE-HANDLER: CALLED!');
- assert($this->isEncodedDataPending());
+ if (!$this->isEncodedDataPending()) {
+ // Throw exception
+ throw new BadMethodCallException('No encoded data is pending but this method was called.');
+ }
// Pop current data from stack
$encodedDataArray = $this->getStackInstance()->popNamed(self::STACKER_NAME_OUTGOING_STREAM);
// Init in this round sent bytes
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: encodedDataArray()=%d has been "popped" from stack %s.', count($encodedDataArray), self::STACKER_NAME_OUTGOING_STREAM));
$sentBytes = 0;
- // Assert on socket instance
- assert(isset($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE]));
- assert($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE] instanceof StorableSocket);
- assert($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE]->isValidSocket());
+ // Check all conditions, throw exceptions if something unexpected happened
+ if (!isset($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE])) {
+ // Array element not found
+ throw new OutOfBoundsException(sprintf('encodedDataArray has no element %s', self::RAW_INDEX_SOCKET_INSTANCE));
+ } elseif (!($encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE] instanceof StorableSocket)) {
+ // Is not a valid instance
+ throw new UnexpectedValueException(sprintf('encodedDataArray[%s] is not an instance of %s', self::RAW_INDEX_SOCKET_INSTANCE, StorableSocket::class));
+ } elseif (!$encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE]->isValidSocket()) {
+ // Invalid socket
+ throw new LogicException(sprintf('encodedDataArray[%s]->socketResource=%s is invalid for some reason', self::RAW_INDEX_SOCKET_INSTANCE, $encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE]->getSocketResource()));
+ }
// Get socket instance and remove it from array
$socketInstance = $encodedDataArray[self::RAW_INDEX_SOCKET_INSTANCE];
// Generate the hash of comparing it
//* DEBUG-DIE: */ ApplicationEntryPoint::exitApplication(sprintf('[%s:%d]: packageInstance=%s', __METHOD__, __LINE__, print_r($packageInstance, TRUE)));
- //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s,senderId=%s,senderPrivateKeyHash=%s', $packageInstance->__toString(), $packageInstance->getSenderId(), $packageInstance->getSenderPrivateKeyHash()));
+ /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('NETWORK-PACKAGE-HANDLER: packageInstance=%s,senderId=%s,senderPrivateKeyHash=%s', $packageInstance->__toString(), $packageInstance->getSenderId(), $packageInstance->getSenderPrivateKeyHash()));
if (empty($packageInstance->getSenderId())) {
// Invalid $packageInstance
throw new InvalidArgumentException('packageInstance does not contain senderId');