Skip to content

Commit

Permalink
Merge pull request #58 from WarriorXK/release/2.6.0
Browse files Browse the repository at this point in the history
Release/2.6.0
  • Loading branch information
WarriorXK authored Dec 31, 2019
2 parents 754f1c1 + 43aa66f commit 6d0270a
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 54 deletions.
1 change: 1 addition & 0 deletions .idea/PHPWebSockets.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/php-test-framework.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4snapshot
- 7.4
- nightly

env:
Expand All @@ -22,7 +22,6 @@ env:
matrix:
fast_finish: true
allow_failures:
- php: 7.4snapshot
- php: nightly

before_script:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5.0
2.6.0
8 changes: 5 additions & 3 deletions src/PHPWebSockets.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ final class PHPWebSockets {
* @param \PHPWebSockets\IStreamContainer[] $updateObjects
* @param float|null $timeout The amount of seconds to wait for updates, setting this value to NULL causes this function to block indefinitely until there is an update
*
* @throws \InvalidArgumentException
*
* @return \Generator|\PHPWebSockets\AUpdate[]
*/
public static function MultiUpdate(array $updateObjects, float $timeout = NULL) : \Generator {
Expand All @@ -173,15 +171,19 @@ public static function MultiUpdate(array $updateObjects, float $timeout = NULL)

}

/** @var \PHPWebSockets\IStreamContainer[] $objectStreamMap */
$objectStreamMap = [];
/** @var resource[] $exceptional */
$exceptional = [];
/** @var resource[] $write */
$write = [];
/** @var resource[] $read */
$read = [];

foreach ($updateObjects as $object) {

if (!$object instanceof \PHPWebSockets\IStreamContainer) {
throw new \InvalidArgumentException('Got invalid object, all provided objects should implement of \PHPWebSockets\IStreamContainer');
throw new \InvalidArgumentException('Got invalid object, all provided objects should implement ' . \PHPWebSockets\IStreamContainer::class);
}

yield from $object->beforeStreamSelect();
Expand Down
16 changes: 11 additions & 5 deletions src/PHPWebSockets/AConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ abstract class AConnection implements IStreamContainer, LoggerAwareInterface {
* Sets the maximum size for the handshake in bytes
*
* @param int $maxLength
*
* @return void
*/
public function setMaxHandshakeLength(int $maxLength) {
$this->_maxHandshakeLength = $maxLength;
Expand Down Expand Up @@ -263,6 +265,8 @@ public function writeUntilEmpty(float $timeout = NULL) : bool {

/**
* Sets that we should close the connection after all our writes have finished
*
* @return void
*/
public function setCloseAfterWrite() {
$this->_closeAfterWrite = TRUE;
Expand Down Expand Up @@ -290,8 +294,6 @@ protected function _afterOpen() {
*
* @param string $newData
*
* @throws \Exception
*
* @return \Generator|\PHPWebSockets\AUpdate[]
*/
protected function _handlePacket(string $newData) : \Generator {
Expand Down Expand Up @@ -666,7 +668,7 @@ protected function _afterReportClose() {
* @param int $opcode
* @param int $frameSize
*
* @throws \Exception
* @return void
*/
public function writeMultiFramed(string $data, int $opcode = \PHPWebSockets::OPCODE_FRAME_TEXT, int $frameSize = 65535) {

Expand Down Expand Up @@ -732,7 +734,7 @@ public function writeRaw(string $data, bool $priority) {
* @param int $opcode
* @param bool $isFinal
*
* @throws \Exception
* @return void
*/
public function write(string $data, int $opcode = \PHPWebSockets::OPCODE_FRAME_TEXT, bool $isFinal = TRUE) {
$this->writeRaw(Framer::Frame($data, $this->_shouldMask(), $opcode, $isFinal), \PHPWebSockets::IsPriorityOpcode($opcode));
Expand Down Expand Up @@ -766,7 +768,7 @@ public function waitUntilDisconnect(float $timeout = NULL) : bool {
* @param string $reason
* @param float $timeout
*
* @throws \Exception
* @return void
*/
public function sendDisconnect(int $code, string $reason = '', float $timeout = 10.0) {

Expand Down Expand Up @@ -809,6 +811,8 @@ protected function _checkRSVBits(array $headers) : bool {
* If TRUE is returned from the callback a memory buffer will be used instead
*
* @param callable|null $callable
*
* @return void
*/
public function setNewMessageStreamCallback(callable $callable = NULL) {
$this->_newMessageStreamCallback = $callable;
Expand Down Expand Up @@ -852,6 +856,8 @@ public function getOpenedTimestamp() {
* Sets the maximum amount of bytes to write per cycle
*
* @param int $rate
*
* @return void
*/
public function setWriteRate(int $rate) {
$this->_writeRate = $rate;
Expand Down
6 changes: 3 additions & 3 deletions src/PHPWebSockets/AUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class AUpdate {
/**
* The source object related to this update
*
* @var object|null
* @var \PHPWebSockets\AConnection|null
*/
protected $_sourceConnection = NULL;

Expand All @@ -51,9 +51,9 @@ abstract class AUpdate {
*/
protected $_code = 0;

public function __construct(int $code, AConnection $sourceObject = NULL) {
public function __construct(int $code, AConnection $sourceConnection = NULL) {

$this->_sourceConnection = $sourceObject;
$this->_sourceConnection = $sourceConnection;
$this->_code = $code;

if (\PHPWebSockets::ShouldUpdateTrace($this)) {
Expand Down
2 changes: 2 additions & 0 deletions src/PHPWebSockets/BasicLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function log($level, $message, array $context = []) {
* Enables or disables debug logging
*
* @param bool $debug
*
* @return void
*/
public function setDebugMode(bool $debug) {
$this->_debug = $debug;
Expand Down
36 changes: 23 additions & 13 deletions src/PHPWebSockets/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ class Client extends AConnection {
*/
protected $_handshakeAccepted = FALSE;

/**
* If we are currently trying to connect async
*
* @var bool
*/
protected $_isConnectingAsync = FALSE;

/**
* The last error received from the stream
*
Expand Down Expand Up @@ -107,15 +114,12 @@ public function __construct(LoggerInterface $logger = NULL) {
* @param resource $resource
* @param string $path
*
* @throws \InvalidArgumentException
* @throws \LogicException
*
* @return bool
*/
public function connectToResource($resource, string $path = '/') {
public function connectToResource($resource, string $path = '/') : bool {

if (!is_resource($resource)) {
throw new \InvalidArgumentException('Argument is not a resource!');
throw new \InvalidArgumentException('Argument 1 is not a resource!');
}

if ($this->isOpen()) {
Expand All @@ -137,18 +141,21 @@ public function connectToResource($resource, string $path = '/') {
* @param string $address
* @param string $path
* @param array $streamContext
*
* @throws \LogicException
* @param bool $async
*
* @return bool
*/
public function connect(string $address, string $path = '/', array $streamContext = []) {
public function connect(string $address, string $path = '/', array $streamContext = [], bool $async = FALSE) {

if ($this->isOpen()) {
throw new \LogicException('The connection is already open!');
}

$this->_stream = @stream_socket_client($address, $this->_streamLastErrorCode, $this->_streamLastError, $this->getConnectTimeout(), STREAM_CLIENT_CONNECT, stream_context_create($streamContext));
$this->_isConnectingAsync = $async;

$flags = ($async ? STREAM_CLIENT_ASYNC_CONNECT : STREAM_CLIENT_CONNECT);

$this->_stream = @stream_socket_client($address, $this->_streamLastErrorCode, $this->_streamLastError, $this->getConnectTimeout(), $flags, stream_context_create($streamContext));
if ($this->_stream === FALSE) {
return FALSE;
}
Expand Down Expand Up @@ -207,17 +214,13 @@ public function getLastError() {
*
* @param float|null $timeout The amount of seconds to wait for updates, setting this value to NULL causes this function to block indefinitely until there is an update
*
* @throws \Exception
*
* @return \Generator|\PHPWebSockets\AUpdate[]
*/
public function update(float $timeout = NULL) : \Generator {
yield from \PHPWebSockets::MultiUpdate([$this], $timeout);
}

/**
* @throws \Exception
*
* @return \Generator|\PHPWebSockets\AUpdate[]
*/
public function handleRead() : \Generator {
Expand All @@ -240,6 +243,8 @@ public function handleRead() : \Generator {

if ($this->_remoteSentDisconnect && $this->_weSentDisconnect) {
yield new Update\Read(Update\Read::C_SOCK_DISCONNECT, $this);
} elseif ($this->_isConnectingAsync) {
yield new Update\Error(Update\Error::C_ASYNC_CONNECT_FAILED, $this);
} else {
yield new Update\Error(Update\Error::C_READ_UNEXPECTED_DISCONNECT, $this);
}
Expand All @@ -250,6 +255,9 @@ public function handleRead() : \Generator {

} else {

// If we've received data we can be sure we're connected
$this->_isConnectingAsync = FALSE;

$handshakeAccepted = $this->handshakeAccepted();
if (!$handshakeAccepted) {

Expand Down Expand Up @@ -349,6 +357,8 @@ public function getUserAgent() : string {
* @see https://tools.ietf.org/html/rfc6455#section-5.3
*
* @param bool $mask
*
* @return void
*/
public function setMasksPayload(bool $mask) {
$this->_shouldMask = $mask;
Expand Down
2 changes: 0 additions & 2 deletions src/PHPWebSockets/Framer.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ public static function GetFramePayload(string $frame, array $headers = NULL) {
* @param bool $isFinal
* @param int $rsv
*
* @throws \Exception
*
* @return string
*/
public static function Frame(string $data, bool $mask, int $opcode = \PHPWebSockets::OPCODE_FRAME_TEXT, bool $isFinal = TRUE, int $rsv = 0) : string {
Expand Down
Loading

0 comments on commit 6d0270a

Please sign in to comment.