Struct RaknetSocket

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

Raknet socket wrapper with local and remote.

Implementations§

Source§

impl RaknetSocket

Source

pub async fn from( addr: &SocketAddr, s: &Arc<UdpSocket>, receiver: Receiver<Vec<u8>>, mtu: u16, collecter: Arc<Mutex<Sender<SocketAddr>>>, raknet_version: u8, ) -> Self

Create a Raknet Socket from a UDP socket with an established Raknet connection

This method is used for RaknetListener, users of the library should not care about it.

Source

pub async fn connect(addr: &SocketAddr) -> Result<Self, RaknetError>

Connect to a Raknet server and return a Raknet socket

§Example
let socket = RaknetSocket::connect("127.0.0.1:19132".parse().unwrap()).await.unwrap();
socket.send(&[0xfe], Reliability::ReliableOrdered).await.unwrap();
let buf = socket.recv().await.unwrap();
if buf[0] == 0xfe{
   //do something
}
Source

pub async fn connect_with_version( addr: &SocketAddr, raknet_version: u8, ) -> Result<Self, RaknetError>

Source

pub async fn close(&self) -> Result<(), RaknetError>

Close Raknet Socket. Normally you don’t need to call this method, the RaknetSocket will be closed automatically when it is released. This method can be called repeatedly.

§Example
let (latency, motd) = socket::RaknetSocket::ping("127.0.0.1:19132".parse().unwrap()).await.unwrap();
assert!((0..10).contains(&latency));
Source

pub async fn ping(addr: &SocketAddr) -> Result<(i64, String), RaknetError>

Unconnected ping a Raknet Server and return latency and motd.

§Example
let (latency, motd) = socket::RaknetSocket::ping("127.0.0.1:19132".parse().unwrap()).await.unwrap();
assert!((0..10).contains(&latency));
Source

pub async fn send(&self, buf: &[u8], r: Reliability) -> Result<(), RaknetError>

Send a packet

packet must be 0xfe as the first byte, using other values of bytes may cause unexpected errors.

Except Reliability::ReliableOrdered, all other reliability packets must be less than MTU - 60 (default 1340 bytes), otherwise RaknetError::PacketSizeExceedMTU will be returned

§Example
let socket = RaknetSocket::connect("127.0.0.1:19132".parse().unwrap()).await.unwrap();
socket.send(&[0xfe], Reliability::ReliableOrdered).await.unwrap();
Source

pub async fn flush(&self) -> Result<(), RaknetError>

Wait all packet acked

§Example
let socket = RaknetSocket::connect("127.0.0.1:19132".parse().unwrap()).await.unwrap();
socket.send(&[0xfe], Reliability::ReliableOrdered).await.unwrap();
socket.flush().await.unwrap();
Source

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

Recv a packet

§Example
let socket = RaknetSocket::connect("127.0.0.1:19132".parse().unwrap()).await.unwrap();
let buf = socket.recv().await.unwrap();
if buf[0] == 0xfe{
   //do something
}
Source

pub fn peer_addr(&self) -> Result<SocketAddr, RaknetError>

Returns the socket address of the remote peer of this Raknet connection.

§Example
let socket = RaknetSocket::connect("127.0.0.1:19132".parse().unwrap()).await.unwrap();
assert_eq!(socket.peer_addr().unwrap(), SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 19132)));
Source

pub fn local_addr(&self) -> Result<SocketAddr, RaknetError>

Returns the socket address of the local half of this Raknet connection.

§Example
let mut socket = RaknetSocket::connect("127.0.0.1:19132".parse().unwrap()).await.unwrap();
assert_eq!(socket.local_addr().unwrap().ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
Source

pub fn raknet_version(&self) -> Result<u8, RaknetError>

return the raknet version used by this connection.

Source

pub fn set_loss_rate(&mut self, stage: u8)

Set the packet loss rate and use it for testing

The stage parameter ranges from 0 to 10, indicating a packet loss rate of 0% to 100%.

§Example
let mut socket = RaknetSocket::connect("127.0.0.1:19132".parse().unwrap()).await.unwrap();
// set 20% loss packet rate.
socket.set_loss_rate(8);

Trait Implementations§

Source§

impl Drop for RaknetSocket

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