-
Notifications
You must be signed in to change notification settings - Fork 23
/
Server.php
204 lines (157 loc) · 4.68 KB
/
Server.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
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/*
----------------------------------------------------
Mpass - Multi-Process Socket Server for PHP
copyright (c) 2010 Laruence
http://www.laruence.com
If you have any questions or comments, please email:
laruence@yahoo.com.cn
*/
define("MPASS_VERSION", "0.1.0");
define("MPASS_DEBUG", TRUE);
/** EOF flag */
define("MPASS_EOF", "\r\n\r\n");
define("MPASS_ROOT", dirname(__FILE__));
require_once(MPASS_ROOT . "/Request.php");
require_once(MPASS_ROOT . "/Worker.php");
require_once(MPASS_ROOT . "/Log.php");
/**
* Mpass Server Class
*
* @package Mpass
* @depend pecl pcntl
*/
class Mpass_Server {
private $is_master = TRUE;
private $_host = NULL;
private $_port = NULL;
private $_socket = NULL;
private $_worker = NULL;
private $_children = 0;
private $_running = FALSE;
public function __construct($host = "127.0.0.1", $port = 9001, Mpass_IExecutor $executor) {
if (!extension_loaded("pcntl")) {
die("Mpass require pcntl extension loaded");
}
/** assure run in cli mode */
if (substr(php_sapi_name(), 0, 3) !== 'cli') {
die("This Program can only be run in CLI mode");
}
if ($host) {
$this->_host = $host;
}
if ($port) {
$this->_port = (int)($port);
}
$this->_worker = new Mpass_Worker($executor);
}
public function run() {
/** no need actually */
set_time_limit(0);
$signals = array(
SIGCHLD => "SIGCHLD",
SIGCLD => "SIGCLD",
SIGINT => "SIGINT",
SIGHUP => "SIGHUP",
SIGQUIT => "SIGQUIT",
);
if (version_compare(phpversion(), "5.3.0", "lt")) {
/* tick use required as of PHP 4.3.0 */
declare(ticks = 1);
}
foreach ($signals as $signal => $name) {
if (!pcntl_signal($signal, array($this, "handler"))) {
die("Install signal handler for {$name} failed");
}
}
$context = stream_context_create();
$dns = "tcp://{$this->_host}:{$this->_port}";
$server = stream_socket_server($dns, $errno, $errstr, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $context);
if (FALSE === $server) {
die($errstr);
} else {
Mpass_Log::log("start listening on {$dns}", __METHOD__);
}
$this->_socket = $server;
$this->_running = TRUE;
while ($this->_running) {
$client = @stream_socket_accept($server, 5);
if (FALSE !== $client) {
Mpass_Log::log("accepted connection from \"" . stream_socket_get_name($client, TRUE) . "\"", __METHOD__);
$this->_execute($client);
}
if (version_compare(phpversion(), "5.3.0", "ge")) {
/** since php 5.3.0 declare statement is deprecated,
* use pcntl_signal_dispatch instead */
pcntl_signal_dispatch();
}
}
return TRUE;
}
public function handler($signo) {
switch(intval($signo)) {
case SIGCLD:
case SIGCHLD:
/** declare = 1, that means one signal may be correspond multi-process die */
while( ($pid = pcntl_wait($status, WNOHANG|WUNTRACED)) > 0 ) {
if (FALSE === pcntl_wifexited($status)) {
Mpass_Log::warn("sub proccess {$pid} exited unormally with code {$status}", __METHOD__);
} else {
Mpass_Log::log("sub proccess {$pid} exited normally", __METHOD__);
}
$this->_children--;
}
break;
case SIGINT:
case SIGQUIT:
case SIGHUP:
$this->_cleanup();
exit(0);
break;
default:
break;
}
}
protected function _execute($client) {
$pid = pcntl_fork();
if ($pid == 0) {
/** this is a child */
$this->is_master = FALSE;
$this->_worker->run($client);
exit(0);
} else {
++$this->_children;
}
return TRUE;
}
protected function _cleanup() {
if (!$this->is_master) {
return;
}
Mpass_Log::log("cleanup", __METHOD__);
$this->_running = FALSE;
while ($this->_children > 0) {
$pid = pcntl_wait($status, WNOHANG | WUNTRACED);
if ($pid > 0) {
if (FALSE === pcntl_wifexited($status)) {
Mpass_Log::warn("sub proccess {$pid} exited unormally with code {$status}", __METHOD__);
} else {
Mpass_Log::log("sub proccess {$pid} exited normally", __METHOD__);
}
$this->_children--;
} else {
continue;
}
}
if ($this->_socket) {
if (version_compare(phpversion(), "5.2.1", "ge")) {
stream_socket_shutdown($this->_socket, STREAM_SHUT_RDWR);
} else {
fclose($client);
}
}
}
public function __destruct() {
}
}
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */