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

WIP: Simple connect function #1250

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion quinn-proto/src/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
collections::{HashMap, VecDeque},
convert::TryFrom,
fmt, iter,
fmt, io, iter,
net::{IpAddr, SocketAddr},
ops::{Index, IndexMut},
sync::Arc,
Expand Down Expand Up @@ -844,6 +844,19 @@ pub enum ConnectError {
UnsupportedVersion,
}

// For compatibility with API consumers
impl From<ConnectError> for io::Error {
fn from(x: ConnectError) -> io::Error {
use ConnectError::*;
let kind = match x {
InvalidDnsName(_) | InvalidRemoteAddress(_) => io::ErrorKind::InvalidData,
EndpointStopping | TooManyConnections | NoDefaultClientConfig => io::ErrorKind::Other,
UnsupportedVersion => io::ErrorKind::InvalidInput,
};
io::Error::new(kind, x)
}
}

/// Reset Tokens which are associated with peer socket addresses
///
/// The standard `HashMap` is used since both `SocketAddr` and `ResetToken` are
Expand Down
21 changes: 21 additions & 0 deletions quinn/src/connect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::{
io,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
};

use crate::{ClientConfig, Connecting, Endpoint};

/// Connect to `server_address`, authenticating it as `server_name`, using a new endpoint
Copy link
Member

Choose a reason for hiding this comment

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

Seems reasonable, though I don't think it should live in its own module. Maybe an associated method for ClientConfig?

pub fn connect(
config: ClientConfig,
server_address: SocketAddr,
server_name: &str,
) -> io::Result<Connecting> {
let bind_addr = match server_address {
SocketAddr::V6(_) => IpAddr::from(Ipv6Addr::UNSPECIFIED),
SocketAddr::V4(_) => IpAddr::from(Ipv4Addr::UNSPECIFIED),
};
let endpoint = Endpoint::client(SocketAddr::new(bind_addr, 0))?;
let fut = endpoint.connect_with(config, server_address, server_name)?;
Ok(fut)
}
2 changes: 2 additions & 0 deletions quinn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use std::time::Duration;

mod broadcast;
mod connect;
mod connection;
mod endpoint;
mod mutex;
Expand All @@ -55,6 +56,7 @@ pub use proto::{
Transmit, TransportConfig, VarInt,
};

pub use crate::connect::connect;
pub use crate::connection::{
Connecting, Connection, Datagrams, IncomingBiStreams, IncomingUniStreams, NewConnection,
OpenBi, OpenUni, SendDatagramError, ZeroRttAccepted,
Expand Down