-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientSocket.class.php
192 lines (127 loc) · 4.1 KB
/
ClientSocket.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/*
2020 Adam Carpentieri, Web Ventures LLC
Class for non-blocking socket connections as used by client
*/
class ClientSocket {
private $null = NULL;
private $waitTimeout = 300; //default - should be set
private $replyData;
private $requestData;
private $conn;
private $timer;
private $connOpen = true;
public function __construct($server, $port, $connectionTimeout=30) {
$this->timer = new Timer();
$this->conn = stream_socket_client("tcp://$server:$port", $errno, $errstr, $connectionTimeout);
if(!$this->conn) {
$this->connOpen = false;
throw new Exception("Could not connect");
}
stream_set_blocking($this->conn, 0);
}
public function sendRequest(string $request, $raw=false) {
$this->requestData = $request;
if(!$this->connOpen)
return;
$requestString = $raw ? $request : strlen($request) . "\n" . $request;
$totalBytesWritten = 0;
while(true) {
$timeout = round($this->waitTimeout - $this->timer->getTotalTime());
if($timeout < 1)
return false;
$timeout_sec = floor($timeout);
$timeout_usec = ($timeout - $timeout_sec) * 1000000;
$write = array($this->conn);
if(($numStreams = stream_select($null, $write, $null, $timeout_sec, $timeout_usec)) === false)
return false;
else if($numStreams) {
if(($bytesWritten = @fwrite($this->conn, substr($requestString, $bytesWritten))) !== false)
$totalBytesWritten += $bytesWritten;
if($totalBytesWritten == strlen($requestString))
break;
}
}
return true;
}
public function getDataOnLine($timeout) {
$timeout = !empty($timeout) ? $timeout : round($this->waitTimeout - $this->timer->getTotalTime());
if($timeout < 1)
return false;
$timeout_sec = floor($timeout);
$timeout_usec = ($timeout - $timeout_sec) * 1000000;
$read = array($this->conn);
$numStreams = @stream_select($read, $null, $null, $timeout_sec, $timeout_usec);
if($numStreams === false)
return false;
if($numStreams)
return stream_get_contents($this->conn);
return false;
}
public function getReply() {
$replyData = $this->getReplyData();
if($replyData !== false)
return $replyData;
while(true) {
$timeout = round($this->waitTimeout - $this->timer->getTotalTime());
if($timeout < 1)
return false;
$timeout_sec = floor($timeout);
$timeout_usec = ($timeout - $timeout_sec) * 1000000;
$read = array($this->conn);
for($i=0; $i<10; $i++) {
if(($numStreams = @stream_select($read, $null, $null, $timeout_sec, $timeout_usec)) !== false)
break;
}
if($numStreams === false)
return false;
if($numStreams) {
$this->replyData .= stream_get_contents($this->conn);
if($this->getReplyData() !== false)
break;
}
}
$this->closeConn();
return $this->getReplyData();
}
private function getReplyDataLength() {
if(empty($this->replyData) || strstr($this->replyData, "\n") === false)
return false;
$length = strtok($this->replyData, "\n");
if(empty($length) || !is_numeric($length))
return false;
return $length;
}
private function getReplyData() {
if(!($length = $this->getReplyDataLength()))
return false;
$replyData = substr($this->replyData, strlen($length . "\n"));
if(strlen($replyData) != $length)
return false;
return $replyData;
}
public function closeConn() {
if(!$this->connOpen)
return false;
fclose($this->conn);
$this->connOpen = false;
}
public function isClosed() {
if(!$this->connOpen)
return true;
if(feof($this->conn)) {
$this->closeConn();
return true;
}
return false;
}
public function setWaitTimeout($waitTimeout) {
$this->waitTimeout = $waitTimeout;
}
public function setPeer($peer) { $this->peer = $peer; }
public function getPeer() { return $this->peer; }
public function __destruct() {
$this->closeConn();
}
}
?>