1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MallardDuck\Whois; |
4
|
|
|
|
5
|
|
|
use MallardDuck\Whois\Exceptions\SocketClientException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A simple socket stream client. |
9
|
|
|
* |
10
|
|
|
* @author mallardduck <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @copyright lucidinternets.com 2020 |
13
|
|
|
* |
14
|
|
|
* @version ?.?.? |
15
|
|
|
*/ |
16
|
|
|
class SocketClient |
17
|
|
|
{ |
18
|
|
|
protected $socketUri = null; |
19
|
|
|
protected $socket = null; |
20
|
|
|
protected $timeout = 30; |
21
|
|
|
protected $connected = false; |
22
|
|
|
|
23
|
26 |
|
public function __construct(string $socketUri, int $timeout = 30) |
24
|
|
|
{ |
25
|
26 |
|
$this->setSocketUri($socketUri); |
26
|
26 |
|
$this->setTimeout($timeout); |
27
|
26 |
|
} |
28
|
|
|
|
29
|
26 |
|
protected function setSocketUri(string $socketUri): string |
30
|
|
|
{ |
31
|
26 |
|
$current = $this->socketUri; |
32
|
26 |
|
$this->socketUri = $socketUri; |
33
|
|
|
|
34
|
26 |
|
return $current ?? ''; |
35
|
|
|
} |
36
|
|
|
|
37
|
26 |
|
protected function setTimeout(int $timeout): int |
38
|
|
|
{ |
39
|
26 |
|
$current = $this->timeout; |
40
|
26 |
|
$this->timeout = $timeout; |
41
|
|
|
|
42
|
26 |
|
return $current; |
43
|
|
|
} |
44
|
|
|
|
45
|
26 |
|
public function connect(): self |
46
|
|
|
{ |
47
|
26 |
|
$fp = stream_socket_client($this->socketUri, $errno, $errstr, $this->timeout); |
48
|
26 |
|
if (!$fp) { |
|
|
|
|
49
|
|
|
$message = sprintf("Stream Connection Failed: %s", $errstr); |
50
|
|
|
throw new SocketClientException($message, $errno); |
51
|
|
|
} |
52
|
|
|
|
53
|
26 |
|
$this->socket = $fp; |
54
|
26 |
|
$this->connected = true; |
55
|
26 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function isConnected(): bool |
59
|
|
|
{ |
60
|
|
|
return $this->connected; |
61
|
|
|
} |
62
|
|
|
|
63
|
26 |
|
public function writeString(string $string) |
64
|
|
|
{ |
65
|
26 |
|
if (!$this->connected) { |
66
|
|
|
$message = sprintf("The calling method %s requires the socket to be connected", __FUNCTION__); |
67
|
|
|
throw new SocketClientException($message); |
68
|
|
|
} |
69
|
26 |
|
return stream_socket_sendto($this->socket, $string); |
70
|
|
|
} |
71
|
|
|
|
72
|
26 |
|
public function readAll(): string |
73
|
|
|
{ |
74
|
26 |
|
if (!$this->connected) { |
75
|
|
|
$message = sprintf("The calling method %s requires the socket to be connected", __FUNCTION__); |
76
|
|
|
throw new SocketClientException($message); |
77
|
|
|
} |
78
|
26 |
|
return stream_get_contents($this->socket); |
79
|
|
|
} |
80
|
|
|
|
81
|
26 |
|
public function disconnect(): self |
82
|
|
|
{ |
83
|
26 |
|
if (!is_null($this->socket)) { |
84
|
26 |
|
stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR); |
85
|
26 |
|
fclose($this->socket); |
86
|
|
|
} |
87
|
26 |
|
$this->socket = null; |
88
|
26 |
|
$this->connected = false; |
89
|
|
|
|
90
|
26 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
6 |
|
public function __destruct() |
94
|
|
|
{ |
95
|
6 |
|
$this->disconnect(); |
96
|
6 |
|
} |
97
|
|
|
} |
98
|
|
|
|