1 | <?php |
||
11 | class Frame { |
||
12 | |||
13 | const RSV123 = 0x70; |
||
14 | const RSV1 = 0x40; |
||
15 | const RSV2 = 0x20; |
||
16 | const RSV3 = 0x10; |
||
17 | |||
18 | const OP_CONTINUE = 0x00; |
||
19 | const OP_TEXT = 0x01; |
||
20 | const OP_BINARY = 0x02; |
||
21 | const OP_CLOSE = 0x08; |
||
22 | const OP_PING = 0x09; |
||
23 | const OP_PONG = 0x0a; |
||
24 | |||
25 | const NAMES = [ |
||
26 | self::OP_CONTINUE => 'CONTINUE', |
||
27 | self::OP_TEXT => 'TEXT', |
||
28 | self::OP_BINARY => 'BINARY', |
||
29 | 0x03 => 'RESERVED DATA 0x03', |
||
30 | 0x04 => 'RESERVED DATA 0x04', |
||
31 | 0x05 => 'RESERVED DATA 0x05', |
||
32 | 0x06 => 'RESERVED DATA 0x06', |
||
33 | 0x07 => 'RESERVED DATA 0x07', |
||
34 | self::OP_CLOSE => 'CLOSE', |
||
35 | self::OP_PING => 'PING', |
||
36 | self::OP_PONG => 'PONG', |
||
37 | 0x0b => 'RESERVED CONTROL 0x0b', |
||
38 | 0x0c => 'RESERVED CONTROL 0x0c', |
||
39 | 0x0d => 'RESERVED CONTROL 0x0d', |
||
40 | 0x0e => 'RESERVED CONTROL 0x0e', |
||
41 | 0x0f => 'RESERVED CONTROL 0x0f', |
||
42 | ]; |
||
43 | |||
44 | const CLOSE_NORMAL = 1000; // mutual closure |
||
45 | const CLOSE_INTERRUPT = 1001; // abrupt closure due to hangups, reboots, "going away" |
||
46 | const CLOSE_PROTOCOL_ERROR = 1002; // invalid behavior / framing |
||
47 | const CLOSE_UNHANDLED_DATA = 1003; // message handler doesn't want the payload |
||
48 | const CLOSE_BAD_DATA = 1007; // message handler can't understand the payload |
||
49 | const CLOSE_POLICY_VIOLATION = 1008; // generic "access denied" |
||
50 | const CLOSE_TOO_LARGE = 1009; // unacceptable payload size |
||
51 | const CLOSE_EXPECTATION = 1010; // peer closed because it wants extensions (listed in the reason) |
||
52 | const CLOSE_INTERNAL_ERROR = 1011; // like http 500 |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $final; |
||
58 | |||
59 | /** |
||
60 | * @var int |
||
61 | */ |
||
62 | protected $opCode; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $payload; |
||
68 | |||
69 | /** |
||
70 | * The RSV bits masked out of the first byte of the frame, as-is. |
||
71 | * |
||
72 | * @var int |
||
73 | */ |
||
74 | protected $rsv; |
||
75 | |||
76 | /** |
||
77 | * @param bool $final |
||
78 | * @param int $rsv |
||
79 | * @param int $opCode |
||
80 | * @param string $payload |
||
81 | */ |
||
82 | public function __construct (bool $final, int $rsv, int $opCode, string $payload) { |
||
88 | |||
89 | /** |
||
90 | * The payload, or `CLOSE` reason. |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | public function __toString () { |
||
100 | |||
101 | /** |
||
102 | * The `CLOSE` code. |
||
103 | * |
||
104 | * https://tools.ietf.org/html/rfc6455#section-5.5.1 |
||
105 | * |
||
106 | * @return int |
||
107 | */ |
||
108 | final public function getCloseCode (): int { |
||
112 | |||
113 | /** |
||
114 | * The `CLOSE` reason. |
||
115 | * |
||
116 | * https://tools.ietf.org/html/rfc6455#section-5.5.1 |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | final public function getCloseReason (): string { |
||
123 | |||
124 | /** |
||
125 | * @return int |
||
126 | */ |
||
127 | final public function getLength (): int { |
||
130 | |||
131 | /** |
||
132 | * @return string |
||
133 | */ |
||
134 | public function getName (): string { |
||
137 | |||
138 | /** |
||
139 | * @return int |
||
140 | */ |
||
141 | final public function getOpCode (): int { |
||
144 | |||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | final public function getPayload (): string { |
||
151 | |||
152 | /** |
||
153 | * @return int |
||
154 | */ |
||
155 | final public function getRsv (): int { |
||
158 | |||
159 | /** |
||
160 | * @return bool |
||
161 | */ |
||
162 | final public function hasRsv1 (): bool { |
||
165 | |||
166 | /** |
||
167 | * @return bool |
||
168 | */ |
||
169 | final public function hasRsv2 (): bool { |
||
172 | |||
173 | /** |
||
174 | * @return bool |
||
175 | */ |
||
176 | final public function hasRsv3 (): bool { |
||
179 | |||
180 | /** |
||
181 | * @return bool |
||
182 | */ |
||
183 | final public function isBinary (): bool { |
||
186 | |||
187 | /** |
||
188 | * @return bool |
||
189 | */ |
||
190 | final public function isClose (): bool { |
||
193 | |||
194 | /** |
||
195 | * @return bool |
||
196 | */ |
||
197 | final public function isContinue (): bool { |
||
200 | |||
201 | /** |
||
202 | * @return bool |
||
203 | */ |
||
204 | final public function isControl (): bool { |
||
207 | |||
208 | /** |
||
209 | * @return bool |
||
210 | */ |
||
211 | final public function isData (): bool { |
||
214 | |||
215 | /** |
||
216 | * @return bool |
||
217 | */ |
||
218 | final public function isFinal (): bool { |
||
221 | |||
222 | /** |
||
223 | * @return bool |
||
224 | */ |
||
225 | final public function isPing (): bool { |
||
228 | |||
229 | /** |
||
230 | * @return bool |
||
231 | */ |
||
232 | final public function isPong (): bool { |
||
235 | |||
236 | /** |
||
237 | * @return bool |
||
238 | */ |
||
239 | final public function isText (): bool { |
||
242 | |||
243 | } |