Skip to content

Commit

Permalink
support creating TcpListener from std/tokio listeners or socket2 socket
Browse files Browse the repository at this point in the history
closes #405
  • Loading branch information
GlenDC committed Feb 8, 2025
1 parent 8c03fb8 commit bb55530
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ percent-encoding = "2.1"
pin-project-lite = "0.2.13"
rustls-pki-types = "^1"
proc-macro2 = "1.0"
socket2 = "0.5.8"
opentelemetry = { version = "0.27.0", default-features = false, features = [
"trace",
] }
Expand Down
1 change: 1 addition & 0 deletions rama-net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ rama-utils = { version = "0.2.0-alpha.7", path = "../rama-utils" }
rustls = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
sha2 = { workspace = true, optional = true }
socket2 = { workspace = true }
tokio = { workspace = true, features = ["macros", "fs", "io-std", "io-util", "net"] }
tracing = { workspace = true }
venndb = { workspace = true, optional = true }
Expand Down
3 changes: 3 additions & 0 deletions rama-net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ pub mod tls;

#[cfg(any(feature = "tls", feature = "http"))]
pub mod fingerprint;

#[cfg(any(windows, unix))]
pub use ::socket2 as socket;
43 changes: 43 additions & 0 deletions rama-tcp/src/server/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,49 @@ impl<S> TcpListener<S> {
}
}

impl From<TokioTcpListener> for TcpListener<()> {
fn from(value: TokioTcpListener) -> Self {
Self {
inner: value,
state: (),
}
}
}

#[cfg(any(windows, unix))]
impl TryFrom<rama_net::socket::Socket> for TcpListener<()> {
type Error = std::io::Error;

#[inline]
fn try_from(value: rama_net::socket::Socket) -> Result<Self, Self::Error> {
let listener = std::net::TcpListener::from(value);
listener.try_into()
}
}

impl TryFrom<std::net::TcpListener> for TcpListener<()> {
type Error = std::io::Error;

fn try_from(value: std::net::TcpListener) -> Result<Self, Self::Error> {
value.set_nonblocking(true)?;
Ok(Self {
inner: TokioTcpListener::from_std(value)?,
state: (),
})
}
}

impl TcpListener<()> {
/// Define the TcpListener's state after it was created,
/// useful in case it wasn't built using the builder.
pub fn with_state<S>(self, state: S) -> TcpListener<S> {
TcpListener {
inner: self.inner,
state,
}
}
}

impl<State> TcpListener<State>
where
State: Clone + Send + Sync + 'static,
Expand Down

0 comments on commit bb55530

Please sign in to comment.