Skip to content

Commit

Permalink
Merge pull request #2 from WarriorXK/release/1.3.0
Browse files Browse the repository at this point in the history
Release/1.3.0
  • Loading branch information
WarriorXK authored Dec 2, 2016
2 parents 7c33e1c + d45fcae commit 80a6ff8
Show file tree
Hide file tree
Showing 20 changed files with 2,033 additions and 1,628 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ notifications:
email:
recipients:
- [email protected]
on_success: always
on_success: change
on_failure: always
131 changes: 87 additions & 44 deletions AConnection.php.inc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

// - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - -
// The MIT License (MIT)
//
Expand Down Expand Up @@ -33,125 +34,145 @@ abstract class AConnection implements IStreamContainer {

/**
* The amount of bytes to read to complete our current frame
* @var integer|NULL
*
* @var int|null
*/
protected $_currentFrameRemainingBytes = NULL;

/**
* The opcode of the current partial message
* @var integer|NULL
*
* @var int|null
*/
protected $_partialMessageOpcode = NULL;

/**
* If we've initiated the disconnect
* @var boolean
*
* @var bool
*/
protected $_weInitiateDisconnect = FALSE;

/**
* If we've received the disconnect message from the remote
* @var boolean
*
* @var bool
*/
protected $_remoteSentDisconnect = FALSE;

/**
* The priority frames ready to be send (Takes priority over the normal frames buffer)
*
* @var string[]
*/
protected $_priorityFramesBuffer = [];

/**
* The maximum size the handshake can become
* @var integer
*
* @var int
*/
protected $_maxHandshakeLength = 8192;

/**
* If we've sent the disconnect message to the remote
* @var boolean
*
* @var bool
*/
protected $_weSentDisconnect = FALSE;

/**
* If we should close the connection after our write buffer has been emptied
* @var boolean
*
* @var bool
*/
protected $_closeAfterWrite = FALSE;

/**
* The partial message if the current message hasn't finished yet
* @var string|NULL
*
* @var string|null
*/
protected $_partialMessage = NULL;

/**
* The frames ready to be send
*
* @var string[]
*/
protected $_framesBuffer = [];

/**
* The write buffer
* @var string|NULL
*
* @var string|null
*/
protected $_writeBuffer = NULL;

/**
* The read buffer
* @var string|NULL
*
* @var string|null
*/
protected $_readBuffer = NULL;

/**
* If the RSV1 property is allowed on this connection
* @var boolean
*
* @var bool
*/
protected $_allowRSV1 = FALSE;

/**
* If the RSV2 property is allowed on this connection
* @var boolean
*
* @var bool
*/
protected $_allowRSV2 = FALSE;

/**
* If the RSV3 property is allowed on this connection
* @var boolean
*
* @var bool
*/
protected $_allowRSV3 = FALSE;

/**
* The amount of bytes we write per cycle
* @var integer
*
* @var int
*/
protected $_writeRate = 16384;

/**
* The amount of bytes we read per cycle
* @var integer
*
* @var int
*/
protected $_readRate = 16384;

/**
* Sets the maximum size for the handshake in bytes
* @param integer $maxLength
*
* @param int $maxLength
*/
public function setMaxHandshakeLength(int $maxLength) {
$this->_maxHandshakeLength = $maxLength;
}

/**
* Returns the maximum size for the handshake in bytes
* @return integer
*
* @return int
*/
public function getMaxHandshakeLength() : int {
return $this->_maxHandshakeLength;
}

/**
* Returns if we have (partial)frames ready to be send
* @return boolean
*
* @return bool
*/
public function isWriteBufferEmpty() : bool {
return empty($this->_priorityFramesBuffer) && empty($this->_framesBuffer) && empty($this->_writeBuffer);
Expand All @@ -166,7 +187,9 @@ abstract class AConnection implements IStreamContainer {

/**
* In here we attempt to find frames and unmask them, returns finished messages if available
*
* @param string $newData
*
* @return \PHPWebSocket\AUpdate[]
*/
protected function _handlePacket(string $newData) : \Generator {
Expand Down Expand Up @@ -369,6 +392,7 @@ abstract class AConnection implements IStreamContainer {

/**
* Writes the current buffer to the connection
*
* @return \PHPWebSocket\AUpdate[]
*/
public function handleWrite() : \Generator {
Expand Down Expand Up @@ -418,9 +442,11 @@ abstract class AConnection implements IStreamContainer {

/**
* Splits the provided data into frames of the specified size and sends them to the remote
* @param string $data
* @param integer $opcode
* @param integer $frameSize
*
* @param string $data
* @param int $opcode
* @param int $frameSize
*
* @throws \Exception
*/
public function writeMultiFramed(string $data, int $opcode = \PHPWebSocket::OPCODE_FRAME_TEXT, int $frameSize = 65535) {
Expand All @@ -444,8 +470,9 @@ abstract class AConnection implements IStreamContainer {

/**
* Writes a raw string to the buffer, if priority is set to TRUE it will be send before normal priority messages
* @param string $data
* @param boolean $priority
*
* @param string $data
* @param bool $priority
*/
public function writeRaw(string $data, bool $priority) {

Expand All @@ -459,18 +486,20 @@ abstract class AConnection implements IStreamContainer {

/**
* Queues a string to be written to the remote
* @param string $data
* @param integer $opcode
* @param boolean $isFinal
*
* @param string $data
* @param int $opcode
* @param bool $isFinal
*/
public function write(string $data, int $opcode = \PHPWebSocket::OPCODE_FRAME_TEXT, bool $isFinal = TRUE) {
$this->writeRaw(Framer::Frame($data, $this->_shouldMask(), $opcode, $isFinal), \PHPWebSocket::IsPriorityOpcode($opcode));
}

/**
* Sends a disconnect message to the remote, this causes the connection to be closed once they responds with its disconnect message
* @param integer $code
* @param string $reason
*
* @param int $code
* @param string $reason
*/
public function sendDisconnect(int $code, string $reason = '') {

Expand All @@ -486,16 +515,19 @@ abstract class AConnection implements IStreamContainer {

/**
* Returns TRUE if we are disconnecting
* @return boolean
*
* @return bool
*/
public function isDisconnecting() : bool {
return $this->_weSentDisconnect || $this->_remoteSentDisconnect;
}

/**
* Checks if the remote is in error by sending us one of the RSV bits
* @param array $headers
* @return boolean
*
* @param array $headers
*
* @return bool
*/
protected function _checkRSVBits(array $headers) : bool {

Expand All @@ -508,93 +540,105 @@ abstract class AConnection implements IStreamContainer {

/**
* Sets if we allow the RSV1 bit to be set
* @param boolean $allow
*
* @param bool $allow
*/
public function setAllowRSV1(bool $allow) {
$this->_allowRSV1 = $allow;
}

/**
* Returns if we allow the RSV1 bit to be set
* @return boolean
*
* @return bool
*/
public function getAllowRSV1() : bool {
return $this->_allowRSV1;
}

/**
* Returns if we allow the RSV2 bit to be set
* @param boolean $allow
*
* @param bool $allow
*/
public function setAllowRSV2(bool $allow) {
$this->_allowRSV2 = $allow;
}

/**
* Returns if we allow the RSV2 bit to be set
* @return boolean
*
* @return bool
*/
public function getAllowRSV2() : bool {
return $this->_allowRSV2;
}

/**
* Returns if we allow the RSV3 bit to be set
* @param boolean $allow
*
* @param bool $allow
*/
public function setAllowRSV3(bool $allow) {
$this->_allowRSV3 = $allow;
}

/**
* Returns if we allow the RSV3 bit to be set
* @return boolean
*
* @return bool
*/
public function getAllowRSV3() : bool {
return $this->_allowRSV3;
}

/**
* Sets the maximum amount of bytes to write per cycle
* @param integer $rate
*
* @param int $rate
*/
public function setWriteRate(int $rate) {
$this->_writeRate = $rate;
}

/**
* Returns the maximum amount of bytes to write per cycle
* @return integer
*
* @return int
*/
public function getWriteRate() : int {
return $this->_writeRate;
}

/**
* Sets the maximum amount of bytes to read per cycle
* @param integer $rate
*
* @param int $rate
*/
public function setReadRate(int $rate) {
$this->_readRate = $rate;
}

/**
* Returns the maximum amount of bytes to read per cycle
* @return integer
*
* @return int
*/
public function getReadRate() : int {
return $this->_readRate;
}

/**
* Returns if the frame we are writing should be masked
* @return boolean
*
* @return bool
*/
abstract protected function _shouldMask() : bool;

/**
* Returns TRUE if our connection is open
* @return boolean
*
* @return bool
*/
abstract public function isOpen() : bool;

Expand All @@ -610,5 +654,4 @@ abstract class AConnection implements IStreamContainer {
}

}

}
Loading

0 comments on commit 80a6ff8

Please sign in to comment.