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
}

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

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

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

Note: AsyncRead requires you to provide a fixed-size buffer for reading data.

However, Raknet is not a streaming protocol, so when you use AsyncRead, if you don’t provide enough buffer size (less than Raknet Packet size), then a Raknet Packet will be split into multiple reads.

This will lead to a similar TCP unpacking problem when you use AsyncRead when you are developing scenarios such as Raknet reverse proxy where the packet size cannot be predicted. https://programmer.ink/think/tcp-packet-sticking-unpacking.html So it is not recommended to use AsyncRead for developing Raknet reverse proxy.

Attempts to read from the AsyncRead into buf. Read more

Note: AsyncWrite is not stream written. Every time the data sent using AsyncWrite will be completely put into the Raknet Packet, so the returned write size is always equal to the incoming buffer size.

Attempt to write bytes from buf into the object. Read more

Attempts to flush the object, ensuring that any buffered data reach their destination. Read more

Initiates or attempts to shut down this writer, returning success when the I/O connection has completely shut down. Read more

Like poll_write, except that it writes from a slice of buffers. Read more

Determines if this writer has an efficient poll_write_vectored implementation. Read more

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Creates a new AsyncRead instance that chains this stream with next. Read more

Pulls some bytes from this source into the specified buffer, returning how many bytes were read. Read more

Pulls some bytes from this source into the specified buffer, advancing the buffer’s internal cursor. Read more

Reads the exact number of bytes required to fill buf. Read more

Reads an unsigned 8 bit integer from the underlying reader. Read more

Reads a signed 8 bit integer from the underlying reader. Read more

Reads an unsigned 16-bit integer in big-endian order from the underlying reader. Read more

Reads a signed 16-bit integer in big-endian order from the underlying reader. Read more

Reads an unsigned 32-bit integer in big-endian order from the underlying reader. Read more

Reads a signed 32-bit integer in big-endian order from the underlying reader. Read more

Reads an unsigned 64-bit integer in big-endian order from the underlying reader. Read more

Reads an signed 64-bit integer in big-endian order from the underlying reader. Read more

Reads an unsigned 128-bit integer in big-endian order from the underlying reader. Read more

Reads an signed 128-bit integer in big-endian order from the underlying reader. Read more

Reads an 32-bit floating point type in big-endian order from the underlying reader. Read more

Reads an 64-bit floating point type in big-endian order from the underlying reader. Read more

Reads an unsigned 16-bit integer in little-endian order from the underlying reader. Read more

Reads a signed 16-bit integer in little-endian order from the underlying reader. Read more

Reads an unsigned 32-bit integer in little-endian order from the underlying reader. Read more

Reads a signed 32-bit integer in little-endian order from the underlying reader. Read more

Reads an unsigned 64-bit integer in little-endian order from the underlying reader. Read more

Reads an signed 64-bit integer in little-endian order from the underlying reader. Read more

Reads an unsigned 128-bit integer in little-endian order from the underlying reader. Read more

Reads an signed 128-bit integer in little-endian order from the underlying reader. Read more

Reads an 32-bit floating point type in little-endian order from the underlying reader. Read more

Reads an 64-bit floating point type in little-endian order from the underlying reader. Read more

Reads all bytes until EOF in this source, placing them into buf. Read more

Reads all bytes until EOF in this source, appending them to buf. Read more

Creates an adaptor which reads at most limit bytes from it. Read more

Writes a buffer into this writer, returning how many bytes were written. Read more

Like write, except that it writes from a slice of buffers. Read more

Writes a buffer into this writer, advancing the buffer’s internal cursor. Read more

Attempts to write an entire buffer into this writer. Read more

Attempts to write an entire buffer into this writer. Read more

Writes an unsigned 8-bit integer to the underlying writer. Read more

Writes an unsigned 8-bit integer to the underlying writer. Read more

Writes an unsigned 16-bit integer in big-endian order to the underlying writer. Read more

Writes a signed 16-bit integer in big-endian order to the underlying writer. Read more

Writes an unsigned 32-bit integer in big-endian order to the underlying writer. Read more

Writes a signed 32-bit integer in big-endian order to the underlying writer. Read more

Writes an unsigned 64-bit integer in big-endian order to the underlying writer. Read more

Writes an signed 64-bit integer in big-endian order to the underlying writer. Read more

Writes an unsigned 128-bit integer in big-endian order to the underlying writer. Read more

Writes an signed 128-bit integer in big-endian order to the underlying writer. Read more

Writes an 32-bit floating point type in big-endian order to the underlying writer. Read more

Writes an 64-bit floating point type in big-endian order to the underlying writer. Read more

Writes an unsigned 16-bit integer in little-endian order to the underlying writer. Read more

Writes a signed 16-bit integer in little-endian order to the underlying writer. Read more

Writes an unsigned 32-bit integer in little-endian order to the underlying writer. Read more

Writes a signed 32-bit integer in little-endian order to the underlying writer. Read more

Writes an unsigned 64-bit integer in little-endian order to the underlying writer. Read more

Writes an signed 64-bit integer in little-endian order to the underlying writer. Read more

Writes an unsigned 128-bit integer in little-endian order to the underlying writer. Read more

Writes an signed 128-bit integer in little-endian order to the underlying writer. Read more

Writes an 32-bit floating point type in little-endian order to the underlying writer. Read more

Writes an 64-bit floating point type in little-endian order to the underlying writer. Read more

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

Shuts down the output stream, ensuring that the value can be dropped cleanly. 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.