Struct UtpSocket

Source
pub struct UtpSocket {
    pub max_retransmission_retries: u32,
    /* private fields */
}
Expand description

A structure that represents a uTP (Micro Transport Protocol) connection between a local socket and a remote socket.

The socket will be closed when the value is dropped (either explicitly or when it goes out of scope).

The default maximum retransmission retries is 5, which translates to about 16 seconds. It can be changed by assigning the desired maximum retransmission retries to a socket’s max_retransmission_retries field. Notice that the initial congestion timeout is 500 ms and doubles with each timeout.

§Examples

use utp::UtpSocket;

let mut socket = UtpSocket::bind("127.0.0.1:1234").expect("Error binding socket");

let mut buf = [0; 1000];
let (amt, _src) = socket.recv_from(&mut buf).expect("Error receiving");

let mut buf = &mut buf[..amt];
buf.reverse();
let _ = socket.send_to(buf).expect("Error sending");

// Close the socket. You can either call `close` on the socket,
// explicitly drop it or just let it go out of scope.
socket.close();

Fields§

§max_retransmission_retries: u32

Maximum retransmission retries

Implementations§

Source§

impl UtpSocket

Source

pub fn bind_with_udp_socket(socket: UdpSocket) -> Result<UtpSocket>

Creates a new UTP socket from the given UDP socket.

Source

pub fn bind<A: ToSocketAddrs>(addr: A) -> Result<UtpSocket>

Creates a new UTP socket from the given address.

The address type can be any implementer of the ToSocketAddr trait. See its documentation for concrete examples.

If more than one valid address is specified, only the first will be used.

Source

pub fn local_addr(&self) -> Result<SocketAddr>

Returns the socket address that this socket was created from.

Source

pub fn peer_addr(&self) -> Result<SocketAddr>

Returns the socket address of the remote peer of this UTP connection.

Source

pub fn connect<A: ToSocketAddrs>(other: A) -> Result<UtpSocket>

Opens a connection to a remote host by hostname or IP address.

The address type can be any implementer of the ToSocketAddr trait. See its documentation for concrete examples.

If more than one valid address is specified, only the first will be used.

Source

pub fn rendezvous_connect<A: ToSocketAddrs>( udp_socket: UdpSocket, other: A, ) -> Result<UtpSocket>

If you have already prepared UDP sockets at each end (e.g. you’re doing hole punching), then the rendezvous connection setup is your choice.

Rendezvous connection will only use the specified socket and addresses, but each end must call rendezvous_connect itself.

This is an unofficial extension to the uTP protocol. Both peers will try to act as initiator and acceptor sockets. Then, the connection id which is numerically lower decides which end will assume which role (initiator or acceptor).

Source

pub fn close(&mut self) -> Result<()>

Gracefully closes connection to peer.

This method allows both peers to receive all packets still in flight.

Source

pub fn recv_from(&mut self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>

Receives data from socket.

On success, returns the number of bytes read and the sender’s address. Returns 0 bytes read after receiving a FIN packet when the remaining in-flight packets are consumed.

Source

pub fn set_read_timeout(&mut self, user_timeout: Option<u64>)

Changes read operations to block for at most the specified number of milliseconds.

Source

pub fn send_to(&mut self, buf: &[u8]) -> Result<usize>

Sends data on the socket to the remote peer. On success, returns the number of bytes written.

Source

pub fn flush(&mut self) -> Result<()>

Consumes acknowledgements for every pending packet.

Source

pub fn send_keepalive(&mut self)

Send a keepalive packet on the stream.

Trait Implementations§

Source§

impl Drop for UtpSocket

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Into<UtpStream> for UtpSocket

Source§

fn into(self) -> UtpStream

Converts this type into the (usually inferred) input type.

Auto Trait Implementations§

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> 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, 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.