-
-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathtcp.rs
More file actions
86 lines (72 loc) · 2.22 KB
/
tcp.rs
File metadata and controls
86 lines (72 loc) · 2.22 KB
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
use pyo3::prelude::*;
use pyo3::types::PyType;
use std::net::{IpAddr, SocketAddr, TcpListener};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket};
use socket2::{Domain, Protocol, Socket, Type};
#[pyclass(module = "granian._granian")]
pub struct ListenerHolder {
socket: TcpListener,
}
#[pymethods]
impl ListenerHolder {
#[cfg(unix)]
#[new]
pub fn new(fd: i32) -> Self {
let socket = unsafe { TcpListener::from_raw_fd(fd) };
Self { socket }
}
#[cfg(windows)]
#[new]
pub fn new(fd: u64) -> Self {
let socket = unsafe { TcpListener::from_raw_socket(fd) };
Self { socket }
}
#[classmethod]
pub fn from_address(_cls: &PyType, address: &str, port: u16, backlog: i32) -> PyResult<Self> {
let address: SocketAddr = (address.parse::<IpAddr>()?, port).into();
let domain = match address {
SocketAddr::V4(_) => Domain::IPV4,
SocketAddr::V6(_) => Domain::IPV6,
};
let socket = Socket::new(domain, Type::STREAM, Some(Protocol::TCP))?;
#[cfg(not(windows))]
{
socket.set_reuse_port(true)?;
}
socket.set_reuse_address(true)?;
socket.set_nodelay(true)?;
socket.bind(&address.into())?;
socket.listen(backlog)?;
Ok(Self { socket: socket.into() })
}
#[cfg(unix)]
pub fn __getstate__(&self, py: Python) -> PyObject {
let fd = self.socket.as_raw_fd();
(fd.into_py(py),).to_object(py)
}
#[cfg(windows)]
pub fn __getstate__(&self, py: Python) -> PyObject {
let fd = self.socket.as_raw_socket();
(fd.into_py(py),).to_object(py)
}
#[cfg(unix)]
pub fn get_fd(&self, py: Python) -> PyObject {
self.socket.as_raw_fd().into_py(py).to_object(py)
}
#[cfg(windows)]
pub fn get_fd(&self, py: Python) -> PyObject {
self.socket.as_raw_socket().into_py(py).to_object(py)
}
}
impl ListenerHolder {
pub fn get_clone(&self) -> TcpListener {
self.socket.try_clone().unwrap()
}
}
pub(crate) fn init_pymodule(module: &PyModule) -> PyResult<()> {
module.add_class::<ListenerHolder>()?;
Ok(())
}