-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom service connectors
- Loading branch information
Showing
7 changed files
with
202 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::{future::Future, io, net::SocketAddr}; | ||
|
||
use tokio::{ | ||
io::{AsyncRead, AsyncWrite}, | ||
net::TcpStream, | ||
}; | ||
|
||
use crate::net::raw::ether::MacAddr; | ||
use crate::svc_table::ServiceType; | ||
|
||
/// Service connection. | ||
pub trait ServiceConnection: AsyncRead + AsyncWrite {} | ||
|
||
impl<T> ServiceConnection for T where T: AsyncRead + AsyncWrite {} | ||
|
||
/// Service connector. | ||
#[trait_variant::make(Send)] | ||
pub trait ServiceConnector { | ||
type Connection: ServiceConnection; | ||
|
||
/// Connect to a given service. | ||
async fn connect( | ||
&self, | ||
svc_type: ServiceType, | ||
mac: MacAddr, | ||
addr: SocketAddr, | ||
) -> io::Result<Self::Connection>; | ||
} | ||
|
||
/// Default service connector. | ||
#[derive(Default, Copy, Clone)] | ||
pub struct DefaultServiceConnector(()); | ||
|
||
impl DefaultServiceConnector { | ||
/// Create a new service connector. | ||
#[inline] | ||
pub const fn new() -> Self { | ||
Self(()) | ||
} | ||
} | ||
|
||
impl ServiceConnector for DefaultServiceConnector { | ||
type Connection = TcpStream; | ||
|
||
#[inline] | ||
fn connect( | ||
&self, | ||
_: ServiceType, | ||
_: MacAddr, | ||
addr: SocketAddr, | ||
) -> impl Future<Output = io::Result<Self::Connection>> { | ||
TcpStream::connect(addr) | ||
} | ||
} |
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
Oops, something went wrong.