use HelperTrait;
/**
- * Socket protocol:
- * - 'tcp' for TCP/IPv4 connections
- * - 'file' for Uni* file-based sockets
+ * "Cached" configuration entries
*/
- private $socketProtocol = StorableSocket::SOCKET_PROTOCOL_INVALID;
+ private static $configEntries = [];
/**
* An array with all valid connection types
*/
- private $connectionTypes = [];
+ private static $connectionTypes = [];
/**
- * Timeout in usec for socket_select()
+ * Socket protocol:
+ * - 'tcp' for TCP/IPv4 connections
+ * - 'file' for Uni* file-based sockets
*/
- private $socketSelectTimeout = 0;
+ private $socketProtocol = StorableSocket::SOCKET_PROTOCOL_INVALID;
/**
* Package data instance
// Call parent constructor
parent::__construct(__CLASS__);
- // Init value
- $this->socketSelectTimeout = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('socket_select_timeout_usec');
-
- // Init array of connection types
- $this->connectionTypes = array(
- StorableSocket::CONNECTION_TYPE_INCOMING,
- StorableSocket::CONNECTION_TYPE_OUTGOING,
- StorableSocket::CONNECTION_TYPE_SERVER
- );
+ // Is the "cache" there?
+ if (count(self::$configEntries) == 0) {
+ // Init value
+ self::$configEntries['socket_select_timeout'] = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('socket_select_timeout_usec');
+
+ // Init array of connection types
+ self::$connectionTypes = [
+ StorableSocket::CONNECTION_TYPE_INCOMING,
+ StorableSocket::CONNECTION_TYPE_OUTGOING,
+ StorableSocket::CONNECTION_TYPE_SERVER,
+ ];
+ }
}
/**
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: peerAddress[%s]=%s,peerPort[%s]=%d - CALLED!', strtoupper($this->getSocketProtocol()), gettype($peerAddress), $peerAddress, gettype($peerPort), $peerPort));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
}
// Init result
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
}
// Get package data
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
}
// Get recipient
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
}
// Get recipient UNL
//* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
}
// Get error code from socket
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
}
// Get error code and message
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
} elseif (is_null($this->getSocketFile())) {
// Throw exception
throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
} elseif (is_null($this->getListenerInstance())) {
// Required listener not set
throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
}
// Try to listen on socket
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
}
// Try to set non-blocking I/O
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
}
// Tries to set option
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
}
// Get recipient UNL
}
// Is it valid?
- $isValid = in_array($connectionType, $this->connectionTypes, TRUE);
+ $isValid = in_array($connectionType, self::$connectionTypes, TRUE);
// Return result
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: isValid=%d - EXIT!', strtoupper($this->getSocketProtocol()), intval($isValid)));
/* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
}
// Init all arrays, at least readers
$excepts = [];
// Check if we have some peers left
- /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Calling socket_select(%d,%d,%d,0%d) ...', strtoupper($this->getSocketProtocol()), count($readers), count($writers), count($excepts), $this->socketSelectTimeout));
+ /* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Calling socket_select(%d,%d,%d,0%d) ...', strtoupper($this->getSocketProtocol()), count($readers), count($writers), count($excepts), self::$configEntries['socket_select_timeout']));
$left = socket_select(
$readers,
$writers,
$excepts,
0,
- $this->socketSelectTimeout
+ self::$configEntries['socket_select_timeout']
);
// Some new peers found?
/* EXTREME-NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
}
/*
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: Clearing socket error, this->socketResource=%s - CALLED!', strtoupper($this->getSocketProtocol()), $this->getSocketResource()));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
} elseif ($this->getLastSocketErrorCode() === 0) {
// Nothing to clear
throw new BadMethodCallException(sprintf('Socket "%s" has no error reported, but method is called.', $this->getSocketResource()));
//* PRINTR-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: socketBuffer=%s', strtoupper($this->getSocketProtocol()), print_r($socketBuffer, TRUE)));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
} elseif (!isset($socketBuffer[NetworkPackageHandler::RAW_INDEX_DIFF])) {
// Opps, should not happen
throw new InvalidArgumentException(sprintf('socketBuffer[%s] is not set.', NetworkPackageHandler::RAW_INDEX_DIFF));
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: method=%s,line=%d,lastSocketErrorCode=%d - CALLED!', strtoupper($this->getSocketProtocol()), $method, $line, $this->getLastSocketErrorCode()));
if (!$this->isValidSocket()) {
// No resource, abort here
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
}
// Get error code for first validation (0 is not an error)
// If the error code is zero, someone called this method without an error
if ($errorCode == 0) {
// No error detected (or previously cleared outside this method)
- throw new NoSocketErrorDetectedException(array($this), BaseListener::EXCEPTION_NO_SOCKET_ERROR);
+ throw new NoSocketErrorDetectedException([$this], BaseListener::EXCEPTION_NO_SOCKET_ERROR);
}
// Get handler (method) name
/* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('%s-SOCKET: level=%d,optionName=%d,optionValue[]=%s - CALLED!', strtoupper($this->getSocketProtocol()), $level, $optionName, gettype($optionValue)));
if (!$this->isValidSocket()) {
// Throw exception
- throw new InvalidSocketException(array($this), self::EXCEPTION_INVALID_SOCKET);
+ throw new InvalidSocketException([$this], self::EXCEPTION_INVALID_SOCKET);
}
// Set it