Skip to content

Commit

Permalink
Restructure proto
Browse files Browse the repository at this point in the history
The existing code has been moved out and is being copied back piece / by
piece while restructuring the code to (hopefully) be more manageable.
  • Loading branch information
carllerche authored Aug 2, 2017
1 parent 13d6866 commit 33bdc05
Show file tree
Hide file tree
Showing 36 changed files with 1,481 additions and 2,950 deletions.
24 changes: 17 additions & 7 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {frame, proto, Peer, ConnectionError, StreamId};

use http;
use futures::{Future, Poll};
use futures::{Future, Poll, Sink, AsyncSink};
use tokio_io::{AsyncRead, AsyncWrite};
use bytes::{Bytes, IntoBuf};

Expand Down Expand Up @@ -29,21 +29,31 @@ pub fn handshake<T>(io: T) -> Handshake<T, Bytes>
///
/// Returns a future which resolves to the connection value once the H2
/// handshake has been completed.
pub fn handshake2<T, B: IntoBuf>(io: T) -> Handshake<T, B>
pub fn handshake2<T, B>(io: T) -> Handshake<T, B>
where T: AsyncRead + AsyncWrite + 'static,
B: IntoBuf + 'static,
{
use tokio_io::io;

debug!("binding client connection");

let handshake = io::write_all(io, b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
.map(|(io, _)| {
.map_err(ConnectionError::from)
.and_then(|(io, _)| {
debug!("client connection bound");

// Use default local settings for now
proto::from_io(io, Default::default())
})
.map_err(ConnectionError::from);
let mut framed_write = proto::framed_write(io);
let settings = frame::Settings::default();

// Send initial settings frame
match framed_write.start_send(settings.into()) {
Ok(AsyncSink::Ready) => {
Ok(proto::from_framed_write(framed_write))
}
Ok(_) => unreachable!(),
Err(e) => Err(ConnectionError::from(e)),
}
});

Handshake { inner: Box::new(handshake) }
}
Expand Down
11 changes: 8 additions & 3 deletions src/frame/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ pub struct PushPromise {
}

impl PushPromise {
pub fn stream_id(&self) -> StreamId {
self.stream_id
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -177,6 +174,14 @@ impl Headers {
})
}

/// Returns `true` if the frame represents trailers
///
/// Trailers are header frames that contain no pseudo headers.
pub fn is_trailers(&self) -> bool {
self.pseudo.method.is_none() &&
self.pseudo.status.is_none()
}

pub fn stream_id(&self) -> StreamId {
self.stream_id
}
Expand Down
55 changes: 13 additions & 42 deletions src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use error::{ConnectionError, Reason};

use bytes::Bytes;

use std::fmt;

/// A helper macro that unpacks a sequence of 4 bytes found in the buffer with
/// the given identifier, starting at the given offset, into the given integer
/// type. Obviously, the integer type should be able to support at least 4
Expand Down Expand Up @@ -54,7 +56,6 @@ pub use self::settings::{

pub const HEADER_LEN: usize = 9;

#[derive(Debug /*, Clone, PartialEq */)]
pub enum Frame<T = Bytes> {
Data(Data<T>),
Headers(Headers),
Expand All @@ -66,50 +67,20 @@ pub enum Frame<T = Bytes> {
}

impl<T> Frame<T> {
pub fn is_connection_frame(&self) -> bool {
use self::Frame::*;

match self {
&Headers(..) |
&Data(..) |
&PushPromise(..) |
&Reset(..) => false,

&WindowUpdate(ref v) => v.stream_id().is_zero(),

&Ping(_) |
&Settings(_) => true,
}
}

pub fn stream_id(&self) -> StreamId {
use self::Frame::*;

match self {
&Headers(ref v) => v.stream_id(),
&Data(ref v) => v.stream_id(),
&PushPromise(ref v) => v.stream_id(),
&WindowUpdate(ref v) => v.stream_id(),
&Reset(ref v) => v.stream_id(),

&Ping(_) |
&Settings(_) => StreamId::zero(),
}
}
}

pub fn is_end_stream(&self) -> bool {
impl<T> fmt::Debug for Frame<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
use self::Frame::*;

match self {
&Headers(ref v) => v.is_end_stream(),
&Data(ref v) => v.is_end_stream(),

&PushPromise(_) |
&WindowUpdate(_) |
&Ping(_) |
&Settings(_) => false,

&Reset(_) => true,
match *self {
Data(..) => write!(fmt, "Frame::Data(..)"),
Headers(ref frame) => write!(fmt, "Frame::Headers({:?})", frame),
PushPromise(ref frame) => write!(fmt, "Frame::PushPromise({:?})", frame),
Settings(ref frame) => write!(fmt, "Frame::Settings({:?})", frame),
Ping(ref frame) => write!(fmt, "Frame::Ping({:?})", frame),
WindowUpdate(ref frame) => write!(fmt, "Frame::WindowUpdate({:?})", frame),
Reset(ref frame) => write!(fmt, "Frame::Reset({:?})", frame),
}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/frame/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ pub struct Ping {
}

impl Ping {
pub fn ping(payload: Payload) -> Ping {
Ping { ack: false, payload }
}

pub fn pong(payload: Payload) -> Ping {
Ping { ack: true, payload }
}
Expand Down
11 changes: 0 additions & 11 deletions src/frame/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,10 @@ impl Settings {
}
}

pub fn new(values: SettingSet) -> Settings {
Settings {
flags: SettingsFlags::empty(),
values: values,
}
}

pub fn is_ack(&self) -> bool {
self.flags.is_ack()
}

pub fn into_set(self) -> SettingSet {
self.values
}

pub fn load(head: Head, payload: &[u8]) -> Result<Settings, Error> {
use self::Setting::*;

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub enum Frame<T, B = Bytes> {
},
PushPromise {
id: StreamId,
promise: (),
promised_id: StreamId,
},
Reset {
id: StreamId,
Expand Down
23 changes: 0 additions & 23 deletions src/proto/apply_settings.rs

This file was deleted.

Loading

0 comments on commit 33bdc05

Please sign in to comment.