Struct axnet::TcpSocket

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

A TCP socket that provides POSIX-like APIs.

  • connect is for TCP clients.
  • bind, listen, and accept are for TCP servers.
  • Other methods are for both TCP clients and servers.

Implementations§

source§

impl TcpSocket

source

pub const fn new() -> Self

Creates a new TCP socket.

source

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

Returns the local address and port, or Err(NotConnected) if not connected.

source

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

Returns the remote address and port, or Err(NotConnected) if not connected.

source

pub fn is_nonblocking(&self) -> bool

Returns whether this socket is in nonblocking mode.

source

pub fn set_nonblocking(&self, nonblocking: bool)

Moves this TCP stream into or out of nonblocking mode.

This will result in read, write, recv and send operations becoming nonblocking, i.e., immediately returning from their calls. If the IO operation is successful, Ok is returned and no further action is required. If the IO operation could not be completed and needs to be retried, an error with kind Err(WouldBlock) is returned.

source

pub fn connect(&self, remote_addr: SocketAddr) -> AxResult

Connects to the given address and port.

The local port is generated automatically.

source

pub fn bind(&self, local_addr: SocketAddr) -> AxResult

Binds an unbound socket to the given address and port.

If the given port is 0, it generates one automatically.

It’s must be called before listen and accept.

source

pub fn listen(&self) -> AxResult

Starts listening on the bound address and port.

It’s must be called after bind and before accept.

source

pub fn accept(&self) -> AxResult<TcpSocket>

Accepts a new connection.

This function will block the calling thread until a new TCP connection is established. When established, a new TcpSocket is returned.

It’s must be called after bind and listen.

source

pub fn shutdown(&self) -> AxResult

Close the connection.

source

pub fn close(&mut self)

Close the transmit half of the tcp socket. It will call close() on smoltcp::socket::tcp::Socket. It should send FIN to remote half.

This function is for shutdown(fd, SHUT_WR) syscall.

It won’t change TCP state. It won’t affect unconnected sockets (listener).

source

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

Receives data from the socket, stores it in the given buffer.

source

pub fn recv_timeout(&self, buf: &mut [u8], ticks: u64) -> AxResult<usize>

Receives data from the socket, stores it in the given buffer.

It will return Err(Timeout) if expired.

source

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

Transmits data in the given buffer.

source

pub fn poll(&self) -> AxResult<PollState>

Whether the socket is readable or writable.

source

pub fn set_nagle_enabled(&self, enabled: bool) -> AxResult

To set the nagle algorithm enabled or not.

source

pub fn nagle_enabled(&self) -> bool

To get the nagle algorithm enabled or not.

source

pub fn with_socket<R>(&self, f: impl FnOnce(Option<&Socket<'_>>) -> R) -> R

To get the socket and call the given function.

If the socket is not connected, it will return None.

Or it will return the result of the given function.

source

pub fn with_socket_mut<R>( &mut self, f: impl FnOnce(Option<&mut Socket<'_>>) -> R ) -> R

To get the mutable socket and call the given function.

If the socket is not connected, it will return None.

Or it will return the result of the given function.

source§

impl TcpSocket

Private methods

source

pub fn is_connected(&self) -> bool

Whether the socket is connected.

Trait Implementations§

source§

impl Drop for TcpSocket

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Read for TcpSocket

source§

fn read(&mut self, buf: &mut [u8]) -> AxResult<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read.
source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), AxError>

Read the exact number of bytes required to fill buf.
source§

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

Read the exact number of bytes required to fill buf.
source§

impl Write for TcpSocket

source§

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

Write a buffer into this writer, returning how many bytes were written.
source§

fn flush(&mut self) -> AxResult

Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
source§

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

Attempts to write an entire buffer into this writer.
source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), AxError>

Writes a formatted string into this writer, returning any error encountered.
source§

impl Sync for TcpSocket

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

§

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.