Skip to content

Commit

Permalink
pin Connection
Browse files Browse the repository at this point in the history
Due to the fact that we store a pointer to the Connection object inside
its own Handshake context, Connection objects cannot be moved (which is
why we return a boxed object from its constructor). Using Pin on it
makes the compiler enforce this.

Fixes #45.
  • Loading branch information
ghedo committed Nov 12, 2019
1 parent 90a7b3e commit 12dd601
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ be linked directly into C/C++ applications.
Building
--------

quiche requires Rust 1.38 or later to build. The latest stable Rust release can
quiche requires Rust 1.39 or later to build. The latest stable Rust release can
be installed using [rustup](https://rustup.rs/).

Once the Rust build environment is setup, the quiche source code can be fetched
Expand Down
4 changes: 2 additions & 2 deletions examples/http3-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct PartialResponse {
}

struct Client {
conn: Box<quiche::Connection>,
conn: std::pin::Pin<Box<quiche::Connection>>,

http3_conn: Option<quiche::h3::Connection>,

Expand Down Expand Up @@ -367,7 +367,7 @@ fn main() {
loop {
let http3_conn = client.http3_conn.as_mut().unwrap();

match http3_conn.poll(client.conn.as_mut()) {
match http3_conn.poll(&mut client.conn) {
Ok((
stream_id,
quiche::h3::Event::Headers { list, .. },
Expand Down
2 changes: 1 addition & 1 deletion examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct PartialResponse {
}

struct Client {
conn: Box<quiche::Connection>,
conn: std::pin::Pin<Box<quiche::Connection>>,

partial_responses: HashMap<u64, PartialResponse>,
}
Expand Down
6 changes: 3 additions & 3 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ pub extern fn quiche_accept(
};

match accept(scid, odcid, config) {
Ok(c) => Box::into_raw(c),
Ok(c) => Box::into_raw(Pin::into_inner(c)),

Err(_) => ptr::null_mut(),
}
Expand All @@ -316,7 +316,7 @@ pub extern fn quiche_connect(
let scid = unsafe { slice::from_raw_parts(scid, scid_len) };

match connect(server_name, scid, config) {
Ok(c) => Box::into_raw(c),
Ok(c) => Box::into_raw(Pin::into_inner(c)),

Err(_) => ptr::null_mut(),
}
Expand Down Expand Up @@ -373,7 +373,7 @@ pub extern fn quiche_conn_new_with_tls(
let tls = unsafe { tls::Handshake::from_ptr(ssl) };

match Connection::with_tls(scid, odcid, config, tls, is_server) {
Ok(c) => Box::into_raw(c),
Ok(c) => Box::into_raw(Pin::into_inner(c)),

Err(_) => ptr::null_mut(),
}
Expand Down
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ extern crate log;
use std::cmp;
use std::time;

use std::pin::Pin;

/// The current QUIC wire version.
pub const PROTOCOL_VERSION: u32 = 0xff00_0017;

Expand Down Expand Up @@ -771,7 +773,7 @@ pub struct Connection {
/// ```
pub fn accept(
scid: &[u8], odcid: Option<&[u8]>, config: &mut Config,
) -> Result<Box<Connection>> {
) -> Result<Pin<Box<Connection>>> {
let conn = Connection::new(scid, odcid, config, true)?;

Ok(conn)
Expand All @@ -794,7 +796,7 @@ pub fn accept(
/// ```
pub fn connect(
server_name: Option<&str>, scid: &[u8], config: &mut Config,
) -> Result<Box<Connection>> {
) -> Result<Pin<Box<Connection>>> {
let conn = Connection::new(scid, None, config, false)?;

if let Some(server_name) = server_name {
Expand Down Expand Up @@ -900,21 +902,21 @@ pub fn retry(
impl Connection {
fn new(
scid: &[u8], odcid: Option<&[u8]>, config: &mut Config, is_server: bool,
) -> Result<Box<Connection>> {
) -> Result<Pin<Box<Connection>>> {
let tls = config.tls_ctx.new_handshake()?;
Connection::with_tls(scid, odcid, config, tls, is_server)
}

fn with_tls(
scid: &[u8], odcid: Option<&[u8]>, config: &mut Config,
tls: tls::Handshake, is_server: bool,
) -> Result<Box<Connection>> {
) -> Result<Pin<Box<Connection>>> {
let max_rx_data = config.local_transport_params.initial_max_data;

let scid_as_hex: Vec<String> =
scid.iter().map(|b| format!("{:02x}", b)).collect();

let mut conn = Box::new(Connection {
let mut conn = Box::pin(Connection {
version: config.version,

dcid: Vec::new(),
Expand Down Expand Up @@ -3167,8 +3169,8 @@ pub mod testing {
use super::*;

pub struct Pipe {
pub client: Box<Connection>,
pub server: Box<Connection>,
pub client: Pin<Box<Connection>>,
pub server: Pin<Box<Connection>>,
}

impl Pipe {
Expand Down

0 comments on commit 12dd601

Please sign in to comment.