Skip to main content

Peer

Struct Peer 

Source
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

Source

pub fn new(socket: UdpSocket, key: Key) -> Self

Creates a new peer with the given socket and encryption key.

Source

pub fn setup<A1, A2>(bind_addr: A1, connect_addr: A2, key: Key) -> Result<Self>

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.

Source

pub fn socket(&self) -> &UdpSocket

Returns a reference to the underlying UDP socket.

Source

pub fn local_addr(&self) -> SocketAddr

Returns the local socket address.

Source

pub fn remote_addr_optional(&self) -> Option<SocketAddr>

Returns the remote socket address if connected, otherwise None.

Source

pub fn remote_addr(&self) -> SocketAddr

Returns the remote socket address, or an unspecified address if not connected.

Source

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.

Source

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.

Source

pub fn set_read_timeout(&self, timeout: Option<Duration>) -> Result<()>

Sets the read timeout for receive operations.

Source

pub fn set_write_timeout(&self, timeout: Option<Duration>) -> Result<()>

Sets the write timeout for send operations.

Source

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.

Source

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

Source§

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)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Peer

§

impl RefUnwindSafe for Peer

§

impl Send for Peer

§

impl Sync for Peer

§

impl Unpin for Peer

§

impl UnsafeUnpin for Peer

§

impl UnwindSafe for Peer

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V