🆙 Fix code in somephp files an refactored and deleted deprecated code 🆙

This commit is contained in:
Remco
2026-01-19 17:25:31 +01:00
parent 3238cdb8e7
commit 6736e8fe60
9 changed files with 145 additions and 92 deletions
+17 -14
View File
@@ -32,16 +32,19 @@ class RconService
if (! $this->socket) {
$error = socket_strerror(socket_last_error());
Log::error("RCON initialization failed: $error");
Log::error("RCON initialization failed: {$error}");
$this->closeConnection();
return;
}
socket_set_option($this->socket, SOL_SOCKET, SO_RCVTIMEO, ['sec' => 5, 'usec' => 0]);
socket_set_option($this->socket, SOL_SOCKET, SO_SNDTIMEO, ['sec' => 5, 'usec' => 0]);
if (! @socket_connect($this->socket, $this->config['ip'], $this->config['port'])) {
$error = socket_strerror(socket_last_error());
Log::error("RCON connection failed: $error");
Log::error("RCON connection failed: {$error}");
$this->closeConnection();
@@ -53,7 +56,7 @@ class RconService
private function closeConnection(): void
{
if ($this->socket instanceof \Socket) {
if ($this->socket instanceof Socket) {
socket_close($this->socket);
}
@@ -70,29 +73,24 @@ class RconService
* @throws RconConnectionException
* @throws JsonException
*/
public function sendCommand(string $command, ?array $data = null)
public function sendCommand(string $command, ?array $data = null): bool
{
if (! $this->isConnected) {
$error = 'RCON command failed: Not connected';
Log::error($error);
Log::error('RCON command failed: Not connected');
$this->closeConnection();
return $this->isConnected;
return false;
}
$payload = json_encode(['key' => $command, 'data' => $data], JSON_THROW_ON_ERROR);
if (! @socket_write($this->socket, $payload, strlen($payload))) {
$error = socket_strerror(socket_last_error($this->socket));
Log::error("RCON command ($command) failed: $error");
Log::error("RCON command ({$command}) failed: {$error}");
$this->closeConnection();
return $this->isConnected;
return false;
}
return $this->isConnected;
return true;
}
/**
@@ -254,4 +252,9 @@ class RconService
'command' => $command,
]);
}
public function __destruct()
{
$this->closeConnection();
}
}