[WebSocket] Performance
No more sprintf calls
This commit is contained in:
parent
0ee6ef6008
commit
3a844edbfd
@ -67,39 +67,22 @@ class Frame implements FrameInterface {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$firstByte = chr(($final ? 128 : 0) + $opcode);
|
$this->defPayLen = strlen($payload);
|
||||||
|
$this->firstByte = ($final ? 128 : 0) + $opcode;
|
||||||
|
$this->secondByte = $this->defPayLen;
|
||||||
|
$this->isCoalesced = true;
|
||||||
|
|
||||||
$raw = '';
|
$ext = '';
|
||||||
$plLen = strlen($payload);
|
if ($this->defPayLen > 65535) {
|
||||||
if ($plLen <= 125) {
|
$ext = pack('NN', 0, $this->defPayLen);
|
||||||
$raw .= sprintf('%08b', $plLen);
|
$this->secondByte = 127;
|
||||||
} elseif ($plLen <= 65535) {
|
} elseif ($this->defPayLen > 125) {
|
||||||
$raw .= sprintf('%08b', 126) . sprintf('%016b', $plLen);
|
$ext = pack('n', $this->defPayLen);
|
||||||
} else { // todo, make sure msg isn't longer than b1x71
|
$this->secondByte = 126;
|
||||||
$raw .= sprintf('%08b', 127) . sprintf('%064b', $plLen);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->addBuffer($firstByte . static::encode($raw) . $payload);
|
$this->data = chr($this->firstByte) . chr($this->secondByte) . $ext . $payload;
|
||||||
}
|
$this->bytesRecvd = 2 + strlen($ext) + $this->defPayLen;
|
||||||
|
|
||||||
/**
|
|
||||||
* Encode the fake binary string to send over the wire
|
|
||||||
* @param string of 1's and 0's
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function encode($in) {
|
|
||||||
if (strlen($in) > 8) {
|
|
||||||
$out = '';
|
|
||||||
|
|
||||||
while (strlen($in) >= 8) {
|
|
||||||
$out .= static::encode(substr($in, 0, 8));
|
|
||||||
$in = substr($in, 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $out;
|
|
||||||
}
|
|
||||||
|
|
||||||
return chr(bindec($in));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -407,7 +390,7 @@ class Frame implements FrameInterface {
|
|||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function getPayloadStartingByte() {
|
public function getPayloadStartingByte() {
|
||||||
return 1 + $this->getNumPayloadBytes() + strlen($this->getMaskingKey());
|
return 1 + $this->getNumPayloadBytes() + ($this->isMasked() ? static::MASK_LENGTH : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -18,6 +18,26 @@ class FrameTest extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->_frame = new Frame;
|
$this->_frame = new Frame;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encode the fake binary string to send over the wire
|
||||||
|
* @param string of 1's and 0's
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function encode($in) {
|
||||||
|
if (strlen($in) > 8) {
|
||||||
|
$out = '';
|
||||||
|
|
||||||
|
while (strlen($in) >= 8) {
|
||||||
|
$out .= static::encode(substr($in, 0, 8));
|
||||||
|
$in = substr($in, 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $out;
|
||||||
|
}
|
||||||
|
|
||||||
|
return chr(bindec($in));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a data provider
|
* This is a data provider
|
||||||
* @param string The UTF8 message
|
* @param string The UTF8 message
|
||||||
@ -63,7 +83,7 @@ class FrameTest extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->setExpectedException('\UnderflowException');
|
$this->setExpectedException('\UnderflowException');
|
||||||
|
|
||||||
if (!empty($bin)) {
|
if (!empty($bin)) {
|
||||||
$this->_frame->addBuffer(Frame::encode($bin));
|
$this->_frame->addBuffer(static::encode($bin));
|
||||||
}
|
}
|
||||||
|
|
||||||
call_user_func(array($this->_frame, $method));
|
call_user_func(array($this->_frame, $method));
|
||||||
@ -91,7 +111,7 @@ class FrameTest extends \PHPUnit_Framework_TestCase {
|
|||||||
* @covers Ratchet\WebSocket\Version\RFC6455\Frame::isFinal
|
* @covers Ratchet\WebSocket\Version\RFC6455\Frame::isFinal
|
||||||
*/
|
*/
|
||||||
public function testFinCodeFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) {
|
public function testFinCodeFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) {
|
||||||
$this->_frame->addBuffer(Frame::encode($bin));
|
$this->_frame->addBuffer(static::encode($bin));
|
||||||
$this->assertEquals($fin, $this->_frame->isFinal());
|
$this->assertEquals($fin, $this->_frame->isFinal());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +122,7 @@ class FrameTest extends \PHPUnit_Framework_TestCase {
|
|||||||
* @covers Ratchet\WebSocket\Version\RFC6455\Frame::getRsv3
|
* @covers Ratchet\WebSocket\Version\RFC6455\Frame::getRsv3
|
||||||
*/
|
*/
|
||||||
public function testGetRsvFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) {
|
public function testGetRsvFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) {
|
||||||
$this->_frame->addBuffer(Frame::encode($bin));
|
$this->_frame->addBuffer(static::encode($bin));
|
||||||
|
|
||||||
$this->assertEquals($rsv1, $this->_frame->getRsv1());
|
$this->assertEquals($rsv1, $this->_frame->getRsv1());
|
||||||
$this->assertEquals($rsv2, $this->_frame->getRsv2());
|
$this->assertEquals($rsv2, $this->_frame->getRsv2());
|
||||||
@ -114,7 +134,7 @@ class FrameTest extends \PHPUnit_Framework_TestCase {
|
|||||||
* @covers Ratchet\WebSocket\Version\RFC6455\Frame::getOpcode
|
* @covers Ratchet\WebSocket\Version\RFC6455\Frame::getOpcode
|
||||||
*/
|
*/
|
||||||
public function testOpcodeFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) {
|
public function testOpcodeFromBits($fin, $rsv1, $rsv2, $rsv3, $opcode, $bin) {
|
||||||
$this->_frame->addBuffer(Frame::encode($bin));
|
$this->_frame->addBuffer(static::encode($bin));
|
||||||
$this->assertEquals($opcode, $this->_frame->getOpcode());
|
$this->assertEquals($opcode, $this->_frame->getOpcode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,8 +173,8 @@ class FrameTest extends \PHPUnit_Framework_TestCase {
|
|||||||
* @covers Ratchet\WebSocket\Version\RFC6455\Frame::getFirstPayloadVal
|
* @covers Ratchet\WebSocket\Version\RFC6455\Frame::getFirstPayloadVal
|
||||||
*/
|
*/
|
||||||
public function testFirstPayloadDesignationValue($bits, $bin) {
|
public function testFirstPayloadDesignationValue($bits, $bin) {
|
||||||
$this->_frame->addBuffer(Frame::encode($this->_firstByteFinText));
|
$this->_frame->addBuffer(static::encode($this->_firstByteFinText));
|
||||||
$this->_frame->addBuffer(Frame::encode($bin));
|
$this->_frame->addBuffer(static::encode($bin));
|
||||||
|
|
||||||
$ref = new \ReflectionClass($this->_frame);
|
$ref = new \ReflectionClass($this->_frame);
|
||||||
$cb = $ref->getMethod('getFirstPayloadVal');
|
$cb = $ref->getMethod('getFirstPayloadVal');
|
||||||
@ -180,8 +200,8 @@ class FrameTest extends \PHPUnit_Framework_TestCase {
|
|||||||
* @covers Ratchet\WebSocket\Version\RFC6455\Frame::getNumPayloadBits
|
* @covers Ratchet\WebSocket\Version\RFC6455\Frame::getNumPayloadBits
|
||||||
*/
|
*/
|
||||||
public function testDetermineHowManyBitsAreUsedToDescribePayload($expected_bits, $bin) {
|
public function testDetermineHowManyBitsAreUsedToDescribePayload($expected_bits, $bin) {
|
||||||
$this->_frame->addBuffer(Frame::encode($this->_firstByteFinText));
|
$this->_frame->addBuffer(static::encode($this->_firstByteFinText));
|
||||||
$this->_frame->addBuffer(Frame::encode($bin));
|
$this->_frame->addBuffer(static::encode($bin));
|
||||||
|
|
||||||
$ref = new \ReflectionClass($this->_frame);
|
$ref = new \ReflectionClass($this->_frame);
|
||||||
$cb = $ref->getMethod('getNumPayloadBits');
|
$cb = $ref->getMethod('getNumPayloadBits');
|
||||||
@ -215,8 +235,8 @@ class FrameTest extends \PHPUnit_Framework_TestCase {
|
|||||||
* @covers Ratchet\WebSocket\Version\RFC6455\Frame::isMasked
|
* @covers Ratchet\WebSocket\Version\RFC6455\Frame::isMasked
|
||||||
*/
|
*/
|
||||||
public function testIsMaskedReturnsExpectedValue($masked, $payload_length, $bin) {
|
public function testIsMaskedReturnsExpectedValue($masked, $payload_length, $bin) {
|
||||||
$this->_frame->addBuffer(Frame::encode($this->_firstByteFinText));
|
$this->_frame->addBuffer(static::encode($this->_firstByteFinText));
|
||||||
$this->_frame->addBuffer(Frame::encode($bin));
|
$this->_frame->addBuffer(static::encode($bin));
|
||||||
|
|
||||||
$this->assertEquals($masked, $this->_frame->isMasked());
|
$this->assertEquals($masked, $this->_frame->isMasked());
|
||||||
}
|
}
|
||||||
@ -235,8 +255,8 @@ class FrameTest extends \PHPUnit_Framework_TestCase {
|
|||||||
* @covers Ratchet\WebSocket\Version\RFC6455\Frame::getPayloadLength
|
* @covers Ratchet\WebSocket\Version\RFC6455\Frame::getPayloadLength
|
||||||
*/
|
*/
|
||||||
public function testGetPayloadLengthWhenOnlyFirstFrameIsUsed($masked, $payload_length, $bin) {
|
public function testGetPayloadLengthWhenOnlyFirstFrameIsUsed($masked, $payload_length, $bin) {
|
||||||
$this->_frame->addBuffer(Frame::encode($this->_firstByteFinText));
|
$this->_frame->addBuffer(static::encode($this->_firstByteFinText));
|
||||||
$this->_frame->addBuffer(Frame::encode($bin));
|
$this->_frame->addBuffer(static::encode($bin));
|
||||||
|
|
||||||
$this->assertEquals($payload_length, $this->_frame->getPayloadLength());
|
$this->assertEquals($payload_length, $this->_frame->getPayloadLength());
|
||||||
}
|
}
|
||||||
@ -267,8 +287,8 @@ class FrameTest extends \PHPUnit_Framework_TestCase {
|
|||||||
* @todo I I wrote the dataProvider incorrectly, skpping for now
|
* @todo I I wrote the dataProvider incorrectly, skpping for now
|
||||||
*/
|
*/
|
||||||
public function testGetMaskingKey($mask) {
|
public function testGetMaskingKey($mask) {
|
||||||
$this->_frame->addBuffer(Frame::encode($this->_firstByteFinText));
|
$this->_frame->addBuffer(static::encode($this->_firstByteFinText));
|
||||||
$this->_frame->addBuffer(Frame::encode($this->_secondByteMaskedSPL));
|
$this->_frame->addBuffer(static::encode($this->_secondByteMaskedSPL));
|
||||||
$this->_frame->addBuffer($mask);
|
$this->_frame->addBuffer($mask);
|
||||||
|
|
||||||
$this->assertEquals($mask, $this->_frame->getMaskingKey());
|
$this->assertEquals($mask, $this->_frame->getMaskingKey());
|
||||||
|
Loading…
Reference in New Issue
Block a user