-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use typestate pattern for UdpSocket and TcpSocket (#17)
* wip * wip * feat: implement typestate pattern for UdpSocket and TcpSocket * feat: implement socket file descriptor abstraction That removes the need for a custom drop for upd and tcp sockets * chore: add .clippy.toml configuration * chore: clippy configuration * style: implement clippy suggestions * refactor: remove need for custom drop impl
- Loading branch information
1 parent
b50abab
commit a13df10
Showing
15 changed files
with
393 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
doc-valid-idents = ["PlayStation", ".."] | ||
|
||
allowed-prefixes = ["To", ".."] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use core::ops::Deref; | ||
|
||
use alloc::rc::Rc; | ||
use psp::sys; | ||
|
||
/// Raw socket file descriptor | ||
/// | ||
/// This is a wrapper around a raw socket file descriptor, which | ||
/// takes care of closing it when no other references to it exist. | ||
/// | ||
/// # Notes | ||
/// The drop implementation of this type calls the close syscall. | ||
/// Closing via drop is best-effort as of now (errors are ignored). | ||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub(crate) struct RawSocketFileDescriptor(pub(crate) i32); | ||
|
||
impl Drop for RawSocketFileDescriptor { | ||
fn drop(&mut self) { | ||
unsafe { | ||
sys::sceNetInetClose(self.0); | ||
}; | ||
} | ||
} | ||
|
||
impl Deref for RawSocketFileDescriptor { | ||
type Target = i32; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
/// Socket file descriptor | ||
/// | ||
/// This is a wrapper around a raw socket file descriptor, which | ||
/// takes care of closing it when no other references to it exist. | ||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
pub struct SocketFileDescriptor(pub(crate) Rc<RawSocketFileDescriptor>); | ||
|
||
impl SocketFileDescriptor { | ||
pub(crate) fn new(fd: i32) -> Self { | ||
Self(Rc::new(RawSocketFileDescriptor(fd))) | ||
} | ||
} | ||
|
||
impl Deref for SocketFileDescriptor { | ||
type Target = i32; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use core::fmt::Debug; | ||
|
||
/// Trait describing the state of a socket | ||
pub trait SocketState: Debug {} | ||
|
||
/// Socket is in an unbound state | ||
#[derive(Debug)] | ||
pub struct Unbound; | ||
impl SocketState for Unbound {} | ||
|
||
/// Socket is in a bound state | ||
#[derive(Debug)] | ||
pub struct Bound; | ||
impl SocketState for Bound {} | ||
|
||
/// Socket is in a connected state | ||
#[derive(Debug)] | ||
pub struct Connected; | ||
impl SocketState for Connected {} | ||
|
||
#[derive(Debug)] | ||
pub struct NotReady; | ||
impl SocketState for NotReady {} | ||
|
||
#[derive(Debug)] | ||
pub struct Ready; | ||
impl SocketState for Ready {} |
Oops, something went wrong.