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 Rfc6455MessageHandlerInterface[] |
||
40 | */ |
||
41 | private $handlers; |
||
42 | |||
43 | 21 | public function __construct(FrameFactory $factory = null, MessageFactory $messageFactory = null) |
|
49 | |||
50 | /** |
||
51 | * This methods process data received from the socket to generate a `Message` entity and/or process handler |
||
52 | * which may answer to some special ws-frames. |
||
53 | * |
||
54 | * Legend: |
||
55 | * - {} stands for bin-frames |
||
56 | * - || stands for ws-frames |
||
57 | * - [] stands for Messages (of potentially many ws-frames) |
||
58 | * - () are here for comment purpose |
||
59 | * |
||
60 | * This method buffer in many ways: |
||
61 | * |
||
62 | * - { [|ws-frame1 (not final) |, |ws-frame2 (final)|] } |
||
63 | * => buffer 2 ws-frames from 1 bin-frame to generate 1 Message |
||
64 | * |
||
65 | * - { [|ws-frame1 (not final) } { ws-frame 1 (final)| } { |ws-frame 2 (final)|] } |
||
66 | * => buffer 2 ws-frames from 3 bin-frame to generate 1 Message |
||
67 | * |
||
68 | * - { [|ws-frame1 (not final)| |ws-frame 2 (final, control frame, is not part of the current message)| |ws-frame3 (final, with ws-frame1)|] } |
||
69 | * => buffer 2 ws-frames from 1 bin-frame to generate 1 Message with a control frame in the middle of the bin-frame. |
||
70 | * |
||
71 | * @param string $data |
||
72 | * @param ConnectionInterface $socket |
||
73 | * @param Message|null $message |
||
74 | * @return \Generator |
||
75 | */ |
||
76 | 13 | public function onData(string $data, ConnectionInterface $socket, Message $message = null) |
|
142 | |||
143 | /** |
||
144 | * @param Message $message |
||
145 | * @param ConnectionInterface $socket |
||
146 | */ |
||
147 | 8 | protected function processHelper(Message $message, ConnectionInterface $socket) |
|
155 | |||
156 | /** |
||
157 | * @param Frame $frame |
||
158 | * @param ConnectionInterface $socket |
||
159 | * |
||
160 | * @return Message |
||
161 | */ |
||
162 | 1 | protected function processControlFrame(Frame $frame, ConnectionInterface $socket) : Message |
|
170 | |||
171 | /** |
||
172 | * @param Rfc6455MessageHandlerInterface $handler |
||
173 | * @return self |
||
174 | */ |
||
175 | 8 | public function addHandler(Rfc6455MessageHandlerInterface $handler) |
|
181 | |||
182 | /** |
||
183 | * @param Frame|string $frame |
||
184 | * @param ConnectionInterface $socket |
||
185 | * @param int $opCode An int representing binary or text data (const of Frame class) |
||
186 | */ |
||
187 | 9 | public function write($frame, ConnectionInterface $socket, int $opCode = Frame::OP_TEXT) |
|
198 | |||
199 | /** |
||
200 | * @return FrameFactory |
||
201 | */ |
||
202 | 2 | public function getFrameFactory(): FrameFactory |
|
206 | |||
207 | /** |
||
208 | * @param ConnectionInterface $socket |
||
209 | */ |
||
210 | public function timeout(ConnectionInterface $socket) |
||
215 | |||
216 | /** |
||
217 | * @param ConnectionInterface $socket |
||
218 | */ |
||
219 | 1 | public function close(ConnectionInterface $socket) |
|
224 | } |
||
225 |