-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_server.php
More file actions
executable file
·292 lines (232 loc) · 8.58 KB
/
php_server.php
File metadata and controls
executable file
·292 lines (232 loc) · 8.58 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
//Reference: https://github.com/ghedipunk/PHP-Websockets
error_reporting(E_ALL);
/* Allow the script to hang around waiting for connections. */
set_time_limit(0);
// set the default timezone to use. Available since PHP 5.1
date_default_timezone_set('UTC');
/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
// ob_implicit_flush();
class l2l_server {
private $ip;
private $port;
private $master_socket;
private $all_sockets;
private $max_buffer_size;
private $backlog;
private $runServer = true;
//TODO: Move this shit to a database, if possible
private $client_sockets;
function __construct($ip, $port, $max_buffer_size = 2048, $backlog = 20) {
$this->max_buffer_size = $max_buffer_size;
$this->ip = $ip;
$this->port = $port;
$this->client_sockets = array();
$this->all_sockets = array();
$this->createSocket();
$this->setSocketOptions();
$this->bindAndListen();
if($this->runServer == true) {
$this->socketInfo("Server started\nListening on: $ip:$port\nMaster socket: ". $this->master_socket);
}
}
private function createSocket() {
$this->master_socket = socket_create(AF_INET, SOCK_STREAM, 0);
if (!is_resource($this->master_socket)) {
$this->socketError("Unable to create socket.", true);
}
// $this->setSocketToNonBlockingMode($this->master_socket, true);
array_push($this->all_sockets, $this->master_socket);
}
private function setSocketToNonBlockingMode($socket, $showStoppingError = false) {
if (!socket_set_nonblock($this->master_socket)) {
$this->socketError("Unable to set non blocking mode for socket.", $showStoppingError);
}
}
private function setSocketOptions() {
//Reference: http://php.net/manual/en/function.socket-get-option.php
//We will record debugging information
if (!socket_set_option($this->master_socket, SOL_SOCKET, SO_DEBUG, 0)) {
$this->socketError("Unable to set debug option on socket", true);
}
//We will Support transmission of broadcast messages
if (!socket_set_option($this->master_socket, SOL_SOCKET, SO_BROADCAST, 0)) {
$this->socketError("Unable to set option to support transmission of broadcast messages on socket", true);
}
//Local addresses will be reused
if (!socket_set_option($this->master_socket, SOL_SOCKET, SO_REUSEADDR, 1)) {
$this->socketError("Unable to set option to reuse local addresses on socket", true);
}
//connections are to be kept active with periodic transmission of messages
if (!socket_set_option($this->master_socket, SOL_SOCKET, SO_KEEPALIVE, 1)) {
$this->socketError("Unable to set option to keep connection alive on socket", true);
}
//We will report the size of the send buffer
if (!socket_set_option($this->master_socket, SOL_SOCKET, SO_SNDBUF, 1)) {
$this->socketError("Unable to set option to report the size of the send buffer on socket", true);
}
//We will report the size of the receive buffer
if (!socket_set_option($this->master_socket, SOL_SOCKET, SO_RCVBUF, 1)) {
$this->socketError("Unable to set option to report the size of the receive buffer on socket", true);
}
//Attempt to send any unsent data at the time socket_close() is called
$linger = array('l_linger' => 1, 'l_onoff' => 1);
if (!socket_set_option($this->master_socket, SOL_SOCKET, SO_LINGER, $linger)) {
$this->socketError("Unable to set option that would have attempted to send unsent data before closing socket, on socket", true);
}
//TODO: support multi-casting
//Reference: http://www.metaswitch.com/resources/what-is-multicast-ip-routing
}
private function bindAndListen() {
if (!socket_bind($this->master_socket, $this->ip, $this->port)) {
$this->socketError("Unable to bind to IP address [' . $this->ip . '] port[' . $this->port . ']'", true);
}
if (!socket_listen($this->master_socket,$this->backlog)) {
$this->socketError("Unable to listen to IP address [' . $this->ip . '] port[' . $this->port . ']'", true);
}
}
private function getSocketName($socket, &$socket_address, &$socket_port) {
if (!socket_getsockname($socket, $socket_address, $socket_port)) {
$this->socketError("Unable to get socket name", false);
}
else {
print 'Details From Socket: IP address [' . $this->ip . '] port[' . $this->port . ']';
}
}
private function socketError($message, $showStoppingError = false) {
print $message . socket_strerror(socket_last_error()) . PHP_EOL;
$this->runServer = !$showStoppingError;
exit;
}
private function socketInfo($message) {
print $message . PHP_EOL;
}
public function run() {
while($this->runServer)
{
$read = $this->all_sockets;
$write = NULL;
$except = NULL;
$num_changed_sockets = socket_select($read, $write, $except, 0);
if ($num_changed_sockets === false) {
/* Error handling */
}
else if ($num_changed_sockets > 0) {
/* At least at one of the sockets something interesting happened */
if (in_array($this->master_socket, $read)) {
$this->acceptNewClientIfNeeded();
}
else {
$this->readFromSocketClients($read);
}
}
}
}
private function acceptNewClientIfNeeded() {
$this->socketInfo("Attempting to accept new client...");
//Handle new connections
if(($client_socket = socket_accept($this->master_socket)) !== false)
{
$this->connect($client_socket);
$this->broadcast("Welcome $client_socket");
}
}
private function readFromSocketClients($read) {
$buffer = null;
$bytes = 0;
// Handle Input From
foreach ($this->client_sockets as $key => $client) { // for each client
if (in_array($client, $read)) {
if (false !== ($bytes = socket_recv($client, $buffer, $this->max_buffer_size, MSG_DONTWAIT))) {
$this->socketInfo("Read $bytes bytes from socket_recv()");
$buffer = trim($buffer);
if(!empty($buffer)) {
$lowerCase = strtolower($buffer);
if(strcmp($lowerCase, "quit") == 0
|| strcmp($lowerCase, "shutdown") == 0) {
socket_close($client);
$this->disconnect($client);
}
}
$message = "$client: $buffer";
$this->broadcast($message);
} else {
// $this->socketError("Failed to read from client socket[$client]", false);
}
}
}
}
private function broadcast($message, $recipient = null) {
$length = strlen($message);
if($length > 0) {
$this->socketInfo("Broadcasting Message=[$message] size=[$length]");
foreach($this->client_sockets as $key => $next_client_socket) {
$st = $message;
$length = strlen($message);
if($next_client_socket != $recipient) {
do {
$sent = socket_write($next_client_socket, $st, strlen($st));
if ($sent === false) {
// continue;
// $this->socketInfo("Error sending message=[$message] client=[$next_client_socket]");
$this->disconnect($next_client_socket);
break;
}
// Check if the entire message has been sented
if ($sent < $length) {
// If not sent the entire message.
// Get the part of the message that has not yet been sented as message
$st = substr($st, $sent);
// Get the length of the not sented part
$length -= $sent;
}
else {
break;
}
} while($length > 0);
}
}
$length = strlen($message);
$this->socketInfo("Message Broadcasted=[$message] size=[$length]");
}
}
private function connect($client_socket) {
$message = "Client $client_socket has connected";
$this->broadcast($message, $client_socket);
// $this->setSocketToNonBlockingMode($client_socket);
array_push($this->client_sockets, $client_socket);
array_push($this->all_sockets, $client_socket);
}
private function disconnect($client_socket) {
$message = "Client $client_socket has been disconnected";
if(($key = array_search($client_socket, $this->client_sockets)) !== false) {
unset($this->client_sockets[$key]);
}
if(($key = array_search($client_socket, $this->all_sockets)) !== false) {
unset($this->all_sockets[$key]);
}
$this->broadcast($message);
}
function __destruct()
{
while(sizeof($this->client_sockets) > 0) {
$client_socket = array_pop($this->client_sockets);
unset($client_socket);
}
socket_close($this->master_socket);
unset($this->ip);
unset($this->port);
unset($this->master_socket);
unset($this->max_buffer_size);
unset($this->backlog);
unset($this->runServer);
}
}
$ip = "192.168.29.225";
$port = 9009;
$max_buffer_size = 4096;
$backlog = 20;
$l2l_server = new l2l_server($ip, $port, $max_buffer_size, $backlog);
$l2l_server->run();
?>