Struct rak_rs::server::Listener

source ·
pub struct Listener {
    pub motd: Motd,
    pub id: u64,
    pub versions: &'static [u8],
    /* private fields */
}
Expand description

The main server struct, this is responsible for listening to connections, and dispatching them to a handler.

If you are having problems with debugging, you can use the rak-rs debug feature, which will print out all packets that are sent and recieved.

Notice:

Currently, the Listener does not support encryption, plugins, or any feature to allow you to hijack the RakNet connection sequence. Currently rak_rs is a pure, bare-bones RakNet implementation.

There is currently an open issue to add support for plugins but this is not a priority, instead you should use the Connection struct to handle your own packets with the recv method.

§A generic example

use rak_rs::server::Listener;

#[async_std::main]
async fn main() {
    // Bind the server to the specified address, but do not start it.
    let mut server = Listener::bind("0.0.0.0:19132").await.unwrap();

    // Begins listening to connections
    server.start().await.unwrap();

    // Start recieving connections
    loop {
        let conn = server.accept().await;
        async_std::task::spawn(handle(conn.unwrap()));
    }
}

// This is a function that handles the connection, this is where you would handle the connection.
async fn handle(mut conn: Connection) {
    loop {
        // this is used to cleanup the connection
        if conn.get_state().await.is_closed() {
            println!("Connection closed!");
            break;
        }

        if let Ok(pk) = conn.recv().await {
            println!("Got a connection packet {:?} ", pk);
        }
    }
}

§Accepting other protocols

This struct allows support for other raknet protocols, however this is not recommended, because occasionally the protocol may change and the Listener may not be updated to support it. This was mainly added for MCPE.

use rak_rs::server::Listener;

#[async_std::main]
async fn main() {
    let mut server = Listener::bind("0.0.0.0:19132").await.unwrap();
    server.versions = &[10, 11];
    server.start().await.unwrap();

    loop {
        // .. same loop as above
    }
}

Fields§

§motd: Motd

If mcpe is true, this is the default MOTD, this is the default MOTD to send to the client. You can change this later by setting a motd in the Conn struct.

§id: u64

A server Id, passed in unconnected pong.

§versions: &'static [u8]

Supported versions

Implementations§

source§

impl Listener

source

pub async fn bind<I: for<'a> Into<PossiblySocketAddr<'a>>>( address: I ) -> Result<Self, ServerError>

Binds a new listener to the specified address provided, this will error if the address is invalid or already in use. This will not start the listener, you must call Listener::start to start listening to connections.

§Example
use rak_rs::server::Listener;

async fn start() {
    let mut server = Listener::bind("").await.unwrap();
}
source

pub async fn start(&mut self) -> Result<(), ServerError>

This method is required to be called before the server can begin listening to connections. However, you must call Listener::bind before you can call this method, as that method is responsible for creating the socket and initializing the server.

§Example
use rak_rs::server::Listener;
async fn start() {
    let mut server = Listener::bind("0.0.0.0:19132").await.unwrap();

    // let's begin to listen to connections
    server.start().await;
}
source

pub async fn accept(&mut self) -> Result<Connection, ServerError>

This method is used to accept a connection, this will block until a connection is available. You can only call this method once both Listener::bind AND Listener::start have. This function is used to recieve and accept connections. Alternatively, you can refuse a connection by dropping it when you accept it.

Warning:

This method will block until a connection is available, this is not recommended to be used in the main thread, instead you should use a task or future to handle connections.

§Example
use rak_rs::server::Listener;
use rak_rs::Connection;

#[async_std::main]
async fn main() {
    let mut server = Listener::bind("0.0.0.0:19132").await.unwrap();
    server.start().await.unwrap();

    loop {
        let conn = server.accept().await;
        async_std::task::spawn(handle(conn.unwrap()));
    }
}

async fn handle(mut conn: Connection) {
   loop {
        let packet = conn.recv().await;
        println!("Received a packet! {:?}", packet);
   }
}
source

pub async fn stop(&mut self) -> Result<(), ServerError>

Stops the Listener, effectively closing the socket and stopping the server. This will also close all connections, and prevent any new connections from being accepted, until Listener::start is called again.

Trait Implementations§

source§

impl Drop for Listener

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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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>,

§

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>,

§

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

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more