Skip to content

Commit

Permalink
Remove unused SocketHolder class (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Sep 5, 2023
1 parent e66cf4e commit e28eb81
Showing 1 changed file with 1 addition and 84 deletions.
85 changes: 1 addition & 84 deletions src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,92 +6,10 @@ use std::net::{IpAddr, SocketAddr, TcpListener};
use std::os::unix::io::{AsRawFd, FromRawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket};
use std::time::Duration;

use socket2::{Domain, Protocol, Socket, TcpKeepalive, Type};
use socket2::{Domain, Protocol, Socket, Type};


#[pyclass(module="granian._granian")]
pub struct SocketHolder {
socket: Socket
}

#[pymethods]
impl SocketHolder {
#[cfg(unix)]
#[new]
pub fn new(fd: i32) -> PyResult<Self> {
let socket = unsafe {
Socket::from_raw_fd(fd)
};
Ok(Self { socket: socket })
}

#[cfg(windows)]
#[new]
pub fn new(fd: u64) -> PyResult<Self> {
let socket = unsafe {
Socket::from_raw_socket(fd)
};
Ok(Self { socket: 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 socket = Socket::new(Domain::IPV4, Type::STREAM, Some(Protocol::TCP))?;
socket.set_reuse_address(true)?;
socket.set_tcp_keepalive(
&TcpKeepalive::new().with_time(Duration::from_secs(0))
)?;
socket.set_nodelay(true)?;
socket.bind(&address.into())?;
socket.listen(backlog)?;
Ok(Self { socket: socket })
}

#[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 SocketHolder {
pub fn get_socket(&self) -> Socket {
self.socket.try_clone().unwrap()
}

pub fn get_listener(&self) -> TcpListener {
self.socket.try_clone().unwrap().into()
}
}

#[pyclass(module="granian._granian")]
pub struct ListenerHolder {
socket: TcpListener
Expand Down Expand Up @@ -169,7 +87,6 @@ impl ListenerHolder {

pub(crate) fn init_pymodule(module: &PyModule) -> PyResult<()> {
module.add_class::<ListenerHolder>()?;
module.add_class::<SocketHolder>()?;

Ok(())
}

0 comments on commit e28eb81

Please sign in to comment.