Struct nex_socket::Socket

source ·
pub struct Socket { /* private fields */ }
Expand description

Socket. Provides cross-platform adapter for system socket.

Implementations§

source§

impl Socket

source

pub fn new(socket_option: SocketOption) -> Result<Socket>

Constructs a new Socket.

source

pub fn bind(&self, addr: SocketAddr) -> Result<()>

Bind socket to address.

source

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

Send packet.

source

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

Send packet to target.

source

pub fn receive(&self, buf: &mut Vec<u8>) -> Result<usize>

Receive packet.

source

pub fn receive_from(&self, buf: &mut Vec<u8>) -> Result<(usize, SocketAddr)>

Receive packet with sender address.

source

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

Write data to the socket and send to the target. Return how many bytes were written.

source

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

Attempts to write an entire buffer into this writer.

source

pub fn read(&self, buf: &mut Vec<u8>) -> Result<usize>

Read data from the socket. Return how many bytes were read.

source

pub fn read_to_end(&self, buf: &mut Vec<u8>) -> Result<usize>

Read all bytes until EOF in this source, placing them into buf.

source

pub fn read_to_end_timeout( &self, buf: &mut Vec<u8>, timeout: Duration ) -> Result<usize>

Read all bytes until EOF in this source, placing them into buf. This ignore io::Error on read_to_end because it is expected when reading response. If no response is received, and io::Error is occurred, return Err.

source

pub fn ttl(&self, ip_version: IpVersion) -> Result<u32>

Get TTL or Hop Limit.

source

pub fn set_ttl(&self, ttl: u32, ip_version: IpVersion) -> Result<()>

Set TTL or Hop Limit.

source

pub fn tos(&self) -> Result<u32>

Get the value of the IP_TOS option for this socket.

source

pub fn set_tos(&self, tos: u32) -> Result<()>

Set the value of the IP_TOS option for this socket.

source

pub fn receive_tos(&self) -> Result<bool>

Get the value of the IP_RECVTOS option for this socket.

source

pub fn set_receive_tos(&self, receive_tos: bool) -> Result<()>

Set the value of the IP_RECVTOS option for this socket.

source

pub fn connect(&self, addr: &SocketAddr) -> Result<()>

Initiate TCP connection.

source

pub fn connect_timeout( &self, addr: &SocketAddr, timeout: Duration ) -> Result<()>

Initiate a connection on this socket to the specified address, only only waiting for a certain period of time for the connection to be established. The non-blocking state of the socket is overridden by this function.

source

pub fn listen(&self, backlog: i32) -> Result<()>

Listen TCP connection.

source

pub fn accept(&self) -> Result<(Socket, SocketAddr)>

Accept TCP connection.

source

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

Get local address.

source

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

Get peer address.

source

pub fn socket_type(&self) -> Result<SocketType>

Get type of the socket.

source

pub fn try_clone(&self) -> Result<Socket>

Create a new socket with the same configuration and bound to the same address.

source

pub fn is_nonblocking(&self) -> Result<bool>

Returns true if this socket is set to nonblocking mode, false otherwise.

source

pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>

Set non-blocking mode.

source

pub fn shutdown(&self, how: Shutdown) -> Result<()>

Shutdown TCP connection.

source

pub fn is_broadcast(&self) -> Result<bool>

Get the value of the SO_BROADCAST option for this socket.

source

pub fn set_broadcast(&self, broadcast: bool) -> Result<()>

Set the value of the SO_BROADCAST option for this socket.

When enabled, this socket is allowed to send packets to a broadcast address.

source

pub fn get_error(&self) -> Result<Option<Error>>

Get the value of the SO_ERROR option on this socket.

source

pub fn keepalive(&self) -> Result<bool>

Get the value of the SO_KEEPALIVE option on this socket.

source

pub fn set_keepalive(&self, keepalive: bool) -> Result<()>

Set value for the SO_KEEPALIVE option on this socket.

Enable sending of keep-alive messages on connection-oriented sockets.

source

pub fn linger(&self) -> Result<Option<Duration>>

Get the value of the SO_LINGER option on this socket.

source

pub fn set_linger(&self, dur: Option<Duration>) -> Result<()>

Set value for the SO_LINGER option on this socket.

source

pub fn receive_buffer_size(&self) -> Result<usize>

Get the value of the SO_RCVBUF option on this socket.

source

pub fn set_receive_buffer_size(&self, size: usize) -> Result<()>

Set value for the SO_RCVBUF option on this socket.

Changes the size of the operating system’s receive buffer associated with the socket.

source

pub fn receive_timeout(&self) -> Result<Option<Duration>>

Get value for the SO_RCVTIMEO option on this socket.

source

pub fn set_receive_timeout(&self, duration: Option<Duration>) -> Result<()>

Set value for the SO_RCVTIMEO option on this socket.

source

pub fn reuse_address(&self) -> Result<bool>

Get value for the SO_REUSEADDR option on this socket.

source

pub fn set_reuse_address(&self, reuse: bool) -> Result<()>

Set value for the SO_REUSEADDR option on this socket.

This indicates that futher calls to bind may allow reuse of local addresses.

source

pub fn send_buffer_size(&self) -> Result<usize>

Get value for the SO_SNDBUF option on this socket.

source

pub fn set_send_buffer_size(&self, size: usize) -> Result<()>

Set value for the SO_SNDBUF option on this socket.

Changes the size of the operating system’s send buffer associated with the socket.

source

pub fn send_timeout(&self) -> Result<Option<Duration>>

Get value for the SO_SNDTIMEO option on this socket.

source

pub fn set_send_timeout(&self, duration: Option<Duration>) -> Result<()>

Set value for the SO_SNDTIMEO option on this socket.

If timeout is None, then write and send calls will block indefinitely.

source

pub fn is_ip_header_included(&self) -> Result<bool>

Get the value of the IP_HDRINCL option on this socket.

source

pub fn set_ip_header_included(&self, include: bool) -> Result<()>

Set the value of the IP_HDRINCL option on this socket.

source

pub fn nodelay(&self) -> Result<bool>

Get the value of the TCP_NODELAY option on this socket.

source

pub fn set_nodelay(&self, nodelay: bool) -> Result<()>

Set the value of the TCP_NODELAY option on this socket.

If set, segments are always sent as soon as possible, even if there is only a small amount of data.

source

pub fn into_tcp_stream(self) -> Result<TcpStream>

Get TCP Stream This function will consume the socket and return a new std::net::TcpStream.

source

pub fn into_tcp_listener(self) -> Result<TcpListener>

Get TCP Listener This function will consume the socket and return a new std::net::TcpListener.

source

pub fn into_udp_socket(self) -> Result<UdpSocket>

Get UDP Socket This function will consume the socket and return a new std::net::UdpSocket.

Trait Implementations§

source§

impl Clone for Socket

source§

fn clone(&self) -> Socket

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Socket

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Socket

§

impl RefUnwindSafe for Socket

§

impl Send for Socket

§

impl Sync for Socket

§

impl Unpin for Socket

§

impl UnwindSafe for Socket

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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

§

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

§

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

§

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<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more