1 | <?php |
||
15 | class Rcon |
||
16 | { |
||
17 | private $host; |
||
18 | private $port; |
||
19 | private $password; |
||
20 | private $timeout; |
||
21 | |||
22 | private $socket; |
||
23 | |||
24 | private $authorized = false; |
||
25 | private $lastResponse = ''; |
||
26 | |||
27 | const PACKET_AUTHORIZE = 5; |
||
28 | const PACKET_COMMAND = 6; |
||
29 | |||
30 | const SERVERDATA_AUTH = 3; |
||
31 | const SERVERDATA_AUTH_RESPONSE = 2; |
||
32 | const SERVERDATA_EXECCOMMAND = 2; |
||
33 | const SERVERDATA_RESPONSE_VALUE = 0; |
||
34 | |||
35 | /** |
||
36 | * Create a new instance of the Rcon class. |
||
37 | * |
||
38 | * @param string $host |
||
39 | * @param integer $port |
||
40 | * @param string $password |
||
41 | * @param integer $timeout |
||
42 | */ |
||
43 | public function __construct($host, $port, $password, $timeout) |
||
50 | |||
51 | /** |
||
52 | * Get the latest response from the server. |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | public function getResponse() |
||
60 | |||
61 | /** |
||
62 | * Connect to a server. |
||
63 | * |
||
64 | * @return boolean |
||
65 | */ |
||
66 | public function connect() |
||
81 | |||
82 | /** |
||
83 | * Disconnect from server. |
||
84 | * |
||
85 | * @return void |
||
86 | */ |
||
87 | public function disconnect() |
||
93 | |||
94 | /** |
||
95 | * True if socket is connected and authorized. |
||
96 | * |
||
97 | * @return boolean |
||
98 | */ |
||
99 | public function isConnected() |
||
103 | |||
104 | /** |
||
105 | * Send a command to the connected server. |
||
106 | * |
||
107 | * @param string $command |
||
108 | * |
||
109 | * @return boolean|mixed |
||
110 | */ |
||
111 | public function sendCommand($command) |
||
132 | |||
133 | /** |
||
134 | * Log into the server with the given credentials. |
||
135 | * |
||
136 | * @return boolean |
||
137 | */ |
||
138 | private function authorize() |
||
154 | |||
155 | /** |
||
156 | * Writes a packet to the socket stream. |
||
157 | * |
||
158 | * @param $packetId |
||
159 | * @param $packetType |
||
160 | * @param string $packetBody |
||
161 | * |
||
162 | * @return void |
||
163 | */ |
||
164 | private function writePacket($packetId, $packetType, $packetBody) |
||
188 | |||
189 | /** |
||
190 | * Read a packet from the socket stream. |
||
191 | * |
||
192 | * @return array |
||
193 | */ |
||
194 | private function readPacket() |
||
212 | } |
||
213 |