1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace Hyphper\Frame; |
4
|
|
|
|
5
|
|
|
use Hyphper\Frame\Exception\InvalidFrameException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The GOAWAY frame informs the remote peer to stop creating streams on this |
9
|
|
|
* connection. It can be sent from the client or the server. Once sent, the |
10
|
|
|
* sender will ignore frames sent on new streams for the remainder of the |
11
|
|
|
* connection. |
12
|
|
|
* |
13
|
|
|
* @package Hyphper\Frame |
14
|
|
|
*/ |
15
|
|
|
class GoAwayFrame extends \Hyphper\Frame |
16
|
|
|
{ |
17
|
|
|
protected $defined_flags = []; |
18
|
|
|
protected $type = 0x07; |
19
|
|
|
protected $stream_association = self::NO_STREAM; |
20
|
|
|
protected $last_stream_id; |
21
|
|
|
protected $error_code; |
22
|
|
|
protected $additional_data; |
23
|
|
|
|
24
|
5 |
View Code Duplication |
public function __construct(array $options = []) |
|
|
|
|
25
|
|
|
{ |
26
|
5 |
|
parent::__construct($options); |
27
|
4 |
|
$this->last_stream_id = (int) ($options['last_stream_id'] ?? 0); |
28
|
4 |
|
$this->error_code = (int) ($options['error_code'] ?? 0); |
29
|
4 |
|
$this->additional_data = (int) ($options['additional_data'] ?? ''); |
30
|
4 |
|
} |
31
|
|
|
|
32
|
1 |
|
public function serializeBody(): string |
33
|
|
|
{ |
34
|
1 |
|
$data = pack( |
35
|
1 |
|
'NN', |
36
|
1 |
|
$this->last_stream_id & 0x7FFFFFFF, |
37
|
1 |
|
$this->error_code |
38
|
|
|
); |
39
|
|
|
|
40
|
1 |
|
$data .= $this->additional_data; |
41
|
|
|
|
42
|
1 |
|
return $data; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Given the body of a frame, parses it into frame data. This populates |
47
|
|
|
* the non-header parts of the frame: that is, it does not populate the |
48
|
|
|
* stream ID or flags. |
49
|
|
|
* |
50
|
|
|
* |
51
|
|
|
* @param string $data |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
2 |
|
public function parseBody(string $data) |
56
|
|
|
{ |
57
|
2 |
View Code Duplication |
if (!$unpack = @unpack('Nlast_stream_id/Nerror_code', substr($data, 0, 8))) { |
|
|
|
|
58
|
1 |
|
throw new InvalidFrameException('Invalid GOAWAY body.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
$this->last_stream_id = $unpack['last_stream_id']; |
62
|
1 |
|
$this->error_code = $unpack['error_code']; |
63
|
|
|
|
64
|
1 |
|
$this->body_len = strlen($data); |
65
|
1 |
|
if (strlen($data) > 8) { |
66
|
1 |
|
$this->additional_data = substr($data, 8); |
67
|
|
|
} |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param int $last_stream_id |
72
|
|
|
* |
73
|
|
|
* @return GoAwayFrame |
74
|
|
|
*/ |
75
|
1 |
|
public function setLastStreamId($last_stream_id) |
76
|
|
|
{ |
77
|
1 |
|
$this->last_stream_id = $last_stream_id; |
78
|
|
|
|
79
|
1 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
|
|
public function getLastStreamId() |
86
|
|
|
{ |
87
|
|
|
return $this->last_stream_id; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param int $error_code |
92
|
|
|
* |
93
|
|
|
* @return GoAwayFrame |
94
|
|
|
*/ |
95
|
1 |
|
public function setErrorCode($error_code) |
96
|
|
|
{ |
97
|
1 |
|
$this->error_code = $error_code; |
98
|
|
|
|
99
|
1 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
public function getErrorCode() |
106
|
|
|
{ |
107
|
|
|
return $this->error_code; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param int $additional_data |
112
|
|
|
* |
113
|
|
|
* @return GoAwayFrame |
114
|
|
|
*/ |
115
|
1 |
|
public function setAdditionalData($additional_data) |
116
|
|
|
{ |
117
|
1 |
|
$this->additional_data = $additional_data; |
118
|
|
|
|
119
|
1 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return int |
124
|
|
|
*/ |
125
|
1 |
|
public function getAdditionalData() |
126
|
|
|
{ |
127
|
1 |
|
return $this->additional_data; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.