1 | <?php |
||
27 | class MessageProcessor |
||
28 | { |
||
29 | /** |
||
30 | * @var FrameFactory |
||
31 | */ |
||
32 | private $frameFactory; |
||
33 | |||
34 | /** |
||
35 | * @var Rfc6455MessageHandlerInterface[] |
||
36 | */ |
||
37 | private $handlers; |
||
38 | |||
39 | 13 | public function __construct(FrameFactory $factory = null) |
|
44 | |||
45 | /** |
||
46 | * This methods process data received from the socket to generate a `Message` entity and/or process handler |
||
47 | * which may answer to some special ws-frames. |
||
48 | * |
||
49 | * Legend: |
||
50 | * - {} stands for bin-frames |
||
51 | * - || stands for ws-frames |
||
52 | * - [] stands for Messages (of potentially many ws-frames) |
||
53 | * - () are here for comment purpose |
||
54 | * |
||
55 | * This method buffer in many ways: |
||
56 | * |
||
57 | * - { [|ws-frame1 (not final) |, |ws-frame2 (final)|] } |
||
58 | * => buffer 2 ws-frames from 1 bin-frame to generate 1 Message |
||
59 | * |
||
60 | * - { [|ws-frame1 (not final) } { ws-frame 1 (final)| } { |ws-frame 2 (final)|] } |
||
61 | * => buffer 2 ws-frames from 3 bin-frame to generate 1 Message |
||
62 | * |
||
63 | * - { [|ws-frame1 (not final)| |ws-frame 2 (final, control frame, is not part of the current message)| |ws-frame3 (final, with ws-frame1)|] } |
||
64 | * => buffer 2 ws-frames from 1 bin-frame to generate 1 Message with a control frame in the middle of the bin-frame. |
||
65 | * |
||
66 | * @param string $data |
||
67 | * @param ConnectionInterface $socket |
||
68 | * @param Message|null $message |
||
69 | * @return \Generator |
||
70 | */ |
||
71 | 11 | public function onData(string $data, ConnectionInterface $socket, Message $message = null) |
|
127 | |||
128 | /** |
||
129 | * @param Message $message |
||
130 | * @param ConnectionInterface $socket |
||
131 | */ |
||
132 | 8 | protected function processHelper(Message $message, ConnectionInterface $socket) |
|
140 | |||
141 | /** |
||
142 | * @param Frame $frame |
||
143 | * @param ConnectionInterface $socket |
||
144 | * |
||
145 | * @return Message |
||
146 | */ |
||
147 | 1 | protected function processControlFrame(Frame $frame, ConnectionInterface $socket) : Message |
|
155 | |||
156 | /** |
||
157 | * @param Rfc6455MessageHandlerInterface $handler |
||
158 | * @return self |
||
159 | */ |
||
160 | 2 | public function addHandler(Rfc6455MessageHandlerInterface $handler) |
|
166 | |||
167 | /** |
||
168 | * @param Frame|string $frame |
||
169 | * @param ConnectionInterface $socket |
||
170 | * @param int $opCode An int representing binary or text data (const of Frame class) |
||
171 | */ |
||
172 | 6 | public function write($frame, ConnectionInterface $socket, int $opCode = Frame::OP_TEXT) |
|
183 | |||
184 | /** |
||
185 | * @return FrameFactory |
||
186 | */ |
||
187 | 2 | public function getFrameFactory(): FrameFactory |
|
191 | |||
192 | /** |
||
193 | * @param ConnectionInterface $socket |
||
194 | */ |
||
195 | public function timeout(ConnectionInterface $socket) |
||
200 | |||
201 | /** |
||
202 | * @param ConnectionInterface $socket |
||
203 | */ |
||
204 | public function close(ConnectionInterface $socket) |
||
208 | } |
||
209 |