|
12 | 12 | * @link https://github.com/stefanak-michal/Bolt |
13 | 13 | * @package Bolt\connection |
14 | 14 | */ |
15 | | -class Socket implements IConnection |
| 15 | +class Socket extends AConnection |
16 | 16 | { |
17 | 17 |
|
18 | | - /** |
19 | | - * @var string |
20 | | - */ |
21 | | - private $ip; |
22 | | - |
23 | | - /** |
24 | | - * @var int |
25 | | - */ |
26 | | - private $port; |
27 | | - |
28 | | - /** |
29 | | - * @var int |
30 | | - */ |
31 | | - private $timeout; |
32 | | - |
33 | 18 | /** |
34 | 19 | * @var resource |
35 | 20 | */ |
36 | 21 | private $socket; |
37 | 22 |
|
38 | | - /** |
39 | | - * @param string $ip |
40 | | - * @param int $port |
41 | | - * @param int $timeout |
42 | | - * @throws Exception |
43 | | - */ |
44 | | - public function __construct(string $ip = '127.0.0.1', int $port = 7687, int $timeout = 15) |
45 | | - { |
46 | | - if (!extension_loaded('sockets')) { |
47 | | - Bolt::error('PHP Extension sockets not enabled'); |
48 | | - } |
49 | | - |
50 | | - $this->ip = $ip; |
51 | | - $this->port = $port; |
52 | | - $this->timeout = $timeout; |
53 | | - } |
54 | | - |
55 | 23 | /** |
56 | 24 | * Create socket connection |
57 | 25 | * @return bool |
58 | 26 | * @throws Exception |
59 | 27 | */ |
60 | 28 | public function connect(): bool |
61 | 29 | { |
| 30 | + if (!extension_loaded('sockets')) { |
| 31 | + throw new ConnectException('PHP Extension sockets not enabled'); |
| 32 | + } |
| 33 | + |
62 | 34 | $this->socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
63 | 35 | if (!is_resource($this->socket)) { |
64 | 36 | Bolt::error('Cannot create socket'); |
@@ -147,23 +119,6 @@ public function read(int $length = 2048): string |
147 | 119 | return $output; |
148 | 120 | } |
149 | 121 |
|
150 | | - /** |
151 | | - * Print buffer as HEX |
152 | | - * @param string $str |
153 | | - * @param bool $write |
154 | | - */ |
155 | | - private function printHex(string $str, bool $write = true) |
156 | | - { |
157 | | - $str = implode(unpack('H*', $str)); |
158 | | - echo '<pre>'; |
159 | | - echo $write ? '> ' : '< '; |
160 | | - foreach (str_split($str, 8) as $chunk) { |
161 | | - echo implode(' ', str_split($chunk, 2)); |
162 | | - echo ' '; |
163 | | - } |
164 | | - echo '</pre>'; |
165 | | - } |
166 | | - |
167 | 122 | /** |
168 | 123 | * Close socket connection |
169 | 124 | */ |
|
0 commit comments