Passed
Push — master ( 102c19...95cf87 )
by Shiyu
04:19 queued 51s
created

UDPRelays::loading()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
rs 10
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;
0 ignored issues
show
Bug introduced by
The type Swoole\Client was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
0 ignored issues
show
Bug introduced by
The constant Carno\Traced\Transport\SWOOLE_SOCK_ASYNC was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Carno\Traced\Transport\SWOOLE_SOCK_SYNC was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Carno\Traced\Transport\SWOOLE_SOCK_UDP was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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