-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.php
More file actions
73 lines (61 loc) · 1.6 KB
/
Copy pathevent.php
File metadata and controls
73 lines (61 loc) · 1.6 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
<?php
$receive = [];
$master = [];
$buffers = [];
$socket = stream_socket_server('tcp://0.0.0.0:8000', $errno, $errstr);
stream_set_blocking($socket, 0);
$id = intval($socket);
$master[$id] = $socket;
echo 'waiting client .....\n';
function ev_accept($socket, $flag, $base){
global $receive;
global $master;
global $buffers;
$conncetion = stream_socket_accept($socket);
stream_set_blocking($conncetion, 0);
$id = intval($conncetion);
echo 'new client '.$id.'\n';
$event = event_new();
event_base_set($event, EV_READ | EV_PERSIST, 'ev_read', $id);
event_base_set($event,$base);
event_add($event);
$master[$id] = $conncetion;
$receive[$id] = '';
$buffers[$id] = $event;
}
function ev_read($buffer, $flag, $id){
global $receive;
global $master;
global $buffers;
while (1) {
$read = @fread($buffer, 1024);
if($read === '' || $read === false){
break;
}
$pos = strpos($read, '\n');
if($pos === false){
$receive[$id] .= $read;
}else{
$receive[$id] .= trim(substr($read, 0, $pos + 1));
$read = substr($read, $pos + 1);
switch ($receive['$id']) {
case 'quit':
echo 'client close conn\n';
unset($master[$id],$buffers[$id]);
break;
default:
echo $receive[$id];
break;
}
$receive[$id] = '';
}
}
}
$base = event_base_new();
$event = event_new();
event_set($event, $socket, EV_READ | EV_PERSIST, 'ev_accept',$base);
event_base_set($event, $base);
event_add($event);
echo 'start run ...\n';
event_base_loop($base);
echo 'this code will not be executed \n';