-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerSocketConn.class.php
102 lines (67 loc) · 2.48 KB
/
ServerSocketConn.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
<?php
/*
2020 Adam Carpentieri, Web Ventures LLC
Class for non-blocking socket connections as used by server socket
Needs to run in a loop in order to handle connections properly.
Recommended no blocking requests in the thread handling this loop or else the socket server can get into degraded state
*/
class ServerSocketConn {
private $conn;
private $connOpen = true;
private $peer;
private $requestData = '';
private $responseData = '';
private $bytesWritten = 0;
public function __construct($conn, $peer) {
$this->conn = $conn;
$this->peer = $peer;
}
public function getRequest() {
$this->requestData .= stream_get_contents($this->conn);
$requestData = $this->getRequestData();
if($requestData === false)
return false;
return $requestData;
}
private function getRequestDataLength() {
if(empty($this->requestData) || strstr($this->requestData, "\n") === false)
return false;
$length = strtok($this->requestData, "\n");
if(empty($length) || !is_numeric($length))
return false;
return $length;
}
private function getRequestData() {
if(!($length = $this->getRequestDataLength()))
return false;
$requestData = substr($this->requestData, strlen($length . "\n"));
if(strlen($requestData) != $length)
return false;
return $requestData;
}
public function isRequestReceived() { return $this->getRequestData() == false ? false: true; }
public function setResponseData(string $data) { $this->responseData = $data; }
public function isResponseReady() { return !empty($this->responseData); }
public function sendResponse() { //need to keep track of how much written + use stream func
if(!$this->isResponseReady() || !$this->connOpen)
return false;
if(($bytesWritten = @fwrite($this->conn, substr($this->responseString(), $this->bytesWritten))) !== false)
$this->bytesWritten += $bytesWritten;
return $this->isResponseSent();
}
public function isResponseSent() { return $this->bytesWritten == strlen($this->responseString()); }
private function responseString() { return strlen($this->responseData) . "\n" . $this->responseData; }
public function getPeer() { return $this->peer; }
public function getConn() { return $this->conn; }
public function isClosed() { return feof($this->conn); }
public function closeConn() {
if(!$this->connOpen)
return;
fclose($this->conn);
$this->connOpen = false;
}
public function __destruct() {
$this->closeConn();
}
}
?>