mirror of
https://github.com/n0-computer/noq.git
synced 2026-11-22 08:12:17 +00:00
Draft tokio bindings
This commit is contained in:
@@ -22,3 +22,6 @@ openssl = { version = "0.10.6", features = ["v111"] }
|
||||
slog-term = "2"
|
||||
assert_matches = "1.1"
|
||||
hex-literal = "0.1.1"
|
||||
|
||||
[workspace]
|
||||
members = ["quicr-tokio"]
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
[package]
|
||||
name = "quicr-tokio"
|
||||
version = "0.1.0"
|
||||
authors = ["Benjamin Saunders <ben.e.saunders@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
quicr = { path = ".." }
|
||||
tokio-reactor = "0.1.1"
|
||||
tokio-udp = "0.1"
|
||||
tokio-io = "0.1"
|
||||
tokio-timer = "0.2.1"
|
||||
slog = "2.1"
|
||||
futures = "0.1.21"
|
||||
fnv = "1.0.6"
|
||||
openssl = "0.10.6"
|
||||
failure = "0.1"
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = "0.1.5"
|
||||
slog-term = "2"
|
||||
rand = "0.4"
|
||||
Binary file not shown.
@@ -0,0 +1,78 @@
|
||||
extern crate tokio;
|
||||
extern crate tokio_timer;
|
||||
extern crate quicr_tokio as quicr;
|
||||
#[macro_use]
|
||||
extern crate failure;
|
||||
#[macro_use]
|
||||
extern crate slog;
|
||||
extern crate slog_term;
|
||||
extern crate futures;
|
||||
extern crate rand;
|
||||
|
||||
use std::net::{UdpSocket, ToSocketAddrs};
|
||||
use std::io::{self, Write};
|
||||
|
||||
use futures::Future;
|
||||
use tokio::executor::current_thread::CurrentThread;
|
||||
|
||||
use slog::{Logger, Drain};
|
||||
use failure::Error;
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
fn main() {
|
||||
let code = {
|
||||
let decorator = slog_term::PlainSyncDecorator::new(std::io::stderr());
|
||||
let drain = slog_term::FullFormat::new(decorator).use_original_order().build().fuse();
|
||||
if let Err(e) = run(Logger::root(drain, o!())) {
|
||||
eprintln!("ERROR: {}", e);
|
||||
1
|
||||
} else { 0 }
|
||||
};
|
||||
::std::process::exit(code);
|
||||
}
|
||||
|
||||
fn run(log: Logger) -> Result<()> {
|
||||
let mut remote_host = ::std::env::args().nth(1).ok_or(format_err!("missing address argument"))?;
|
||||
let remote = remote_host.to_socket_addrs()?.next().ok_or(format_err!("couldn't resolve to an address"))?;
|
||||
if let Some(x) = remote_host.rfind(':') { remote_host.truncate(x); }
|
||||
|
||||
let socket = UdpSocket::bind("[::]:0")?;
|
||||
let mut protocols = Vec::new();
|
||||
const PROTO: &[u8] = b"hq-11";
|
||||
protocols.push(PROTO.len() as u8);
|
||||
protocols.extend_from_slice(PROTO);
|
||||
let config = quicr::Config {
|
||||
protocols,
|
||||
..quicr::Config::default()
|
||||
};
|
||||
|
||||
let reactor = tokio::reactor::Reactor::new()?;
|
||||
let timer = tokio_timer::Timer::new(reactor);
|
||||
|
||||
let (endpoint, driver, _) = quicr::Endpoint::from_std(&tokio::reactor::Handle::current(), timer.handle(), socket, log.clone(), config, rand::random(), None)?;
|
||||
let mut executor = CurrentThread::new_with_park(timer);
|
||||
|
||||
executor.spawn(
|
||||
endpoint.connect(&remote, Some(remote_host.as_bytes()))
|
||||
.map_err(|e| eprintln!("failed to connect: {}", e))
|
||||
.and_then(|(conn, _)| {
|
||||
println!("connected!");
|
||||
conn.open(quicr::Directionality::Bi).map_err(|e| eprintln!("failed to open stream: {}", e))
|
||||
})
|
||||
.and_then(|stream| {
|
||||
println!("opened a stream");
|
||||
tokio::io::write_all(stream, b"GET /index.html\r\n").map_err(|e| eprintln!("failed to send request: {}", e))
|
||||
})
|
||||
.and_then(|(stream, _)| tokio::io::shutdown(stream).map_err(|e| eprintln!("failed to shutdown stream: {}", e)))
|
||||
.and_then(|stream| stream.read_to_end(usize::max_value()).map_err(|e| eprintln!("failed to read response: {}", e)))
|
||||
.map(|data| {
|
||||
io::stdout().write_all(&data).unwrap();
|
||||
io::stdout().flush().unwrap();
|
||||
println!("done")
|
||||
})
|
||||
);
|
||||
executor.block_on(driver).map_err(|e| e.into_inner().unwrap())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,108 @@
|
||||
extern crate tokio;
|
||||
extern crate tokio_timer;
|
||||
extern crate quicr_tokio as quicr;
|
||||
#[macro_use]
|
||||
extern crate failure;
|
||||
#[macro_use]
|
||||
extern crate slog;
|
||||
extern crate slog_term;
|
||||
extern crate futures;
|
||||
extern crate rand;
|
||||
extern crate openssl;
|
||||
|
||||
use std::net::UdpSocket;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::fmt;
|
||||
|
||||
use futures::{Future, Stream};
|
||||
use tokio::executor::current_thread::CurrentThread;
|
||||
use failure::{ResultExt, Fail};
|
||||
|
||||
use openssl::pkey::{PKey, Private};
|
||||
use openssl::x509::X509;
|
||||
|
||||
use slog::{Logger, Drain};
|
||||
use failure::Error;
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
pub struct PrettyErr<'a>(&'a Fail);
|
||||
impl<'a> fmt::Display for PrettyErr<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Display::fmt(&self.0, f)?;
|
||||
let mut x: &Fail = self.0;
|
||||
while let Some(cause) = x.cause() {
|
||||
f.write_str(": ")?;
|
||||
fmt::Display::fmt(&cause, f)?;
|
||||
x = cause;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ErrorExt {
|
||||
fn pretty(&self) -> PrettyErr;
|
||||
}
|
||||
|
||||
impl ErrorExt for Error {
|
||||
fn pretty(&self) -> PrettyErr { PrettyErr(self.cause()) }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let code = {
|
||||
let decorator = slog_term::PlainSyncDecorator::new(std::io::stderr());
|
||||
let drain = slog_term::FullFormat::new(decorator).use_original_order().build().fuse();
|
||||
if let Err(e) = run(Logger::root(drain, o!())) {
|
||||
eprintln!("ERROR: {}", e.pretty());
|
||||
1
|
||||
} else { 0 }
|
||||
};
|
||||
::std::process::exit(code);
|
||||
}
|
||||
|
||||
fn run(log: Logger) -> Result<()> {
|
||||
let socket = UdpSocket::bind("[::]:4433")?;
|
||||
let mut protocols = Vec::new();
|
||||
const PROTO: &[u8] = b"hq-11";
|
||||
protocols.push(PROTO.len() as u8);
|
||||
protocols.extend_from_slice(PROTO);
|
||||
let config = quicr::Config {
|
||||
protocols,
|
||||
max_remote_bi_streams: 64,
|
||||
..quicr::Config::default()
|
||||
};
|
||||
|
||||
let reactor = tokio::reactor::Reactor::new()?;
|
||||
let timer = tokio_timer::Timer::new(reactor);
|
||||
|
||||
let key;
|
||||
let cert;
|
||||
{
|
||||
let mut key_file = File::open("key.der").context("failed to open key.der")?;
|
||||
let mut data = Vec::new();
|
||||
key_file.read_to_end(&mut data).context("failed reading key.der")?;
|
||||
key = PKey::<Private>::private_key_from_der(&data).context("failed to load key.der")?;
|
||||
data.clear();
|
||||
|
||||
let mut cert_file = File::open("cert.der").context("failed to open cert.der")?;
|
||||
cert_file.read_to_end(&mut data).context("failed reading cert.der")?;
|
||||
cert = X509::from_der(&data).context("failed to load cert.der")?;
|
||||
}
|
||||
|
||||
let (endpoint, driver, incoming) = quicr::Endpoint::from_std(&tokio::reactor::Handle::current(), timer.handle(), socket,
|
||||
log.clone(), config, rand::random(),
|
||||
Some(quicr::ListenConfig { private_key: &key, cert: &cert }))?;
|
||||
let mut executor = CurrentThread::new_with_park(timer);
|
||||
|
||||
executor.spawn(incoming.for_each(move |conn| {
|
||||
info!(log, "got connection");
|
||||
let log = log.clone();
|
||||
|
||||
Ok(())
|
||||
}));
|
||||
|
||||
executor.block_on(driver).map_err(|e| e.into_inner().unwrap())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,578 @@
|
||||
extern crate quicr;
|
||||
extern crate tokio_reactor;
|
||||
extern crate tokio_udp;
|
||||
extern crate tokio_io;
|
||||
extern crate tokio_timer;
|
||||
#[macro_use]
|
||||
extern crate slog;
|
||||
extern crate futures;
|
||||
extern crate fnv;
|
||||
extern crate openssl;
|
||||
#[macro_use]
|
||||
extern crate failure;
|
||||
|
||||
use std::{io, mem};
|
||||
use std::net::{SocketAddr, SocketAddrV6};
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use std::collections::VecDeque;
|
||||
use std::time::{Instant, Duration};
|
||||
|
||||
use tokio_udp::UdpSocket;
|
||||
use tokio_io::{AsyncWrite};
|
||||
use tokio_timer::{Delay, timer};
|
||||
use slog::Logger;
|
||||
use futures::{Future, Poll, Async};
|
||||
use futures::Stream as FuturesStream;
|
||||
use futures::unsync::{oneshot, mpsc};
|
||||
use futures::task::{self, Task};
|
||||
use futures::stream::FuturesUnordered;
|
||||
use fnv::{FnvHashMap, FnvHashSet};
|
||||
use openssl::ssl;
|
||||
|
||||
pub use quicr::{Config, ListenConfig, PersistentState, ConnectionError, TransportError, ReadError, WriteError, Directionality};
|
||||
|
||||
#[derive(Debug, Fail)]
|
||||
pub enum Error {
|
||||
#[fail(display = "{}", _0)]
|
||||
Io(io::Error),
|
||||
#[fail(display = "{}", _0)]
|
||||
Transport(TransportError),
|
||||
#[fail(display = "{}", _0)]
|
||||
Ssl(openssl::ssl::Error),
|
||||
}
|
||||
|
||||
impl From<io::Error> for Error { fn from(x: io::Error) -> Self { Error::Io(x) } }
|
||||
impl From<TransportError> for Error { fn from(x: TransportError) -> Self { Error::Transport(x) } }
|
||||
impl From<ssl::Error> for Error { fn from(x: ssl::Error) -> Self { Error::Ssl(x) } }
|
||||
|
||||
struct EndpointInner {
|
||||
log: Logger,
|
||||
timer: timer::Handle,
|
||||
socket: UdpSocket,
|
||||
inner: quicr::Endpoint,
|
||||
outgoing: VecDeque<(SocketAddrV6, Box<[u8]>)>,
|
||||
epoch: Instant,
|
||||
pending: FnvHashMap<quicr::ConnectionHandle, Pending>,
|
||||
// TODO: Replace this with something custom that avoids using oneshots to cancel
|
||||
timers: FuturesUnordered<Timer>,
|
||||
incoming: mpsc::UnboundedSender<NewConnection>,
|
||||
driver: Option<Task>,
|
||||
}
|
||||
|
||||
struct Pending {
|
||||
blocked_writers: FnvHashMap<quicr::StreamId, Task>,
|
||||
blocked_readers: FnvHashMap<quicr::StreamId, Task>,
|
||||
connecting: Option<oneshot::Sender<Option<ConnectionError>>>,
|
||||
uni_opening: VecDeque<oneshot::Sender<Result<quicr::StreamId, ConnectionLost>>>,
|
||||
bi_opening: VecDeque<oneshot::Sender<Result<quicr::StreamId, ConnectionLost>>>,
|
||||
cancel_loss_detect: Option<oneshot::Sender<()>>,
|
||||
cancel_idle: Option<oneshot::Sender<()>>,
|
||||
incoming_streams: VecDeque<quicr::StreamId>,
|
||||
incoming_streams_reader: Option<Task>,
|
||||
remote_recv_streams: FnvHashSet<quicr::StreamId>,
|
||||
finishing: FnvHashMap<quicr::StreamId, oneshot::Sender<()>>,
|
||||
}
|
||||
|
||||
impl Pending {
|
||||
pub fn new(connecting: Option<oneshot::Sender<Option<ConnectionError>>>) -> Self { Self {
|
||||
blocked_writers: FnvHashMap::default(),
|
||||
blocked_readers: FnvHashMap::default(),
|
||||
connecting,
|
||||
uni_opening: VecDeque::new(),
|
||||
bi_opening: VecDeque::new(),
|
||||
cancel_loss_detect: None,
|
||||
cancel_idle: None,
|
||||
incoming_streams: VecDeque::new(),
|
||||
incoming_streams_reader: None,
|
||||
remote_recv_streams: FnvHashSet::default(),
|
||||
finishing: FnvHashMap::default(),
|
||||
}}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Endpoint(Rc<RefCell<EndpointInner>>);
|
||||
|
||||
/// A future that drives an endpoint
|
||||
pub struct Driver(Rc<RefCell<EndpointInner>>);
|
||||
|
||||
pub type Incoming = mpsc::UnboundedReceiver<NewConnection>;
|
||||
|
||||
impl Endpoint {
|
||||
pub fn from_std(reactor: &tokio_reactor::Handle, timer: timer::Handle, socket: std::net::UdpSocket,
|
||||
log: Logger, config: Config, state: PersistentState, listen: Option<ListenConfig>) ->
|
||||
Result<(Self, Driver, Incoming), Error>
|
||||
{
|
||||
let (send, recv) = mpsc::unbounded();
|
||||
let rc = Rc::new(RefCell::new(EndpointInner {
|
||||
timer,
|
||||
log: log.clone(),
|
||||
socket: UdpSocket::from_std(socket, reactor)?,
|
||||
inner: quicr::Endpoint::new(log, config, state, listen)?,
|
||||
outgoing: VecDeque::new(),
|
||||
epoch: Instant::now(),
|
||||
pending: FnvHashMap::default(),
|
||||
timers: FuturesUnordered::new(),
|
||||
incoming: send,
|
||||
driver: None,
|
||||
}));
|
||||
Ok((Endpoint(rc.clone()), Driver(rc), recv))
|
||||
}
|
||||
|
||||
pub fn connect(&self, addr: &SocketAddr, hostname: Option<&[u8]>) -> Box<Future<Item=(Connection, IncomingStreams), Error=ConnectionError>> {
|
||||
let (send, recv) = oneshot::channel();
|
||||
let conn = {
|
||||
let mut endpoint = self.0.borrow_mut();
|
||||
let conn = endpoint.inner.connect(normalize(*addr), hostname);
|
||||
endpoint.pending.insert(conn, Pending::new(Some(send)));
|
||||
conn
|
||||
};
|
||||
let endpoint = self.clone();
|
||||
let conn = Rc::new(ConnectionInner { endpoint: endpoint.clone(), conn });
|
||||
Box::new(
|
||||
recv.map_err(|_| unreachable!())
|
||||
.and_then(move |err| if let Some(err) = err { Err(err) } else {
|
||||
Ok((Connection(conn.clone()), IncomingStreams { endpoint, conn }))
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NewConnection {
|
||||
pub connection: Connection,
|
||||
pub incoming_streams: IncomingStreams,
|
||||
pub address: SocketAddr,
|
||||
}
|
||||
|
||||
impl Future for Driver {
|
||||
type Item = ();
|
||||
type Error = io::Error;
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
let mut buf = [0; 64 * 1024];
|
||||
let mut endpoint = self.0.borrow_mut();
|
||||
let endpoint = &mut *endpoint;
|
||||
if endpoint.driver.is_none() { endpoint.driver = Some(task::current()); }
|
||||
let now = micros_from(endpoint.epoch.elapsed());
|
||||
loop {
|
||||
loop {
|
||||
match endpoint.socket.poll_recv_from(&mut buf) {
|
||||
Ok(Async::Ready((n, addr))) => {
|
||||
endpoint.inner.handle(now, normalize(addr), (&buf[0..n]).into());
|
||||
}
|
||||
Ok(Async::NotReady) => { break; }
|
||||
// Ignore ECONNRESET as it's undefined in QUIC and may be injected by an attacker
|
||||
Err(ref e) if e.kind() == io::ErrorKind::ConnectionReset => { continue; }
|
||||
Err(e) => { return Err(e); }
|
||||
}
|
||||
}
|
||||
while let Some(event) = endpoint.inner.poll() {
|
||||
use quicr::Event::*;
|
||||
match event {
|
||||
Connected { connection, address, .. } => {
|
||||
if let Some(c) = endpoint.pending.get_mut(&connection).unwrap().connecting.take() {
|
||||
// Graceful close should be handled by drop impl
|
||||
let _ = c.send(None);
|
||||
} else {
|
||||
let conn = Rc::new(ConnectionInner { endpoint: Endpoint(self.0.clone()), conn: connection });
|
||||
let _ = endpoint.incoming.unbounded_send(NewConnection {
|
||||
connection: Connection(conn.clone()),
|
||||
incoming_streams: IncomingStreams { endpoint: Endpoint(self.0.clone()), conn },
|
||||
address: address.into(),
|
||||
});
|
||||
}
|
||||
}
|
||||
ConnectionLost { connection, reason } => {
|
||||
if let Some(c) = endpoint.pending.get_mut(&connection).unwrap().connecting.take() {
|
||||
// Graceful close should be handled by drop impl
|
||||
let _ = c.send(Some(reason));
|
||||
}
|
||||
}
|
||||
StreamWritable { connection, stream } => {
|
||||
if let Some(writer) = endpoint.pending.get_mut(&connection).unwrap().blocked_writers.remove(&stream) {
|
||||
writer.notify();
|
||||
}
|
||||
}
|
||||
StreamReadable { connection, stream } => {
|
||||
let pending = endpoint.pending.get_mut(&connection).unwrap();
|
||||
if let Some(reader) = pending.blocked_readers.remove(&stream) {
|
||||
reader.notify();
|
||||
}
|
||||
if !pending.remote_recv_streams.contains(&stream) {
|
||||
pending.remote_recv_streams.insert(stream);
|
||||
pending.incoming_streams.push_back(stream);
|
||||
if let Some(x) = pending.incoming_streams_reader.take() { x.notify(); }
|
||||
}
|
||||
}
|
||||
StreamAvailable { connection, directionality } => {
|
||||
let pending = endpoint.pending.get_mut(&connection).unwrap();
|
||||
let queue = match directionality {
|
||||
Directionality::Uni => &mut pending.uni_opening,
|
||||
Directionality::Bi => &mut pending.bi_opening,
|
||||
};
|
||||
while let Some(ch) = queue.pop_front() {
|
||||
if let Some(id) = endpoint.inner.open(connection, directionality) {
|
||||
let _ = ch.send(Ok(id));
|
||||
} else {
|
||||
queue.push_front(ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
StreamFinished { connection, stream } => {
|
||||
let _ = endpoint.pending.get_mut(&connection).unwrap()
|
||||
.finishing.remove(&stream).unwrap().send(());
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut blocked = false;
|
||||
while !endpoint.outgoing.is_empty() {
|
||||
{
|
||||
let front = endpoint.outgoing.front().unwrap();
|
||||
match endpoint.socket.poll_send_to(&front.1, &front.0.into()) {
|
||||
Ok(Async::Ready(_)) => {}
|
||||
Ok(Async::NotReady) => { blocked = true; break; }
|
||||
Err(ref e) if e.kind() == io::ErrorKind::PermissionDenied => { blocked = true; break; }
|
||||
Err(e) => { return Err(e); }
|
||||
}
|
||||
}
|
||||
endpoint.outgoing.pop_front();
|
||||
}
|
||||
while let Some(io) = endpoint.inner.poll_io(now) {
|
||||
use quicr::Io::*;
|
||||
match io {
|
||||
Transmit { destination, packet } => {
|
||||
if !blocked {
|
||||
match endpoint.socket.poll_send_to(&packet, &destination.into()) {
|
||||
Ok(Async::Ready(_)) => {}
|
||||
Ok(Async::NotReady) => { blocked = true; }
|
||||
Err(ref e) if e.kind() == io::ErrorKind::PermissionDenied => { blocked = true; }
|
||||
Err(e) => { return Err(e); }
|
||||
}
|
||||
}
|
||||
if blocked {
|
||||
endpoint.outgoing.push_front((destination, packet));
|
||||
}
|
||||
}
|
||||
TimerStart { connection, timer, time } => {
|
||||
// Loss detection and idle timers start before the connection is established
|
||||
let pending = endpoint.pending.entry(connection).or_insert_with(|| Pending::new(None));
|
||||
use quicr::Timer::*;
|
||||
let mut cancel = match timer {
|
||||
LossDetection => Some(&mut pending.cancel_loss_detect),
|
||||
Idle => Some(&mut pending.cancel_idle),
|
||||
Close => None
|
||||
};
|
||||
let instant = endpoint.epoch + duration_micros(time);
|
||||
if let Some(cancel) = cancel.as_mut().and_then(|x| x.take()) {
|
||||
let _ = cancel.send(());
|
||||
}
|
||||
let (send, recv) = oneshot::channel();
|
||||
if let Some(cancel) = cancel { *cancel = Some(send); }
|
||||
trace!(endpoint.log, "timer start"; "timer" => ?timer, "time" => ?duration_micros(time));
|
||||
endpoint.timers.push(Timer {
|
||||
conn: connection,
|
||||
ty: timer,
|
||||
delay: endpoint.timer.delay(instant),
|
||||
cancel: recv,
|
||||
});
|
||||
}
|
||||
TimerStop { connection, timer } => {
|
||||
trace!(endpoint.log, "timer stop"; "timer" => ?timer);
|
||||
let pending = endpoint.pending.get_mut(&connection).unwrap();
|
||||
use quicr::Timer::*;
|
||||
match timer {
|
||||
LossDetection => { pending.cancel_loss_detect.take().map(|x| x.send(()).unwrap()); }
|
||||
Idle => { pending.cancel_idle.take().map(|x| x.send(())); }
|
||||
Close => { unreachable!() }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut fired = false;
|
||||
loop {
|
||||
match endpoint.timers.poll() {
|
||||
Ok(Async::Ready(Some(Some((conn, timer))))) => {
|
||||
trace!(endpoint.log, "timeout"; "timer" => ?timer);
|
||||
endpoint.inner.timeout(now, conn, timer);
|
||||
fired = true;
|
||||
}
|
||||
Ok(Async::Ready(Some(None))) => {}
|
||||
Ok(Async::Ready(None)) | Ok(Async::NotReady) => { break; }
|
||||
Err(()) => unreachable!()
|
||||
}
|
||||
}
|
||||
if !fired { break; }
|
||||
}
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
||||
|
||||
fn duration_micros(x: u64) -> Duration { Duration::new(x / (1000 * 1000), (x % (1000 * 1000)) as u32 * 1000) }
|
||||
fn micros_from(x: Duration) -> u64 { x.as_secs() * 1000 * 1000 + (x.subsec_nanos() / 1000) as u64 }
|
||||
|
||||
fn normalize(x: SocketAddr) -> SocketAddrV6 {
|
||||
match x {
|
||||
SocketAddr::V6(x) => x,
|
||||
SocketAddr::V4(x) => SocketAddrV6::new(x.ip().to_ipv6_mapped(), x.port(), 0, 0),
|
||||
}
|
||||
}
|
||||
|
||||
struct ConnectionInner {
|
||||
endpoint: Endpoint,
|
||||
conn: quicr::ConnectionHandle,
|
||||
}
|
||||
|
||||
pub struct Connection(Rc<ConnectionInner>);
|
||||
|
||||
#[derive(Copy, Clone, Debug, Fail)]
|
||||
#[fail(display = "connection lost")]
|
||||
pub struct ConnectionLost;
|
||||
|
||||
impl Connection {
|
||||
pub fn open(&self, directionality: Directionality) -> Box<Future<Item=Stream, Error=ConnectionLost>> {
|
||||
let (send, recv) = oneshot::channel();
|
||||
{
|
||||
let mut endpoint = self.0.endpoint.0.borrow_mut();
|
||||
if let Some(x) = endpoint.inner.open(self.0.conn, directionality) {
|
||||
let _ = send.send(Ok(x));
|
||||
} else {
|
||||
let pending = endpoint.pending.get_mut(&self.0.conn).unwrap();
|
||||
match directionality {
|
||||
Directionality::Uni => { pending.uni_opening.push_back(send); }
|
||||
Directionality::Bi => { pending.bi_opening.push_back(send); }
|
||||
}
|
||||
}
|
||||
}
|
||||
let endpoint = self.0.endpoint.clone();
|
||||
let conn = self.0.clone();
|
||||
Box::new(
|
||||
recv.map_err(|_| unreachable!())
|
||||
.and_then(|result| result)
|
||||
.map(move |stream| Stream::new(endpoint, conn, stream))
|
||||
)
|
||||
}
|
||||
|
||||
pub fn close(&self, error_code: u16, reason: &[u8]) {
|
||||
let endpoint = &mut *self.0.endpoint.0.borrow_mut();
|
||||
endpoint.inner.close(micros_from(endpoint.epoch.elapsed()), self.0.conn, error_code, reason.into());
|
||||
endpoint.driver.as_ref().map(|x| x.notify());
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ConnectionInner {
|
||||
fn drop(&mut self) {
|
||||
let endpoint = &mut *self.endpoint.0.borrow_mut();
|
||||
endpoint.inner.close(micros_from(endpoint.epoch.elapsed()), self.conn, 0, (&[][..]).into());
|
||||
endpoint.driver.as_ref().map(|x| x.notify());
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Stream {
|
||||
endpoint: Endpoint,
|
||||
conn: Rc<ConnectionInner>,
|
||||
stream: quicr::StreamId,
|
||||
finishing: Option<oneshot::Receiver<()>>,
|
||||
stop_reason: Option<u16>,
|
||||
finished: bool,
|
||||
recvd: bool,
|
||||
}
|
||||
|
||||
impl Stream {
|
||||
fn new(endpoint: Endpoint, conn: Rc<ConnectionInner>, stream: quicr::StreamId) -> Self { Self {
|
||||
endpoint, conn, stream,
|
||||
finishing: None,
|
||||
stop_reason: None,
|
||||
finished: false,
|
||||
recvd: false,
|
||||
}}
|
||||
|
||||
pub fn directionality(&self) -> Directionality { self.stream.directionality() }
|
||||
}
|
||||
|
||||
impl Stream {
|
||||
/// The error code provided by the remote application explaining why we must stop writing to this stream
|
||||
pub fn stop_reason(&self) -> Option<u16> { self.stop_reason }
|
||||
|
||||
pub fn poll_read_unordered(&mut self) -> Poll<(Box<[u8]>, u64), ReadError> {
|
||||
let endpoint = &mut *self.endpoint.0.borrow_mut();
|
||||
use ReadError::*;
|
||||
let pending = endpoint.pending.get_mut(&self.conn.conn).unwrap();
|
||||
match endpoint.inner.read_unordered(self.conn.conn, self.stream) {
|
||||
Ok((bytes, offset)) => Ok(Async::Ready((bytes.to_vec().into(), offset))),
|
||||
Err(Blocked) => {
|
||||
pending.blocked_readers.insert(self.stream, task::current());
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
Err(e@Reset { .. }) => {
|
||||
pending.remote_recv_streams.remove(&self.stream);
|
||||
Err(e)
|
||||
}
|
||||
Err(e@Finished) => {
|
||||
pending.remote_recv_streams.remove(&self.stream);
|
||||
self.recvd = true;
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll_write(&mut self, buf: &[u8]) -> Poll<usize, WriteError> {
|
||||
let mut endpoint = self.endpoint.0.borrow_mut();
|
||||
let n = match endpoint.inner.write(self.conn.conn, self.stream, buf.into()) {
|
||||
Ok(()) => buf.len(),
|
||||
Err((ref unwritten, WriteError::Blocked)) if unwritten.len() < buf.len() => buf.len() - unwritten.len(),
|
||||
Err((_, WriteError::Blocked)) => {
|
||||
endpoint.pending.get_mut(&self.conn.conn).unwrap().blocked_writers.insert(self.stream, task::current());
|
||||
return Ok(Async::NotReady);
|
||||
}
|
||||
Err((_, WriteError::Stopped { error_code })) => {
|
||||
self.stop_reason = Some(error_code);
|
||||
return Err(WriteError::Stopped { error_code });
|
||||
}
|
||||
};
|
||||
endpoint.driver.as_ref().map(|x| x.notify());
|
||||
Ok(Async::Ready(n))
|
||||
}
|
||||
|
||||
pub fn poll_finish(&mut self) -> Async<()> {
|
||||
let mut endpoint = self.endpoint.0.borrow_mut();
|
||||
if self.finishing.is_none() {
|
||||
endpoint.inner.finish(self.conn.conn, self.stream);
|
||||
let (send, recv) = oneshot::channel();
|
||||
self.finishing = Some(recv);
|
||||
endpoint.pending.get_mut(&self.conn.conn).unwrap().finishing.insert(self.stream, send);
|
||||
}
|
||||
let r = self.finishing.as_mut().unwrap().poll().unwrap();
|
||||
if let Async::Ready(()) = r { self.finished = true; }
|
||||
r
|
||||
}
|
||||
|
||||
pub fn reset(&self, error_code: u16) {
|
||||
let endpoint = &mut *self.endpoint.0.borrow_mut();
|
||||
endpoint.inner.reset(self.conn.conn, self.stream, error_code);
|
||||
endpoint.driver.as_ref().map(|x| x.notify());
|
||||
}
|
||||
|
||||
pub fn read_to_end(self, size_limit: usize) -> ReadToEnd {
|
||||
ReadToEnd { stream: self, size_limit, buffer: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
impl io::Write for Stream {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
match Stream::poll_write(self, buf) {
|
||||
Ok(Async::Ready(n)) => Ok(n),
|
||||
Ok(Async::NotReady) => Err(io::Error::new(io::ErrorKind::WouldBlock, "stream blocked")),
|
||||
Err(WriteError::Stopped { .. }) => Err(io::Error::new(io::ErrorKind::ConnectionReset, "peer stopped this stream")),
|
||||
Err(WriteError::Blocked) => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> { Ok(()) }
|
||||
}
|
||||
|
||||
impl AsyncWrite for Stream {
|
||||
fn shutdown(&mut self) -> Poll<(), io::Error> {
|
||||
Ok(self.poll_finish())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Stream {
|
||||
fn drop(&mut self) {
|
||||
let endpoint = &mut *self.endpoint.0.borrow_mut();
|
||||
let (send, recv) = if self.stream.initiator() == endpoint.inner.get_side(self.conn.conn) {
|
||||
(true, self.stream.directionality() == Directionality::Bi)
|
||||
} else {
|
||||
(self.stream.directionality() == Directionality::Bi, true)
|
||||
};
|
||||
if send && !self.finished {
|
||||
endpoint.inner.reset(self.conn.conn, self.stream, 0);
|
||||
}
|
||||
if recv && !self.recvd {
|
||||
endpoint.inner.stop_sending(self.conn.conn, self.stream, 0);
|
||||
}
|
||||
endpoint.driver.as_ref().map(|x| x.notify());
|
||||
}
|
||||
}
|
||||
|
||||
struct Timer {
|
||||
conn: quicr::ConnectionHandle,
|
||||
ty: quicr::Timer,
|
||||
delay: Delay,
|
||||
cancel: oneshot::Receiver<()>,
|
||||
}
|
||||
|
||||
impl Future for Timer {
|
||||
type Item = Option<(quicr::ConnectionHandle, quicr::Timer)>;
|
||||
type Error = (); // FIXME
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
if self.ty != quicr::Timer::Close {
|
||||
if let Async::Ready(()) = self.cancel.poll().unwrap() {
|
||||
return Ok(Async::Ready(None));
|
||||
}
|
||||
}
|
||||
match self.delay.poll() {
|
||||
Err(e) => panic!("unexpected timer error: {}", e),
|
||||
Ok(Async::NotReady) => Ok(Async::NotReady),
|
||||
Ok(Async::Ready(())) => Ok(Async::Ready(Some((self.conn, self.ty)))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IncomingStreams {
|
||||
endpoint: Endpoint,
|
||||
conn: Rc<ConnectionInner>,
|
||||
}
|
||||
|
||||
impl FuturesStream for IncomingStreams {
|
||||
type Item = Stream;
|
||||
type Error = (); // FIXME
|
||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
||||
let mut endpoint = self.endpoint.0.borrow_mut();
|
||||
let pending = endpoint.pending.get_mut(&self.conn.conn).unwrap();
|
||||
if let Some(x) = pending.incoming_streams.pop_front() {
|
||||
return Ok(Async::Ready(Some(Stream::new(self.endpoint.clone(), self.conn.clone(), x))));
|
||||
}
|
||||
pending.incoming_streams_reader = Some(task::current());
|
||||
return Ok(Async::NotReady);
|
||||
}
|
||||
}
|
||||
|
||||
/// Uses unordered reads to be more efficient than using `AsyncRead`
|
||||
pub struct ReadToEnd {
|
||||
stream: Stream,
|
||||
buffer: Vec<u8>,
|
||||
size_limit: usize,
|
||||
}
|
||||
|
||||
impl Future for ReadToEnd {
|
||||
type Item = Box<[u8]>;
|
||||
type Error = ReadError;
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
let endpoint = &mut *self.stream.endpoint.0.borrow_mut();
|
||||
use ReadError::*;
|
||||
let pending = endpoint.pending.get_mut(&self.stream.conn.conn).unwrap();
|
||||
loop {
|
||||
match endpoint.inner.read_unordered(self.stream.conn.conn, self.stream.stream) {
|
||||
Ok((data, offset)) => {
|
||||
let len = self.buffer.len().max(offset as usize + data.len());
|
||||
if len > self.size_limit { return Err(Finished); }
|
||||
self.buffer.resize(len, 0);
|
||||
self.buffer[offset as usize..offset as usize+data.len()].copy_from_slice(&data);
|
||||
}
|
||||
Err(Blocked) => {
|
||||
pending.blocked_readers.insert(self.stream.stream, task::current());
|
||||
return Ok(Async::NotReady);
|
||||
}
|
||||
Err(e@Reset { .. }) => {
|
||||
pending.remote_recv_streams.remove(&self.stream.stream);
|
||||
return Err(e);
|
||||
}
|
||||
Err(Finished) => {
|
||||
self.stream.recvd = true;
|
||||
pending.remote_recv_streams.remove(&self.stream.stream);
|
||||
return Ok(Async::Ready(mem::replace(&mut self.buffer, Vec::new()).into()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user