1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Hyphper\Frame; |
4
|
|
|
|
5
|
|
|
use Hyphper\Frame\Exception\InvalidFrameException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The RST_STREAM frame allows for abnormal termination of a stream. When sent |
9
|
|
|
* by the initiator of a stream, it indicates that they wish to cancel the |
10
|
|
|
* stream or that an error condition has occurred. When sent by the receiver |
11
|
|
|
* of a stream, it indicates that either the receiver is rejecting the stream, |
12
|
|
|
* requesting that the stream be cancelled or that an error condition has |
13
|
|
|
* occurred. |
14
|
|
|
* |
15
|
|
|
* @package Hyphper\Frame |
16
|
|
|
*/ |
17
|
|
|
class RstStreamFrame extends \Hyphper\Frame |
18
|
|
|
{ |
19
|
|
|
protected $defined_flags = []; |
20
|
|
|
protected $type = 0x03; |
21
|
|
|
protected $stream_association = self::HAS_STREAM; |
22
|
|
|
protected $error_code; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* RstStreamFrame constructor. |
26
|
|
|
* |
27
|
|
|
* @param array $options |
28
|
|
|
*/ |
29
|
5 |
|
public function __construct(array $options = []) |
30
|
|
|
{ |
31
|
5 |
|
parent::__construct($options); |
32
|
4 |
|
$this->error_code = (int) ($options['error_code'] ?? null); |
33
|
4 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
1 |
|
public function serializeBody(): string |
39
|
|
|
{ |
40
|
1 |
|
return pack('N', $this->error_code); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Given the body of a frame, parses it into frame data. This populates |
45
|
|
|
* the non-header parts of the frame: that is, it does not populate the |
46
|
|
|
* stream ID or flags. |
47
|
|
|
* |
48
|
|
|
* |
49
|
|
|
* @param string $data |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
2 |
|
public function parseBody(string $data) |
54
|
|
|
{ |
55
|
2 |
|
if (strlen($data) != 4) { |
56
|
1 |
|
throw new InvalidFrameException(sprintf( |
57
|
1 |
|
"RST_STREAM must have 4 byte body: actual length %s.", |
58
|
|
|
strlen($data) |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
if (!$unpack = @unpack('Nerror_code', $data)) { |
63
|
|
|
throw new InvalidFrameException('Invalid RST_STREAM body'); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$this->error_code = $unpack['error_code']; |
67
|
1 |
|
$this->body_len = strlen($data); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
1 |
|
public function getErrorCode() |
74
|
|
|
{ |
75
|
1 |
|
return $this->error_code; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param int $error_code |
80
|
|
|
* @return RstStreamFrame |
81
|
|
|
*/ |
82
|
1 |
|
public function setErrorCode($error_code) |
83
|
|
|
{ |
84
|
1 |
|
$this->error_code = $error_code; |
85
|
1 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|