-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): allow custom implementations of listener (#1217)
* feat(server): allow custom implementations of listener * feat(listen): directly use Arc<dyn, remove enum for listener * feat(listen): hide listen dyn requirement at most at possible * feat(listener): remove listen implement for box<listendyn>
- Loading branch information
Showing
6 changed files
with
79 additions
and
64 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
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 |
---|---|---|
@@ -1,43 +1,51 @@ | ||
use std::io; | ||
|
||
#[cfg(feature = "quic")] | ||
use xitca_io::net::QuicListenerBuilder; | ||
use xitca_io::net::{QuicListener, QuicListenerBuilder}; | ||
#[cfg(unix)] | ||
use xitca_io::net::UnixListener; | ||
use xitca_io::net::{Listener, TcpListener}; | ||
use xitca_io::net::{Listen, TcpListener}; | ||
|
||
use tracing::info; | ||
|
||
/// Helper trait for converting listener types and register them to xitca-server | ||
/// By delay the conversion and make the process happen in server thread(s) it avoid possible panic due to runtime locality. | ||
pub trait IntoListener: Send { | ||
fn into_listener(self) -> io::Result<Listener>; | ||
type Listener: Listen; | ||
|
||
fn into_listener(self) -> io::Result<Self::Listener>; | ||
} | ||
|
||
impl IntoListener for std::net::TcpListener { | ||
fn into_listener(self) -> io::Result<Listener> { | ||
type Listener = TcpListener; | ||
|
||
fn into_listener(self) -> io::Result<Self::Listener> { | ||
self.set_nonblocking(true)?; | ||
let listener = TcpListener::from_std(self)?; | ||
info!("Started Tcp listening on: {:?}", listener.local_addr().ok()); | ||
Ok(Listener::Tcp(listener)) | ||
Ok(listener) | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
impl IntoListener for std::os::unix::net::UnixListener { | ||
fn into_listener(self) -> io::Result<Listener> { | ||
type Listener = UnixListener; | ||
|
||
fn into_listener(self) -> io::Result<Self::Listener> { | ||
self.set_nonblocking(true)?; | ||
let listener = UnixListener::from_std(self)?; | ||
info!("Started Unix listening on: {:?}", listener.local_addr().ok()); | ||
Ok(Listener::Unix(listener)) | ||
Ok(listener) | ||
} | ||
} | ||
|
||
#[cfg(feature = "quic")] | ||
impl IntoListener for QuicListenerBuilder { | ||
fn into_listener(self) -> io::Result<Listener> { | ||
type Listener = QuicListener; | ||
|
||
fn into_listener(self) -> io::Result<Self::Listener> { | ||
let udp = self.build()?; | ||
info!("Started Udp listening on: {:?}", udp.endpoint().local_addr().ok()); | ||
Ok(Listener::Udp(udp)) | ||
Ok(udp) | ||
} | ||
} |
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