1 | <?php |
||
26 | class MessageProcessor |
||
27 | { |
||
28 | /** |
||
29 | * @var FrameFactory |
||
30 | */ |
||
31 | private $frameFactory; |
||
32 | |||
33 | /** |
||
34 | * @var MessageFactory |
||
35 | */ |
||
36 | private $messageFactory; |
||
37 | |||
38 | /** |
||
39 | * @var Rfc6455FrameHandlerInterface[] |
||
40 | */ |
||
41 | private $handlers; |
||
42 | |||
43 | /** |
||
44 | * @var boolean |
||
45 | */ |
||
46 | private $writeMasked; |
||
47 | |||
48 | 15 | public function __construct($writeMasked = false, FrameFactory $factory = null, MessageFactory $messageFactory = null) |
|
55 | |||
56 | /** |
||
57 | * This methods process data received from the socket to generate a `Message` entity and/or process handler |
||
58 | * which may answer to some special ws-frames. |
||
59 | * |
||
60 | * Legend: |
||
61 | * - {} stands for bin-frames |
||
62 | * - || stands for ws-frames |
||
63 | * - [] stands for Messages (of potentially many ws-frames) |
||
64 | * - () are here for comment purpose |
||
65 | * |
||
66 | * This method buffer in many ways: |
||
67 | * |
||
68 | * - { [|ws-frame1 (not final) |, |ws-frame2 (final)|] } |
||
69 | * => buffer 2 ws-frames from 1 bin-frame to generate 1 Message |
||
70 | * |
||
71 | * - { [|ws-frame1 (not final) } { ws-frame 1 (final)| } { |ws-frame 2 (final)|] } |
||
72 | * => buffer 2 ws-frames from 3 bin-frame to generate 1 Message |
||
73 | * |
||
74 | * - { [|ws-frame1 (not final)| |ws-frame 2 (final, control frame, is not part of the current message)| |ws-frame3 (final, with ws-frame1)|] } |
||
75 | * => buffer 2 ws-frames from 1 bin-frame to generate 1 Message with a control frame in the middle of the bin-frame. |
||
76 | * |
||
77 | * @param string $data |
||
78 | * @param ConnectionInterface $socket |
||
79 | * @param Message|null $message |
||
80 | * @return \Generator |
||
81 | */ |
||
82 | 13 | public function onData(string $data, ConnectionInterface $socket, Message $message = null) |
|
148 | |||
149 | /** |
||
150 | * @param Message $message |
||
151 | * @param ConnectionInterface $socket |
||
152 | */ |
||
153 | 8 | protected function processHelper(Message $message, ConnectionInterface $socket) |
|
161 | |||
162 | /** |
||
163 | * @param Frame $frame |
||
164 | * @param ConnectionInterface $socket |
||
165 | * |
||
166 | * @return Message |
||
167 | */ |
||
168 | 1 | protected function processControlFrame(Frame $frame, ConnectionInterface $socket) : Message |
|
176 | |||
177 | /** |
||
178 | * @param Rfc6455FrameHandlerInterface $handler |
||
179 | * @return self |
||
180 | */ |
||
181 | 2 | public function addHandler(Rfc6455FrameHandlerInterface $handler) |
|
187 | |||
188 | /** |
||
189 | * @param Frame|string $frame |
||
190 | * @param ConnectionInterface $socket |
||
191 | * @param int $opCode An int representing binary or text data (const of Frame class) |
||
192 | */ |
||
193 | 8 | public function write($frame, ConnectionInterface $socket, int $opCode = Frame::OP_TEXT) |
|
208 | |||
209 | /** |
||
210 | * @return FrameFactory |
||
211 | */ |
||
212 | 2 | public function getFrameFactory(): FrameFactory |
|
216 | |||
217 | /** |
||
218 | * @param ConnectionInterface $socket |
||
219 | */ |
||
220 | public function timeout(ConnectionInterface $socket) |
||
225 | |||
226 | /** |
||
227 | * @param ConnectionInterface $socket |
||
228 | * @param int $status |
||
229 | * @param string|null $reason |
||
230 | */ |
||
231 | public function close(ConnectionInterface $socket, int $status = Frame::CLOSE_NORMAL, string $reason = null) |
||
236 | } |
||
237 |