1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpCache\IO; |
4
|
|
|
|
5
|
|
|
use PhpCache\IO\Exception\IOException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Description of CacheIOHandler. |
9
|
|
|
* |
10
|
|
|
* @author dude920228 |
11
|
|
|
*/ |
12
|
|
|
class CacheIOHandler |
13
|
|
|
{ |
14
|
|
|
const SOCKET_TYPE_IP = 'ip'; |
15
|
|
|
const SOCKET_TYPE_FILE = 'file'; |
16
|
|
|
|
17
|
|
|
private $location; |
18
|
|
|
private $serverPort; |
19
|
|
|
private $bufferLength; |
20
|
|
|
private $socketType; |
21
|
|
|
|
22
|
|
|
public function __construct($location, $serverPort, $bufferLength, $socketType) |
23
|
|
|
{ |
24
|
|
|
$this->location = $location; |
25
|
|
|
$this->serverPort = $serverPort; |
26
|
|
|
$this->bufferLength = $bufferLength; |
27
|
|
|
$this->socketType = $socketType; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function writeToSocket($socket, $dataString) |
31
|
|
|
{ |
32
|
|
|
$bytes = socket_write($socket, $dataString, strlen($dataString)); |
33
|
|
|
|
34
|
|
|
return $bytes; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function createServerSocket() |
38
|
|
|
{ |
39
|
|
|
$socketType = AF_INET; |
40
|
|
|
if ($this->socketType == self::SOCKET_TYPE_FILE) { |
41
|
|
|
$socketType = AF_UNIX; |
42
|
|
|
} |
43
|
|
|
$socket = socket_create($socketType, SOCK_STREAM, 0); |
44
|
|
|
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1); |
45
|
|
|
$bindResult = socket_bind($socket, $this->location, $this->serverPort); |
46
|
|
|
if (!$bindResult) { |
47
|
|
|
$errorCode = socket_last_error($socket); |
48
|
|
|
$errorMsg = socket_strerror($errorCode); |
49
|
|
|
|
50
|
|
|
throw new IOException( |
51
|
|
|
sprintf( |
52
|
|
|
"Couldn't create server socket on ip: %s, port: %d. Reason: %s", |
53
|
|
|
$this->location, |
54
|
|
|
$this->serverPort, |
55
|
|
|
$errorMsg |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
socket_listen($socket); |
60
|
|
|
|
61
|
|
|
return $socket; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function createClientSocket() |
65
|
|
|
{ |
66
|
|
|
$socketType = AF_INET; |
67
|
|
|
if ($this->socketType == self::SOCKET_TYPE_FILE) { |
68
|
|
|
$socketType = AF_UNIX; |
69
|
|
|
} |
70
|
|
|
$socket = socket_create($socketType, SOCK_STREAM, 0); |
71
|
|
|
$connectionResult = socket_connect( |
72
|
|
|
$socket, |
73
|
|
|
$this->location, |
74
|
|
|
$this->serverPort |
75
|
|
|
); |
76
|
|
|
if (!$connectionResult) { |
77
|
|
|
$errorCode = socket_last_error($socket); |
78
|
|
|
$errorMsg = socket_strerror($errorCode); |
79
|
|
|
|
80
|
|
|
throw new IOException( |
81
|
|
|
sprintf( |
82
|
|
|
"Couldn't connect to server socket on ip: %s, port: %d. Reason: %s", |
83
|
|
|
$this->location, |
84
|
|
|
$this->serverPort, |
85
|
|
|
$errorMsg |
86
|
|
|
) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $socket; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function readFromSocket($socket) |
94
|
|
|
{ |
95
|
|
|
$recv = ''; |
96
|
|
|
$buffer = ''; |
97
|
|
|
while (socket_recv($socket, $buffer, $this->bufferLength, MSG_WAITALL)) { |
98
|
|
|
$recv .= $buffer; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $recv; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function closeSocket($socket) |
105
|
|
|
{ |
106
|
|
|
socket_close($socket); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function removeSocket() |
110
|
|
|
{ |
111
|
|
|
if ($this->socketType == self::SOCKET_TYPE_FILE) { |
112
|
|
|
unlink($this->location); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getServerIp() |
117
|
|
|
{ |
118
|
|
|
return $this->location; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getServerPort() |
122
|
|
|
{ |
123
|
|
|
return $this->serverPort; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getBufferLength() |
127
|
|
|
{ |
128
|
|
|
return $this->bufferLength; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|