diff --git a/src/Frame/HybiFrame.php b/src/Frame/HybiFrame.php index 0d8a7c1..d331a7a 100644 --- a/src/Frame/HybiFrame.php +++ b/src/Frame/HybiFrame.php @@ -69,7 +69,7 @@ class HybiFrame extends Frame */ public function encode(string $payload, int $type = Protocol::TYPE_TEXT, bool $masked = false): Frame { - if (!\is_int($type) || !\in_array($type, Protocol::FRAME_TYPES)) { + if (!\in_array($type, Protocol::FRAME_TYPES, true)) { throw new InvalidArgumentException('Invalid frame type'); } @@ -77,50 +77,52 @@ public function encode(string $payload, int $type = Protocol::TYPE_TEXT, bool $m $this->masked = $masked; $this->payload = $payload; $this->length = \strlen($this->payload); - $this->offset_mask = null; - $this->offset_payload = null; $this->buffer = "\x00\x00"; + // FIN + opcode byte $this->buffer[self::BYTE_HEADER] = \chr( (self::BITFIELD_TYPE & $this->type) - | (self::BITFIELD_FINAL & \PHP_INT_MAX) + | self::BITFIELD_FINAL ); - $masked_bit = (self::BITFIELD_MASKED & ($this->masked ? \PHP_INT_MAX : 0)); + $masked_bit = $this->masked ? self::BITFIELD_MASKED : 0; if ($this->length <= 125) { $this->buffer[self::BYTE_INITIAL_LENGTH] = \chr( - (self::BITFIELD_INITIAL_LENGTH & $this->length) | $masked_bit + ($this->length & self::BITFIELD_INITIAL_LENGTH) + | $masked_bit ); - } elseif ($this->length <= 65536) { + } elseif ($this->length <= 65535) { $this->buffer[self::BYTE_INITIAL_LENGTH] = \chr( - (self::BITFIELD_INITIAL_LENGTH & 126) | $masked_bit + (126 & self::BITFIELD_INITIAL_LENGTH) + | $masked_bit ); $this->buffer .= \pack('n', $this->length); } else { $this->buffer[self::BYTE_INITIAL_LENGTH] = \chr( - (self::BITFIELD_INITIAL_LENGTH & 127) | $masked_bit + (127 & self::BITFIELD_INITIAL_LENGTH) + | $masked_bit ); - - if (\PHP_INT_MAX > 2147483647) { - $this->buffer .= \pack('NN', $this->length >> 32, $this->length); - } else { + if (\PHP_INT_SIZE === 4) { + // J is not available on 32-bit PHP $this->buffer .= \pack('NN', 0, $this->length); + } else { + $this->buffer .= \pack('J', $this->length); } } if ($this->masked) { $this->mask = $this->generateMask(); + $this->offset_mask = \strlen($this->buffer); $this->buffer .= $this->mask; + $this->offset_payload = \strlen($this->buffer); $this->buffer .= $this->mask($this->payload); } else { + $this->offset_payload = \strlen($this->buffer); $this->buffer .= $this->payload; } - $this->offset_mask = $this->getMaskOffset(); - $this->offset_payload = $this->getPayloadOffset(); - return $this; } @@ -196,7 +198,6 @@ protected function getMaskOffset(): int if (!isset($this->offset_mask)) { $offset = self::BYTE_INITIAL_LENGTH + 1; $offset += $this->getLengthSize(); - $this->offset_mask = $offset; } @@ -233,7 +234,6 @@ protected function getInitialLength(): int if (!isset($this->buffer[self::BYTE_INITIAL_LENGTH])) { throw new FrameException('Cannot yet tell expected length'); } - $a = (int) (\ord($this->buffer[self::BYTE_INITIAL_LENGTH]) & self::BITFIELD_INITIAL_LENGTH); return (int) (\ord($this->buffer[self::BYTE_INITIAL_LENGTH]) & self::BITFIELD_INITIAL_LENGTH); } @@ -258,7 +258,6 @@ protected function getPayloadOffset(): int if (!isset($this->offset_payload)) { $offset = $this->getMaskOffset(); $offset += $this->getMaskSize(); - $this->offset_payload = $offset; } @@ -294,7 +293,7 @@ public function getType(): int $type = (int) (\ord($this->buffer[self::BYTE_HEADER]) & self::BITFIELD_TYPE); - if (!\in_array($type, Protocol::FRAME_TYPES)) { + if (!\in_array($type, Protocol::FRAME_TYPES, true)) { throw new FrameException('Invalid payload type'); }