-
Notifications
You must be signed in to change notification settings - Fork 313
Expand file tree
/
Copy pathConnection.php
More file actions
470 lines (402 loc) · 12.3 KB
/
Connection.php
File metadata and controls
470 lines (402 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?php
/*
* This file is part of Chrome PHP.
*
* (c) Soufiane Ghzal <sghzal@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace HeadlessChromium\Communication;
use Evenement\EventEmitter;
use HeadlessChromium\Communication\Socket\SocketInterface;
use HeadlessChromium\Communication\Socket\WaitForDataInterface;
use HeadlessChromium\Communication\Socket\Wrench;
use HeadlessChromium\Exception\CommunicationException;
use HeadlessChromium\Exception\CommunicationException\CannotReadResponse;
use HeadlessChromium\Exception\CommunicationException\CantSyncEventsException;
use HeadlessChromium\Exception\CommunicationException\InvalidResponse;
use HeadlessChromium\Exception\OperationTimedOut;
use HeadlessChromium\Exception\TargetDestroyed;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Wrench\Client as WrenchBaseClient;
class Connection extends EventEmitter implements LoggerAwareInterface
{
use LoggerAwareTrait;
public const EVENT_TARGET_CREATED = 'method:Target.targetCreated';
public const EVENT_TARGET_INFO_CHANGED = 'method:Target.targetInfoChanged';
public const EVENT_TARGET_DESTROYED = 'method:Target.targetDestroyed';
/**
* When strict mode is enabled communication error will result in exceptions.
*
* @var bool
*/
protected $strict = true;
/**
* time in ms to wait between each message to be sent
* That helps to see what is happening when debugging.
*
* @var int
*/
protected $delay;
/**
* time in ms when the previous message was sent. Used to know how long to wait for before send next message
* (only when $delay is set).
*
* @var int
*/
private $lastMessageSentTime;
/**
* @var SocketInterface
*/
protected $wsClient;
/**
* List of response sent from the remote host and that are waiting to be read.
*
* @var array
*/
protected $responseBuffer = [];
/**
* Default timeout for send sync in ms.
*
* @var int
*/
protected $sendSyncDefaultTimeout;
/**
* @var Session[]
*/
protected $sessions = [];
/**
* @var array array of data received and waiting to be read
*/
protected $receivedData = [];
/**
* @var array<string, string>
*/
protected $httpHeaders = [];
/**
* CommunicationChannel constructor.
*
* @param SocketInterface|string $socketClient
* @param int|null $sendSyncDefaultTimeout
*/
public function __construct($socketClient, ?LoggerInterface $logger = null, ?int $sendSyncDefaultTimeout = null)
{
// set or create logger
$this->setLogger($logger ?? new NullLogger());
// set timeout
$this->sendSyncDefaultTimeout = $sendSyncDefaultTimeout ?? 5000;
// create socket client
if (\is_string($socketClient)) {
$socketClient = new Wrench(new WrenchBaseClient($socketClient, 'http://127.0.0.1'), $this->logger);
} elseif (!\is_object($socketClient) && !$socketClient instanceof SocketInterface) {
throw new \InvalidArgumentException('$socketClient param should be either a SockInterface instance or a web socket uri string');
}
$this->wsClient = $socketClient;
}
/**
* @return LoggerInterface
*/
public function getLogger(): LoggerInterface
{
return $this->logger;
}
/**
* Set the delay to apply everytime before data are sent.
*
* @param int $delay
*/
public function setConnectionDelay(int $delay): void
{
$this->delay = $delay;
}
/**
* @param array<string, string> $headers
*
* @return void
*/
public function setConnectionHttpHeaders(array $headers): void
{
$this->httpHeaders = $headers;
}
/**
* @return array<string, string>
*/
public function getConnectionHttpHeaders(): array
{
return $this->httpHeaders;
}
/**
* Gets the default timeout used when sending a message synchronously.
*
* @return int
*/
public function getSendSyncDefaultTimeout(): int
{
return $this->sendSyncDefaultTimeout;
}
/**
* @return bool
*/
public function isStrict(): bool
{
return $this->strict;
}
/**
* @param bool $strict
*/
public function setStrict(bool $strict): void
{
$this->strict = $strict;
}
/**
* Connects to the server.
*
* @return bool Whether a new connection was made
*/
public function connect()
{
return $this->wsClient->connect();
}
/**
* Disconnects the underlying socket, and marks the client as disconnected.
*
* @return bool
*/
public function disconnect()
{
return $this->wsClient->disconnect();
}
/**
* Returns whether the client is currently connected.
*
* @return bool true if connected
*/
public function isConnected()
{
return $this->wsClient->isConnected();
}
/**
* Wait before sending next message.
*/
private function waitForDelay(): void
{
if ($this->lastMessageSentTime) {
$currentTime = (int) (\hrtime(true) / 1000 / 1000);
// if not enough time was spent until last message was sent, wait
if ($this->lastMessageSentTime + $this->delay > $currentTime) {
$timeToWait = ($this->lastMessageSentTime + $this->delay) - $currentTime;
\usleep($timeToWait * 1000);
}
}
$this->lastMessageSentTime = (int) (\hrtime(true) / 1000 / 1000);
}
/**
* Sends the given message and returns a response reader.
*
* @param Message $message
*
* @throws CommunicationException
*
* @return ResponseReader
*/
public function sendMessage(Message $message): ResponseReader
{
// if delay enabled wait before sending message
if ($this->delay > 0) {
$this->waitForDelay();
}
$sent = $this->wsClient->sendData((string) $message);
if (!$sent) {
$message = 'Message could not be sent.';
if (!$this->isConnected()) {
$message .= ' Reason: the connection is closed.';
} else {
$message .= ' Reason: unknown.';
}
throw new CommunicationException($message);
}
return new ResponseReader($message, $this);
}
/**
* @param Message $message
* @param int|null $timeout
*
* @throws OperationTimedOut
*
* @return Response
*/
public function sendMessageSync(Message $message, ?int $timeout = null): Response
{
$responseReader = $this->sendMessage($message);
$response = $responseReader->waitForResponse($timeout);
return $response;
}
/**
* Create a session for the given target id.
*
* @param string $targetId
* @param ?string $sessionId
*
* @return Session
*/
public function createSession($targetId, $sessionId = null): Session
{
if (null === $sessionId) {
$response = $this->sendMessageSync(
new Message('Target.attachToTarget', ['targetId' => $targetId, 'flatten' => true])
);
if (empty($response['result'])) {
throw new TargetDestroyed('The target was destroyed.');
}
$sessionId = $response['result']['sessionId'];
}
$session = new Session($targetId, $sessionId, $this);
$this->sessions[$sessionId] = $session;
$session->on('destroyed', function () use ($sessionId): void {
$this->logger->debug('✘ session('.$sessionId.') was destroyed and unreferenced.');
unset($this->sessions[$sessionId]);
});
return $session;
}
/**
* Receive and stack data from the socket.
*/
private function receiveData(): void
{
$this->receivedData = \array_merge($this->receivedData, $this->wsClient->receiveData());
}
/**
* Read data from CRI and store messages.
*
* @throws CannotReadResponse
* @throws InvalidResponse
*
* @return bool true if data were received
*/
public function readData()
{
$hasData = false;
while ($this->readLine()) {
$hasData = true;
}
return $hasData;
}
public function readLine()
{
// if buffer empty, then read from input
if (empty($this->receivedData)) {
$this->receiveData();
}
// dispatch first line of buffer
$datum = \array_shift($this->receivedData);
if ($datum) {
return $this->dispatchMessage($datum);
}
return false;
}
public function processAllEvents(): void
{
if (false === $this->wsClient instanceof WaitForDataInterface) {
throw new CantSyncEventsException();
}
$hasData = $this->wsClient->waitForData(0);
if ($hasData) {
$this->receiveData();
}
}
/**
* Dispatches the message and either stores the response or emits an event.
*
* @throws InvalidResponse
*
* @return bool
*
* @internal
*/
private function dispatchMessage(string $message, ?Session $session = null)
{
try {
$response = \json_decode($message, true, 512, \JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
if ($this->isStrict()) {
throw new CannotReadResponse('Response from chrome remote interface is not a valid JSON response', 0, $e);
}
return false;
}
// response must be array
if (!\is_array($response)) {
if ($this->isStrict()) {
throw new CannotReadResponse('Response from chrome remote interface was not a valid array');
}
return false;
}
// id is required to identify the response
if (!isset($response['id'])) {
if (isset($response['method'])) {
if ('Target.receivedMessageFromTarget' == $response['method']) {
$session = $this->sessions[$response['params']['sessionId']];
return $this->dispatchMessage($response['params']['message'], $session);
} else {
if (!$session && isset($response['sessionId'])) {
$session = $this->sessions[$response['sessionId']] ?? null;
}
if ($session) {
$this->logger->debug(
'session('.$session->getSessionId().'): ⇶ dispatching method:'.$response['method']
);
$session->emit('method:'.$response['method'], [$response['params']]);
} else {
$this->logger->debug('connection: ⇶ dispatching method:'.$response['method']);
$this->emit('method:'.$response['method'], [$response['params']]);
}
}
return false;
}
if ($this->isStrict()) {
throw new InvalidResponse('Response from chrome remote interface did not provide a valid message id');
}
return false;
}
// store response
$this->responseBuffer[$response['id']] = $response;
return true;
}
/**
* True if a response for the given id exists.
*
* @param string $id
*
* @return bool
*/
public function hasResponseForId($id)
{
return \array_key_exists($id, $this->responseBuffer);
}
/**
* @param string $id
*
* @return array|null
*/
public function getResponseForId($id)
{
if (\array_key_exists($id, $this->responseBuffer)) {
$data = $this->responseBuffer[$id];
unset($this->responseBuffer[$id]);
return $data;
}
return null;
}
/**
* @param string $sessionId
*
* @return bool
*/
public function isSessionDestroyed($sessionId)
{
return !isset($this->sessions[$sessionId]);
}
}