1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the PHP Generics package. |
5
|
|
|
* |
6
|
|
|
* @package Generics |
7
|
|
|
*/ |
8
|
|
|
namespace Generics\Socket; |
9
|
|
|
|
10
|
|
|
use Generics\Streams\SocketStream; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This abstract class provides basic socket functionality |
14
|
|
|
* |
15
|
|
|
* @author Maik Greubel <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
abstract class Socket implements SocketStream |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The socket handle |
22
|
|
|
* |
23
|
|
|
* @var resource |
24
|
|
|
*/ |
25
|
|
|
protected $handle; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The socket endpoint |
29
|
|
|
* |
30
|
|
|
* @var Endpoint |
31
|
|
|
*/ |
32
|
|
|
protected $endpoint; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create a new socket |
36
|
|
|
* |
37
|
|
|
* @param Endpoint $endpoint |
38
|
|
|
* The endpoint for the socket |
39
|
|
|
*/ |
40
|
11 |
|
public function __construct(Endpoint $endpoint) |
41
|
|
|
{ |
42
|
11 |
|
$this->endpoint = $endpoint; |
43
|
11 |
|
$this->handle = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
44
|
|
|
|
45
|
11 |
|
if (! is_resource($this->handle)) { |
46
|
|
|
$code = socket_last_error(); |
47
|
|
|
throw new SocketException(socket_strerror($code), $code); |
48
|
|
|
} |
49
|
11 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Clean up |
53
|
|
|
*/ |
54
|
11 |
|
public function __destruct() |
55
|
|
|
{ |
56
|
11 |
|
$this->close(); |
57
|
11 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritDoc} |
61
|
|
|
* @see \Generics\Streams\Stream::close() |
62
|
|
|
*/ |
63
|
|
|
public function close() |
64
|
6 |
|
{ |
65
|
|
|
if (is_resource($this->handle)) { |
66
|
6 |
|
socket_close($this->handle); |
67
|
6 |
|
$this->handle = null; |
68
|
6 |
|
} |
69
|
6 |
|
} |
70
|
6 |
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
* @see \Generics\Streams\Stream::ready() |
74
|
|
|
*/ |
75
|
|
|
public function ready() |
76
|
|
|
{ |
77
|
8 |
|
if (! is_resource($this->handle)) { |
78
|
|
|
return false; |
79
|
8 |
|
} |
80
|
|
|
|
81
|
|
|
$read = array( |
82
|
|
|
$this->handle |
83
|
|
|
); |
84
|
8 |
|
$write = null; |
85
|
8 |
|
$except = null; |
86
|
8 |
|
|
87
|
8 |
|
$num = @socket_select($read, $write, $except, 0); |
88
|
|
|
|
89
|
8 |
|
if ($num === false) { |
90
|
|
|
$code = socket_last_error($this->handle); |
91
|
8 |
|
throw new SocketException(socket_strerror($code), $code); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($num < 1) { |
95
|
|
|
return false; |
96
|
8 |
|
} |
97
|
8 |
|
|
98
|
|
|
if (! in_array($this->handle, $read)) { |
99
|
|
|
return false; |
100
|
6 |
|
} |
101
|
|
|
|
102
|
|
|
return true; |
103
|
|
|
} |
104
|
6 |
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritDoc} |
107
|
|
|
* @see \Generics\Streams\OutputStream::isWriteable() |
108
|
|
|
*/ |
109
|
|
|
public function isWriteable() |
110
|
|
|
{ |
111
|
|
|
if (! is_resource($this->handle)) { |
112
|
1 |
|
return false; |
113
|
|
|
} |
114
|
1 |
|
|
115
|
|
|
$read = null; |
116
|
|
|
$write = array( |
117
|
|
|
$this->handle |
118
|
1 |
|
); |
119
|
|
|
$except = null; |
120
|
1 |
|
|
121
|
1 |
|
$num = @socket_select($read, $write, $except, 0, 0); |
122
|
1 |
|
|
123
|
|
|
if ($num === false) { |
124
|
1 |
|
$code = socket_last_error($this->handle); |
125
|
|
|
throw new SocketException(socket_strerror($code), $code); |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
if ($num < 1) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
1 |
|
|
132
|
|
|
if (! in_array($this->handle, $write)) { |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
1 |
|
|
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
/** |
140
|
|
|
* {@inheritDoc} |
141
|
|
|
* @see \Countable::count() |
142
|
|
|
*/ |
143
|
|
|
public function count() |
144
|
|
|
{ |
145
|
|
|
throw new SocketException("Cannot count elements of socket"); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritDoc} |
150
|
|
|
* @see \Generics\Streams\InputStream::read() |
151
|
|
|
*/ |
152
|
|
|
public function read($length = 1, $offset = null) |
153
|
|
|
{ |
154
|
|
|
if (($buf = @socket_read($this->handle, $length)) === false) { |
155
|
|
|
$buf = null; |
156
|
|
|
$code = socket_last_error(); |
157
|
6 |
|
if ($code != 0) { |
158
|
|
|
if ($code != 10053) { |
159
|
6 |
|
throw new SocketException(socket_strerror($code), $code); |
160
|
|
|
} else { |
161
|
|
|
$this->handle = null; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $buf; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritDoc} |
171
|
6 |
|
* @see \Generics\Streams\OutputStream::write() |
172
|
|
|
*/ |
173
|
|
|
public function write($buffer) |
174
|
|
|
{ |
175
|
|
|
if (($written = @socket_write($this->handle, "{$buffer}\0")) === false) { |
176
|
|
|
$code = socket_last_error(); |
177
|
|
|
throw new SocketException(socket_strerror($code), $code); |
178
|
|
|
} |
179
|
7 |
|
|
180
|
|
|
if ($written != strlen($buffer) + 1) { |
181
|
7 |
|
throw new SocketException("Could not write all {bytes} bytes to socket ({written} written)", array( |
|
|
|
|
182
|
|
|
'bytes' => strlen($buffer), |
183
|
|
|
'written' => $written |
184
|
|
|
)); |
185
|
|
|
} |
186
|
7 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get the socket endpoint |
190
|
|
|
* |
191
|
|
|
* @return \Generics\Socket\Endpoint |
192
|
7 |
|
*/ |
193
|
|
|
public function getEndpoint() |
194
|
|
|
{ |
195
|
|
|
return $this->endpoint; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
7 |
|
* {@inheritDoc} |
200
|
|
|
* @see \Generics\Streams\OutputStream::flush() |
201
|
7 |
|
*/ |
202
|
|
|
public function flush() |
203
|
|
|
{ |
204
|
|
|
// There is no function to flush a socket. This is only possible for file descriptors. |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* {@inheritDoc} |
209
|
|
|
* @see \Generics\Streams\Stream::isOpen() |
210
|
|
|
*/ |
211
|
|
|
public function isOpen() |
212
|
|
|
{ |
213
|
|
|
return is_resource($this->handle); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: