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.
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
impl Listener
sourcepub async fn bind<I: for<'a> Into<PossiblySocketAddr<'a>>>(
address: I
) -> Result<Self, ServerError>
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();
}
sourcepub async fn start(&mut self) -> Result<(), ServerError>
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;
}
sourcepub async fn accept(&mut self) -> Result<Connection, ServerError>
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.
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);
}
}
sourcepub async fn stop(&mut self) -> Result<(), ServerError>
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.