Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tokio based async support #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

tokio based async support #3

wants to merge 6 commits into from

Conversation

aav
Copy link
Contributor

@aav aav commented Jan 6, 2020

Meanwhile, I created a prototype for tokio/async support. It seems to work in my setup (in combination with rust-async-coap). But it's far from being final.

What do you think?

Copy link
Owner

@TimonPost TimonPost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! I love this idea.

  • Do we want to stick to tokio, what about async-std.
  • Can you put the working example into the examples folder?


[features]
vendored = ["openssl/vendored"]
async = ["futures", "tokio", "tokio-openssl"]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this to be an option. Great!

@@ -75,12 +75,12 @@ impl DtlsAcceptor {
pub fn accept<S: fmt::Debug>(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we support this builder to work for async?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is in progress

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done and pushed to the same branch


#[cfg(feature="async")]
#[derive(Debug)]
pub enum AsyncConnectError<S> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this over to error.rs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left it here, for a moment by purpose. The way it is now is not too elegant, and I wanted to ask you for an opinion. Actually, I can see three alternatives here:

  1. move AsyncConnectError to error.rs and let it be like this
  2. extend error::Error with another choice tokio_openssl::HandshakeError
  3. change both connect / async_connect error option to something like Box so it will be able to accommodate anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved AsyncConnectError to error.rs, although alternatives mentioned above are still valid

fn from(stream: SslStream<S>) -> Self {
DtlsStream(stream)
}
}


pub trait DtlsStreamExt<S> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this idea

@@ -9,81 +9,33 @@ use std::{fmt, io};
/// and both the server and the client are ready for receiving and sending
/// data. Bytes read from a `DtlsStream` are decrypted from `S` and bytes written
/// to a `DtlsStream` are encrypted when passing through to `S`.
pub struct DtlsStream<S>(ssl::SslStream<S>);
pub struct DtlsStream<C>(pub(crate) C);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to make this public since we have the SyncStream now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as I undertsand - yes

@@ -35,9 +35,9 @@ where
/// This corresponds to [`SSL_do_handshake`].
///
/// [`SSL_do_handshake`]: https://www.openssl.org/docs/manmaster/man3/SSL_do_handshake.html
pub fn handshake(self) -> Result<DtlsStream<S>, HandshakeError<S>> {
pub fn handshake(self) -> Result<SyncDtlsStream<S>, HandshakeError<S>> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though, how are we going to use this for async now?

Copy link
Contributor Author

@aav aav Jan 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ssl::HandshakeError::WouldBlock is properly handled inside tokio_openssl, and nothing has to be done in the user code


use crate::{DtlsStream, DtlsStreamExt};

pub type AsyncDtlsStream<S> = DtlsStream<AsyncSslStream<S>>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AsyncDtlsStream and SyncDtlsStream are the same types until used in UdpChannel. Maybe we do not have to create separate type definitions for those?

@aav
Copy link
Contributor Author

aav commented Jan 10, 2020

  • I will explore more if it will be possible to support both tokio and async-std
  • support for acceptors, examples and yet to come. What is in there, is just the initial version.

@TimonPost
Copy link
Owner

If we implement futures::Stream for some stream, then the user is able to use either async-std or tokio. We do not have to support the runtime right?

@aav
Copy link
Contributor Author

aav commented Jan 21, 2020

async example is now in examples/

@aav
Copy link
Contributor Author

aav commented Jan 21, 2020

Right now, it would be difficult to get rid of tokio dependency because we strongly depend on tokio-openssl, which, in turn, depends on tokio. This dependency is not very significant, since the only things it takes from tokio are AsyncRead, AsyncWrite, which have their counterparties in futures::io.

Perhaps it should be possible to convince the tokio-openssl author to make his library more neutral.

@aav
Copy link
Contributor Author

aav commented Jan 21, 2020

aha, this is was already discussed. So I would wait until futures-openssl will be available.

tokio-rs/tokio-openssl#12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants