-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathAgentClient.php
More file actions
101 lines (78 loc) · 2.4 KB
/
AgentClient.php
File metadata and controls
101 lines (78 loc) · 2.4 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
<?php
declare(strict_types=1);
namespace Sentry\Agent\Transport;
use Sentry\HttpClient\HttpClientInterface;
use Sentry\HttpClient\Request;
use Sentry\HttpClient\Response;
use Sentry\Options;
class AgentClient implements HttpClientInterface
{
/**
* @var string
*/
private $host;
/**
* @var int
*/
private $port;
/**
* @var resource|null
*/
private $socket;
public function __construct(string $host = '127.0.0.1', int $port = 5148)
{
$this->host = $host;
$this->port = $port;
}
public function __destruct()
{
$this->disconnect();
}
/**
* @phpstan-assert-if-true resource $this->socket
*/
private function connect(): bool
{
if ($this->socket !== null) {
return true;
}
// We set the timeout to 10ms to avoid blocking the request for too long if the agent is not running
// @TODO: 10ms should be low enough? Do we want to go lower and/or make this configurable? Only applies to initial connection.
$socket = fsockopen($this->host, $this->port, $errorNo, $errorMsg, 0.01);
// @TODO: Error handling? See $errorNo and $errorMsg
if ($socket === false) {
return false;
}
// @TODO: Set a timeout for the socket to prevent blocking (?) if the socket connection stops working after the connection (e.g. the agent is stopped) if needed
$this->socket = $socket;
return true;
}
private function disconnect(): void
{
if ($this->socket === null) {
return;
}
fclose($this->socket);
$this->socket = null;
}
private function send(string $message): void
{
if (!$this->connect()) {
return;
}
// @TODO: Make sure we don't send more than 2^32 - 1 bytes
$contentLength = pack('N', \strlen($message) + 4);
// @TODO: Error handling?
fwrite($this->socket, $contentLength . $message);
}
public function sendRequest(Request $request, Options $options): Response
{
$body = $request->getStringBody();
if (empty($body)) {
return new Response(400, [], 'Request body is empty');
}
$this->send($body);
// Since we are sending async there is no feedback so we always return an empty response
return new Response(202, [], '');
}
}