#[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
impl TcpSocket
Sourcepub fn new() -> Result<TcpSocket<Unbound>, SocketError>
pub fn new() -> Result<TcpSocket<Unbound>, SocketError>
Create a TCP socket
§Returns
A new TCP socket
§Errors
SocketError::ErrnoWithDescription
if the socket could not be created
Source§impl<S: SocketState> TcpSocket<S>
impl<S: SocketState> TcpSocket<S>
Sourcepub fn send_flags(&self) -> SocketSendFlags
pub fn send_flags(&self) -> SocketSendFlags
Flags used when sending data
Sourcepub fn set_send_flags(&mut self, send_flags: SocketSendFlags)
pub fn set_send_flags(&mut self, send_flags: SocketSendFlags)
Set the flags used when sending data
Sourcepub fn recv_flags(&self) -> SocketRecvFlags
pub fn recv_flags(&self) -> SocketRecvFlags
Flags used when receiving data
Sourcepub fn set_recv_flags(&mut self, recv_flags: SocketRecvFlags)
pub fn set_recv_flags(&mut self, recv_flags: SocketRecvFlags)
Set the flags used when receiving data
Source§impl TcpSocket<Unbound>
impl TcpSocket<Unbound>
Sourcepub fn connect(
self,
remote: SocketAddr,
) -> Result<TcpSocket<Connected>, SocketError>
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 successfulErr(String)
if the connection was unsuccessful.
§Errors
SocketError::UnsupportedAddressFamily
if the address family is not supported (only IPv4 is supported)- Any other
SocketError
if the connection was unsuccessful
Source§impl TcpSocket<Connected>
impl TcpSocket<Connected>
Sourcepub fn internal_read(&self, buf: &mut [u8]) -> Result<usize, SocketError>
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 readErr(SocketError)
if the read was unsuccessful.
§Errors
- A
SocketError
if the read was unsuccessful
§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.
Sourcepub fn internal_write(&mut self, buf: &[u8]) -> Result<usize, SocketError>
pub fn internal_write(&mut self, buf: &[u8]) -> Result<usize, SocketError>
Trait Implementations§
Source§impl<S: Clone + SocketState, B: Clone + SocketBuffer> Clone for TcpSocket<S, B>
impl<S: Clone + SocketState, B: Clone + SocketBuffer> Clone for TcpSocket<S, B>
Source§impl<S: Debug + SocketState, B: Debug + SocketBuffer> Debug for TcpSocket<S, B>
impl<S: Debug + SocketState, B: Debug + SocketBuffer> Debug for TcpSocket<S, B>
Source§impl<S: SocketState> ErrorType for TcpSocket<S>
impl<S: SocketState> ErrorType for TcpSocket<S>
Source§type Error = SocketError
type Error = SocketError
Source§impl<S: Hash + SocketState, B: Hash + SocketBuffer> Hash for TcpSocket<S, B>
impl<S: Hash + SocketState, B: Hash + SocketBuffer> Hash for TcpSocket<S, B>
Source§impl<S: SocketState> OptionType for TcpSocket<S>
impl<S: SocketState> OptionType for TcpSocket<S>
type Options<'a> = SocketOptions
Source§impl<S: PartialEq + SocketState, B: PartialEq + SocketBuffer> PartialEq for TcpSocket<S, B>
impl<S: PartialEq + SocketState, B: PartialEq + SocketBuffer> PartialEq for TcpSocket<S, B>
Source§impl Read for TcpSocket<Connected>
impl Read for TcpSocket<Connected>
Source§fn read<'m>(&'m mut self, buf: &'m mut [u8]) -> Result<usize, Self::Error>
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 readErr(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>>
fn read_exact( &mut self, buf: &mut [u8], ) -> Result<(), ReadExactError<Self::Error>>
buf
. Read moreSource§impl Write for TcpSocket<Connected>
impl Write for TcpSocket<Connected>
Source§fn write<'m>(&'m mut self, buf: &'m [u8]) -> Result<usize, Self::Error>
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