Struct nex_socket::Socket
source · pub struct Socket { /* private fields */ }
Expand description
Socket. Provides cross-platform adapter for system socket.
Implementations§
source§impl Socket
impl Socket
sourcepub fn new(socket_option: SocketOption) -> Result<Socket>
pub fn new(socket_option: SocketOption) -> Result<Socket>
Constructs a new Socket.
sourcepub fn bind(&self, addr: SocketAddr) -> Result<()>
pub fn bind(&self, addr: SocketAddr) -> Result<()>
Bind socket to address.
sourcepub fn receive_from(&self, buf: &mut Vec<u8>) -> Result<(usize, SocketAddr)>
pub fn receive_from(&self, buf: &mut Vec<u8>) -> Result<(usize, SocketAddr)>
Receive packet with sender address.
sourcepub fn write(&self, buf: &[u8]) -> Result<usize>
pub fn write(&self, buf: &[u8]) -> Result<usize>
Write data to the socket and send to the target. Return how many bytes were written.
sourcepub fn write_all(&self, buf: &[u8]) -> Result<()>
pub fn write_all(&self, buf: &[u8]) -> Result<()>
Attempts to write an entire buffer into this writer.
sourcepub fn read(&self, buf: &mut Vec<u8>) -> Result<usize>
pub fn read(&self, buf: &mut Vec<u8>) -> Result<usize>
Read data from the socket. Return how many bytes were read.
sourcepub fn read_to_end(&self, buf: &mut Vec<u8>) -> Result<usize>
pub fn read_to_end(&self, buf: &mut Vec<u8>) -> Result<usize>
Read all bytes until EOF in this source, placing them into buf.
sourcepub fn read_to_end_timeout(
&self,
buf: &mut Vec<u8>,
timeout: Duration
) -> Result<usize>
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.
sourcepub fn set_tos(&self, tos: u32) -> Result<()>
pub fn set_tos(&self, tos: u32) -> Result<()>
Set the value of the IP_TOS option for this socket.
sourcepub fn receive_tos(&self) -> Result<bool>
pub fn receive_tos(&self) -> Result<bool>
Get the value of the IP_RECVTOS option for this socket.
sourcepub fn set_receive_tos(&self, receive_tos: bool) -> Result<()>
pub fn set_receive_tos(&self, receive_tos: bool) -> Result<()>
Set the value of the IP_RECVTOS option for this socket.
sourcepub fn connect(&self, addr: &SocketAddr) -> Result<()>
pub fn connect(&self, addr: &SocketAddr) -> Result<()>
Initiate TCP connection.
sourcepub fn connect_timeout(
&self,
addr: &SocketAddr,
timeout: Duration
) -> Result<()>
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.
sourcepub fn accept(&self) -> Result<(Socket, SocketAddr)>
pub fn accept(&self) -> Result<(Socket, SocketAddr)>
Accept TCP connection.
sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Get local address.
sourcepub fn peer_addr(&self) -> Result<SocketAddr>
pub fn peer_addr(&self) -> Result<SocketAddr>
Get peer address.
sourcepub fn socket_type(&self) -> Result<SocketType>
pub fn socket_type(&self) -> Result<SocketType>
Get type of the socket.
sourcepub fn try_clone(&self) -> Result<Socket>
pub fn try_clone(&self) -> Result<Socket>
Create a new socket with the same configuration and bound to the same address.
sourcepub fn is_nonblocking(&self) -> Result<bool>
pub fn is_nonblocking(&self) -> Result<bool>
Returns true if this socket is set to nonblocking mode, false otherwise.
sourcepub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
Set non-blocking mode.
sourcepub fn is_broadcast(&self) -> Result<bool>
pub fn is_broadcast(&self) -> Result<bool>
Get the value of the SO_BROADCAST option for this socket.
sourcepub fn set_broadcast(&self, broadcast: bool) -> Result<()>
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.
sourcepub fn get_error(&self) -> Result<Option<Error>>
pub fn get_error(&self) -> Result<Option<Error>>
Get the value of the SO_ERROR
option on this socket.
sourcepub fn keepalive(&self) -> Result<bool>
pub fn keepalive(&self) -> Result<bool>
Get the value of the SO_KEEPALIVE
option on this socket.
sourcepub fn set_keepalive(&self, keepalive: bool) -> Result<()>
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.
sourcepub fn linger(&self) -> Result<Option<Duration>>
pub fn linger(&self) -> Result<Option<Duration>>
Get the value of the SO_LINGER option on this socket.
sourcepub fn set_linger(&self, dur: Option<Duration>) -> Result<()>
pub fn set_linger(&self, dur: Option<Duration>) -> Result<()>
Set value for the SO_LINGER option on this socket.
sourcepub fn receive_buffer_size(&self) -> Result<usize>
pub fn receive_buffer_size(&self) -> Result<usize>
Get the value of the SO_RCVBUF
option on this socket.
sourcepub fn set_receive_buffer_size(&self, size: usize) -> Result<()>
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.
sourcepub fn receive_timeout(&self) -> Result<Option<Duration>>
pub fn receive_timeout(&self) -> Result<Option<Duration>>
Get value for the SO_RCVTIMEO option on this socket.
sourcepub fn set_receive_timeout(&self, duration: Option<Duration>) -> Result<()>
pub fn set_receive_timeout(&self, duration: Option<Duration>) -> Result<()>
Set value for the SO_RCVTIMEO
option on this socket.
sourcepub fn reuse_address(&self) -> Result<bool>
pub fn reuse_address(&self) -> Result<bool>
Get value for the SO_REUSEADDR
option on this socket.
sourcepub fn set_reuse_address(&self, reuse: bool) -> Result<()>
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.
sourcepub fn send_buffer_size(&self) -> Result<usize>
pub fn send_buffer_size(&self) -> Result<usize>
Get value for the SO_SNDBUF
option on this socket.
sourcepub fn set_send_buffer_size(&self, size: usize) -> Result<()>
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.
sourcepub fn send_timeout(&self) -> Result<Option<Duration>>
pub fn send_timeout(&self) -> Result<Option<Duration>>
Get value for the SO_SNDTIMEO
option on this socket.
sourcepub fn set_send_timeout(&self, duration: Option<Duration>) -> Result<()>
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.
sourcepub fn is_ip_header_included(&self) -> Result<bool>
pub fn is_ip_header_included(&self) -> Result<bool>
Get the value of the IP_HDRINCL option on this socket.
sourcepub fn set_ip_header_included(&self, include: bool) -> Result<()>
pub fn set_ip_header_included(&self, include: bool) -> Result<()>
Set the value of the IP_HDRINCL
option on this socket.
sourcepub fn set_nodelay(&self, nodelay: bool) -> Result<()>
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.
sourcepub fn into_tcp_stream(self) -> Result<TcpStream>
pub fn into_tcp_stream(self) -> Result<TcpStream>
Get TCP Stream This function will consume the socket and return a new std::net::TcpStream.
sourcepub fn into_tcp_listener(self) -> Result<TcpListener>
pub fn into_tcp_listener(self) -> Result<TcpListener>
Get TCP Listener This function will consume the socket and return a new std::net::TcpListener.
sourcepub fn into_udp_socket(self) -> Result<UdpSocket>
pub fn into_udp_socket(self) -> Result<UdpSocket>
Get UDP Socket This function will consume the socket and return a new std::net::UdpSocket.