1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwooleTW\Http\Websocket; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Push |
9
|
|
|
*/ |
10
|
|
|
class Push |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
protected $opcode; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
protected $sender; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $descriptors; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $broadcast; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $assigned; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $event; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string|null |
44
|
|
|
*/ |
45
|
|
|
protected $message; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Push constructor. |
49
|
|
|
* |
50
|
|
|
* @param int $opcode |
51
|
|
|
* @param int $sender |
52
|
|
|
* @param array $descriptors |
53
|
|
|
* @param bool $broadcast |
54
|
|
|
* @param bool $assigned |
55
|
|
|
* @param string $event |
56
|
|
|
* @param string|null $message |
57
|
|
|
*/ |
58
|
|
|
protected function __construct( |
59
|
|
|
int $opcode, |
60
|
|
|
int $sender, |
61
|
|
|
array $descriptors, |
62
|
|
|
bool $broadcast, |
63
|
|
|
bool $assigned, |
64
|
|
|
string $event, |
65
|
|
|
string $message = null |
66
|
|
|
) |
67
|
|
|
{ |
68
|
|
|
$this->opcode = $opcode; |
69
|
|
|
$this->sender = $sender; |
70
|
|
|
$this->descriptors = $descriptors; |
71
|
|
|
$this->broadcast = $broadcast; |
72
|
|
|
$this->assigned = $assigned; |
73
|
|
|
$this->event = $event; |
74
|
|
|
$this->message = $message; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Static constructor |
79
|
|
|
* |
80
|
|
|
* @param array $data |
81
|
|
|
* |
82
|
|
|
* @return \SwooleTW\Http\Websocket\Push |
83
|
|
|
*/ |
84
|
|
|
public static function new(array $data) |
85
|
|
|
{ |
86
|
|
|
$opcode = Arr::get($data, 'opcode', 1); |
87
|
|
|
$sender = Arr::get($data, 'sender', 0); |
88
|
|
|
$descriptors = Arr::get($data, 'fds', []); |
89
|
|
|
$broadcast = Arr::get($data, 'broadcast', false); |
90
|
|
|
$assigned = Arr::get($data, 'assigned', false); |
91
|
|
|
$event = Arr::get($data, 'event', ''); |
92
|
|
|
$message = Arr::get($data, 'message', null); |
93
|
|
|
|
94
|
|
|
return new static($opcode, $sender, $descriptors, $broadcast, $assigned, $event, $message); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
|
|
public function getOpcode(): int |
101
|
|
|
{ |
102
|
|
|
return $this->opcode; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
|
|
public function getSender(): int |
109
|
|
|
{ |
110
|
|
|
return $this->sender; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function getDescriptors(): array |
117
|
|
|
{ |
118
|
|
|
return $this->descriptors; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param int $descriptor |
123
|
|
|
*/ |
124
|
|
|
public function addDescriptor($descriptor): void |
125
|
|
|
{ |
126
|
|
|
$this->descriptors[] = $descriptor; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array $descriptors |
131
|
|
|
*/ |
132
|
|
|
public function mergeDescriptor(array $descriptors): void |
133
|
|
|
{ |
134
|
|
|
$this->descriptors[] = array_merge($this->descriptors, $descriptors); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param int $descriptor |
139
|
|
|
* |
140
|
|
|
* @return bool |
141
|
|
|
*/ |
142
|
|
|
public function hasDescriptor(int $descriptor): bool |
143
|
|
|
{ |
144
|
|
|
return in_array($descriptor, $this->descriptors, true); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function hasOwnDescriptor(): bool |
151
|
|
|
{ |
152
|
|
|
return $this->hasDescriptor($this->sender); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
public function isBroadcast(): bool |
159
|
|
|
{ |
160
|
|
|
return $this->broadcast; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
|
|
public function isAssigned(): bool |
167
|
|
|
{ |
168
|
|
|
return $this->assigned; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function getEvent(): string |
175
|
|
|
{ |
176
|
|
|
return $this->event; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return string|null |
181
|
|
|
*/ |
182
|
|
|
public function getMessage(): ?string |
183
|
|
|
{ |
184
|
|
|
return $this->message; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
public function isBroadcastToAllDescriptors(): bool |
191
|
|
|
{ |
192
|
|
|
return $this->isBroadcast() && ! $this->isAssigned() && count($this->descriptors) > 0; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param int $descriptor |
197
|
|
|
* |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
public function isBroadcastToDescriptor(int $descriptor): bool |
201
|
|
|
{ |
202
|
|
|
return $this->isBroadcast() && $this->getSender() === $descriptor; |
203
|
|
|
} |
204
|
|
|
} |