Trait Connection

Source
pub trait Connection {
    type Address: Address;
    type Error: SocketError;

    // Required methods
    fn init(&mut self) -> Result<Self::Address, Self::Error>;
    fn send(&mut self, buffer: &[u8]) -> Result<usize, Self::Error>;
    fn receive(
        &mut self,
        buffer: &mut [u8; 4096],
    ) -> Result<Option<PacketReceived>, Self::Error>;
}
Available on crate feature connected only.
Expand description

A trait for implementing connection based sockets, similar to Socket.

An implementation for std::net::TcpStream is provided out of the box.

An implementation of ReadWrite is provided with an address type of ().

Required Associated Types§

Source

type Address: Address

The address type to use, which must implement Address.

Source

type Error: SocketError

Errors returned by this connection.

Required Methods§

Source

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

Initialize the socket with options passed down by ENet.

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

Source

fn send(&mut self, 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<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 amount of bytes received should be returned. Packets received may be complete or partial. See PacketReceived for more info.

Implementations on Foreign Types§

Source§

impl Connection for TcpStream

Source§

type Address = SocketAddr

Source§

type Error = Error

Source§

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

Source§

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

Source§

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

Implementors§