-
Notifications
You must be signed in to change notification settings - Fork 74
/
Server.php
139 lines (115 loc) · 4.81 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
<?php
namespace morozovsk\websocket;
class Server
{
public function __construct($config) {
$this->config = $config;
}
public function start() {
$pid = @file_get_contents($this->config['pid']);
if ($pid) {
if (posix_getpgid($pid)) {
die("already started\r\n");
} else {
unlink($this->config['pid']);
}
}
if (empty($this->config['websocket']) && empty($this->config['localsocket']) && empty($this->config['master'])) {
die("error: config: !websocket && !localsocket && !master\r\n");
}
$server = $service = $master = null;
if (!empty($this->config['websocket'])) {
//open server socket
$server = stream_socket_server($this->config['websocket'], $errorNumber, $errorString);
stream_set_blocking($server, 0);
if (!$server) {
die("error: stream_socket_server: $errorString ($errorNumber)\r\n");
}
}
if (!empty($this->config['localsocket'])) {
//create a socket for the processing of messages from scripts
$service = stream_socket_server($this->config['localsocket'], $errorNumber, $errorString);
stream_set_blocking($service, 0);
if (!$service) {
die("error: stream_socket_server: $errorString ($errorNumber)\r\n");
}
}
if (!empty($this->config['master'])) {
//create a socket for the processing of messages from slaves
$master = stream_socket_client($this->config['master'], $errorNumber, $errorString);
stream_set_blocking($master, 0);
if (!$master) {
die("error: stream_socket_client: $errorString ($errorNumber)\r\n");
}
}
if (!empty($this->config['eventDriver']) && $this->config['eventDriver'] == 'libevent') {
class_alias('morozovsk\websocket\GenericLibevent', 'morozovsk\websocket\Generic');
} elseif (!empty($this->config['eventDriver']) && $this->config['eventDriver'] == 'event') {
class_alias('morozovsk\websocket\GenericEvent', 'morozovsk\websocket\Generic');
} else {
class_alias('morozovsk\websocket\GenericSelect', 'morozovsk\websocket\Generic');
}
file_put_contents($this->config['pid'], posix_getpid());
//list($pid, $master, $workers) = $this->spawnWorkers();//create child processes
/*if ($pid) {//мастер
file_put_contents($this->config['pid'], $pid);
fclose($server);//master will not handle incoming connections on the main socket
$masterClass = $this->config['master']['class'];
$master = new $masterClass ($service, $workers);//master will process messages from the script and send them to a worker
if (!empty($this->config['master']['timer'])) {
$master->timer = $this->config['worker']['timer'];
}
$master->start();
} else {//воркер*/
$workerClass = $this->config['class'];
$worker = new $workerClass ($server, $service, $master);
if (!empty($this->config['timer'])) {
$worker->timer = $this->config['timer'];
}
$worker->start();
//}
}
/*protected function spawnWorkers() {
$master = null;
$workers = array();
for ($i=0; $i<$this->config['master']['workers']; $i++) {
//create a pair of sockets through which the master and the worker is to be linked
$pair = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
$pid = pcntl_fork();//create a fork
if ($pid == -1) {
die("error: pcntl_fork\r\n");
} elseif ($pid) { //master
fclose($pair[0]);
$workers[intval($pair[1])] = $pair[1];//one of the pair is in the master
} else { //worker
fclose($pair[1]);
$master = $pair[0];//second of pair is in worker
break;
}
}
return array($pid, $master, $workers);
}*/
public function stop() {
$pid = @file_get_contents($this->config['pid']);
if ($pid) {
posix_kill($pid, SIGTERM);
for ($i=0;$i=10;$i++) {
sleep(1);
if (!posix_getpgid($pid)) {
unlink($this->config['pid']);
return;
}
}
die("don't stopped\r\n");
} else {
die("already stopped\r\n");
}
}
public function restart() {
$pid = @file_get_contents($this->config['pid']);
if ($pid) {
$this->stop();
}
$this->start();
}
}