Struct TcpSocket

Source
#[repr(C)]
pub struct TcpSocket<S: SocketState = Unbound, B: SocketBuffer = Vec<u8>> { /* private fields */ }
Expand description

A TCP socket

§Safety

This is a wrapper around a raw socket file descriptor.

The socket is closed when the struct is dropped. Closing via drop is best-effort.

§Notes

The structure implements EasySocket. This allows you to interact with the socket using a simplified API. However, you are still free to use it like a normal Linux socket like you would do in C.

Using it as an easy socket allows you to use it in the following way:

use psp::net::TcpSocket;

let socket = TcpSocket::new().unwrap();
let socket_options = SocketOptions{ remote: addr };
let socket = socket.open(socket_options).unwrap();
socket.write(b"hello world").unwrap();
socket.flush().unwrap();
// no need to call close, as drop will do it

Implementations§

Source§

impl TcpSocket

Source

pub fn new() -> Result<TcpSocket<Unbound>, SocketError>

Create a TCP socket

§Returns

A new TCP socket

§Errors
Source§

impl<S: SocketState> TcpSocket<S>

Source

pub fn fd(&self) -> i32

Return the underlying socket’s file descriptor

Source

pub fn send_flags(&self) -> SocketSendFlags

Flags used when sending data

Source

pub fn set_send_flags(&mut self, send_flags: SocketSendFlags)

Set the flags used when sending data

Source

pub fn recv_flags(&self) -> SocketRecvFlags

Flags used when receiving data

Source

pub fn set_recv_flags(&mut self, recv_flags: SocketRecvFlags)

Set the flags used when receiving data

Source§

impl TcpSocket<Unbound>

Source

pub fn connect( self, remote: SocketAddr, ) -> Result<TcpSocket<Connected>, SocketError>

Connect to a remote host

§Parameters
  • remote: The remote host to connect to
§Returns
  • Ok(()) if the connection was successful
  • Err(String) if the connection was unsuccessful.
§Errors
Source§

impl TcpSocket<Connected>

Source

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

Read from the socket

# Returns

  • Ok(usize) if the read was successful. The number of bytes read
  • Err(SocketError) if the read was unsuccessful.
§Errors
§Notes

“Low level” read function. Read data from the socket and store it in the buffer. This should not be used if you want to use this socket EasySocket style.

Source

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

Write to the socket

§Errors

Trait Implementations§

Source§

impl<S: Clone + SocketState, B: Clone + SocketBuffer> Clone for TcpSocket<S, B>

Source§

fn clone(&self) -> TcpSocket<S, B>

Returns a duplicate 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<S: Debug + SocketState, B: Debug + SocketBuffer> Debug for TcpSocket<S, B>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<S: SocketState> ErrorType for TcpSocket<S>

Source§

type Error = SocketError

Error type of all the IO operations on this type.
Source§

impl<S: Hash + SocketState, B: Hash + SocketBuffer> Hash for TcpSocket<S, B>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Open<'_, '_> for TcpSocket<Unbound>

Source§

fn open(self, options: &Self::Options<'_>) -> Result<Self::Return, Self::Error>
where Self: Sized,

Return a TCP socket connected to the remote specified in options

Source§

type Return = TcpSocket<Connected>

Source§

impl<S: SocketState> OptionType for TcpSocket<S>

Source§

impl<S: PartialEq + SocketState, B: PartialEq + SocketBuffer> PartialEq for TcpSocket<S, B>

Source§

fn eq(&self, other: &TcpSocket<S, B>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Read for TcpSocket<Connected>

Source§

fn read<'m>(&'m mut self, buf: &'m mut [u8]) -> Result<usize, Self::Error>

Read from the socket

§Parameters
  • buf: The buffer where the read data will be stored

# Returns

  • Ok(usize) if the read was successful. The number of bytes read
  • Err(SocketError) if the read was unsuccessful.
§Errors
  • [SocketError::NotConnected] if the socket is not connected
  • A SocketError if the read was unsuccessful
Source§

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

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

impl Write for TcpSocket<Connected>

Source§

fn write<'m>(&'m mut self, buf: &'m [u8]) -> Result<usize, Self::Error>

Write to the socket

§Errors
  • [SocketError::NotConnected] if the socket is not connected
  • A SocketError if the write was unsuccessful
Source§

fn flush(&mut self) -> Result<(), SocketError>

Flush the socket

§Errors
Source§

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

Write an entire buffer into this writer. Read more
Source§

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

Write a formatted string into this writer, returning any error encountered. Read more
Source§

impl EasySocket for TcpSocket<Connected>

Source§

impl<S: Eq + SocketState, B: Eq + SocketBuffer> Eq for TcpSocket<S, B>

Source§

impl<S: SocketState, B: SocketBuffer> StructuralPartialEq for TcpSocket<S, B>

Auto Trait Implementations§

§

impl<S, B> Freeze for TcpSocket<S, B>
where B: Freeze,

§

impl<S, B> RefUnwindSafe for TcpSocket<S, B>

§

impl<S = Unbound, B = Vec<u8>> !Send for TcpSocket<S, B>

§

impl<S = Unbound, B = Vec<u8>> !Sync for TcpSocket<S, B>

§

impl<S, B> Unpin for TcpSocket<S, B>
where B: Unpin, S: Unpin,

§

impl<S, B> UnwindSafe for TcpSocket<S, B>
where B: UnwindSafe, S: UnwindSafe,

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