felixms / arma-rcon-class-php

A lightweight client for sending commands easily to a BattlEye server.
MIT License
46 stars 22 forks source link

try and catch no error? #18

Closed majoess closed 7 years ago

majoess commented 7 years ago

Im using Laravel, I have the following code, comming from a form, there comes no error if the connection is 100% wrong.

      if ( $rcon ) {
        try {
          $rconconnection = new ARC($rconhost, $rconpass, $rconport);
          $rconconnection
                      ->sayGlobal('VitControl: Connection successful!')
                      ->disconnect()
          ;
        } catch (Exception $e) {
          $exception = $e->getMessage();

          return \Redirect::route('addServerForm')
            ->with('message', $exception);
        }
        return \Redirect::route('addServerForm')
            ->with('message', 'Server registered!');
      } else {
      return \Redirect::route('addServerForm')
        ->with('message', 'Server registered!');
      }

    }
felixms commented 7 years ago

I'll take a look tomorrow :)

felixms commented 7 years ago

ARC currently checks if the socket is created and if writing the message to the socket was successful. It does not validate the returned packages from the server nor it does acknowledge them.

The server's BE RCon tries to send a server message packet 5 times for 10 seconds. If the client fails to acknowledge the packet within this time, it will be removed from BE RCon's list of authenticated clients and will no longer be able to issue any commands.

(see here)

This will also not be done in the future, because this project is not meant to establish a long lasting connection, only sending commands quickly. Furthermore PHP is not a good language for those actions.

So you cannot really say, if the server exists without checking the received packages (not done).

As a work-around, you might get the player list and check if it is empty. If so, the server might not exist, because you normally receive a table, even if there are no players online.

Example:

try {
    $rconconnection = new ARC($rconhost, $rconpass, $rconport);

    if (empty($rconconnection->getPlayers()) {
        throw new Exception('Connection failed!');
    } else {
        $rconconnection
            ->sayGlobal('VitControl: Connection successful!')
            ->disconnect()
        ;
    }
} catch (Exception $e) {
    $exception = $e->getMessage();

    return \Redirect::route('addServerForm')
        ->with('message', $exception);
}

I hope I was of help to you :)