|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Votifier PHP Client |
|
5
|
|
|
* |
|
6
|
|
|
* @package VotifierClient |
|
7
|
|
|
* @author Manuele Vaccari <[email protected]> |
|
8
|
|
|
* @copyright Copyright (c) 2017-2020 Manuele Vaccari <[email protected]> |
|
9
|
|
|
* @license https://github.com/D3strukt0r/votifier-client-php/blob/master/LICENSE.txt GNU General Public License v3.0 |
|
10
|
|
|
* @link https://github.com/D3strukt0r/votifier-client-php |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace D3strukt0r\VotifierClient; |
|
14
|
|
|
|
|
15
|
|
|
use D3strukt0r\VotifierClient\ServerType\ServerTypeInterface; |
|
16
|
|
|
use Exception; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The class ServerConnection is used to create a connection to a server. |
|
20
|
|
|
*/ |
|
21
|
|
|
class ServerConnection |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ServerTypeInterface the server type information package |
|
25
|
|
|
*/ |
|
26
|
|
|
private $serverType; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var resource the connection to the server |
|
30
|
|
|
*/ |
|
31
|
|
|
private $s; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Creates the ServerConnection object. |
|
35
|
|
|
* |
|
36
|
|
|
* @param ServerTypeInterface $serverType (Required) The server type information package to connect to |
|
37
|
|
|
* |
|
38
|
|
|
* @throws Exception |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(ServerTypeInterface $serverType) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->serverType = $serverType; |
|
43
|
|
|
$s = fsockopen($serverType->getHost(), $serverType->getPort(), $errorNumber, $errorString, 3); |
|
44
|
|
|
if (false === $s) { |
|
45
|
|
|
throw new Exception($errorString, $errorNumber); |
|
46
|
|
|
} |
|
47
|
|
|
$this->s = $s; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Closes the connection when the object is destroyed. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __destruct() |
|
54
|
|
|
{ |
|
55
|
|
|
if ($this->s) { |
|
56
|
|
|
fclose($this->s); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Sends a string to the server. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $string (Required) The string which should be sent to the server |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool returns true if string was sent, or false if not |
|
66
|
|
|
*/ |
|
67
|
|
|
public function send(string $string): bool |
|
68
|
|
|
{ |
|
69
|
|
|
if (!$this->s) { |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (false === fwrite($this->s, $string)) { |
|
74
|
|
|
$this->__destruct(); |
|
75
|
|
|
|
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Reads a string which is being received from the server. |
|
84
|
|
|
* |
|
85
|
|
|
* @param int $length (Optional) The length of the requested string |
|
86
|
|
|
* |
|
87
|
|
|
* @return string|null returns the string that was received from the server |
|
88
|
|
|
*/ |
|
89
|
|
|
public function receive(int $length = 64): ?string |
|
90
|
|
|
{ |
|
91
|
|
|
if (!$this->s) { |
|
92
|
|
|
return null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (!$s = fread($this->s, $length)) { |
|
96
|
|
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $s; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|