psp_net/socket/
tcp.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#![allow(clippy::module_name_repetitions)]

use alloc::vec::Vec;
use embedded_io::{ErrorType, Read, Write};

use core::net::SocketAddr;
use psp::sys;

use core::ffi::c_void;

use crate::traits::io::{EasySocket, Open, OptionType};
use crate::traits::SocketBuffer;
use crate::types::{SocketOptions, SocketRecvFlags, SocketSendFlags};

use super::super::netc;

use super::error::SocketError;
use super::sce::SocketFileDescriptor;
use super::state::{Connected, SocketState, Unbound};
use super::ToSockaddr;

/// 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:
/// ```no_run
/// 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
/// ```
#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TcpSocket<S: SocketState = Unbound, B: SocketBuffer = Vec<u8>> {
    /// The socket file descriptor
    pub(super) fd: SocketFileDescriptor,
    /// The buffer to store data to send
    buffer: B,
    /// flags for send calls
    send_flags: SocketSendFlags,
    /// flags for recv calls
    recv_flags: SocketRecvFlags,
    /// marker for the socket state
    _marker: core::marker::PhantomData<S>,
}

impl TcpSocket {
    /// Create a TCP socket
    ///
    /// # Returns
    /// A new TCP socket
    ///
    /// # Errors
    /// - [`SocketError::Errno`] if the socket could not be created
    #[allow(dead_code)]
    pub fn new() -> Result<TcpSocket<Unbound>, SocketError> {
        let fd = unsafe { sys::sceNetInetSocket(i32::from(netc::AF_INET), netc::SOCK_STREAM, 0) };
        if fd < 0 {
            Err(SocketError::Errno(unsafe { sys::sceNetInetGetErrno() }))
        } else {
            let fd = SocketFileDescriptor::new(fd);
            Ok(TcpSocket {
                fd,
                buffer: Vec::with_capacity(0),
                send_flags: SocketSendFlags::empty(),
                recv_flags: SocketRecvFlags::empty(),
                _marker: core::marker::PhantomData,
            })
        }
    }
}

impl<S: SocketState> TcpSocket<S> {
    /// Return the underlying socket's file descriptor
    #[must_use]
    pub fn fd(&self) -> i32 {
        *self.fd
    }

    /// Flags used when sending data
    #[must_use]
    pub fn send_flags(&self) -> SocketSendFlags {
        self.send_flags
    }

    /// Set the flags used when sending data
    pub fn set_send_flags(&mut self, send_flags: SocketSendFlags) {
        self.send_flags = send_flags;
    }

    /// Flags used when receiving data
    #[must_use]
    pub fn recv_flags(&self) -> SocketRecvFlags {
        self.recv_flags
    }

    /// Set the flags used when receiving data
    pub fn set_recv_flags(&mut self, recv_flags: SocketRecvFlags) {
        self.recv_flags = recv_flags;
    }
}

impl TcpSocket<Unbound> {
    #[must_use]
    fn transition(self) -> TcpSocket<Connected> {
        TcpSocket {
            fd: self.fd,
            buffer: Vec::default(),
            send_flags: self.send_flags,
            recv_flags: self.recv_flags,
            _marker: core::marker::PhantomData,
        }
    }

    /// 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
    /// - [`SocketError::UnsupportedAddressFamily`] if the address family is not supported (only IPv4 is supported)
    /// - Any other [`SocketError`] if the connection was unsuccessful
    pub fn connect(self, remote: SocketAddr) -> Result<TcpSocket<Connected>, SocketError> {
        match remote {
            SocketAddr::V4(v4) => {
                let sockaddr = v4.to_sockaddr();

                if unsafe {
                    sys::sceNetInetConnect(
                        *self.fd,
                        &sockaddr,
                        core::mem::size_of::<netc::sockaddr_in>() as u32,
                    )
                } < 0
                {
                    let errno = unsafe { sys::sceNetInetGetErrno() };
                    Err(SocketError::Errno(errno))
                } else {
                    Ok(self.transition())
                }
            }
            SocketAddr::V6(_) => Err(SocketError::UnsupportedAddressFamily),
        }
    }
}

impl TcpSocket<Connected> {
    /// 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
    /// - 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.
    pub fn internal_read(&self, buf: &mut [u8]) -> Result<usize, SocketError> {
        let result = unsafe {
            sys::sceNetInetRecv(
                *self.fd,
                buf.as_mut_ptr().cast::<c_void>(),
                buf.len(),
                self.recv_flags.as_i32(),
            )
        };
        if result < 0 {
            Err(SocketError::Errno(unsafe { sys::sceNetInetGetErrno() }))
        } else {
            Ok(result as usize)
        }
    }

    /// Write to the socket
    ///
    /// # Errors
    /// - A [`SocketError`] if the write was unsuccessful
    pub fn internal_write(&mut self, buf: &[u8]) -> Result<usize, SocketError> {
        self.buffer.append_buffer(buf);
        self.send()
    }

    fn internal_flush(&mut self) -> Result<(), SocketError> {
        while !self.buffer.is_empty() {
            self.send()?;
        }
        Ok(())
    }

    fn send(&mut self) -> Result<usize, SocketError> {
        let result = unsafe {
            sys::sceNetInetSend(
                *self.fd,
                self.buffer.as_slice().as_ptr().cast::<c_void>(),
                self.buffer.len(),
                self.send_flags.as_i32(),
            )
        };
        if result < 0 {
            Err(SocketError::Errno(unsafe { sys::sceNetInetGetErrno() }))
        } else {
            self.buffer.shift_left_buffer(result as usize);
            Ok(result as usize)
        }
    }
}

impl<S: SocketState> ErrorType for TcpSocket<S> {
    type Error = SocketError;
}

impl<S: SocketState> OptionType for TcpSocket<S> {
    type Options<'a> = SocketOptions;
}

impl Open<'_, '_> for TcpSocket<Unbound> {
    type Return = TcpSocket<Connected>;
    /// Return a TCP socket connected to the remote specified in `options`
    fn open(self, options: &'_ Self::Options<'_>) -> Result<Self::Return, Self::Error>
    where
        Self: Sized,
    {
        let socket = self.connect(options.remote())?;
        Ok(socket)
    }
}

impl Read for TcpSocket<Connected> {
    /// 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
    fn read<'m>(&'m mut self, buf: &'m mut [u8]) -> Result<usize, Self::Error> {
        self.internal_read(buf)
    }
}

impl Write for TcpSocket<Connected> {
    /// Write to the socket
    ///
    /// # Errors
    /// - [`SocketError::NotConnected`] if the socket is not connected
    /// - A [`SocketError`] if the write was unsuccessful
    fn write<'m>(&'m mut self, buf: &'m [u8]) -> Result<usize, Self::Error> {
        self.internal_write(buf)
    }

    /// Flush the socket
    ///
    /// # Errors
    /// - A [`SocketError`] if the flush was unsuccessful
    fn flush(&mut self) -> Result<(), SocketError> {
        self.internal_flush()
    }
}

impl EasySocket for TcpSocket<Connected> {}