forked from kevinowino869/mitrobill
update PEAR
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
|
||||
/**
|
||||
* Wrapper for network stream functionality.
|
||||
|
||||
*
|
||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||
|
||||
@ -14,7 +15,7 @@ This package abstracts this away, so that when you want to get exactly N amount
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 1.0.0a5
|
||||
* @version 1.0.0b2
|
||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||
*/
|
||||
/**
|
||||
|
@ -2,19 +2,20 @@
|
||||
|
||||
/**
|
||||
* Wrapper for network stream functionality.
|
||||
*
|
||||
|
||||
*
|
||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||
|
||||
This package abstracts this away, so that when you want to get exactly N amount of bytes, you can be sure the upper levels of your app will be dealing with N bytes. Oh, and the functionality is nicely wrapped in an object (but that's just the icing on the cake).
|
||||
*
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
*
|
||||
* @category Net
|
||||
* @package PEAR2_Net_Transmitter
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 1.0.0a5
|
||||
* @version 1.0.0b2
|
||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||
*/
|
||||
/**
|
||||
@ -24,9 +25,9 @@ namespace PEAR2\Net\Transmitter;
|
||||
|
||||
/**
|
||||
* A filter collection.
|
||||
*
|
||||
*
|
||||
* Represents a collection of stream filters.
|
||||
*
|
||||
*
|
||||
* @category Net
|
||||
* @package PEAR2_Net_Transmitter
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
@ -37,21 +38,25 @@ namespace PEAR2\Net\Transmitter;
|
||||
class FilterCollection implements \SeekableIterator, \Countable
|
||||
{
|
||||
/**
|
||||
* @var array The filter collection itself.
|
||||
* The filter collection itself.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $filters = array();
|
||||
|
||||
|
||||
/**
|
||||
* @var int A pointer, as required by SeekableIterator.
|
||||
* A pointer, as required by SeekableIterator.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $position = 0;
|
||||
|
||||
|
||||
/**
|
||||
* Appends a filter to the collection
|
||||
*
|
||||
*
|
||||
* @param string $name The name of the filter.
|
||||
* @param array $params An array of parameters for the filter.
|
||||
*
|
||||
*
|
||||
* @return $this The collection itself.
|
||||
*/
|
||||
public function append($name, array $params = array())
|
||||
@ -59,19 +64,19 @@ class FilterCollection implements \SeekableIterator, \Countable
|
||||
$this->filters[] = array((string) $name, $params);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inserts the filter before a position.
|
||||
*
|
||||
*
|
||||
* Inserts the specified filter before a filter at a specified position. The
|
||||
* new filter takes the specified position, while previous filters are moved
|
||||
* forward by one.
|
||||
*
|
||||
*
|
||||
* @param int $position The position before which the filter will be
|
||||
* inserted.
|
||||
* @param string $name The name of the filter.
|
||||
* @param array $params An array of parameters for the filter.
|
||||
*
|
||||
*
|
||||
* @return $this The collection itself.
|
||||
*/
|
||||
public function insertBefore($position, $name, array $params = array())
|
||||
@ -94,12 +99,12 @@ class FilterCollection implements \SeekableIterator, \Countable
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Removes a filter at a specified position.
|
||||
*
|
||||
*
|
||||
* @param int $position The position from which to remove a filter.
|
||||
*
|
||||
*
|
||||
* @return $this The collection itself.
|
||||
*/
|
||||
public function removeAt($position)
|
||||
@ -108,10 +113,10 @@ class FilterCollection implements \SeekableIterator, \Countable
|
||||
$this->filters = array_values($this->filters);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clears the collection
|
||||
*
|
||||
*
|
||||
* @return $this The collection itself.
|
||||
*/
|
||||
public function clear()
|
||||
@ -122,7 +127,7 @@ class FilterCollection implements \SeekableIterator, \Countable
|
||||
|
||||
/**
|
||||
* Gets the number of filters in the collection.
|
||||
*
|
||||
*
|
||||
* @return int The number of filters in the collection.
|
||||
*/
|
||||
public function count()
|
||||
@ -132,7 +137,7 @@ class FilterCollection implements \SeekableIterator, \Countable
|
||||
|
||||
/**
|
||||
* Resets the pointer to 0.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE if the collection is not empty, FALSE otherwise.
|
||||
*/
|
||||
public function rewind()
|
||||
@ -142,9 +147,9 @@ class FilterCollection implements \SeekableIterator, \Countable
|
||||
|
||||
/**
|
||||
* Moves the pointer to a specified position.
|
||||
*
|
||||
*
|
||||
* @param int $position The position to move to.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE if the specified position is valid, FALSE otherwise.
|
||||
*/
|
||||
public function seek($position)
|
||||
@ -152,10 +157,10 @@ class FilterCollection implements \SeekableIterator, \Countable
|
||||
$this->position = $position;
|
||||
return $this->valid();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the current position.
|
||||
*
|
||||
*
|
||||
* @return int The current position.
|
||||
*/
|
||||
public function getCurrentPosition()
|
||||
@ -165,7 +170,7 @@ class FilterCollection implements \SeekableIterator, \Countable
|
||||
|
||||
/**
|
||||
* Moves the pointer forward by 1.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE if the new position is valid, FALSE otherwise.
|
||||
*/
|
||||
public function next()
|
||||
@ -176,8 +181,8 @@ class FilterCollection implements \SeekableIterator, \Countable
|
||||
|
||||
/**
|
||||
* Gets the filter name at the current pointer position.
|
||||
*
|
||||
* @return string The name of the filter at the current position.
|
||||
*
|
||||
* @return string|false The name of the filter at the current position.
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
@ -186,9 +191,9 @@ class FilterCollection implements \SeekableIterator, \Countable
|
||||
|
||||
/**
|
||||
* Gets the filter parameters at the current pointer position.
|
||||
*
|
||||
* @return array An array of parameters for the filter at the current
|
||||
* position.
|
||||
*
|
||||
* @return array|false An array of parameters for the filter at the current
|
||||
* position, or FALSE if the position is not valid.
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
@ -197,7 +202,7 @@ class FilterCollection implements \SeekableIterator, \Countable
|
||||
|
||||
/**
|
||||
* Moves the pointer backwards by 1.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE if the new position is valid, FALSE otherwise.
|
||||
*/
|
||||
public function prev()
|
||||
@ -208,7 +213,7 @@ class FilterCollection implements \SeekableIterator, \Countable
|
||||
|
||||
/**
|
||||
* Moves the pointer to the last valid position.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE if the collection is not empty, FALSE otherwise.
|
||||
*/
|
||||
public function end()
|
||||
@ -219,7 +224,7 @@ class FilterCollection implements \SeekableIterator, \Countable
|
||||
|
||||
/**
|
||||
* Checks if the pointer is still pointing to an existing offset.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE if the pointer is valid, FALSE otherwise.
|
||||
*/
|
||||
public function valid()
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
/**
|
||||
* Wrapper for network stream functionality.
|
||||
|
||||
*
|
||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||
|
||||
@ -14,7 +15,7 @@ This package abstracts this away, so that when you want to get exactly N amount
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 1.0.0a5
|
||||
* @version 1.0.0b2
|
||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||
*/
|
||||
/**
|
||||
|
@ -2,19 +2,20 @@
|
||||
|
||||
/**
|
||||
* Wrapper for network stream functionality.
|
||||
*
|
||||
|
||||
*
|
||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||
|
||||
This package abstracts this away, so that when you want to get exactly N amount of bytes, you can be sure the upper levels of your app will be dealing with N bytes. Oh, and the functionality is nicely wrapped in an object (but that's just the icing on the cake).
|
||||
*
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
*
|
||||
* @category Net
|
||||
* @package PEAR2_Net_Transmitter
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 1.0.0a5
|
||||
* @version 1.0.0b2
|
||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||
*/
|
||||
/**
|
||||
@ -24,10 +25,10 @@ namespace PEAR2\Net\Transmitter;
|
||||
|
||||
/**
|
||||
* A network transmitter.
|
||||
*
|
||||
* This is a convinience wrapper for network streams. Used to ensure data
|
||||
*
|
||||
* This is a convenience wrapper for network streams. Used to ensure data
|
||||
* integrity.
|
||||
*
|
||||
*
|
||||
* @category Net
|
||||
* @package PEAR2_Net_Transmitter
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
@ -62,22 +63,27 @@ abstract class NetworkStream extends Stream
|
||||
* negotiated between 1.0 and 1.2).
|
||||
*/
|
||||
const CRYPTO_TLS = 'TLS';
|
||||
|
||||
|
||||
/**
|
||||
* @var string The type of stream. Can be either "_CLIENT" or "_SERVER".
|
||||
* Used to complement the encryption type. Must be set by child classes
|
||||
* for {@link setCrypto()} to work properly.
|
||||
* The type of stream. Can be either "_CLIENT" or "_SERVER".
|
||||
*
|
||||
* Used to complement the encryption type. Must be set by child classes
|
||||
* for {@link setCrypto()} to work properly.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $streamType = '';
|
||||
|
||||
/**
|
||||
* @var string The current cryptography setting.
|
||||
* The current cryptography setting.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $crypto = '';
|
||||
|
||||
/**
|
||||
* Wraps around the specified stream.
|
||||
*
|
||||
*
|
||||
* @param resource $stream The stream to wrap around.
|
||||
*/
|
||||
public function __construct($stream)
|
||||
@ -87,7 +93,7 @@ abstract class NetworkStream extends Stream
|
||||
|
||||
/**
|
||||
* Gets the current cryptography setting.
|
||||
*
|
||||
*
|
||||
* @return string One of this class' CRYPTO_* constants.
|
||||
*/
|
||||
public function getCrypto()
|
||||
@ -97,10 +103,10 @@ abstract class NetworkStream extends Stream
|
||||
|
||||
/**
|
||||
* Sets the current connection's cryptography setting.
|
||||
*
|
||||
*
|
||||
* @param string $type The encryption type to set. Must be one of this
|
||||
* class' CRYPTO_* constants.
|
||||
*
|
||||
*
|
||||
* @return boolean TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function setCrypto($type)
|
||||
@ -123,12 +129,12 @@ abstract class NetworkStream extends Stream
|
||||
|
||||
/**
|
||||
* Checks whether the stream is available for operations.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE if the stream is available, FALSE otherwise.
|
||||
*/
|
||||
public function isAvailable()
|
||||
{
|
||||
if (parent::isStream($this->stream)) {
|
||||
if ($this->isStream($this->stream)) {
|
||||
if ($this->isBlocking && feof($this->stream)) {
|
||||
return false;
|
||||
}
|
||||
@ -137,14 +143,14 @@ abstract class NetworkStream extends Stream
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the size of a stream's buffer.
|
||||
*
|
||||
* @param int $size The desired size of the buffer, in bytes.
|
||||
* @param string $direction The buffer of which direction to set. Valid
|
||||
*
|
||||
* @param int $size The desired size of the buffer, in bytes.
|
||||
* @param int $direction The buffer of which direction to set. Valid
|
||||
* values are the DIRECTION_* constants.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function setBuffer($size, $direction = self::DIRECTION_ALL)
|
||||
@ -157,15 +163,15 @@ abstract class NetworkStream extends Stream
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Shutdown a full-duplex connection
|
||||
*
|
||||
*
|
||||
* Shutdowns (partially or not) a full-duplex connection.
|
||||
*
|
||||
* @param string $direction The direction for which to disable further
|
||||
*
|
||||
* @param int $direction The direction for which to disable further
|
||||
* communications.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function shutdown($direction = self::DIRECTION_ALL)
|
||||
|
@ -2,19 +2,20 @@
|
||||
|
||||
/**
|
||||
* Wrapper for network stream functionality.
|
||||
*
|
||||
|
||||
*
|
||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||
|
||||
This package abstracts this away, so that when you want to get exactly N amount of bytes, you can be sure the upper levels of your app will be dealing with N bytes. Oh, and the functionality is nicely wrapped in an object (but that's just the icing on the cake).
|
||||
*
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
*
|
||||
* @category Net
|
||||
* @package PEAR2_Net_Transmitter
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 1.0.0a5
|
||||
* @version 1.0.0b2
|
||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||
*/
|
||||
/**
|
||||
@ -29,7 +30,7 @@ use Exception as E;
|
||||
|
||||
/**
|
||||
* Exception thrown when something goes wrong with the connection.
|
||||
*
|
||||
*
|
||||
* @category Net
|
||||
* @package PEAR2_Net_Transmitter
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
@ -40,18 +41,22 @@ class SocketException extends StreamException
|
||||
{
|
||||
|
||||
/**
|
||||
* @var int The system level error code.
|
||||
* The system level error code.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $errorNo;
|
||||
|
||||
/**
|
||||
* @var string The system level error message.
|
||||
* The system level error message.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $errorStr;
|
||||
|
||||
/**
|
||||
* Creates a new socket exception.
|
||||
*
|
||||
*
|
||||
* @param string $message The Exception message to throw.
|
||||
* @param int $code The Exception code.
|
||||
* @param E|null $previous Previous exception thrown,
|
||||
@ -62,7 +67,7 @@ class SocketException extends StreamException
|
||||
* successfully before the failure.
|
||||
* On failure when receiving, this is a string/stream holding
|
||||
* the contents received successfully before the failure.
|
||||
* NULL if the failure occured before the operation started.
|
||||
* NULL if the failure occurred before the operation started.
|
||||
* @param int $errorNo The system level error number.
|
||||
* @param string $errorStr The system level
|
||||
* error message.
|
||||
@ -82,7 +87,7 @@ class SocketException extends StreamException
|
||||
|
||||
/**
|
||||
* Gets the system level error code on the socket.
|
||||
*
|
||||
*
|
||||
* @return int The system level error number.
|
||||
*/
|
||||
public function getSocketErrorNumber()
|
||||
@ -95,7 +100,7 @@ class SocketException extends StreamException
|
||||
|
||||
/**
|
||||
* Gets the system level error message on the socket.
|
||||
*
|
||||
*
|
||||
* @return string The system level error message.
|
||||
*/
|
||||
public function getSocketErrorMessage()
|
||||
@ -105,7 +110,7 @@ class SocketException extends StreamException
|
||||
|
||||
/**
|
||||
* Returns a string representation of the exception.
|
||||
*
|
||||
*
|
||||
* @return string The exception as a string.
|
||||
*/
|
||||
public function __toString()
|
||||
|
@ -2,19 +2,20 @@
|
||||
|
||||
/**
|
||||
* Wrapper for network stream functionality.
|
||||
*
|
||||
|
||||
*
|
||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||
|
||||
This package abstracts this away, so that when you want to get exactly N amount of bytes, you can be sure the upper levels of your app will be dealing with N bytes. Oh, and the functionality is nicely wrapped in an object (but that's just the icing on the cake).
|
||||
*
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
*
|
||||
* @category Net
|
||||
* @package PEAR2_Net_Transmitter
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 1.0.0a5
|
||||
* @version 1.0.0b2
|
||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||
*/
|
||||
/**
|
||||
@ -26,11 +27,11 @@ use Exception as E;
|
||||
|
||||
/**
|
||||
* A stream transmitter.
|
||||
*
|
||||
* This is a convinience wrapper for stream functionality. Used to ensure data
|
||||
*
|
||||
* This is a convenience wrapper for stream functionality. Used to ensure data
|
||||
* integrity. Designed for TCP sockets, but it has intentionally been made to
|
||||
* accept any stream.
|
||||
*
|
||||
*
|
||||
* @category Net
|
||||
* @package PEAR2_Net_Transmitter
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
@ -57,32 +58,44 @@ class Stream
|
||||
const DIRECTION_ALL = 3;
|
||||
|
||||
/**
|
||||
* @var resource The stream to wrap around.
|
||||
* The stream to wrap around.
|
||||
*
|
||||
* @var resource
|
||||
*/
|
||||
protected $stream;
|
||||
|
||||
/**
|
||||
* @var bool Whether to automaticaly close the stream on
|
||||
* object destruction if it's not a persistent one. Setting this to
|
||||
* FALSE may be useful if you're only using this class "part time",
|
||||
* while setting it to TRUE might be useful if you're doing some
|
||||
* "on offs".
|
||||
* Whether to automatically close the stream on object destruction if
|
||||
* it's not a persistent one.
|
||||
*
|
||||
* Setting this to FALSE may be useful if you're only using this class
|
||||
* "part time", while setting it to TRUE might be useful if you're doing
|
||||
* some "one offs".
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $autoClose = false;
|
||||
|
||||
/**
|
||||
* @var bool A flag that tells whether or not the stream is persistent.
|
||||
* A flag that tells whether or not the stream is persistent.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $persist;
|
||||
|
||||
/**
|
||||
* @var bool Whether the wrapped stream is in blocking mode or not.
|
||||
* Whether the wrapped stream is in blocking mode or not.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $isBlocking = true;
|
||||
|
||||
|
||||
/**
|
||||
* @var array An associative array with the chunk size of each direction.
|
||||
* Key is the direction, value is the size in bytes as integer.
|
||||
* An associative array with the chunk size of each direction.
|
||||
*
|
||||
* Key is the direction, value is the size in bytes as integer.
|
||||
*
|
||||
* @var array<int,int>
|
||||
*/
|
||||
protected $chunkSize = array(
|
||||
self::DIRECTION_SEND => 0xFFFFF, self::DIRECTION_RECEIVE => 0xFFFFF
|
||||
@ -90,14 +103,14 @@ class Stream
|
||||
|
||||
/**
|
||||
* Wraps around the specified stream.
|
||||
*
|
||||
*
|
||||
* @param resource $stream The stream to wrap around.
|
||||
* @param bool $autoClose Whether to automaticaly close the stream on
|
||||
* @param bool $autoClose Whether to automatically close the stream on
|
||||
* object destruction if it's not a persistent one. Setting this to
|
||||
* FALSE may be useful if you're only using this class "part time",
|
||||
* while setting it to TRUE might be useful if you're doing some
|
||||
* "on offs".
|
||||
*
|
||||
*
|
||||
* @see static::isFresh()
|
||||
*/
|
||||
public function __construct($stream, $autoClose = false)
|
||||
@ -117,12 +130,14 @@ class Stream
|
||||
|
||||
/**
|
||||
* PHP error handler for connection errors.
|
||||
*
|
||||
*
|
||||
* @param string $level Level of PHP error raised. Ignored.
|
||||
* @param string $message Message raised by PHP.
|
||||
*
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws SocketException That's how the error is handled.
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
protected function handleError($level, $message)
|
||||
@ -132,9 +147,9 @@ class Stream
|
||||
|
||||
/**
|
||||
* Checks if a given variable is a stream resource.
|
||||
*
|
||||
*
|
||||
* @param mixed $var The variable to check.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public static function isStream($var)
|
||||
@ -145,11 +160,11 @@ class Stream
|
||||
|
||||
/**
|
||||
* Checks whether the wrapped stream is fresh.
|
||||
*
|
||||
*
|
||||
* Checks whether the wrapped stream is fresh. A stream is considered fresh
|
||||
* if there hasn't been any activity on it. Particularly useful for
|
||||
* detecting reused persistent connections.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE if the socket is fresh, FALSE otherwise.
|
||||
*/
|
||||
public function isFresh()
|
||||
@ -159,8 +174,8 @@ class Stream
|
||||
|
||||
/**
|
||||
* Checks whether the wrapped stream is a persistent one.
|
||||
*
|
||||
* @return bool TRUE if the stream is a persistent one, FALSE otherwise.
|
||||
*
|
||||
* @return bool TRUE if the stream is a persistent one, FALSE otherwise.
|
||||
*/
|
||||
public function isPersistent()
|
||||
{
|
||||
@ -169,8 +184,8 @@ class Stream
|
||||
|
||||
/**
|
||||
* Checks whether the wrapped stream is a blocking one.
|
||||
*
|
||||
* @return bool TRUE if the stream is a blocking one, FALSE otherwise.
|
||||
*
|
||||
* @return bool TRUE if the stream is a blocking one, FALSE otherwise.
|
||||
*/
|
||||
public function isBlocking()
|
||||
{
|
||||
@ -179,9 +194,9 @@ class Stream
|
||||
|
||||
/**
|
||||
* Sets blocking mode.
|
||||
*
|
||||
*
|
||||
* @param bool $block Sets whether the stream is in blocking mode.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function setIsBlocking($block)
|
||||
@ -193,28 +208,28 @@ class Stream
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the timeout for the stream.
|
||||
*
|
||||
*
|
||||
* @param int $seconds Timeout in seconds.
|
||||
* @param int $microseconds Timeout in microseconds to be added to the
|
||||
* seconds.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function setTimeout($seconds, $microseconds = 0)
|
||||
{
|
||||
return stream_set_timeout($this->stream, $seconds, $microseconds);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the size of a stream's buffer.
|
||||
*
|
||||
* @param int $size The desired size of the buffer, in bytes.
|
||||
* @param string $direction The buffer of which direction to set. Valid
|
||||
*
|
||||
* @param int $size The desired size of the buffer, in bytes.
|
||||
* @param int $direction The buffer of which direction to set. Valid
|
||||
* values are the DIRECTION_* constants.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function setBuffer($size, $direction = self::DIRECTION_ALL)
|
||||
@ -230,18 +245,18 @@ class Stream
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the size of the chunk.
|
||||
*
|
||||
*
|
||||
* To ensure data integrity, as well as to allow for lower memory
|
||||
* consumption, data is sent/received in chunks. This function
|
||||
* allows you to set the size of each chunk. The default is 0xFFFFF.
|
||||
*
|
||||
* @param int $size The desired size of the chunk, in bytes.
|
||||
* @param string $direction The chunk of which direction to set. Valid
|
||||
*
|
||||
* @param int $size The desired size of the chunk, in bytes.
|
||||
* @param int $direction The chunk of which direction to set. Valid
|
||||
* values are the DIRECTION_* constants.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function setChunk($size, $direction = self::DIRECTION_ALL)
|
||||
@ -262,14 +277,14 @@ class Stream
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the size of the chunk.
|
||||
*
|
||||
* @param string $direction The chunk of which direction to get. Valid
|
||||
*
|
||||
* @param int $direction The chunk of which direction to get. Valid
|
||||
* values are the DIRECTION_* constants.
|
||||
*
|
||||
* @return int|array|false The chunk size in bytes,
|
||||
*
|
||||
* @return int|array<int,int>|false The chunk size in bytes,
|
||||
* or an array of chunk sizes with the directions as keys.
|
||||
* FALSE on invalid direction.
|
||||
*/
|
||||
@ -287,18 +302,18 @@ class Stream
|
||||
|
||||
/**
|
||||
* Sends a string or stream over the wrapped stream.
|
||||
*
|
||||
*
|
||||
* Sends a string or stream over the wrapped stream. If a seekable stream is
|
||||
* provided, it will be seeked back to the same position it was passed as,
|
||||
* regardless of the $offset parameter.
|
||||
*
|
||||
*
|
||||
* @param string|resource $contents The string or stream to send.
|
||||
* @param int $offset The offset from which to start sending.
|
||||
* If a stream is provided, and this is set to NULL, sending will start
|
||||
* from the current stream position.
|
||||
* @param int $length The maximum length to send. If omitted,
|
||||
* the string/stream will be sent to its end.
|
||||
*
|
||||
*
|
||||
* @return int The number of bytes sent.
|
||||
*/
|
||||
public function send($contents, $offset = null, $length = null)
|
||||
@ -318,21 +333,22 @@ class Stream
|
||||
) {
|
||||
break;
|
||||
}
|
||||
$bytesNow = @fwrite(
|
||||
$this->stream,
|
||||
fread($contents, $chunkSize)
|
||||
);
|
||||
if (0 != $bytesNow) {
|
||||
$bytes += $bytesNow;
|
||||
} elseif ($this->isBlocking || false === $bytesNow) {
|
||||
throw $this->createException(
|
||||
'Failed while sending stream.',
|
||||
2,
|
||||
null,
|
||||
$bytes
|
||||
$contentsToSend = fread($contents, $chunkSize);
|
||||
if ('' != $contentsToSend) {
|
||||
$bytesNow = @fwrite(
|
||||
$this->stream,
|
||||
$contentsToSend
|
||||
);
|
||||
} else {
|
||||
usleep(300000);
|
||||
if (0 != $bytesNow) {
|
||||
$bytes += $bytesNow;
|
||||
} elseif ($this->isBlocking || false === $bytesNow) {
|
||||
throw $this->createException(
|
||||
'Failed while sending stream.',
|
||||
2,
|
||||
null,
|
||||
$bytes
|
||||
);
|
||||
}
|
||||
}
|
||||
$this->isAcceptingData(null);
|
||||
}
|
||||
@ -364,8 +380,6 @@ class Stream
|
||||
null,
|
||||
$bytes
|
||||
);
|
||||
} else {
|
||||
usleep(300000);
|
||||
}
|
||||
$this->isAcceptingData(null);
|
||||
}
|
||||
@ -375,13 +389,13 @@ class Stream
|
||||
|
||||
/**
|
||||
* Reads from the wrapped stream to receive.
|
||||
*
|
||||
*
|
||||
* Reads from the wrapped stream to receive content as a string.
|
||||
*
|
||||
*
|
||||
* @param int $length The number of bytes to receive.
|
||||
* @param string $what Descriptive string about what is being received
|
||||
* (used in exception messages).
|
||||
*
|
||||
*
|
||||
* @return string The received content.
|
||||
*/
|
||||
public function receive($length, $what = 'data')
|
||||
@ -412,16 +426,16 @@ class Stream
|
||||
|
||||
/**
|
||||
* Reads from the wrapped stream to receive.
|
||||
*
|
||||
*
|
||||
* Reads from the wrapped stream to receive content as a stream.
|
||||
*
|
||||
*
|
||||
* @param int $length The number of bytes to receive.
|
||||
* @param FilterCollection $filters A collection of filters to apply to the
|
||||
* stream while receiving. Note that the filters will not be present on
|
||||
* the stream after receiving is done.
|
||||
* @param string $what Descriptive string about what is being
|
||||
* received (used in exception messages).
|
||||
*
|
||||
*
|
||||
* @return resource The received content.
|
||||
*/
|
||||
public function receiveStream(
|
||||
@ -432,16 +446,16 @@ class Stream
|
||||
$result = fopen('php://temp', 'r+b');
|
||||
$appliedFilters = array();
|
||||
if (null !== $filters) {
|
||||
foreach ($filters as $filtername => $params) {
|
||||
foreach ($filters as $filterName => $params) {
|
||||
$appliedFilters[] = stream_filter_append(
|
||||
$result,
|
||||
$filtername,
|
||||
$filterName,
|
||||
STREAM_FILTER_WRITE,
|
||||
$params
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$chunkSize = $this->chunkSize[self::DIRECTION_RECEIVE];
|
||||
while ($length > 0) {
|
||||
while ($this->isAvailable()) {
|
||||
@ -477,10 +491,10 @@ class Stream
|
||||
|
||||
/**
|
||||
* Checks whether the stream is available for operations.
|
||||
*
|
||||
*
|
||||
* For network streams, this means whether the other end has closed the
|
||||
* connection.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE if the stream is available, FALSE otherwise.
|
||||
*/
|
||||
public function isAvailable()
|
||||
@ -490,13 +504,14 @@ class Stream
|
||||
|
||||
/**
|
||||
* Checks whether there is data to be read from the wrapped stream.
|
||||
*
|
||||
* @param int|null $sTimeout If theere isn't data awaiting currently,
|
||||
*
|
||||
* @param int|null $sTimeout If there isn't data awaiting currently,
|
||||
* wait for it this many seconds for data to arrive. If NULL is
|
||||
* specified, wait indefinetly for that.
|
||||
* specified, wait indefinitely for that.
|
||||
* @param int $usTimeout Microseconds to add to the waiting time.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE if there is data to be read, FALSE otherwise.
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.ShortVariable)
|
||||
*/
|
||||
public function isDataAwaiting($sTimeout = 0, $usTimeout = 0)
|
||||
@ -522,14 +537,15 @@ class Stream
|
||||
|
||||
/**
|
||||
* Checks whether the wrapped stream can be written to without a block.
|
||||
*
|
||||
*
|
||||
* @param int|null $sTimeout If the stream isn't currently accepting data,
|
||||
* wait for it this many seconds to start accepting data. If NULL is
|
||||
* specified, wait indefinetly for that.
|
||||
* specified, wait indefinitely for that.
|
||||
* @param int $usTimeout Microseconds to add to the waiting time.
|
||||
*
|
||||
* @return bool TRUE if the wrapped stream would not block on a write, FALSE
|
||||
* otherwise.
|
||||
*
|
||||
* @return bool TRUE if the wrapped stream would not block on a write,
|
||||
* FALSE otherwise.
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.ShortVariable)
|
||||
*/
|
||||
public function isAcceptingData($sTimeout = 0, $usTimeout = 0)
|
||||
@ -567,7 +583,7 @@ class Stream
|
||||
|
||||
/**
|
||||
* Closes the opened stream, even if it is a persistent one.
|
||||
*
|
||||
*
|
||||
* @return bool TRUE on success, FALSE on failure.
|
||||
*/
|
||||
public function close()
|
||||
@ -577,10 +593,10 @@ class Stream
|
||||
|
||||
/**
|
||||
* Creates a new exception.
|
||||
*
|
||||
*
|
||||
* Creates a new exception. Used by the rest of the functions in this class.
|
||||
* Override in derived classes for custom exception handling.
|
||||
*
|
||||
*
|
||||
* @param string $message The exception message.
|
||||
* @param int $code The exception code.
|
||||
* @param E|null $previous Previous exception thrown,
|
||||
@ -591,7 +607,7 @@ class Stream
|
||||
* successfully before the failure.
|
||||
* On failure when receiving, this is a string/stream holding
|
||||
* the contents received successfully before the failure.
|
||||
*
|
||||
*
|
||||
* @return StreamException The exception to then be thrown.
|
||||
*/
|
||||
protected function createException(
|
||||
|
@ -2,19 +2,20 @@
|
||||
|
||||
/**
|
||||
* Wrapper for network stream functionality.
|
||||
*
|
||||
|
||||
*
|
||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||
|
||||
This package abstracts this away, so that when you want to get exactly N amount of bytes, you can be sure the upper levels of your app will be dealing with N bytes. Oh, and the functionality is nicely wrapped in an object (but that's just the icing on the cake).
|
||||
*
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
*
|
||||
* @category Net
|
||||
* @package PEAR2_Net_Transmitter
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 1.0.0a5
|
||||
* @version 1.0.0b2
|
||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||
*/
|
||||
/**
|
||||
@ -34,7 +35,7 @@ use Exception as E;
|
||||
|
||||
/**
|
||||
* Exception thrown when something goes wrong with the connection.
|
||||
*
|
||||
*
|
||||
* @category Net
|
||||
* @package PEAR2_Net_Transmitter
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
@ -44,18 +45,21 @@ use Exception as E;
|
||||
class StreamException extends RuntimeException implements Exception
|
||||
{
|
||||
/**
|
||||
* @var int|string|resource|null The fragment up until the point of failure.
|
||||
* On failure with sending, this is the number of bytes sent
|
||||
* successfully before the failure.
|
||||
* On failure when receiving, this is a string/stream holding
|
||||
* the contents received successfully before the failure.
|
||||
* NULL if the failure occured before the operation started.
|
||||
* The fragment up until the point of failure.
|
||||
*
|
||||
* On failure with sending, this is the number of bytes sent successfully
|
||||
* before the failure.
|
||||
* On failure when receiving, this is a string/stream holding the contents
|
||||
* received successfully before the failure.
|
||||
* NULL if the failure occurred before the operation started.
|
||||
*
|
||||
* @var int|string|resource|null
|
||||
*/
|
||||
protected $fragment = null;
|
||||
|
||||
/**
|
||||
* Creates a new stream exception.
|
||||
*
|
||||
*
|
||||
* @param string $message The Exception message to throw.
|
||||
* @param int $code The Exception code.
|
||||
* @param E|null $previous Previous exception thrown,
|
||||
@ -66,7 +70,7 @@ class StreamException extends RuntimeException implements Exception
|
||||
* successfully before the failure.
|
||||
* On failure when receiving, this is a string/stream holding
|
||||
* the contents received successfully before the failure.
|
||||
* NULL if the failure occured before the operation started.
|
||||
* NULL if the failure occurred before the operation started.
|
||||
*/
|
||||
public function __construct(
|
||||
$message,
|
||||
@ -80,14 +84,14 @@ class StreamException extends RuntimeException implements Exception
|
||||
|
||||
/**
|
||||
* Gets the stream fragment.
|
||||
*
|
||||
*
|
||||
* @return int|string|resource|null The fragment up until the
|
||||
* point of failure.
|
||||
* On failure with sending, this is the number of bytes sent
|
||||
* successfully before the failure.
|
||||
* On failure when receiving, this is a string/stream holding
|
||||
* the contents received successfully before the failure.
|
||||
* NULL if the failure occured before the operation started.
|
||||
* NULL if the failure occurred before the operation started.
|
||||
*/
|
||||
public function getFragment()
|
||||
{
|
||||
@ -99,7 +103,7 @@ class StreamException extends RuntimeException implements Exception
|
||||
|
||||
/**
|
||||
* Returns a string representation of the exception.
|
||||
*
|
||||
*
|
||||
* @return string The exception as a string.
|
||||
*/
|
||||
public function __toString()
|
||||
|
@ -2,19 +2,20 @@
|
||||
|
||||
/**
|
||||
* Wrapper for network stream functionality.
|
||||
*
|
||||
|
||||
*
|
||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||
|
||||
This package abstracts this away, so that when you want to get exactly N amount of bytes, you can be sure the upper levels of your app will be dealing with N bytes. Oh, and the functionality is nicely wrapped in an object (but that's just the icing on the cake).
|
||||
*
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
*
|
||||
* @category Net
|
||||
* @package PEAR2_Net_Transmitter
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 1.0.0a5
|
||||
* @version 1.0.0b2
|
||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||
*/
|
||||
/**
|
||||
@ -35,10 +36,10 @@ use Exception as E;
|
||||
|
||||
/**
|
||||
* A socket transmitter.
|
||||
*
|
||||
* This is a convinience wrapper for socket functionality. Used to ensure data
|
||||
*
|
||||
* This is a convenience wrapper for socket functionality. Used to ensure data
|
||||
* integrity.
|
||||
*
|
||||
*
|
||||
* @category Net
|
||||
* @package PEAR2_Net_Transmitter
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
@ -49,27 +50,41 @@ class TcpClient extends NetworkStream
|
||||
{
|
||||
|
||||
/**
|
||||
* @var int The error code of the last error on the socket.
|
||||
* The error code of the last error on the socket.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $errorNo = 0;
|
||||
|
||||
/**
|
||||
* @var string The error message of the last error on the socket.
|
||||
* The error message of the last error on the socket.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $errorStr = null;
|
||||
|
||||
|
||||
/**
|
||||
* @var SHM Persistent connection handler. Remains NULL for non-persistent
|
||||
* connections.
|
||||
* Persistent connection handler.
|
||||
*
|
||||
* Remains NULL for non-persistent connections.
|
||||
*
|
||||
* @var SHM
|
||||
*/
|
||||
protected $shmHandler = null;
|
||||
|
||||
|
||||
/**
|
||||
* @var array An array with all connections from this PHP request (as keys)
|
||||
* and their lock state (as a value).
|
||||
* An array with all connections from this PHP request (as keys)
|
||||
* and their lock state (as a value).
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $lockState = array();
|
||||
|
||||
|
||||
/**
|
||||
* Mappings from a protocol name to an URI scheme.
|
||||
*
|
||||
* @var array<string,string>
|
||||
*/
|
||||
protected static $cryptoScheme = array(
|
||||
parent::CRYPTO_OFF => 'tcp',
|
||||
parent::CRYPTO_SSL2 => 'sslv2',
|
||||
@ -77,27 +92,29 @@ class TcpClient extends NetworkStream
|
||||
parent::CRYPTO_SSL => 'ssl',
|
||||
parent::CRYPTO_TLS => 'tls'
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* @var string The URI of this connection.
|
||||
* The URI of this connection.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $uri;
|
||||
|
||||
/**
|
||||
* Creates a new connection with the specified options.
|
||||
*
|
||||
*
|
||||
* @param string $host Hostname (IP or domain) of the server.
|
||||
* @param int $port The port on the server.
|
||||
* @param bool $persist Whether or not the connection should be a
|
||||
* persistent one.
|
||||
* @param float $timeout The timeout for the connection.
|
||||
* @param string $key A string that uniquely identifies the
|
||||
* connection.
|
||||
* connection. Ignored for non-persistent connections.
|
||||
* @param string $crypto Encryption setting. Must be one of the
|
||||
* NetworkStream::CRYPTO_* constants. By default, encryption is
|
||||
* disabled. If the setting has an associated scheme for it, it will be
|
||||
* used, and if not, the setting will be adjusted right after the
|
||||
* connection is estabilished.
|
||||
* connection is established.
|
||||
* @param resource $context A context for the socket.
|
||||
*/
|
||||
public function __construct(
|
||||
@ -114,16 +131,10 @@ class TcpClient extends NetworkStream
|
||||
if (strpos($host, ':') !== false) {
|
||||
$host = "[{$host}]";
|
||||
}
|
||||
$flags = STREAM_CLIENT_CONNECT;
|
||||
if ($persist) {
|
||||
$flags |= STREAM_CLIENT_PERSISTENT;
|
||||
}
|
||||
|
||||
$timeout
|
||||
= null == $timeout ? ini_get('default_socket_timeout') : $timeout;
|
||||
|
||||
$key = rawurlencode($key);
|
||||
|
||||
if (null === $context) {
|
||||
$context = stream_context_get_default();
|
||||
} elseif ((!is_resource($context))
|
||||
@ -133,7 +144,14 @@ class TcpClient extends NetworkStream
|
||||
}
|
||||
$hasCryptoScheme = array_key_exists($crypto, static::$cryptoScheme);
|
||||
$scheme = $hasCryptoScheme ? static::$cryptoScheme[$crypto] : 'tcp';
|
||||
$this->uri = "{$scheme}://{$host}:{$port}/{$key}";
|
||||
$flags = STREAM_CLIENT_CONNECT;
|
||||
if ($persist) {
|
||||
$flags |= STREAM_CLIENT_PERSISTENT;
|
||||
$key = rawurlencode($key);
|
||||
$this->uri = "{$scheme}://{$host}:{$port}/{$key}";
|
||||
} else {
|
||||
$this->uri = "{$scheme}://{$host}:{$port}";
|
||||
}
|
||||
set_error_handler(array($this, 'handleError'));
|
||||
try {
|
||||
parent::__construct(
|
||||
@ -168,7 +186,9 @@ class TcpClient extends NetworkStream
|
||||
} elseif (parent::CRYPTO_OFF !== $crypto) {
|
||||
$this->setCrypto($crypto);
|
||||
}
|
||||
$this->setIsBlocking(parent::CRYPTO_OFF === $crypto);
|
||||
if (parent::CRYPTO_OFF !== $crypto) {
|
||||
$this->setIsBlocking(false);
|
||||
}
|
||||
|
||||
if ($persist) {
|
||||
$this->shmHandler = SHM::factory(
|
||||
@ -180,9 +200,9 @@ class TcpClient extends NetworkStream
|
||||
|
||||
/**
|
||||
* Creates a new exception.
|
||||
*
|
||||
*
|
||||
* Creates a new exception. Used by the rest of the functions in this class.
|
||||
*
|
||||
*
|
||||
* @param string $message The exception message.
|
||||
* @param int $code The exception code.
|
||||
* @param E|null $previous Previous exception thrown,
|
||||
@ -193,7 +213,7 @@ class TcpClient extends NetworkStream
|
||||
* successfully before the failure.
|
||||
* On failure when receiving, this is a string/stream holding
|
||||
* the contents received successfully before the failure.
|
||||
*
|
||||
*
|
||||
* @return SocketException The exception to then be thrown.
|
||||
*/
|
||||
protected function createException(
|
||||
@ -211,26 +231,27 @@ class TcpClient extends NetworkStream
|
||||
$this->errorStr
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Locks transmission.
|
||||
*
|
||||
*
|
||||
* Locks transmission in one or more directions. Useful when dealing with
|
||||
* persistent connections. Note that every send/receive call implicitly
|
||||
* calls this function and then restores it to the previous state. You only
|
||||
* need to call this function if you need to do an uninterrputed sequence of
|
||||
* need to call this function if you need to do an uninterrupted sequence of
|
||||
* such calls.
|
||||
*
|
||||
*
|
||||
* @param int $direction The direction(s) to have locked. Acceptable values
|
||||
* are the DIRECTION_* constants. If a lock for a direction can't be
|
||||
* obtained immediatly, the function will block until one is aquired.
|
||||
* Note that if you specify {@link DIRECTION_ALL}, the sending lock will
|
||||
* be obtained before the receiving one, and if obtaining the receiving
|
||||
* lock afterwards fails, the sending lock will be released too.
|
||||
* obtained immediately, the function will block until one is acquired.
|
||||
* Note that if you specify {@link static::DIRECTION_ALL},
|
||||
* the sending lock will be obtained before the receiving one,
|
||||
* and if obtaining the receiving lock afterwards fails,
|
||||
* the sending lock will be released too.
|
||||
* @param bool $replace Whether to replace all locks with the specified
|
||||
* ones. Setting this to FALSE will make the function only obtain the
|
||||
* locks which are not already obtained.
|
||||
*
|
||||
*
|
||||
* @return int|false The previous state or FALSE if the connection is not
|
||||
* persistent or arguments are invalid.
|
||||
*/
|
||||
@ -256,7 +277,7 @@ class TcpClient extends NetworkStream
|
||||
throw new LockException('Unable to release sending lock.');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
if ($direction & self::DIRECTION_RECEIVE) {
|
||||
if (($old & self::DIRECTION_RECEIVE)
|
||||
@ -292,23 +313,24 @@ class TcpClient extends NetworkStream
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Sends a string or stream to the server.
|
||||
*
|
||||
*
|
||||
* Sends a string or stream to the server. If a seekable stream is
|
||||
* provided, it will be seeked back to the same position it was passed as,
|
||||
* regardless of the $offset parameter.
|
||||
*
|
||||
*
|
||||
* @param string|resource $contents The string or stream to send.
|
||||
* @param int $offset The offset from which to start sending.
|
||||
* If a stream is provided, and this is set to NULL, sending will start
|
||||
* from the current stream position.
|
||||
* @param int $length The maximum length to send. If omitted,
|
||||
* the string/stream will be sent to its end.
|
||||
*
|
||||
*
|
||||
* @return int The number of bytes sent.
|
||||
* @throws E
|
||||
*/
|
||||
public function send($contents, $offset = null, $length = null)
|
||||
{
|
||||
@ -332,13 +354,13 @@ class TcpClient extends NetworkStream
|
||||
|
||||
/**
|
||||
* Receives data from the server.
|
||||
*
|
||||
*
|
||||
* Receives data from the server as a string.
|
||||
*
|
||||
*
|
||||
* @param int $length The number of bytes to receive.
|
||||
* @param string $what Descriptive string about what is being received
|
||||
* (used in exception messages).
|
||||
*
|
||||
*
|
||||
* @return string The received content.
|
||||
*/
|
||||
public function receive($length, $what = 'data')
|
||||
@ -363,16 +385,16 @@ class TcpClient extends NetworkStream
|
||||
|
||||
/**
|
||||
* Receives data from the server.
|
||||
*
|
||||
*
|
||||
* Receives data from the server as a stream.
|
||||
*
|
||||
*
|
||||
* @param int $length The number of bytes to receive.
|
||||
* @param FilterCollection $filters A collection of filters to apply to the
|
||||
* stream while receiving. Note that the filters will not be present on
|
||||
* the stream after receiving is done.
|
||||
* @param string $what Descriptive string about what is being
|
||||
* received (used in exception messages).
|
||||
*
|
||||
*
|
||||
* @return resource The received content.
|
||||
*/
|
||||
public function receiveStream(
|
||||
|
@ -2,19 +2,20 @@
|
||||
|
||||
/**
|
||||
* Wrapper for network stream functionality.
|
||||
*
|
||||
|
||||
*
|
||||
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
|
||||
|
||||
This package abstracts this away, so that when you want to get exactly N amount of bytes, you can be sure the upper levels of your app will be dealing with N bytes. Oh, and the functionality is nicely wrapped in an object (but that's just the icing on the cake).
|
||||
*
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
*
|
||||
* @category Net
|
||||
* @package PEAR2_Net_Transmitter
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
* @copyright 2011 Vasil Rangelov
|
||||
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
|
||||
* @version 1.0.0a5
|
||||
* @version 1.0.0b2
|
||||
* @link http://pear2.php.net/PEAR2_Net_Transmitter
|
||||
*/
|
||||
/**
|
||||
@ -26,12 +27,12 @@ use Exception as E;
|
||||
|
||||
/**
|
||||
* A transmitter for connections to a socket server.
|
||||
*
|
||||
* This is a convinience wrapper for functionality of socket server connections.
|
||||
*
|
||||
* This is a convenience wrapper for functionality of socket server connections.
|
||||
* Used to ensure data integrity. Server handling is not part of the class in
|
||||
* order to allow its usage as part of various server implementations (e.g. fork
|
||||
* and/or sequential).
|
||||
*
|
||||
*
|
||||
* @category Net
|
||||
* @package PEAR2_Net_Transmitter
|
||||
* @author Vasil Rangelov <boen.robot@gmail.com>
|
||||
@ -42,21 +43,26 @@ class TcpServerConnection extends NetworkStream
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string The IP address of the connected client.
|
||||
* The IP address of the connected client.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $peerIP;
|
||||
|
||||
/**
|
||||
* @var int The port of the connected client.
|
||||
* The port of the connected client.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $peerPort;
|
||||
|
||||
/**
|
||||
* Creates a new connection with the specified options.
|
||||
*
|
||||
* @param resource $server A socket server, created with
|
||||
* {@link stream_socket_server()}.
|
||||
* @param float $timeout The timeout for the connection.
|
||||
*
|
||||
* @param resource $server A socket server, created with
|
||||
* {@link stream_socket_server()}.
|
||||
* @param float|null $timeout The timeout for the connection. Leaving this
|
||||
* to NULL uses the default socket timeout.
|
||||
*/
|
||||
public function __construct($server, $timeout = null)
|
||||
{
|
||||
@ -71,15 +77,15 @@ class TcpServerConnection extends NetworkStream
|
||||
set_error_handler(array($this, 'handleError'));
|
||||
try {
|
||||
parent::__construct(
|
||||
stream_socket_accept($server, $timeout, $peername)
|
||||
stream_socket_accept($server, $timeout, $peerName)
|
||||
);
|
||||
restore_error_handler();
|
||||
$portString = strrchr($peername, ':');
|
||||
$portString = strrchr($peerName, ':');
|
||||
$this->peerPort = (int) substr($portString, 1);
|
||||
$ipString = substr(
|
||||
$peername,
|
||||
$peerName,
|
||||
0,
|
||||
strlen($peername) - strlen($portString)
|
||||
strlen($peerName) - strlen($portString)
|
||||
);
|
||||
if (strpos($ipString, '[') === 0
|
||||
&& strpos(strrev($ipString), ']') === 0
|
||||
@ -96,20 +102,20 @@ class TcpServerConnection extends NetworkStream
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the IP address of the connected client.
|
||||
*
|
||||
*
|
||||
* @return string The IP address of the connected client.
|
||||
*/
|
||||
public function getPeerIP()
|
||||
{
|
||||
return $this->peerIP;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the port of the connected client.
|
||||
*
|
||||
*
|
||||
* @return int The port of the connected client.
|
||||
*/
|
||||
public function getPeerPort()
|
||||
@ -119,16 +125,16 @@ class TcpServerConnection extends NetworkStream
|
||||
|
||||
/**
|
||||
* Creates a new exception.
|
||||
*
|
||||
*
|
||||
* Creates a new exception. Used by the rest of the functions in this class.
|
||||
*
|
||||
*
|
||||
* @param string $message The exception message.
|
||||
* @param int $code The exception code.
|
||||
* @param E|null $previous Previous exception thrown, or NULL if there
|
||||
* is none.
|
||||
* @param string|null $fragment The fragment up until the point of failure.
|
||||
* NULL if the failure occured before the operation started.
|
||||
*
|
||||
* NULL if the failure occurred before the operation started.
|
||||
*
|
||||
* @return SocketException The exception to then be thrown.
|
||||
*/
|
||||
protected function createException(
|
||||
|
Reference in New Issue
Block a user