pub struct Peer { /* private fields */ }Expand description
A UDP peer that can send and receive encrypted messages.
Each peer maintains a UDP socket and can connect to at most one remote endpoint at a time. All messages are encrypted using AES-128-GCM before transmission.
Implementations§
Source§impl Peer
impl Peer
Sourcepub fn new(socket: UdpSocket, key: Key) -> Self
pub fn new(socket: UdpSocket, key: Key) -> Self
Creates a new peer with the given socket and encryption key.
Sourcepub fn setup<A1, A2>(bind_addr: A1, connect_addr: A2, key: Key) -> Result<Self>where
A1: ToSocketAddrs,
A2: ToSocketAddrs,
pub fn setup<A1, A2>(bind_addr: A1, connect_addr: A2, key: Key) -> Result<Self>where
A1: ToSocketAddrs,
A2: ToSocketAddrs,
Creates a new peer, binds to bind_addr, and connects to connect_addr.
This is a convenience method that combines socket creation, binding, and connection.
Use "0.0.0.0:0" or "[::]:0" for connect_addr to create an unconnected peer.
Sourcepub fn local_addr(&self) -> SocketAddr
pub fn local_addr(&self) -> SocketAddr
Returns the local socket address.
Sourcepub fn remote_addr_optional(&self) -> Option<SocketAddr>
pub fn remote_addr_optional(&self) -> Option<SocketAddr>
Returns the remote socket address if connected, otherwise None.
Sourcepub fn remote_addr(&self) -> SocketAddr
pub fn remote_addr(&self) -> SocketAddr
Returns the remote socket address, or an unspecified address if not connected.
Sourcepub fn connect<A: ToSocketAddrs>(&self, addr: A) -> Result<()>
pub fn connect<A: ToSocketAddrs>(&self, addr: A) -> Result<()>
Connects to the specified remote address.
This establishes the peer’s target for communication. Both send() and
recv() operations require the peer to be connected to function.
Sourcepub fn disconnect(&self) -> Result<()>
pub fn disconnect(&self) -> Result<()>
Disconnects from the current remote address.
After disconnecting, both send() and recv() calls will fail until
the peer is reconnected to a remote address.
Sourcepub fn set_read_timeout(&self, timeout: Option<Duration>) -> Result<()>
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> Result<()>
Sets the read timeout for receive operations.
Sourcepub fn set_write_timeout(&self, timeout: Option<Duration>) -> Result<()>
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> Result<()>
Sets the write timeout for send operations.
Sourcepub fn send(&mut self, buffer: &mut Vec<u8>) -> Result<()>
pub fn send(&mut self, buffer: &mut Vec<u8>) -> Result<()>
Encrypts and sends the contents of the buffer to the connected peer.
The buffer is modified in-place during encryption - a 28-byte overhead (16-byte authentication tag + 12-byte nonce) is appended to the end.
Returns an error if not connected to a peer, if encryption fails, or on network errors.
Sourcepub fn recv(&mut self, buffer: &mut Vec<u8>) -> Result<()>
pub fn recv(&mut self, buffer: &mut Vec<u8>) -> Result<()>
Receives and decrypts a message into the buffer.
The buffer must be large enough to hold the entire encrypted message. After receiving, the buffer is truncated to the message length, then the 28-byte crypto overhead is removed from the end during decryption. The buffer is resized to match the original message length.
Returns an error if not connected to a peer, if decryption fails, or on network errors.
Trait Implementations§
Source§impl Clone for Peer
impl Clone for Peer
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Clones the peer, including its socket and encryption state.
This allows for multiple mutable references to the same peer.
The socket is cloned using try_clone(), which may fail in
extreme cases if the underlying system resources are not available.
Returns a new peer with the same configuration.
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more