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