Skip to content

Commit 33bdc05

Browse files
authored
Restructure proto
The existing code has been moved out and is being copied back piece / by piece while restructuring the code to (hopefully) be more manageable.
1 parent 13d6866 commit 33bdc05

36 files changed

+1481
-2950
lines changed

src/client.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use {frame, proto, Peer, ConnectionError, StreamId};
22

33
use http;
4-
use futures::{Future, Poll};
4+
use futures::{Future, Poll, Sink, AsyncSink};
55
use tokio_io::{AsyncRead, AsyncWrite};
66
use bytes::{Bytes, IntoBuf};
77

@@ -29,21 +29,31 @@ pub fn handshake<T>(io: T) -> Handshake<T, Bytes>
2929
///
3030
/// Returns a future which resolves to the connection value once the H2
3131
/// handshake has been completed.
32-
pub fn handshake2<T, B: IntoBuf>(io: T) -> Handshake<T, B>
32+
pub fn handshake2<T, B>(io: T) -> Handshake<T, B>
3333
where T: AsyncRead + AsyncWrite + 'static,
34+
B: IntoBuf + 'static,
3435
{
3536
use tokio_io::io;
3637

3738
debug!("binding client connection");
3839

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

43-
// Use default local settings for now
44-
proto::from_io(io, Default::default())
45-
})
46-
.map_err(ConnectionError::from);
45+
let mut framed_write = proto::framed_write(io);
46+
let settings = frame::Settings::default();
47+
48+
// Send initial settings frame
49+
match framed_write.start_send(settings.into()) {
50+
Ok(AsyncSink::Ready) => {
51+
Ok(proto::from_framed_write(framed_write))
52+
}
53+
Ok(_) => unreachable!(),
54+
Err(e) => Err(ConnectionError::from(e)),
55+
}
56+
});
4757

4858
Handshake { inner: Box::new(handshake) }
4959
}

src/frame/headers.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ pub struct PushPromise {
4949
}
5050

5151
impl PushPromise {
52-
pub fn stream_id(&self) -> StreamId {
53-
self.stream_id
54-
}
5552
}
5653

5754
#[derive(Debug)]
@@ -177,6 +174,14 @@ impl Headers {
177174
})
178175
}
179176

177+
/// Returns `true` if the frame represents trailers
178+
///
179+
/// Trailers are header frames that contain no pseudo headers.
180+
pub fn is_trailers(&self) -> bool {
181+
self.pseudo.method.is_none() &&
182+
self.pseudo.status.is_none()
183+
}
184+
180185
pub fn stream_id(&self) -> StreamId {
181186
self.stream_id
182187
}

src/frame/mod.rs

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use error::{ConnectionError, Reason};
33

44
use bytes::Bytes;
55

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

5557
pub const HEADER_LEN: usize = 9;
5658

57-
#[derive(Debug /*, Clone, PartialEq */)]
5859
pub enum Frame<T = Bytes> {
5960
Data(Data<T>),
6061
Headers(Headers),
@@ -66,50 +67,20 @@ pub enum Frame<T = Bytes> {
6667
}
6768

6869
impl<T> Frame<T> {
69-
pub fn is_connection_frame(&self) -> bool {
70-
use self::Frame::*;
71-
72-
match self {
73-
&Headers(..) |
74-
&Data(..) |
75-
&PushPromise(..) |
76-
&Reset(..) => false,
77-
78-
&WindowUpdate(ref v) => v.stream_id().is_zero(),
79-
80-
&Ping(_) |
81-
&Settings(_) => true,
82-
}
83-
}
84-
85-
pub fn stream_id(&self) -> StreamId {
86-
use self::Frame::*;
87-
88-
match self {
89-
&Headers(ref v) => v.stream_id(),
90-
&Data(ref v) => v.stream_id(),
91-
&PushPromise(ref v) => v.stream_id(),
92-
&WindowUpdate(ref v) => v.stream_id(),
93-
&Reset(ref v) => v.stream_id(),
94-
95-
&Ping(_) |
96-
&Settings(_) => StreamId::zero(),
97-
}
98-
}
70+
}
9971

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

103-
match self {
104-
&Headers(ref v) => v.is_end_stream(),
105-
&Data(ref v) => v.is_end_stream(),
106-
107-
&PushPromise(_) |
108-
&WindowUpdate(_) |
109-
&Ping(_) |
110-
&Settings(_) => false,
111-
112-
&Reset(_) => true,
76+
match *self {
77+
Data(..) => write!(fmt, "Frame::Data(..)"),
78+
Headers(ref frame) => write!(fmt, "Frame::Headers({:?})", frame),
79+
PushPromise(ref frame) => write!(fmt, "Frame::PushPromise({:?})", frame),
80+
Settings(ref frame) => write!(fmt, "Frame::Settings({:?})", frame),
81+
Ping(ref frame) => write!(fmt, "Frame::Ping({:?})", frame),
82+
WindowUpdate(ref frame) => write!(fmt, "Frame::WindowUpdate({:?})", frame),
83+
Reset(ref frame) => write!(fmt, "Frame::Reset({:?})", frame),
11384
}
11485
}
11586
}

src/frame/ping.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ pub struct Ping {
1212
}
1313

1414
impl Ping {
15-
pub fn ping(payload: Payload) -> Ping {
16-
Ping { ack: false, payload }
17-
}
18-
1915
pub fn pong(payload: Payload) -> Ping {
2016
Ping { ack: true, payload }
2117
}

src/frame/settings.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,10 @@ impl Settings {
6666
}
6767
}
6868

69-
pub fn new(values: SettingSet) -> Settings {
70-
Settings {
71-
flags: SettingsFlags::empty(),
72-
values: values,
73-
}
74-
}
75-
7669
pub fn is_ack(&self) -> bool {
7770
self.flags.is_ack()
7871
}
7972

80-
pub fn into_set(self) -> SettingSet {
81-
self.values
82-
}
83-
8473
pub fn load(head: Head, payload: &[u8]) -> Result<Settings, Error> {
8574
use self::Setting::*;
8675

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub enum Frame<T, B = Bytes> {
6161
},
6262
PushPromise {
6363
id: StreamId,
64-
promise: (),
64+
promised_id: StreamId,
6565
},
6666
Reset {
6767
id: StreamId,

src/proto/apply_settings.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)