Struct Connection

Source
pub struct Connection {
    pub stream: TcpStream,
    pub addr: (String, u16),
}
Expand description

Represents a TCP connection to a Minecraft server.

§Examples

use std::time::Duration;
use mc_ping::connection::Connection;
let addr = ("play.example.com".to_string(), 25565);
let mut conn = Connection::connect(addr).await?;
let status = conn.ping().await?;
println!("Server status: {:?}", status);

Fields§

§stream: TcpStream

Underlying TCP stream.

§addr: (String, u16)

Server address as (IP/domain, port).

Implementations§

Source§

impl Connection

Source

pub async fn connect(addr: (String, u16)) -> Result<Self>

Connects to a Minecraft server at the specified address.

If the resolve feature is enabled, attempts to resolve domain names to IP addresses before connecting.

§Errors

Returns an error if the connection or DNS resolution fails.

§Examples
use mc_ping::connection::Connection;
let addr = ("localhost".to_string(), 25565);
let conn = Connection::connect(addr).await?;
Source

pub async fn connect_timeout( addr: (String, u16), _timeout: Duration, ) -> Result<Self>

Connects to a Minecraft server with a timeout.

Attempts to connect and returns an error if the timeout elapses.

§Errors

Returns an error if the connection fails or times out.

§Examples
use mc_ping::connection::Connection;
let addr = ("example.com".to_string(), 25565);
match Connection::connect_timeout(addr, Duration::from_secs(5)).await {
    Ok(conn) => println!("Connected!"),
    Err(e) => println!("Failed to connect: {}", e),
}
Source

pub async fn send_handshake(&mut self) -> Result<()>

Sends the Minecraft handshake packet.

This prepares the connection for further communication such as status query or login.

§Errors

Returns an error if writing to the TCP stream fails.

§Examples
use mc_ping::connection::Connection;
let addr = ("127.0.0.1".to_string(), 25565);
let mut conn = Connection::connect(addr).await?;
conn.send_handshake().await?;
Source

pub async fn get_status(&mut self) -> Result<ServerStatus>

Queries the server status.

Sends a status query and reads the response, returning a parsed ServerStatus. Assumes handshake has been sent beforehand.

§Errors

Returns an error if sending or receiving fails, or parsing fails.

§Examples
use mc_ping::connection::Connection;
let addr = ("localhost".to_string(), 25565);
let mut conn = Connection::connect(addr).await?;
conn.send_handshake().await?;
let status = conn.get_status().await?;
println!("Status: {:?}", status);
Source

pub async fn ping(&mut self) -> Result<ServerStatus>

Performs a full ping: sends handshake + status query and returns server status.

Convenient for a single-step status check.

§Errors

Returns an error if any network or parsing step fails.

§Examples
use mc_ping::connection::Connection;
let addr = ("play.example.com".to_string(), 25565);
let mut conn = Connection::connect(addr).await?;
let status = conn.ping().await?;
println!("Server status: {:?}", status);

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.