-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerSocket.class.php
170 lines (113 loc) · 3.65 KB
/
ServerSocket.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
<?php
/*
2020 Adam Carpentieri, Web Ventures LLC
Class for non-blocking server connection paired with ServerSocketConn class
TODO
1. Disconnect serversocketconn's after timeout period
2. Make worker server and fax queue server OOP - no need for callback
*/
class ServerSocket {
private $null = NULL;
private $socket;
private $port;
private $connections = array();
private $read = array();
private $write = array();
private $serverOpen = true;
private $requestHandler;
public function __construct($port, $requestHandler) {
if(!is_numeric($port) || !is_callable($requestHandler))
throw new Exception("Bad parameters provided");
$this->port = $port;
$this->requestHandler = $requestHandler;
$this->socket = stream_socket_server("tcp://0.0.0.0:$port", $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN);
if(!$this->socket) {
$this->serverOpen = false;
throw new Exception("Could not connect");
}
stream_set_blocking($this->socket, 0);
}
public function checkForIncoming($timeout=10) {
$metaData = stream_get_meta_data($this->socket);
if($metaData['timed_out'])
error_log('server socket timed out: ' . $this->port);
if ($c = @stream_socket_accept($this->socket, empty($this->connections) ? $timeout : 0, $peer)) {
stream_set_blocking($c, 0);
$this->connections[$peer] = new ServerSocketConn($c, $peer);
}
}
public function readAndWrite($timeout=5) {
$metaData = stream_get_meta_data($this->socket);
if($metaData['timed_out'])
error_log('server socket timed out: ' . $this->port);
if(!count($this->connections))
return;
$read = $this->getConns('all');
$write = $this->getConns('write');
$timeout_sec = floor($timeout);
$timeout_usec = ($timeout - $timeout_sec) * 1000000;
if (stream_select($read, $write, $null, $timeout_sec, $timeout_usec) === false)
return false;
$this->readData($read);
$this->writeData($write);
return true;
}
private function readData($read) {
foreach($read as $c) {
$peer = stream_socket_get_name($c, true);
if(!($connection = $this->getConnFromPeer($peer)))
continue;
if($connection->isClosed()) {
unset($this->connections[$peer]);
continue;
}
if($connection->isRequestReceived()) //already received everything - should just be waiting
continue;
$connection->getRequest();
if($connection->isRequestReceived()) {
$handler = $this->requestHandler;
if($connection->isClosed()) { //async request
$handler($connection->getRequest());
unset($this->connections[$peer]);
}
else {
if($handler($connection->getRequest(), $connection) === false)
unset($this->connections[$peer]);
}
}
}
}
private function writeData($write) {
foreach($write as $c) {
$peer = stream_socket_get_name($c, true);
if(!($connection = $this->getConnFromPeer($peer)))
continue;
$connection->sendResponse();
if($connection->isResponseSent())
unset($this->connections[$peer]);
}
}
private function getConns($type='all') {
$conns = array();
foreach($this->connections as $connection) {
if($type == 'all' || ($type == 'write' && $connection->isResponseReady()))
$conns[] = $connection->getConn();
}
return $conns;
}
private function getConnFromPeer($peer) {
if(!isset($this->connections[$peer]))
return false;
return $this->connections[$peer];
}
public function closeServer() {
if(!$this->serverOpen)
return;
fclose($this->socket);
$this->serverOpen = false;
}
public function __destruct() {
$this->closeServer();
}
}
?>