|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* UDP Relays |
|
4
|
|
|
* User: moyo |
|
5
|
|
|
* Date: Jul 23, 2019 |
|
6
|
|
|
* Time: 10:41 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Carno\Traced\Transport; |
|
10
|
|
|
|
|
11
|
|
|
use Carno\Net\Address; |
|
12
|
|
|
use Carno\Promise\Promise; |
|
13
|
|
|
use Carno\Promise\Promised; |
|
14
|
|
|
use Carno\Tracing\Contracts\Transport; |
|
15
|
|
|
use Swoole\Client; |
|
|
|
|
|
|
16
|
|
|
use Closure; |
|
17
|
|
|
|
|
18
|
|
|
class UDPRelays implements Transport |
|
19
|
|
|
{ |
|
20
|
|
|
public const MAGIC = ' ZU'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Address |
|
24
|
|
|
*/ |
|
25
|
|
|
private $endpoint = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $identify = 'unknown'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Closure |
|
34
|
|
|
*/ |
|
35
|
|
|
private $sender = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var resource |
|
39
|
|
|
*/ |
|
40
|
|
|
private $socket = null; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var Client |
|
44
|
|
|
*/ |
|
45
|
|
|
private $client = null; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* UDPRelays constructor. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct() |
|
51
|
|
|
{ |
|
52
|
|
|
if (class_exists('\\Swoole\\Client')) { |
|
53
|
|
|
$this->mkSwoole(); |
|
54
|
|
|
} elseif (function_exists('socket_create')) { |
|
55
|
|
|
$this->mkSockets(); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
*/ |
|
61
|
|
|
private function mkSwoole() : void |
|
62
|
|
|
{ |
|
63
|
|
|
$this->client = new Client(SWOOLE_SOCK_UDP, PHP_SAPI === 'cli' ? SWOOLE_SOCK_ASYNC : SWOOLE_SOCK_SYNC); |
|
|
|
|
|
|
64
|
|
|
$this->sender = function (string $data) { |
|
65
|
|
|
$this->client->sendto($this->endpoint->host(), $this->endpoint->port(), $data); |
|
66
|
|
|
}; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
*/ |
|
71
|
|
|
private function mkSockets() : void |
|
72
|
|
|
{ |
|
73
|
|
|
$this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); |
|
74
|
|
|
$this->sender = function (string $data) { |
|
75
|
|
|
socket_sendto( |
|
76
|
|
|
$this->socket, |
|
77
|
|
|
$data, |
|
78
|
|
|
strlen($data), |
|
79
|
|
|
MSG_EOF, |
|
80
|
|
|
$this->endpoint->host(), |
|
81
|
|
|
$this->endpoint->port() |
|
82
|
|
|
); |
|
83
|
|
|
}; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param Address $endpoint |
|
88
|
|
|
* @param string $identify |
|
89
|
|
|
*/ |
|
90
|
|
|
public function connect(Address $endpoint, string $identify = null) : void |
|
91
|
|
|
{ |
|
92
|
|
|
$this->endpoint = $endpoint; |
|
93
|
|
|
$this->identify = trim($identify, " /\t\n"); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return Promised |
|
98
|
|
|
*/ |
|
99
|
|
|
public function disconnect() : Promised |
|
100
|
|
|
{ |
|
101
|
|
|
$this->socket && socket_close($this->socket); |
|
102
|
|
|
$this->client && $this->client = null; |
|
103
|
|
|
return Promise::resolved(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $data |
|
108
|
|
|
*/ |
|
109
|
|
|
public function loading(string $data) : void |
|
110
|
|
|
{ |
|
111
|
|
|
$this->sender && ($this->sender)($this->packing($data)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $payload |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
private function packing(string $payload) : string |
|
119
|
|
|
{ |
|
120
|
|
|
return |
|
121
|
|
|
self::MAGIC . |
|
122
|
|
|
pack('N', strlen($this->identify)) . |
|
123
|
|
|
$this->identify . |
|
124
|
|
|
pack('N', strlen($payload)) . |
|
125
|
|
|
$payload |
|
126
|
|
|
; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
*/ |
|
131
|
|
|
public function flushing() : void |
|
132
|
|
|
{ |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths