-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSocket.php
More file actions
208 lines (181 loc) · 5.19 KB
/
WebSocket.php
File metadata and controls
208 lines (181 loc) · 5.19 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
<?php
/**
* @author David Stelter <david.stelter@gmail.com>
* @copyright Copyright (c) 2011, David Stelter
* @license http://www.opensource.org/licenses/MIT
* @link http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17
*/
class WebSocket {
const MASTER_CONNECTION_BACKLOG = 15;
const WS_MAGIC_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
const APP_DEFAULT = 'default';
const SOCK_BUFFER_SIZE = 4096;
private $controlSock = null;
private $bindAddress = null;
private $bindPort = null;
private $logfile = null;
private $children = array();
private $socks = array();
private $apps = array();
/**
* @param $name
* @param $host = null
*/
public function registerApp($name, $host = null) {
if (! $host) {
$host = self::APP_DEFAULT;
}
$this->apps[$host] = $name;
$this->log("Registered app '$name' for host '$host'");
} // registerApp()
public function getApp($host) {
if (isset($this->apps[$host])) {
return $this->apps[$host];
} else {
return $this->getDefaultApp();
}
} // getApp()
public function getDefaultApp() {
if (isset($this->apps[self::APP_DEFAULT])) {
return $this->apps[self::APP_DEFAULT];
} else {
return null;
}
} // getDefaultApp()
/**
* @param $address
* @param $port
*/
public function __construct($address, $port) {
if (! is_numeric($port)) {
throw new InvalidArgumentException('Port number must be numeric');
}
$this->bindAddress = $address;
$this->bindPort = (int) $port;
} // __construct()
/**
* Run server main loop
*/
public function run() {
if (! is_resource($this->controlSock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
throw new RuntimeException('master socket creation failed');
}
if (! socket_set_option($this->controlSock, SOL_SOCKET, SO_REUSEADDR, 1)) {
throw new RuntimeException('master socket option SO_REUSEADDR failed');
}
if (! socket_bind($this->controlSock, $this->bindAddress, $this->bindPort)) {
throw new RuntimeException('bind to ' . $this->getAddress() . ' failed');
}
if (! socket_listen($this->controlSock, self::MASTER_CONNECTION_BACKLOG)) {
throw new RuntimeException('listen failed on master socket');
}
$this->log('Listening on ' . self::getSocketNameString($this->controlSock));
while (true) {
if (is_resource($client = socket_accept($this->controlSock))) {
$this->log('new connection initiated by client ' . self::getPeerString($client));
$this->newClient($client);
} else {
$this->log('error accepting connection');
}
} // while (true)
} // run()
public static function getPeerString($sock) {
$addr;
$port;
if (! socket_getpeername($sock, $addr, $port)) {
return null;
} else {
return "{$addr}:{$port}";
}
} // getPeerString()
/**
* @param resource $sock
*/
private function disconnect($sock) {
if (is_resource($sock)) {
$this->log('disconnecting socket ' . self::getSocketNameString($sock));
socket_close($sock);
}
} // disconnect()
/**
* Returns the name in 'address':'port' format of the socket $sock. If $sock is not a resource,
* null is returned. On error, false is returned.
* @param resource $sock
* @return mixed
*/
public static function getSocketNameString($sock) {
if (is_resource($sock)) {
$addr;
$port;
if (socket_getsockname($sock, $addr, $port)) {
return "{$addr}:{$port}";
} else {
return false;
}
} else {
return null;
}
} // getSocketNameString()
private function newClient($sock) {
if (is_resource($sock)) {
$this->socks[$sock] = $sock;
if (($pid = pcntl_fork()) == 0) { // child
$this->runChild($sock);
} else { // parent
$this->log('forked child ' . $pid);
$this->children[$pid] = self::getPeerString($sock);
}
}
} // newClient()
private function runChild($childSock) {
if (is_resource($this->controlSock)) {
socket_close($this->controlSock);
}
$connection = new WebSocketConnection($this, $childSock);
$this->log('Connecting client socket ' . self::getSocketNameString($childSock));
while (true) {
if (($bytes = socket_recv($childSock, $buf, self::SOCK_BUFFER_SIZE, 0)) == 0) {
$this->disconnect($childSock);
exit;
} else {
if ($connection->isEstablished()) {
$connection->process($buf);
} else { // do handshake and set up application
$connection->doHandshake($buf);
if (! $host = $connection->getHost()) {
$host = self::APP_DEFAULT;
}
if ($appClass = self::getApp($host)) {
$this->log('App ' . $appClass . ' found for host ' . $host);
$app = new $appClass($connection);
$connection->setApp($app);
} else {
$this->log('No app registered for ' . $host);
}
}
}
} // while (true)
} // runChild()
/**
* @param string $message
*/
public function log($message) {
if ($this->logfile) {
error_log($message, 3, $this->logfile);
} else {
error_log($message);
}
} // log()
/**
* Ensures graceful socket closure.
*/
public function __destruct() {
if (is_resource($this->controlSock)) {
socket_close($sock);
}
} // __destruct()
public function getAddress() {
return $this->bindAddress . ':' . $this->bindPort;
} // getAddress()
} // WebSocket{}
//$server = new WebSocket('localhost', 12345);