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>;
}
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§
Sourcetype Error: SocketError
type Error: SocketError
Errors returned by this connection.
Required Methods§
Sourcefn init(&mut self) -> Result<Self::Address, Self::Error>
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
.
Sourcefn send(&mut self, buffer: &[u8]) -> Result<usize, Self::Error>
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.
Sourcefn receive(
&mut self,
buffer: &mut [u8; 4096],
) -> Result<Option<PacketReceived>, Self::Error>
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.