Struct Connection

Source
pub struct Connection {
    pub address: SocketAddr,
    pub state: Arc<Mutex<ConnectionState>>,
    /* private fields */
}
Expand description

The connection struct contains the logic for a connection to the server. The following methods are the most important:

Warning:

This struct does not provide an API for connecting to other peers, for that you should use the Client struct.

Fields§

§address: SocketAddr

The address of the connection This is internally tokenized by rak-rs

§state: Arc<Mutex<ConnectionState>>

Implementations§

Source§

impl Connection

Source

pub async fn new( address: SocketAddr, socket: &Arc<UdpSocket>, net: Receiver<Vec<u8>>, notifier: Arc<Sender<SocketAddr>>, mtu: u16, ) -> Self

Initializes a new Connection instance.

Source

pub async fn recv(&mut self) -> Result<Vec<u8>, RecvError>

This method is used to recieve packets from the client connection. Packets that are recieved here are packets sent by the peer expected to be handled by the server.

This is where you would handle your packets from the client.

§Example

This is a snippet of code you would use after you’ve accepted a connection from the server with [Listener::accept()].

use rakrs::connection::Connection;

async fn handle(mut conn: Connection) {
    loop {
        // get a packet sent from the client
        if let Ok(pk) = conn.recv().await {
            println!("Got a connection packet {:?} ", pk);
        }
    }
}
Source

pub async fn is_closed(&self) -> bool

Source

pub async fn send( &self, buffer: &[u8], immediate: bool, ) -> Result<(), SendQueueError>

This method is used to send payloads to the connection. This method internally will encode your payload into a RakNet packet, and send it to the client.

§Example

This is a snippet of code you would use when you need to send a payload to the client.

use rakrs::connection::Connection;

async fn send_payload(mut conn: Connection) {
    conn.send(&[0x01, 0x02, 0x03], true).await.unwrap();
}
Source

pub async fn close(&self)

This method should be used when you are ready to disconnect the client. this method will attempt to send a disconnect packet to the client, and then close the connection.

Trait Implementations§

Source§

impl Drop for Connection

Source§

fn drop(&mut self)

Executes the destructor for this type. 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