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

Raknet socket wrapper with local and remote.

Implementations

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.

Connect to a Raknet server and return a Raknet socket

Example
let mut 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
}
socket.close().await.unwarp(); // you need to manually close raknet connection

Close Raknet Socket

The Raknet Socket needs to be closed manually, and if no valid data packets are received for more than 1 minute, the Raknet connection will be closed automatically. This method can be called repeatedly.

Example
let mut socket = RaknetSocket::connect("127.0.0.1:19132".parse().unwrap()).await.unwrap();
socket.close().await.unwarp();

Unconnected ping a Raknet Server and return latency

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

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 mut socket = RaknetSocket::connect("127.0.0.1:19132".parse().unwrap()).await.unwrap();
socket.send(&[0xfe], Reliability::ReliableOrdered).await.unwrap();

Recv a packet

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

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)));

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)));

Set the packet loss rate and use it for testing

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

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);

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.