|
1
|
|
|
<?php |
|
2
|
|
|
namespace Dazzle\Http\Socket; |
|
3
|
|
|
|
|
4
|
|
|
use Dazzle\Socket\SocketInterface; |
|
5
|
|
|
use Dazzle\Socket\SocketListenerInterface; |
|
6
|
|
|
use Dazzle\Http\Null\NullServer; |
|
7
|
|
|
use Dazzle\Http\NetworkComponentAwareInterface; |
|
8
|
|
|
use Dazzle\Http\NetworkConnection; |
|
9
|
|
|
use Dazzle\Http\NetworkMessage; |
|
10
|
|
|
use Dazzle\Http\NetworkComponentInterface; |
|
11
|
|
|
use Error; |
|
12
|
|
|
use Exception; |
|
13
|
|
|
|
|
14
|
|
|
class SocketServer implements SocketServerInterface, NetworkComponentAwareInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var SocketListenerInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $socket; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var NetworkComponentInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $component; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param NetworkComponentInterface $component |
|
28
|
|
|
* @param SocketListenerInterface $socket |
|
29
|
|
|
*/ |
|
30
|
55 |
|
public function __construct(SocketListenerInterface $socket, NetworkComponentInterface $component = null) |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
55 |
|
$this->socket = $socket; |
|
34
|
55 |
|
$this->component = $component === null ? new NullServer() : $component; |
|
35
|
|
|
|
|
36
|
55 |
|
$socket->on('connect', [ $this, 'handleConnect' ]); |
|
37
|
55 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* |
|
41
|
|
|
*/ |
|
42
|
15 |
|
public function __destruct() |
|
43
|
|
|
{ |
|
44
|
15 |
|
unset($this->socket); |
|
45
|
15 |
|
unset($this->component); |
|
46
|
15 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @override |
|
50
|
|
|
* @inheritDoc |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function start() |
|
53
|
|
|
{ |
|
54
|
1 |
|
$this->socket->start(); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @override |
|
59
|
|
|
* @inheritDoc |
|
60
|
|
|
*/ |
|
61
|
3 |
|
public function stop() |
|
62
|
|
|
{ |
|
63
|
3 |
|
$this->socket->close(); |
|
64
|
3 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @override |
|
68
|
|
|
* @inheritDoc |
|
69
|
|
|
*/ |
|
70
|
24 |
|
public function setComponent(NetworkComponentInterface $component = null) |
|
71
|
|
|
{ |
|
72
|
24 |
|
$this->component = $component === null ? new NullServer() : $component; |
|
73
|
24 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @override |
|
77
|
|
|
* @inheritDoc |
|
78
|
|
|
*/ |
|
79
|
3 |
|
public function getComponent() |
|
80
|
|
|
{ |
|
81
|
3 |
|
return $this->component; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Handler triggered when a new connection is received from SocketListener. |
|
86
|
|
|
* |
|
87
|
|
|
* @param SocketListenerInterface $server |
|
88
|
|
|
* @param SocketInterface $socket |
|
89
|
|
|
*/ |
|
90
|
7 |
|
public function handleConnect($server, $socket) |
|
91
|
|
|
{ |
|
92
|
7 |
|
$socket->conn = new NetworkConnection($socket); |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
try |
|
95
|
|
|
{ |
|
96
|
7 |
|
$this->component->handleConnect($socket->conn); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
6 |
|
$socket->on('data', [ $this, 'handleData' ]); |
|
99
|
6 |
|
$socket->on('error', [ $this, 'handleError' ]); |
|
100
|
6 |
|
$socket->on('close', [ $this, 'handleDisconnect' ]); |
|
101
|
|
|
} |
|
102
|
1 |
|
catch (Error $ex) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->close($socket); |
|
105
|
|
|
} |
|
106
|
1 |
|
catch (Exception $ex) |
|
107
|
|
|
{ |
|
108
|
1 |
|
$this->close($socket); |
|
109
|
|
|
} |
|
110
|
7 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Handler triggered when an existing connection is being closed. |
|
114
|
|
|
* |
|
115
|
|
|
* @param SocketInterface $socket |
|
116
|
|
|
*/ |
|
117
|
6 |
View Code Duplication |
public function handleDisconnect($socket) |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
try |
|
120
|
|
|
{ |
|
121
|
6 |
|
$this->component->handleDisconnect($socket->conn); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
1 |
|
catch (Error $ex) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->handleError($socket, $ex); |
|
126
|
|
|
} |
|
127
|
1 |
|
catch (Exception $ex) |
|
128
|
|
|
{ |
|
129
|
1 |
|
$this->handleError($socket, $ex); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
6 |
|
unset($socket->conn); |
|
133
|
6 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Handler triggered when a new data is received from existing connection. |
|
137
|
|
|
* |
|
138
|
|
|
* @param SocketInterface $socket |
|
139
|
|
|
* @param mixed $data |
|
140
|
|
|
*/ |
|
141
|
6 |
|
public function handleData($socket, $data) |
|
142
|
|
|
{ |
|
143
|
|
|
try |
|
144
|
|
|
{ |
|
145
|
6 |
|
$this->component->handleMessage($socket->conn, new NetworkMessage($data)); |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
1 |
|
catch (Error $ex) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->handleError($socket, $ex); |
|
150
|
|
|
} |
|
151
|
1 |
|
catch (Exception $ex) |
|
152
|
|
|
{ |
|
153
|
1 |
|
$this->handleError($socket, $ex); |
|
154
|
|
|
} |
|
155
|
6 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Handler triggered when an error has occured during doing operation on existing connection. |
|
159
|
|
|
* |
|
160
|
|
|
* @param SocketInterface $socket |
|
161
|
|
|
* @param Error|Exception $ex |
|
162
|
|
|
*/ |
|
163
|
2 |
View Code Duplication |
public function handleError($socket, $ex) |
|
|
|
|
|
|
164
|
|
|
{ |
|
165
|
|
|
try |
|
166
|
|
|
{ |
|
167
|
2 |
|
$this->component->handleError($socket->conn, $ex); |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
1 |
|
catch (Error $ex) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->close($socket); |
|
172
|
|
|
} |
|
173
|
1 |
|
catch (Exception $ex) |
|
174
|
|
|
{ |
|
175
|
1 |
|
$this->close($socket); |
|
176
|
|
|
} |
|
177
|
2 |
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Close socket. |
|
181
|
|
|
* |
|
182
|
|
|
* @param SocketInterface $socket |
|
183
|
|
|
*/ |
|
184
|
1 |
|
protected function close(SocketInterface $socket) |
|
185
|
|
|
{ |
|
186
|
1 |
|
$socket->close(); |
|
187
|
1 |
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: