-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from TheButlah/runtime-agnostic-codecs
Made code (mostly) runtime and transport independent
- Loading branch information
Showing
14 changed files
with
213 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//! General types and traits to facilitate compatibility across async runtimes | ||
use crate::codec::ZmqCodec; | ||
|
||
// We use dynamic dispatch to avoid complicated generics and simplify things | ||
type Inner = futures_codec::Framed<Box<dyn Frameable>, ZmqCodec>; | ||
|
||
// Enables us to have multiple bounds on the dyn trait in `InnerFramed` | ||
pub trait Frameable: futures::AsyncWrite + futures::AsyncRead + Unpin + Send {} | ||
impl<T> Frameable for T where T: futures::AsyncWrite + futures::AsyncRead + Unpin + Send {} | ||
|
||
/// Equivalent to [`futures_codec::Framed<T, ZmqCodec>`] or | ||
/// [`tokio_util::codec::Framed`] | ||
pub(crate) struct FramedIo(Inner); | ||
impl FramedIo { | ||
pub fn new(frameable: Box<dyn Frameable>) -> Self { | ||
let inner = futures_codec::Framed::new(frameable, ZmqCodec::new()); | ||
Self(inner) | ||
} | ||
} | ||
|
||
impl std::ops::Deref for FramedIo { | ||
type Target = Inner; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl std::ops::DerefMut for FramedIo { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} |
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
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
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 @@ | ||
pub mod tcp; |
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,30 @@ | ||
// TODO: Conditionally compile things | ||
mod tokio; | ||
|
||
use self::tokio as tk; | ||
use crate::codec::FramedIo; | ||
use crate::endpoint::{Endpoint, Host, Port}; | ||
use crate::ZmqResult; | ||
|
||
use std::net::SocketAddr; | ||
|
||
pub(crate) async fn connect(host: Host, port: Port) -> ZmqResult<(FramedIo, SocketAddr)> { | ||
tk::connect(host, port).await | ||
} | ||
|
||
/// Spawns an async task that listens for tcp connections at the provided | ||
/// address. | ||
/// | ||
/// `cback` will be invoked when a tcp connection is accepted. If the result was | ||
/// `Ok`, we get a tuple containing the framed raw socket, along with the ip | ||
/// address of the remote connection accepted. | ||
pub(crate) async fn begin_accept<T>( | ||
host: Host, | ||
port: Port, | ||
cback: impl Fn(ZmqResult<(FramedIo, SocketAddr)>) -> T + Send + 'static, | ||
) -> ZmqResult<(Endpoint, futures::channel::oneshot::Sender<bool>)> | ||
where | ||
T: std::future::Future<Output = ()> + Send + 'static, | ||
{ | ||
tk::begin_accept(host, port, cback).await | ||
} |
Oops, something went wrong.