-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoycache.php
155 lines (114 loc) · 3.92 KB
/
toycache.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
#!/usr/bin/php
<?php
/**
* Remote variable cache storage.
* Class ToyCache
*/
class ToyCache
{
protected $connections = [];
protected $address = '0.0.0.0';
protected $port = 9500;
protected $sock;
public function __construct()
{
$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
}
public function __destruct()
{
socket_close($this->sock);
}
public function stop()
{
foreach ($this->connections as $conn) {
socket_close($conn);
}
socket_close($this->sock);
}
/**
* Starts the server
*/
public function start()
{
try {
// Make address and port reusable
socket_setopt($this->sock, SOL_SOCKET, SO_REUSEADDR, 1);
socket_setopt($this->sock, SOL_SOCKET, SO_REUSEPORT, 1);
// Bind Address and port to listen clients connections
socket_bind($this->sock, $this->address, $this->port);
socket_listen($this->sock);
socket_set_nonblock($this->sock);
// Main loop
while (true) {
$connection = socket_accept($this->sock);
if ($connection == false) {
usleep(100);
} elseif ($connection > 0) {
$this->connections[] = $connection;
socket_getpeername($connection, $ip);
echo "Server: New client connected ", $ip, "\n";
$this->handleClient($connection);
} else {
echo 'Error:' . socket_strerror($connection);
}
}
} catch (Exception $e) {
echo 'Error: ', $e->getMessage();
}
}
/**
* Fork process to handle client interaction.
* @param $connection
*/
public function handleClient($connection)
{
$storage = [];
$pid = pcntl_fork();
if ($pid == -1) { // Fork error
echo "Error: Failure to handle client";
socket_close($connection);
} elseif ($pid == 0) { // Child process
while (true) {
while($buf = @socket_read($connection, 1024, PHP_NORMAL_READ))
if($buf = trim($buf))
break;
if (false == $buf) {
socket_getpeername($connection, $ip);
echo "Server: Client disconnection ", $ip, "\n";
break;
} else {
$command = substr($buf, 0, strpos($buf, ' ', 0));
if (empty($command)) {
$command = $buf;
} else {
$args = explode(' ', substr($buf, strpos($buf, ' ')+1));
}
switch ($command) {
case 'SET':
echo 'Server: SET on key: ', $args[0],' value: ', $args[1], "\n";
$storage[$args[0]] = $args[1];
break;
case 'GET':
if (!isset($storage[$args[0]])) {
$value = null;
} else {
$value = $storage[$args[0]];
}
echo 'Server: GET on key: ', $args[0],' value: ', $value, "\n";
socket_write($connection, $value, strlen($value));
break;
case 'FLUSH':
$storage = null;
$storage = [];
break;
}
echo 'Command: ', $command, ' Message: ', $buf, "\n";
}
}
} else { // parent
socket_close($connection);
}
}
}
$server = new ToyCache();
$server->start();