forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
200 lines (184 loc) · 5 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
<?php
$serv = new swoole_server("0.0.0.0", 9501);
$serv->addlistener('0.0.0.0', 9502, SWOOLE_SOCK_UDP);
$serv->set(array(
'worker_num' => 2,
//'open_eof_check' => true,
//'package_eof' => "\r\n",
'task_worker_num' => 2,
//'task_ipc_mode' => 1,
'dispatch_mode' => 1,
//'daemonize' => 1,
//'log_file' => '/tmp/swoole.log',
//'heartbeat_idle_time' => 10,
//'heartbeat_check_interval' => 10,
));
function my_onStart(swoole_server $serv)
{
global $argv;
swoole_set_process_name("php {$argv[0]}: master");
echo "MasterPid={$serv->master_pid}|Manager_pid={$serv->manager_pid}\n";
echo "Server: start.Swoole version is [".SWOOLE_VERSION."]\n";
//$serv->addtimer(5000);
}
function my_log($msg)
{
echo "#".posix_getpid()."\t".$msg.PHP_EOL;
}
function my_onShutdown($serv)
{
echo "Server: onShutdown\n";
}
function my_onTimer($serv, $interval)
{
my_log("Server:Timer Call.Interval=$interval");
}
function my_onClose($serv, $fd, $from_id)
{
//my_log("Client[$fd@$from_id]: fd=$fd is closed");
}
function my_onConnect($serv, $fd, $from_id)
{
//throw new Exception("hello world");
//echo "Client[$fd@$from_id]: Connect.\n";
}
function my_onWorkerStart($serv, $worker_id)
{
global $argv;
if($worker_id >= $serv->setting['worker_num']) {
swoole_set_process_name("php {$argv[0]}: task_worker");
} else {
swoole_set_process_name("php {$argv[0]}: worker");
}
echo "WorkerStart: MasterPid={$serv->master_pid}|Manager_pid={$serv->manager_pid}";
echo "|WorkerId={$serv->worker_id}|WorkerPid={$serv->worker_pid}\n";
//if ($worker_id == 1)
//{
// $serv->addtimer(2000); //500ms
// $serv->addtimer(6000); //500ms
//}
}
function my_onWorkerStop($serv, $worker_id)
{
echo "WorkerStop[$worker_id]|pid=".posix_getpid().".\n";
}
function my_onReceive(swoole_server $serv, $fd, $from_id, $data)
{
my_log("received: $data");
$cmd = trim($data);
if($cmd == "reload")
{
$serv->reload($serv);
}
elseif($cmd == "task")
{
$task_id = $serv->task("hello world");
echo "Dispath AsyncTask: id=$task_id\n";
}
elseif($cmd == "taskwait")
{
$result = $serv->taskwait("hello world", 2);
echo "SyncTask: result=$result\n";
}
elseif ($cmd == "hellotask")
{
$serv->task("hellotask");
}
elseif($cmd == "close")
{
$serv->send($fd, "close connection\n");
$result = $serv->close($fd);
}
elseif($cmd == "info")
{
$info = $serv->connection_info($fd);
$serv->send($fd, 'Info: '.var_export($info, true).PHP_EOL);
}
elseif($cmd == "broadcast")
{
broadcast($serv, $fd, "hello from $fd\n");
}
//这里故意调用一个不存在的函数
elseif($cmd == "error")
{
hello_no_exists();
}
//关闭fd
elseif(substr($cmd, 0, 5) == "close")
{
$close_fd = substr($cmd, 6);
$serv->close($close_fd);
}
elseif($cmd == "shutdown")
{
$serv->shutdown();
}
else
{
$ret = $serv->send($fd, 'Swoole: '.$data, $from_id);
var_dump($ret);
//$serv->close($fd);
}
//echo "Client:Data. fd=$fd|from_id=$from_id|data=$data";
//$serv->deltimer(800);
//swoole_server_send($serv, $other_fd, "Server: $data", $other_from_id);
}
function my_onTask(swoole_server $serv, $task_id, $from_id, $data)
{
if ($data == "hellotask")
{
broadcast($serv, 0, "hellotask");
}
else
{
echo "AsyncTask[PID=".posix_getpid()."]: task_id=$task_id.".PHP_EOL;
return "Task OK";
}
}
function my_onFinish(swoole_server $serv, $task_id, $data)
{
echo "AsyncTask Finish: result={$data}. PID=".posix_getpid().PHP_EOL;
}
function my_onWorkerError(swoole_server $serv, $worker_id, $worker_pid, $exit_code)
{
echo "worker abnormal exit. WorkerId=$worker_id|Pid=$worker_pid|ExitCode=$exit_code\n";
}
function broadcast($serv, $fd = 0, $data = "hello")
{
$start_fd = 0;
echo "broadcast\n";
while(true)
{
$conn_list = $serv->connection_list($start_fd, 10);
if($conn_list === false)
{
break;
}
$start_fd = end($conn_list);
foreach($conn_list as $conn)
{
if($conn === $fd) continue;
sleep(5);
$ret1 = $serv->send($conn, $data);
var_dump($ret1);
$ret2 = $serv->close($conn);
var_dump($ret2);
}
}
}
$serv->on('Start', 'my_onStart');
$serv->on('Connect', 'my_onConnect');
$serv->on('Receive', 'my_onReceive');
$serv->on('Close', 'my_onClose');
$serv->on('Shutdown', 'my_onShutdown');
$serv->on('Timer', 'my_onTimer');
$serv->on('WorkerStart', 'my_onWorkerStart');
$serv->on('WorkerStop', 'my_onWorkerStop');
$serv->on('Task', 'my_onTask');
$serv->on('Finish', 'my_onFinish');
$serv->on('WorkerError', 'my_onWorkerError');
$serv->on('ManagerStart', function($serv) {
global $argv;
swoole_set_process_name("php {$argv[0]}: manager");
});
$serv->start();