|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Swoole client |
|
4
|
|
|
* User: moyo |
|
5
|
|
|
* Date: 27/09/2017 |
|
6
|
|
|
* Time: 4:57 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Carno\Socket\Powered\Swoole; |
|
10
|
|
|
|
|
11
|
|
|
use Carno\Net\Address; |
|
12
|
|
|
use Carno\Net\Events; |
|
13
|
|
|
use Carno\Socket\Connection; |
|
14
|
|
|
use Carno\Socket\Contracts\TClient; |
|
15
|
|
|
use Carno\Socket\Options; |
|
16
|
|
|
use Carno\Socket\Powered\Swoole\Chips\Buffered; |
|
17
|
|
|
use Swoole\Client as SWClient; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class Client implements TClient |
|
20
|
|
|
{ |
|
21
|
|
|
use Buffered; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private $acceptEvs = [ |
|
27
|
|
|
'bufferFull', 'bufferEmpty', |
|
28
|
|
|
'connect', 'close', |
|
29
|
|
|
'receive', |
|
30
|
|
|
'error', |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Address |
|
35
|
|
|
*/ |
|
36
|
|
|
private $address = null; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Events |
|
40
|
|
|
*/ |
|
41
|
|
|
private $events = null; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var SWClient |
|
45
|
|
|
*/ |
|
46
|
|
|
private $client = null; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Address $address |
|
50
|
|
|
* @param Events $events |
|
51
|
|
|
* @param Options $options |
|
52
|
|
|
* @return TClient |
|
53
|
|
|
*/ |
|
54
|
|
|
public function connect(Address $address, Events $events, Options $options = null) : TClient |
|
55
|
|
|
{ |
|
56
|
|
|
$this->address = $address; |
|
57
|
|
|
$this->events = $events; |
|
58
|
|
|
|
|
59
|
|
|
$this->client = new SWClient(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
$options && $this->client->set($options->config()); |
|
62
|
|
|
|
|
63
|
|
|
foreach ($this->acceptEvs as $ev) { |
|
64
|
|
|
$this->client->on($ev, [$this, sprintf('ev%s', ucfirst($ev))]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->client->connect($address->host(), $address->port()); |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param SWClient $client |
|
74
|
|
|
*/ |
|
75
|
|
|
public function evConnect(SWClient $client) : void |
|
76
|
|
|
{ |
|
77
|
|
|
$local = $client->getsockname(); |
|
78
|
|
|
|
|
79
|
|
|
$this->events->notify( |
|
80
|
|
|
Events\Socket::CONNECTED, |
|
81
|
|
|
(new Connection) |
|
82
|
|
|
->setLocal($local['host'], $local['port']) |
|
83
|
|
|
->setRemote($this->address->host(), $this->address->port()) |
|
84
|
|
|
->from($this) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param SWClient $client |
|
90
|
|
|
*/ |
|
91
|
|
|
public function evClose(SWClient $client) : void |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
$this->events->notify( |
|
94
|
|
|
Events\Socket::CLOSED, |
|
95
|
|
|
(new Connection) |
|
96
|
|
|
->setRemote($this->address->host(), $this->address->port()) |
|
97
|
|
|
->from($this) |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param SWClient $client |
|
103
|
|
|
*/ |
|
104
|
|
|
public function evError(SWClient $client) : void |
|
|
|
|
|
|
105
|
|
|
{ |
|
106
|
|
|
$this->events->notify( |
|
107
|
|
|
Events\Socket::ERROR, |
|
108
|
|
|
(new Connection) |
|
109
|
|
|
->setRemote($this->address->host(), $this->address->port()) |
|
110
|
|
|
->from($this) |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param SWClient $client |
|
116
|
|
|
* @param string $data |
|
117
|
|
|
*/ |
|
118
|
|
|
public function evReceive(SWClient $client, string $data) : void |
|
|
|
|
|
|
119
|
|
|
{ |
|
120
|
|
|
$this->events->notify( |
|
121
|
|
|
Events\Socket::RECEIVED, |
|
122
|
|
|
(new Connection) |
|
123
|
|
|
->setReceived($data) |
|
124
|
|
|
->setRemote($this->address->host(), $this->address->port()) |
|
125
|
|
|
->from($this) |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param int $conn |
|
131
|
|
|
* @return bool |
|
132
|
|
|
*/ |
|
133
|
|
|
public function connected(int $conn = 0) : bool |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->client->isConnected(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param string $data |
|
140
|
|
|
* @param int $conn |
|
141
|
|
|
* @return bool |
|
142
|
|
|
*/ |
|
143
|
|
|
public function send(string $data, int $conn = 0) : bool |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->connected($conn) ? !! $this->client->send($data) : false; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param int $conn |
|
150
|
|
|
* @return bool |
|
151
|
|
|
*/ |
|
152
|
|
|
public function close(int $conn = 0) : bool |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->connected($conn) ? $this->client->close() : false; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
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