Files
.github
admin
install
pages_template
qrcode
system
autoload
PEAR2
Cache
Console
Net
RouterOS
Transmitter
Exception.php
FilterCollection.php
LockException.php
NetworkStream.php
SocketException.php
Stream.php
StreamException.php
TcpClient.php
TcpServerConnection.php
Autoload.php
mail
Admin.php
App.php
Balance.php
File.php
Hookers.php
Http.php
Lang.php
Log.php
Message.php
Mikrotik.php
Package.php
Paginator.php
Password.php
Radius.php
Text.php
Timezone.php
User.php
Validator.php
index.html
cache
controllers
lan
paymentgateway
plugin
uploads
vendor
.htaccess
api.php
boot.php
composer.json
composer.lock
cron.php
cron_reminder.php
index.html
orm.php
updates.json
ui
.gitignore
.htaccess_firewall
CHANGELOG.md
LICENSE
README.md
composer.json
config.sample.php
favicon.ico
index.php
init.php
update.php
version.json
mitrobill/system/autoload/PEAR2/Net/Transmitter/NetworkStream.php

188 lines
5.5 KiB
PHP
Raw Normal View History

2017-03-11 02:51:06 +07:00
<?php
/**
* Wrapper for network stream functionality.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* 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).
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* PHP version 5
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @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
2023-10-05 16:55:44 +07:00
* @version 1.0.0b2
2017-03-11 02:51:06 +07:00
* @link http://pear2.php.net/PEAR2_Net_Transmitter
*/
/**
* The namespace declaration.
*/
namespace PEAR2\Net\Transmitter;
/**
* A network transmitter.
2023-10-05 16:55:44 +07:00
*
* This is a convenience wrapper for network streams. Used to ensure data
2017-03-11 02:51:06 +07:00
* integrity.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @category Net
* @package PEAR2_Net_Transmitter
* @author Vasil Rangelov <boen.robot@gmail.com>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @link http://pear2.php.net/PEAR2_Net_Transmitter
*/
abstract class NetworkStream extends Stream
{
/**
* Used in {@link setCrypto()} to disable encryption.
*/
const CRYPTO_OFF = '';
/**
* Used in {@link setCrypto()} to set encryption to either SSLv2 or SSLv3,
* depending on what the other end supports.
*/
const CRYPTO_SSL = 'SSLv23';
/**
* Used in {@link setCrypto()} to set encryption to SSLv2.
*/
const CRYPTO_SSL2 = 'SSLv2';
/**
* Used in {@link setCrypto()} to set encryption to SSLv3.
*/
const CRYPTO_SSL3 = 'SSLv3';
/**
* Used in {@link setCrypto()} to set encryption to TLS (exact version
* negotiated between 1.0 and 1.2).
*/
const CRYPTO_TLS = 'TLS';
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
2023-10-05 16:55:44 +07:00
* 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
2017-03-11 02:51:06 +07:00
*/
protected $streamType = '';
/**
2023-10-05 16:55:44 +07:00
* The current cryptography setting.
*
* @var string
2017-03-11 02:51:06 +07:00
*/
protected $crypto = '';
/**
* Wraps around the specified stream.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @param resource $stream The stream to wrap around.
*/
public function __construct($stream)
{
parent::__construct($stream, true);
}
/**
* Gets the current cryptography setting.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @return string One of this class' CRYPTO_* constants.
*/
public function getCrypto()
{
return $this->crypto;
}
/**
* Sets the current connection's cryptography setting.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @param string $type The encryption type to set. Must be one of this
* class' CRYPTO_* constants.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @return boolean TRUE on success, FALSE on failure.
*/
public function setCrypto($type)
{
if (self::CRYPTO_OFF === $type) {
$result = stream_socket_enable_crypto($this->stream, false);
} else {
$result = stream_socket_enable_crypto(
$this->stream,
true,
constant("STREAM_CRYPTO_METHOD_{$type}{$this->streamType}")
);
}
if ($result) {
$this->crypto = $type;
}
return $result;
}
/**
* Checks whether the stream is available for operations.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @return bool TRUE if the stream is available, FALSE otherwise.
*/
public function isAvailable()
{
2023-10-05 16:55:44 +07:00
if ($this->isStream($this->stream)) {
2017-03-11 02:51:06 +07:00
if ($this->isBlocking && feof($this->stream)) {
return false;
}
$meta = stream_get_meta_data($this->stream);
return !$meta['eof'];
}
return false;
}
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
* Sets the size of a stream's buffer.
2023-10-05 16:55:44 +07:00
*
* @param int $size The desired size of the buffer, in bytes.
* @param int $direction The buffer of which direction to set. Valid
2017-03-11 02:51:06 +07:00
* values are the DIRECTION_* constants.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @return bool TRUE on success, FALSE on failure.
*/
public function setBuffer($size, $direction = self::DIRECTION_ALL)
{
$result = parent::setBuffer($size, $direction);
if (self::DIRECTION_SEND === $direction
&& function_exists('stream_set_chunk_size') && !$result
) {
return false !== @stream_set_chunk_size($this->stream, $size);
}
return $result;
}
2023-10-05 16:55:44 +07:00
2017-03-11 02:51:06 +07:00
/**
* Shutdown a full-duplex connection
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* Shutdowns (partially or not) a full-duplex connection.
2023-10-05 16:55:44 +07:00
*
* @param int $direction The direction for which to disable further
2017-03-11 02:51:06 +07:00
* communications.
2023-10-05 16:55:44 +07:00
*
2017-03-11 02:51:06 +07:00
* @return bool TRUE on success, FALSE on failure.
*/
public function shutdown($direction = self::DIRECTION_ALL)
{
$directionMap = array(
self::DIRECTION_ALL => STREAM_SHUT_RDWR,
self::DIRECTION_SEND => STREAM_SHUT_WR,
self::DIRECTION_RECEIVE => STREAM_SHUT_RD
);
return array_key_exists($direction, $directionMap)
&& stream_socket_shutdown($this->stream, $directionMap[$direction]);
}
}