1 | <?php |
||
9 | class Socket |
||
10 | { |
||
11 | /** |
||
12 | * Longest possible response preface: |
||
13 | * <code>USING <tubeName=max 200 byte>\r\n</code> |
||
14 | */ |
||
15 | const MAX_SINGLE_RESPONSE_LENGTH = 208; |
||
16 | |||
17 | /** @var bool */ |
||
18 | protected $connected = false; |
||
19 | |||
20 | /** @var string */ |
||
21 | protected $hostname; |
||
22 | |||
23 | /** @var int */ |
||
24 | protected $port; |
||
25 | |||
26 | /** @var resource */ |
||
27 | protected $socket; |
||
28 | |||
29 | /** @var string */ |
||
30 | private $readBuffer = ''; |
||
31 | |||
32 | /** |
||
33 | * @param string $hostname |
||
34 | * @param int $port |
||
35 | * @throws SocketException When socket creation fails. Will have the underlying code and message. |
||
36 | */ |
||
37 | 35 | public function __construct($hostname = Server::DEFAULT_HOST, $port = Server::DEFAULT_PORT) |
|
44 | |||
45 | /** |
||
46 | * @param string $data |
||
47 | * @return int bytes written |
||
48 | * @throws SocketException When the connection is dropped in the middle of writing. |
||
49 | */ |
||
50 | 4 | public function write($data) |
|
71 | |||
72 | /** |
||
73 | * Reads data from the connected socket in chunks of MAX_SINGLE_RESPONSE_LENGTH |
||
74 | * |
||
75 | * The MAX_SINGLE_RESPONSE_LENGTH should always capture at least the first line of a response, assuming the longest |
||
76 | * possible response is the USING <tubename>\r\n response, which could be up to 208 bytes in length, for any valid |
||
77 | * <tubename>. As a result, this function will, in reality, read some overflow of data for any response containing |
||
78 | * data. This data is saved in a read-buffer, which `readData()` will read from first. |
||
79 | * |
||
80 | * @see readData() |
||
81 | * |
||
82 | * @param string $endOfLine |
||
83 | * @return string |
||
84 | * @throws SocketException |
||
85 | */ |
||
86 | 7 | public function readLine($endOfLine = Server::EOL) |
|
100 | |||
101 | /** |
||
102 | * Read exactly $bytes of data from the connected sockets |
||
103 | * |
||
104 | * If there is not enough data to read $bytes from the read-buffer created by a previous `readLine` call combined |
||
105 | * with the actual socket, this will block until data becomes available. Use with care. |
||
106 | * |
||
107 | * @param int $bytes |
||
108 | * @return string |
||
109 | * @throws SocketException When the connection drops during reading |
||
110 | */ |
||
111 | 6 | public function readData($bytes) |
|
127 | |||
128 | /** |
||
129 | * @param int $bytes |
||
130 | * @return bool |
||
131 | * @throws SocketException |
||
132 | */ |
||
133 | 10 | protected function read($bytes) |
|
145 | |||
146 | /** |
||
147 | * @return boolean |
||
148 | */ |
||
149 | 5 | public function isConnected() |
|
153 | |||
154 | /** |
||
155 | * @return string |
||
156 | */ |
||
157 | 4 | public function getHostname() |
|
161 | |||
162 | /** |
||
163 | * @return int |
||
164 | */ |
||
165 | 4 | public function getPort() |
|
169 | |||
170 | /** |
||
171 | * @return resource |
||
172 | */ |
||
173 | 2 | public function getRaw() |
|
177 | |||
178 | /** |
||
179 | * @throws SocketException |
||
180 | */ |
||
181 | 2 | public function disconnect() |
|
188 | |||
189 | /** |
||
190 | * @throws SocketException When connecting fails |
||
191 | */ |
||
192 | 16 | public function connect() |
|
198 | |||
199 | /** |
||
200 | * @throws SocketException Ensure the Socket is connected. If it is not, throws and exception |
||
201 | */ |
||
202 | 16 | protected function ensureConnected() |
|
208 | |||
209 | /** |
||
210 | * @return SocketException |
||
211 | */ |
||
212 | 9 | protected function createSocketException() |
|
219 | |||
220 | /** |
||
221 | * Attempt to close the socket if it is still open. |
||
222 | */ |
||
223 | 34 | public function __destruct() |
|
227 | |||
228 | 35 | protected function createSocket() |
|
234 | } |
||
235 |
If you suppress an error, we recommend checking for the error condition explicitly: