Merge branch 'master' into ver/flowio

This commit is contained in:
Oliver Gould
2017-07-10 01:15:36 +00:00
18 changed files with 557 additions and 168 deletions

View File

@@ -1,10 +1,10 @@
use frame::{util, Frame, Head, Error, StreamId, Kind};
use bytes::{BufMut, Bytes};
use bytes::{BufMut, Bytes, Buf};
#[derive(Debug)]
pub struct Data {
pub struct Data<T = Bytes> {
stream_id: StreamId,
data: Bytes,
data: T,
flags: DataFlag,
pad_len: Option<u8>,
}
@@ -16,8 +16,8 @@ const END_STREAM: u8 = 0x1;
const PADDED: u8 = 0x8;
const ALL: u8 = END_STREAM | PADDED;
impl Data {
pub fn load(head: Head, mut payload: Bytes) -> Result<Data, Error> {
impl Data<Bytes> {
pub fn load(head: Head, mut payload: Bytes) -> Result<Self, Error> {
let flags = DataFlag::load(head.flag());
let pad_len = if flags.is_padded() {
@@ -34,35 +34,56 @@ impl Data {
pad_len: pad_len,
})
}
}
impl<T> Data<T> {
pub fn new(stream_id: StreamId, data: T) -> Self {
Data {
stream_id: stream_id,
data: data,
flags: DataFlag::default(),
pad_len: None,
}
}
pub fn stream_id(&self) -> StreamId {
self.stream_id
}
pub fn len(&self) -> usize {
self.data.len()
}
pub fn is_end_stream(&self) -> bool {
self.flags.is_end_stream()
}
pub fn encode<T: BufMut>(&self, dst: &mut T) {
self.head().encode(self.len(), dst);
dst.put(&self.data);
pub fn set_end_stream(&mut self) {
self.flags.set_end_stream()
}
pub fn head(&self) -> Head {
Head::new(Kind::Data, self.flags.into(), self.stream_id)
}
pub fn into_payload(self) -> Bytes {
pub fn into_payload(self) -> T {
self.data
}
}
impl From<Data> for Frame {
fn from(src: Data) -> Frame {
impl<T: Buf> Data<T> {
pub fn len(&self) -> usize {
self.data.remaining()
}
pub fn encode_chunk<U: BufMut>(&mut self, dst: &mut U) {
if self.len() > dst.remaining_mut() {
unimplemented!();
}
self.head().encode(self.len(), dst);
dst.put(&mut self.data);
}
}
impl<T> From<Data<T>> for Frame<T> {
fn from(src: Data<T>) -> Self {
Frame::Data(src)
}
}
@@ -86,11 +107,22 @@ impl DataFlag {
self.0 & END_STREAM == END_STREAM
}
pub fn set_end_stream(&mut self) {
self.0 |= END_STREAM
}
pub fn is_padded(&self) -> bool {
self.0 & PADDED == PADDED
}
}
impl Default for DataFlag {
/// Returns a `HeadersFlag` value with `END_HEADERS` set.
fn default() -> Self {
DataFlag(0)
}
}
impl From<DataFlag> for u8 {
fn from(src: DataFlag) -> u8 {
src.0

View File

@@ -1,6 +1,8 @@
use hpack;
use error::{ConnectionError, Reason};
use bytes::Bytes;
/// 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
@@ -53,8 +55,8 @@ pub use self::settings::{
pub const HEADER_LEN: usize = 9;
#[derive(Debug /*, Clone, PartialEq */)]
pub enum Frame {
Data(Data),
pub enum Frame<T = Bytes> {
Data(Data<T>),
Headers(Headers),
PushPromise(PushPromise),
Settings(Settings),

View File

@@ -71,8 +71,8 @@ impl Ping {
}
}
impl From<Ping> for Frame {
fn from(src: Ping) -> Frame {
impl<T> From<Ping> for Frame<T> {
fn from(src: Ping) -> Frame<T> {
Frame::Ping(src)
}
}

View File

@@ -175,8 +175,8 @@ impl Settings {
}
}
impl From<Settings> for Frame {
fn from(src: Settings) -> Frame {
impl<T> From<Settings> for Frame<T> {
fn from(src: Settings) -> Frame<T> {
Frame::Settings(src)
}
}

View File

@@ -1,23 +1,21 @@
use StreamId;
use byteorder::{ByteOrder, NetworkEndian};
use byteorder::NetworkEndian;
use bytes::{BufMut};
use frame::{self, Head, Kind, Error};
const INCREMENT_MASK: u32 = 1 << 31;
type Increment = u32;
const SIZE_INCREMENT_MASK: u32 = 1 << 31;
#[derive(Debug)]
pub struct WindowUpdate {
stream_id: StreamId,
increment: Increment,
size_increment: u32,
}
impl WindowUpdate {
pub fn new(stream_id: StreamId, increment: Increment) -> WindowUpdate {
pub fn new(stream_id: StreamId, size_increment: u32) -> WindowUpdate {
WindowUpdate {
stream_id,
increment,
size_increment,
}
}
@@ -25,18 +23,24 @@ impl WindowUpdate {
self.stream_id
}
pub fn increment(&self) -> Increment {
self.increment
pub fn size_increment(&self) -> u32 {
self.size_increment
}
/// Builds a `WindowUpdate` frame from a raw frame.
pub fn load(head: Head, bytes: &[u8]) -> Result<WindowUpdate, Error> {
pub fn load(head: Head, payload: &[u8]) -> Result<WindowUpdate, Error> {
debug_assert_eq!(head.kind(), ::frame::Kind::WindowUpdate);
if payload.len() != 4 {
return Err(Error::BadFrameSize);
}
// Clear the most significant bit, as that is reserved and MUST be ignored
// when received.
let size_increment = unpack_octets_4!(payload, 0, u32) & !SIZE_INCREMENT_MASK;
Ok(WindowUpdate {
stream_id: head.stream_id(),
// Clear the most significant bit, as that is reserved and MUST be ignored
// when received.
increment: NetworkEndian::read_u32(bytes) & !INCREMENT_MASK,
size_increment,
})
}
@@ -44,7 +48,7 @@ impl WindowUpdate {
trace!("encoding WINDOW_UPDATE; id={:?}", self.stream_id);
let head = Head::new(Kind::Ping, 0, self.stream_id);
head.encode(4, dst);
dst.put_u32::<NetworkEndian>(self.increment);
dst.put_u32::<NetworkEndian>(self.size_increment);
}
}