Struct RconClient

Source
pub struct RconClient { /* private fields */ }
Expand description

Struct that stores the connection and other state of the RCON protocol with the server.

§Examples

use mc_query::rcon::RconClient;
use tokio::io::Result;

#[tokio::main]
async fn main() -> Result<()> {
    let mut client = RconClient::new("localhost", 25575).await?;
    client.authenticate("password").await?;

    let output = client.run_command("time set day").await?;
    println!("{output}");

    Ok(())
}

Implementations§

Source§

impl RconClient

Source

pub async fn new(host: &str, port: u16) -> Result<Self>

Construct an RconClient that connects to the given host and port. Note: to authenticate use the authenticate method, this method does not take a password.

Clients constructed this way will wait arbitrarily long (maybe forever!) to recieve a response from the server. To set a timeout, see [with_timeout] or [set_timeout].

§Arguments
  • host - A string slice that holds the hostname of the server to connect to.
  • port - The port to connect to.
§Errors

Returns Err if there was a network error.

Source

pub async fn with_timeout( host: &str, port: u16, timeout: Duration, ) -> Result<Self>

Construct an RconClient that connects to the given host and port, and a connection timeout. Note: to authenticate use the authenticate method, this method does not take a password.

Note that timeouts are not precise, and may vary on the order of milliseconds, because of the way the async event loop works.

§Arguments
  • host - A string slice that holds the hostname of the server to connect to.
  • port - The port to connect to.
  • timeout - A duration to wait for each response to arrive in.
§Errors

Returns Err if there was a network error.

Source

pub fn set_timeout(&mut self, timeout: Option<Duration>)

Change the timeout for future requests.

§Arguments
  • timeout - an option specifying the duration to wait for a response. if none, the client may wait forever.
Source

pub async fn disconnect(self) -> Result<()>

Disconnect from the server and close the RCON connection.

§Errors

Returns Err if there was an issue closing the connection.

Source

pub async fn authenticate(&mut self, password: &str) -> Result<()>

Authenticate with the server, with the given password.

If authentication fails, this method will return RconProtocolError::AuthFailed.

§Arguments
  • password - A string slice that holds the RCON password.
§Errors

Returns the raw tokio::io::Error if there was a network error. Returns an apprpriate RconProtocolError if the authentication failed for other reasons. Also returns an error if a timeout is set, and the response is not recieved in that timeframe.

Source

pub async fn run_command(&mut self, command: &str) -> Result<String>

Run the given command on the server and return the result.

§Arguments
  • command - A string slice that holds the command to run. Must be ASCII and under 1446 bytes in length.
§Errors

Returns an error if there was a network issue or an RconProtocolError for other failures. Also returns an error if a timeout was set and a response was not recieved in that timeframe.

Trait Implementations§

Source§

impl Debug for RconClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V