-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
301 lines (258 loc) · 7.81 KB
/
Client.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**
* Novutec Domain Tools
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @category Novutec
* @package MessagePackRpcClient
* @copyright Copyright (c) 2007 - 2013 Novutec Inc. (http://www.novutec.com)
* @license http://www.apache.org/licenses/LICENSE-2.0
*/
/**
* @namespace Novutec\MessagePackRpcClient
*/
namespace Novutec\MessagePackRpcClient;
/**
* Client of MessagePackRpcClient
*
* @category Novutec
* @package MessagePackRpcClient
* @copyright Copyright (c) 2007 - 2013 Novutec Inc. (http://www.novutec.com)
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link http://msgpack.org/
* @link https://github.com/msgpack/msgpack-php
*/
class Client
{
/**
* Address to the socket to connect to
*
* @var string
* @access private
*/
private $socket;
/**
* Socket resource also referred to as an endpoint of communication
*
* @var resource
* @access private
*/
private $sock;
/**
* Initiate a connection to address using the socket resource,
* which must be a valid socket resource
*
* @var boolean
* @access private
*/
private $conn;
/**
* The maximum number of bytes read is specified by the length parameter.
*
* @var integer
* @access private
*/
private $length;
/**
* Activate log messages
*
* @var boolean
* @access private
*/
private $debug;
/**
* Log message is appended to the file destination.
*
* @var string
* @access private
*/
private $destination;
/**
* Number of seconds until connect() method calls timeout.
* By default 30 seconds
*
* @var integer
* @access private
*/
private $timeout;
/**
* Set variables and calls connect() method
*
* @param string $socket
* @param integer $length
* @param integer $timeout
* @param boolean $debug
* @param string $destination
* @return boolean
*/
public function __construct($socket = 'tcp://localhost:9000', $length = 1024, $timeout = 30, $debug = false,
$destination = '/log/msgpack-rpc-client.log')
{
$this->socket = $socket;
$this->length = $length;
$this->timeout = $timeout;
if ($debug) {
$this->set_debug($debug, $destination);
}
}
/**
* Set debug mode and log file destination
*
* @param boolean $debug
* @param string $log_destination
* @return void
*/
public function set_debug($debug = false, $destination = '/log/msgpack-rpc-client.log')
{
$this->debug = $debug;
$this->destination = $destination;
}
/**
* Creates and initiates a socket connection
*
* @throws ConnectErrorException
* @return boolean
*/
public function connect()
{
if ($this->conn) {
return true;
}
$errno = $errstr = null;
$this->sock = @stream_socket_client($this->socket, $errno, $errstr, $this->timeout);
if (! is_resource($this->sock)) {
throw \Novutec\MessagePackRpcClient\AbstractException::factory('ConnectError', $errstr);
}
if ($this->debug) {
$this->log('Connected to Remote Socket: ' . $this->socket, $this->sock);
}
$this->conn = true;
return $this->conn;
}
/**
* Sends data to the socket and receives the response
*
* @throws WriteErrorException
* @throws ReadErrorException
* @throws ResponseErrorException
* @throws TypeErrorException
* @throws ServerErrorException
* @param string &$method
* @param array &$params
* @param integer &$msgid
* @return mixed
*/
public function call(&$method, &$params = array(), &$msgid = 0)
{
if (! $this->conn) {
$this->connect();
}
$buffer = '';
$buffer_length = 0;
$data = array(0 => 0, 1 => $msgid, 2 => $method, 3 => $params);
@stream_set_blocking($this->sock, 1);
if ($this->debug) {
$this->log('Set option to socket: blocking', $this->sock);
}
$packed_data = $this->pack($data);
$send = @fwrite($this->sock, $packed_data);
if ($this->debug) {
$this->log('Write to socket: ' . json_encode($data), $send);
}
if ($send != strlen($packed_data)) {
throw \Novutec\MessagePackRpcClient\AbstractException::factory('WriteError', 'Could not write to the socket. (tried/send): ' .
'(' . $send . '/' . strlen($packed_data) . ')');
}
@stream_set_blocking($this->sock, 0);
if ($this->debug) {
$this->log('Set option to socket: non-blocking', $this->sock);
}
$read = $write = array($this->sock);
$except = null;
$unpacker = new \MessagePackUnpacker();
do {
if (stream_select($read, $write, $except, $this->timeout) === false) {
break;
}
$recv = @fread($this->sock, $this->length);
if ($this->debug && $recv != '') {
$this->log('Read from socket - length: ' . strlen($recv), $recv);
}
$unpacker->feed($recv);
if ($recv === false) {
throw \Novutec\MessagePackRpcClient\AbstractException::factory('ReadError', 'Could not read from socket.');
}
} while (! $unpacker->execute());
$data = $unpacker->data();
$unpacker->reset();
if ($this->debug) {
$this->log('Unpack received data: ' . json_encode($data), $data);
}
if (sizeof($data) != 4) {
throw \Novutec\MessagePackRpcClient\AbstractException::factory('ResponseError', 'Number of elements within received data is wrong.');
}
if ($data[0] != 1) {
throw \Novutec\MessagePackRpcClient\AbstractException::factory('TypeError', 'Received data has the wrong type.');
}
if ($data[2] != '') {
throw \Novutec\MessagePackRpcClient\AbstractException::factory('ServerError', isset($data[2][0]) ? $data[2][0] : 'Got an error from server.');
}
return $data[3];
}
/**
* Use MsgPack to serialize
*
* @see https://github.com/msgpack/msgpack-php
* @param array &$data
* @return mixed
*/
private function pack(&$data)
{
return msgpack_pack($data);
}
/**
* Use MsgPack to unserialize
*
* @see https://github.com/msgpack/msgpack-php
* @param string &$data
* @return array
*/
private function unpack(&$data)
{
return msgpack_unpack($data);
}
/**
* Uses PHP error_log() function to write messages to file destination
*
* @param string $msg
* @param boolean $bool
* @return boolean
*/
private function log($msg, $bool = false)
{
return error_log('[' . date('Y-m-d H:i:s') . '] ' .
(! $bool ? 'ERROR: ' . socket_strerror(socket_last_error()) . ' - ' : '') . $msg .
"\n", 3, MSGPACK_PATH . $this->destination);
}
/**
* Closes a socket resource
*
* @return void
*/
public function __destruct()
{
if ($this->conn) {
fclose($this->sock);
}
}
}