Trait Socket

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

Source

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.

Source

type Error: SocketError

Errors returned by this socket.

Required Methods§

Source

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.

Source

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§

Source

fn init(&mut self, _socket_options: SocketOptions) -> Result<(), Self::Error>

Initialize the socket with options passed down by ENet.

Called in Host::new. If this function returns an error, it is bubbled up through Host::new.

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.

Implementations on Foreign Types§

Source§

impl Socket for UdpSocket

Source§

type Address = SocketAddr

Source§

type Error = Error

Source§

fn init(&mut self, _socket_options: SocketOptions) -> Result<(), Error>

Source§

fn send(&mut self, address: SocketAddr, buffer: &[u8]) -> Result<usize, Error>

Source§

fn receive( &mut self, buffer: &mut [u8; 4096], ) -> Result<Option<(SocketAddr, PacketReceived)>, Error>

Implementors§

Source§

impl<A: Address + 'static, E: SocketError> Socket for ReadWrite<A, E>