1 | <?php |
||
23 | abstract class Frame |
||
24 | { |
||
25 | const FRAMES = [ |
||
26 | 0x0 => 'DataFrame', |
||
27 | 0x01 => 'HeadersFrame', |
||
28 | 0x02 => 'PriorityFrame', |
||
29 | 0x03 => 'RstStreamFrame', |
||
30 | 0x04 => 'SettingsFrame', |
||
31 | 0x05 => 'PushPromiseFrame', |
||
32 | 0x06 => 'PingFrame', |
||
33 | 0x07 => 'GoAwayFrame', |
||
34 | 0x08 => 'WindowUpdateFrame', |
||
35 | 0x09 => 'ContinuationFrame', |
||
36 | 0xA => 'AltSvcFrame', |
||
37 | ]; |
||
38 | |||
39 | const HAS_STREAM = 1; |
||
40 | const NO_STREAM = 2; |
||
41 | const EITHER_STREAM = 4; |
||
42 | |||
43 | /** |
||
44 | * @var array The flags defined on this type of frame. |
||
45 | */ |
||
46 | protected $defined_flags = []; |
||
47 | |||
48 | /** |
||
49 | * @var Flags |
||
50 | */ |
||
51 | protected $flags; |
||
52 | |||
53 | /** |
||
54 | * @var int The byte used to define the type of the frame. |
||
55 | */ |
||
56 | protected $type; |
||
57 | |||
58 | /** |
||
59 | * @var int The length of the frame |
||
60 | */ |
||
61 | protected $length; |
||
62 | |||
63 | /** |
||
64 | * If true, the frame's stream_id must be non-zero. If false, |
||
65 | * it must be zero. If null, stream_id is ignored. |
||
66 | * |
||
67 | * @var int |
||
68 | */ |
||
69 | protected $stream_association; |
||
70 | |||
71 | /** |
||
72 | * @var int |
||
73 | */ |
||
74 | protected $stream_id = 0; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $body; |
||
80 | |||
81 | /** |
||
82 | * @var int |
||
83 | */ |
||
84 | protected $body_len = 0; |
||
85 | |||
86 | /** |
||
87 | * Frame constructor. |
||
88 | * |
||
89 | * @param array $options |
||
90 | */ |
||
91 | 71 | public function __construct(array $options = []) |
|
110 | |||
111 | /** |
||
112 | * @param string $data |
||
113 | * |
||
114 | * @return Frame |
||
115 | * @throws InvalidFrameException |
||
116 | * @throws UnknownFrameException |
||
117 | */ |
||
118 | public static function parseFrame(string $data): Frame |
||
127 | |||
128 | /** |
||
129 | * Takes a 9-byte frame header and returns a tuple of the appropriate |
||
130 | * Frame object and the length that needs to be read from the socket. |
||
131 | * This populates the flags field, and determines how long the body is. |
||
132 | * |
||
133 | * @param string $header |
||
134 | * @throws UnknownFrameException If a frame of unknown type is received. |
||
135 | */ |
||
136 | 54 | public static function parseFrameHeader(string $header): Frame |
|
163 | |||
164 | /** |
||
165 | * @return int |
||
166 | */ |
||
167 | 28 | public function getLength() |
|
171 | |||
172 | /** |
||
173 | * @param int $length |
||
174 | */ |
||
175 | public function setLength($length) |
||
179 | |||
180 | /** |
||
181 | * Convert a frame into a bytestring, representing the serialized form of |
||
182 | * the frame. |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | 22 | public function serialize():string |
|
210 | |||
211 | /** |
||
212 | * @param $flag_byte |
||
213 | * |
||
214 | * @return Flags |
||
215 | * @throws Frame\Exception\InvalidFlagException |
||
216 | */ |
||
217 | 42 | public function parseFlags(int $flag_byte): Flags |
|
227 | |||
228 | abstract public function serializeBody(): string; |
||
229 | |||
230 | /** |
||
231 | * Given the body of a frame, parses it into frame data. This populates |
||
232 | * the non-header parts of the frame: that is, it does not populate the |
||
233 | * stream ID or flags. |
||
234 | * |
||
235 | * |
||
236 | * @param string $data |
||
237 | * @return void |
||
238 | */ |
||
239 | abstract public function parseBody(string $data); |
||
240 | |||
241 | /** |
||
242 | * @return Flags |
||
243 | */ |
||
244 | 14 | public function getFlags() |
|
248 | |||
249 | /** |
||
250 | * @param array $flags |
||
251 | * @return $this |
||
252 | */ |
||
253 | 3 | public function setFlags(array $flags) |
|
263 | |||
264 | /** |
||
265 | * @return int |
||
266 | */ |
||
267 | 14 | public function getBodyLen() |
|
271 | |||
272 | /** |
||
273 | * @param int $stream_id |
||
274 | * |
||
275 | * @return Frame |
||
276 | */ |
||
277 | public function setStreamId($stream_id) |
||
283 | |||
284 | /** |
||
285 | * @return int |
||
286 | */ |
||
287 | 1 | public function getStreamId() |
|
291 | |||
292 | 1 | public function __debugInfo() |
|
312 | } |
||
313 |