Frame.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Engine\WebSocket;
  12. use Hyperf\Engine\Contract\WebSocket\FrameInterface;
  13. use Hyperf\Engine\Exception\InvalidArgumentException;
  14. use Hyperf\Engine\Http\Stream;
  15. use Psr\Http\Message\StreamInterface;
  16. use Swoole\WebSocket\Frame as SwooleFrame;
  17. use function Hyperf\Engine\swoole_get_flags_from_frame;
  18. class Frame implements FrameInterface
  19. {
  20. /**
  21. * @deprecated
  22. */
  23. public const PING = '27890027';
  24. /**
  25. * @deprecated
  26. */
  27. public const PONG = '278a0027';
  28. protected ?StreamInterface $payloadData = null;
  29. public function __construct(
  30. protected bool $fin = true,
  31. protected bool $rsv1 = false,
  32. protected bool $rsv2 = false,
  33. protected bool $rsv3 = false,
  34. protected int $opcode = Opcode::TEXT,
  35. protected int $payloadLength = 0,
  36. protected string $maskingKey = '',
  37. mixed $payloadData = '',
  38. ) {
  39. if ($payloadData !== null && $payloadData !== '') {
  40. $this->setPayloadData($payloadData);
  41. }
  42. }
  43. public function __toString()
  44. {
  45. return $this->toString();
  46. }
  47. public function getOpcode(): int
  48. {
  49. return $this->opcode;
  50. }
  51. public function setOpcode(int $opcode): static
  52. {
  53. $this->opcode = $opcode;
  54. return $this;
  55. }
  56. public function withOpcode(int $opcode): static
  57. {
  58. return (clone $this)->setOpcode($opcode);
  59. }
  60. public function getFin(): bool
  61. {
  62. return $this->fin;
  63. }
  64. public function setFin(bool $fin): static
  65. {
  66. $this->fin = $fin;
  67. return $this;
  68. }
  69. public function withFin(bool $fin): static
  70. {
  71. return (clone $this)->setFin($fin);
  72. }
  73. public function getRSV1(): bool
  74. {
  75. return $this->rsv1;
  76. }
  77. public function setRSV1(bool $rsv1): static
  78. {
  79. $this->rsv1 = $rsv1;
  80. return $this;
  81. }
  82. public function withRSV1(bool $rsv1): static
  83. {
  84. return (clone $this)->setRSV1($rsv1);
  85. }
  86. public function getRSV2(): bool
  87. {
  88. return $this->rsv2;
  89. }
  90. public function setRSV2(bool $rsv2): static
  91. {
  92. $this->rsv2 = $rsv2;
  93. return $this;
  94. }
  95. public function withRSV2(bool $rsv2): static
  96. {
  97. return (clone $this)->setRSV2($rsv2);
  98. }
  99. public function getRSV3(): bool
  100. {
  101. return $this->rsv3;
  102. }
  103. public function setRSV3(bool $rsv3): static
  104. {
  105. $this->rsv3 = $rsv3;
  106. return $this;
  107. }
  108. public function withRSV3(bool $rsv3): static
  109. {
  110. return (clone $this)->setRSV3($rsv3);
  111. }
  112. public function getPayloadLength(): int
  113. {
  114. return $this->payloadData?->getSize() ?? 0;
  115. }
  116. public function setPayloadLength(int $payloadLength): static
  117. {
  118. $this->payloadLength = $payloadLength;
  119. return $this;
  120. }
  121. public function withPayloadLength(int $payloadLength): static
  122. {
  123. return (clone $this)->setPayloadLength($payloadLength);
  124. }
  125. public function getMask(): bool
  126. {
  127. return ! empty($this->maskingKey);
  128. }
  129. public function getMaskingKey(): string
  130. {
  131. return $this->maskingKey;
  132. }
  133. public function setMaskingKey(string $maskingKey): static
  134. {
  135. $this->maskingKey = $maskingKey;
  136. return $this;
  137. }
  138. public function withMaskingKey(string $maskingKey): static
  139. {
  140. return (clone $this)->setMaskingKey($maskingKey);
  141. }
  142. public function getPayloadData(): StreamInterface
  143. {
  144. return $this->payloadData;
  145. }
  146. public function setPayloadData(mixed $payloadData): static
  147. {
  148. $this->payloadData = new Stream((string) $payloadData);
  149. return $this;
  150. }
  151. public function withPayloadData(mixed $payloadData): static
  152. {
  153. return (clone $this)->setPayloadData($payloadData);
  154. }
  155. public function toString(bool $withoutPayloadData = false): string
  156. {
  157. return SwooleFrame::pack(
  158. (string) $this->getPayloadData(),
  159. $this->getOpcode(),
  160. swoole_get_flags_from_frame($this)
  161. );
  162. }
  163. public static function from(mixed $frame): static
  164. {
  165. if (! $frame instanceof SwooleFrame) {
  166. throw new InvalidArgumentException('The frame is invalid.');
  167. }
  168. return new Frame(
  169. (bool) ($frame->flags & SWOOLE_WEBSOCKET_FLAG_FIN),
  170. (bool) ($frame->flags & SWOOLE_WEBSOCKET_FLAG_RSV1),
  171. (bool) ($frame->flags & SWOOLE_WEBSOCKET_FLAG_RSV2),
  172. (bool) ($frame->flags & SWOOLE_WEBSOCKET_FLAG_RSV3),
  173. $frame->opcode,
  174. strlen($frame->data),
  175. $frame->flags & SWOOLE_WEBSOCKET_FLAG_MASK ? '258E' : '',
  176. $frame->data
  177. );
  178. }
  179. }