pub trait Socket: Sized {
type Address: Address;
type Error: SocketError;
// Required methods
fn send(
&mut self,
address: Self::Address,
buffer: &[u8],
) -> Result<usize, Self::Error>;
fn receive(
&mut self,
buffer: &mut [u8; 4096],
) -> Result<Option<(Self::Address, PacketReceived)>, Self::Error>;
// Provided method
fn init(
&mut self,
_socket_options: SocketOptions,
) -> Result<(), Self::Error> { ... }
}
Expand description
A trait for implementing the underlying data transport layer ENet uses.
An implementation for std::net::UdpSocket
is provided out of the box.
If implementing this trait is cumbersome, is may be easier to use
ReadWrite
.
Required Associated Types§
Sourcetype Address: Address
type Address: Address
The address type to use, which must implement Address
.
An example is the standard library’s std::net::SocketAddr
, used with
std::net::UdpSocket
.
Sourcetype Error: SocketError
type Error: SocketError
Errors returned by this socket.
Required Methods§
Sourcefn send(
&mut self,
address: Self::Address,
buffer: &[u8],
) -> Result<usize, Self::Error>
fn send( &mut self, address: Self::Address, buffer: &[u8], ) -> Result<usize, Self::Error>
Try to send data. Should return the number of bytes successfully sent, or an error.
Sourcefn receive(
&mut self,
buffer: &mut [u8; 4096],
) -> Result<Option<(Self::Address, PacketReceived)>, Self::Error>
fn receive( &mut self, buffer: &mut [u8; 4096], ) -> Result<Option<(Self::Address, PacketReceived)>, Self::Error>
Try to receive data from the socket into a buffer of size MTU_MAX
.
A received packet should be written into the provided buffer. If a packet is received that
is larger than MTU_MAX
, it should simply be discarded. ENet will never send a packet
that is larger than this maximum, so if one is received, it was not sent by ENet.
The return value should be Ok(None)
if no packet was received. If a packet was received,
the address of the peer socket, as well as the amount of bytes received should be returned.
Packets received may be complete or partial. See PacketReceived
for more info.
Provided Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.