reactors/io/
socket.rs

1#[cfg_attr(target_family = "windows", path = "socket/socket_win32.rs")]
2#[cfg_attr(target_family = "unix", path = "socket/socket_unix.rs")]
3mod socket;
4pub use socket::*;
5
6pub mod tcp;
7pub mod udp;
8
9pub mod sys {
10    use std::{io::Result, net::SocketAddr, task::Poll, time::Duration};
11
12    use crate::io::{IoReactor, RawFd};
13
14    /// System native socket interface.
15    pub trait Socket: Sized {
16        /// Create new system raw socket
17        fn socket(ip_v4: bool, sock_type: i32, protocol: i32) -> Result<RawFd>;
18
19        /// Create new raw tcp socket
20        fn tcp(ip_v4: bool) -> Result<RawFd>;
21        /// Create new raw udp socket
22        fn udp(ip_v4: bool) -> Result<RawFd>;
23
24        /// Bind socket to [`addr`](SocketAddr)
25        fn bind(fd: RawFd, addr: SocketAddr) -> Result<()>;
26
27        /// Stream socket start listen incoming connection.
28        fn listen(fd: RawFd) -> Result<()>;
29
30        /// Create new wrapper socket and bind to [`reactor`](IoReactor).
31        ///
32        /// If this method return an error, the implementation must release the input `fd` resource.
33        fn new(ip_v4: bool, fd: RawFd, reactor: IoReactor) -> Result<Self>;
34
35        /// Close native socket
36        fn close(&mut self);
37
38        /// Start an async connect operator.
39        fn poll_connect(
40            self: std::pin::Pin<&mut Self>,
41            cx: &mut std::task::Context<'_>,
42            remote: SocketAddr,
43            timeout: Option<Duration>,
44        ) -> Poll<Result<()>>;
45    }
46
47    /// Socket [`ReadBuffer`](crate::reactor::ReactorHandle::ReadBuffer)
48    pub enum ReadBuffer<'cx> {
49        Stream(&'cx mut [u8]),
50        Datagram(&'cx mut [u8], &'cx mut Option<SocketAddr>),
51
52        Accept(&'cx mut Option<RawFd>, &'cx mut Option<SocketAddr>),
53    }
54
55    /// Socket [`WriteBuffer`](crate::reactor::ReactorHandle::WriteBuffer)
56    pub enum WriteBuffer<'cx> {
57        Stream(&'cx [u8]),
58        Datagram(&'cx [u8], &'cx SocketAddr),
59    }
60}