forked from dantswain/BelugaIPC
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphpClient.php
87 lines (75 loc) · 1.87 KB
/
phpClient.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
<?php
function connectError($errno, $errstr)
{
if($errno == 2)
{
// do nothing, this just means the IPC server is down
return true;
}
else
{
// let the system handle this error
return false;
}
}
class belugaClient
{
private $host;
private $port;
private $socket;
private $timeout = 2;
private $connected = false;
private $error_file = "errors_php.log";
private $last_response = "";
function __construct()
{
}
function __destruct()
{
if($this->connected)
{
fclose($this->socket);
}
}
function connect($to_host, $on_port, $with_timeout = 2)
{
$this->timeout = $with_timeout;
$this->host = $to_host;
$this->port = $on_port;
set_error_handler("connectError");
$this->socket = fsockopen($this->host,
$this->port,
$errnum,
$errstr,
$this->timeout);
if(!is_resource($this->socket))
{
error_log($errstr . "\n", 3, $this->error_file);
$this->connected = false;
}
else
{
$this->last_response = trim(fgets($this->socket));
$this->connected = true;
}
restore_error_handler();
return $this->connected;
}
function isConnected()
{
return $this->connected;
}
function lastResponse(){return $this->last_response;}
function sendMessage($message)
{
if(!$this->connected)
{
return "Not connected";
}
$message = trim($message) . "\n";
fputs($this->socket, $message);
$this->last_response = trim(fgets($this->socket));
return $this->last_response;
}
}
?>