[WebSocket] Performance

No more sprintf calls
This commit is contained in:
Chris Boden 2012-07-15 11:57:42 -04:00
parent 2f8bb3395b
commit a2edc02d97

View File

@ -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);
} }
/** /**