nit on naming

This commit is contained in:
Oliver Gould
2017-07-16 17:08:39 +00:00
parent 76dbb5d285
commit 06d9978c53
7 changed files with 103 additions and 59 deletions

View File

@@ -1,7 +1,7 @@
use {ConnectionError, Frame, FrameSize}; use {ConnectionError, Frame, FrameSize};
use client::Client; use client::Client;
use frame::{self, StreamId}; use frame::{self, SettingSet, StreamId};
use proto::{self, Peer, ReadySink, FlowTransporter, WindowSize}; use proto::{self, ControlSettings, Peer, ReadySink, ControlFlow, WindowSize};
use server::Server; use server::Server;
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
@@ -32,21 +32,33 @@ pub fn new<T, P, B>(transport: proto::Transport<T, P, B::Buf>)
} }
} }
impl<T, P, B> Connection<T, P, B>
where T: FlowTransporter, impl<T, P, B> ControlSettings for Connection<T, P, B>
where T: ControlSettings,
B: IntoBuf, B: IntoBuf,
{ {
/// Polls for the amount of additional data that may be sent to a remote. fn update_local_settings(&mut self, local: frame::SettingSet) -> Result<(), ConnectionError> {
/// self.inner.update_local_settings(local)
/// Connection and stream updates are distinct. }
pub fn poll_remote_window_update(&mut self, id: StreamId) -> Poll<WindowSize, ConnectionError> {
fn local_settings(&self) -> &SettingSet {
self.inner.local_settings()
}
fn remote_settings(&self) -> &SettingSet {
self.inner.remote_settings()
}
}
impl<T, P, B> ControlFlow for Connection<T, P, B>
where T: ControlFlow,
B: IntoBuf,
{
fn poll_remote_window_update(&mut self, id: StreamId) -> Poll<WindowSize, ConnectionError> {
self.inner.poll_remote_window_update(id) self.inner.poll_remote_window_update(id)
} }
/// Increases the amount of data that the remote endpoint may send. fn grow_local_window(&mut self, id: StreamId, incr: WindowSize) -> Result<(), ConnectionError> {
///
/// Connection and stream updates are distinct.
pub fn grow_local_window(&mut self, id: StreamId, incr: WindowSize) -> Result<(), ConnectionError> {
self.inner.grow_local_window(id, incr) self.inner.grow_local_window(id, incr)
} }
} }

View File

@@ -13,10 +13,10 @@ pub struct FlowControl<T> {
/// Tracks the connection-level flow control window for receiving data from the /// Tracks the connection-level flow control window for receiving data from the
/// remote. /// remote.
local_flow_controller: FlowController, local_flow_controller: FlowControlState,
/// Tracks the onnection-level flow control window for receiving data from the remote. /// Tracks the onnection-level flow control window for receiving data from the remote.
remote_flow_controller: FlowController, remote_flow_controller: FlowControlState,
/// Holds the list of streams on which local window updates may be sent. /// Holds the list of streams on which local window updates may be sent.
// XXX It would be cool if this didn't exist. // XXX It would be cool if this didn't exist.
@@ -34,7 +34,7 @@ pub struct FlowControl<T> {
impl<T, U> FlowControl<T> impl<T, U> FlowControl<T>
where T: Stream<Item = Frame, Error = ConnectionError>, where T: Stream<Item = Frame, Error = ConnectionError>,
T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>, T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>,
T: StreamTransporter T: ControlStreams
{ {
pub fn new(initial_local_window_size: u32, pub fn new(initial_local_window_size: u32,
initial_remote_window_size: u32, initial_remote_window_size: u32,
@@ -45,8 +45,8 @@ impl<T, U> FlowControl<T>
inner, inner,
initial_local_window_size, initial_local_window_size,
initial_remote_window_size, initial_remote_window_size,
local_flow_controller: FlowController::new(initial_local_window_size), local_flow_controller: FlowControlState::new(initial_local_window_size),
remote_flow_controller: FlowController::new(initial_remote_window_size), remote_flow_controller: FlowControlState::new(initial_remote_window_size),
blocked_remote_window_update: None, blocked_remote_window_update: None,
sending_local_window_update: None, sending_local_window_update: None,
pending_local_window_updates: VecDeque::new(), pending_local_window_updates: VecDeque::new(),
@@ -54,7 +54,7 @@ impl<T, U> FlowControl<T>
} }
} }
impl<T: StreamTransporter> FlowControl<T> { impl<T: ControlStreams> FlowControl<T> {
fn claim_local_window(&mut self, id: &StreamId, len: WindowSize) -> Result<(), ConnectionError> { fn claim_local_window(&mut self, id: &StreamId, len: WindowSize) -> Result<(), ConnectionError> {
let res = if id.is_zero() { let res = if id.is_zero() {
self.local_flow_controller.claim_window(len) self.local_flow_controller.claim_window(len)
@@ -106,7 +106,7 @@ impl<T: StreamTransporter> FlowControl<T> {
} }
} }
impl<T: StreamTransporter> FlowTransporter for FlowControl<T> { impl<T: ControlStreams> ControlFlow for FlowControl<T> {
fn poll_remote_window_update(&mut self, id: StreamId) -> Poll<WindowSize, ConnectionError> { fn poll_remote_window_update(&mut self, id: StreamId) -> Poll<WindowSize, ConnectionError> {
if id.is_zero() { if id.is_zero() {
if let Some(sz) = self.remote_flow_controller.take_window_update() { if let Some(sz) = self.remote_flow_controller.take_window_update() {
@@ -139,7 +139,7 @@ impl<T: StreamTransporter> FlowTransporter for FlowControl<T> {
} }
} }
impl<T: StreamTransporter> StreamTransporter for FlowControl<T> { impl<T: ControlStreams> ControlStreams for FlowControl<T> {
#[inline] #[inline]
fn streams(&self) -> &StreamMap { fn streams(&self) -> &StreamMap {
self.inner.streams() self.inner.streams()
@@ -153,7 +153,7 @@ impl<T: StreamTransporter> StreamTransporter for FlowControl<T> {
impl<T, U> FlowControl<T> impl<T, U> FlowControl<T>
where T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>, where T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>,
T: StreamTransporter, T: ControlStreams,
{ {
/// Returns ready when there are no pending window updates to send. /// Returns ready when there are no pending window updates to send.
fn poll_send_local_window_updates(&mut self) -> Poll<(), ConnectionError> { fn poll_send_local_window_updates(&mut self) -> Poll<(), ConnectionError> {
@@ -199,7 +199,7 @@ impl<T, U> FlowControl<T>
/// > positive. /// > positive.
impl<T> ApplySettings for FlowControl<T> impl<T> ApplySettings for FlowControl<T>
where T: ApplySettings, where T: ApplySettings,
T: StreamTransporter T: ControlStreams
{ {
fn apply_local_settings(&mut self, set: &frame::SettingSet) -> Result<(), ConnectionError> { fn apply_local_settings(&mut self, set: &frame::SettingSet) -> Result<(), ConnectionError> {
self.inner.apply_local_settings(set)?; self.inner.apply_local_settings(set)?;
@@ -248,7 +248,7 @@ impl<T> ApplySettings for FlowControl<T>
impl<T> Stream for FlowControl<T> impl<T> Stream for FlowControl<T>
where T: Stream<Item = Frame, Error = ConnectionError>, where T: Stream<Item = Frame, Error = ConnectionError>,
T: StreamTransporter, T: ControlStreams,
{ {
type Item = T::Item; type Item = T::Item;
type Error = T::Error; type Error = T::Error;
@@ -278,7 +278,7 @@ impl<T> Stream for FlowControl<T>
impl<T, U> Sink for FlowControl<T> impl<T, U> Sink for FlowControl<T>
where T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>, where T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>,
T: ReadySink, T: ReadySink,
T: StreamTransporter, T: ControlStreams,
{ {
type SinkItem = T::SinkItem; type SinkItem = T::SinkItem;
type SinkError = T::SinkError; type SinkError = T::SinkError;
@@ -318,7 +318,7 @@ impl<T, U> ReadySink for FlowControl<T>
where T: Stream<Item = Frame, Error = ConnectionError>, where T: Stream<Item = Frame, Error = ConnectionError>,
T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>, T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>,
T: ReadySink, T: ReadySink,
T: StreamTransporter, T: ControlStreams,
{ {
fn poll_ready(&mut self) -> Poll<(), ConnectionError> { fn poll_ready(&mut self) -> Poll<(), ConnectionError> {
try_ready!(self.inner.poll_ready()); try_ready!(self.inner.poll_ready());

View File

@@ -6,7 +6,7 @@ pub struct WindowUnderflow;
pub const DEFAULT_INITIAL_WINDOW_SIZE: WindowSize = 65_535; pub const DEFAULT_INITIAL_WINDOW_SIZE: WindowSize = 65_535;
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct FlowController { pub struct FlowControlState {
/// Amount that may be claimed. /// Amount that may be claimed.
window_size: WindowSize, window_size: WindowSize,
/// Amount to be removed by future increments. /// Amount to be removed by future increments.
@@ -16,15 +16,15 @@ pub struct FlowController {
next_window_update: WindowSize, next_window_update: WindowSize,
} }
impl Default for FlowController { impl Default for FlowControlState {
fn default() -> Self { fn default() -> Self {
Self::new(DEFAULT_INITIAL_WINDOW_SIZE) Self::new(DEFAULT_INITIAL_WINDOW_SIZE)
} }
} }
impl FlowController { impl FlowControlState {
pub fn new(window_size: WindowSize) -> FlowController { pub fn new(window_size: WindowSize) -> FlowControlState {
FlowController { FlowControlState {
window_size, window_size,
underflow: 0, underflow: 0,
next_window_update: 0, next_window_update: 0,
@@ -78,6 +78,6 @@ impl FlowController {
#[test] #[test]
fn test() { fn test() {
let mut fc = FlowController::new(65_535); let mut fc = FlowControlState::new(65_535);
} }

View File

@@ -1,4 +1,5 @@
use {frame, ConnectionError, Peer, StreamId}; use {frame, ConnectionError, Peer, StreamId};
use frame::SettingSet;
use bytes::{Buf, IntoBuf}; use bytes::{Buf, IntoBuf};
use fnv::FnvHasher; use fnv::FnvHasher;
use futures::*; use futures::*;
@@ -9,7 +10,7 @@ use tokio_io::codec::length_delimited;
mod connection; mod connection;
mod flow_control; mod flow_control;
mod flow_controller; mod flow_control_state;
mod framed_read; mod framed_read;
mod framed_write; mod framed_write;
mod ping_pong; mod ping_pong;
@@ -20,7 +21,7 @@ mod stream_tracker;
pub use self::connection::Connection; pub use self::connection::Connection;
pub use self::flow_control::FlowControl; pub use self::flow_control::FlowControl;
pub use self::flow_controller::{FlowController, WindowUnderflow}; pub use self::flow_control_state::{FlowControlState, WindowUnderflow};
pub use self::framed_read::FramedRead; pub use self::framed_read::FramedRead;
pub use self::framed_write::FramedWrite; pub use self::framed_write::FramedWrite;
pub use self::ping_pong::PingPong; pub use self::ping_pong::PingPong;
@@ -97,19 +98,36 @@ impl StreamMap {
} }
} }
/// Allows settings to be applied from the top of the stack to the lower levels.d /// Allows settings updates to be pushed "down" the transport (i.e. below Settings).
pub trait ApplySettings { pub trait ApplySettings {
fn apply_local_settings(&mut self, set: &frame::SettingSet) -> Result<(), ConnectionError>; fn apply_local_settings(&mut self, set: &frame::SettingSet) -> Result<(), ConnectionError>;
fn apply_remote_settings(&mut self, set: &frame::SettingSet) -> Result<(), ConnectionError>; fn apply_remote_settings(&mut self, set: &frame::SettingSet) -> Result<(), ConnectionError>;
} }
pub trait StreamTransporter { /// Exposes settings to "upper" layers of the transport (i.e. above Settings).
pub trait ControlSettings {
fn update_local_settings(&mut self, set: frame::SettingSet) -> Result<(), ConnectionError>;
fn local_settings(&self) -> &SettingSet;
fn remote_settings(&self) -> &SettingSet;
}
/// Exposes stream states to "upper" layers of the transport (i.e. above StreamTracker).
pub trait ControlStreams {
fn streams(&self)-> &StreamMap; fn streams(&self)-> &StreamMap;
fn streams_mut(&mut self) -> &mut StreamMap; fn streams_mut(&mut self) -> &mut StreamMap;
} }
pub trait FlowTransporter { /// Exposes flow control states to "upper" layers of the transport (i.e. above
/// FlowControl).
pub trait ControlFlow {
/// Asks the flow controller for unreported send capacity on a stream.
///
/// Errors if the given stream is not active.
fn poll_remote_window_update(&mut self, id: StreamId) -> Poll<WindowSize, ConnectionError>; fn poll_remote_window_update(&mut self, id: StreamId) -> Poll<WindowSize, ConnectionError>;
/// Attempts to increase the receive capacity of a stream.
///
/// Errors if the given stream is not active.
fn grow_local_window(&mut self, id: StreamId, incr: WindowSize) -> Result<(), ConnectionError>; fn grow_local_window(&mut self, id: StreamId, incr: WindowSize) -> Result<(), ConnectionError>;
} }
@@ -128,10 +146,10 @@ pub fn from_io<T, P, B>(io: T, settings: frame::SettingSet)
// weird, but oh well... // weird, but oh well...
// //
// We first create a Settings directly around a framed writer // We first create a Settings directly around a framed writer
let settings = Settings::new( let transport = Settings::new(
framed_write, settings); framed_write, settings);
from_server_handshaker(settings) from_server_handshaker(transport)
} }
/// Create a transport prepared to handle the server handshake. /// Create a transport prepared to handle the server handshake.
@@ -145,7 +163,6 @@ pub fn server_handshaker<T, B>(io: T, settings: frame::SettingSet)
B: Buf, B: Buf,
{ {
let framed_write = FramedWrite::new(io); let framed_write = FramedWrite::new(io);
Settings::new(framed_write, settings) Settings::new(framed_write, settings)
} }

View File

@@ -1,8 +1,7 @@
use {StreamId, ConnectionError}; use {StreamId, ConnectionError};
use frame::{self, Frame}; use frame::{self, Frame, SettingSet};
use proto::{ApplySettings, ReadySink, StreamMap, StreamTransporter, FlowTransporter, WindowSize}; use proto::*;
use futures::*;
use tokio_io::AsyncRead; use tokio_io::AsyncRead;
use bytes::BufMut; use bytes::BufMut;
@@ -16,10 +15,10 @@ pub struct Settings<T> {
inner: T, inner: T,
// Our settings // Our settings
local: frame::SettingSet, local: SettingSet,
// Peer settings // Peer settings
remote: frame::SettingSet, remote: SettingSet,
// Number of acks remaining to send to the peer // Number of acks remaining to send to the peer
remaining_acks: usize, remaining_acks: usize,
@@ -34,22 +33,22 @@ pub struct Settings<T> {
impl<T, U> Settings<T> impl<T, U> Settings<T>
where T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>, where T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>,
{ {
pub fn new(inner: T, local: frame::SettingSet) -> Settings<T> { pub fn new(inner: T, local: SettingSet) -> Settings<T> {
Settings { Settings {
inner: inner, inner: inner,
local: local, local: local,
remote: frame::SettingSet::default(), remote: SettingSet::default(),
remaining_acks: 0, remaining_acks: 0,
is_local_dirty: true, is_local_dirty: true,
received_remote: false, received_remote: false,
} }
} }
pub fn local_settings(&self) -> &frame::SettingSet { pub fn local_settings(&self) -> &SettingSet {
&self.local &self.local
} }
pub fn remote_settings(&self) -> &frame::SettingSet { pub fn remote_settings(&self) -> &SettingSet {
&self.local &self.local
} }
@@ -96,7 +95,7 @@ impl<T, U> Settings<T>
} }
} }
impl<T: StreamTransporter> StreamTransporter for Settings<T> { impl<T: ControlStreams> ControlStreams for Settings<T> {
fn streams(&self) -> &StreamMap { fn streams(&self) -> &StreamMap {
self.inner.streams() self.inner.streams()
} }
@@ -106,7 +105,7 @@ impl<T: StreamTransporter> StreamTransporter for Settings<T> {
} }
} }
impl<T: FlowTransporter> FlowTransporter for Settings<T> { impl<T: ControlFlow> ControlFlow for Settings<T> {
fn poll_remote_window_update(&mut self, id: StreamId) -> Poll<WindowSize, ConnectionError> { fn poll_remote_window_update(&mut self, id: StreamId) -> Poll<WindowSize, ConnectionError> {
self.inner.poll_remote_window_update(id) self.inner.poll_remote_window_update(id)
} }
@@ -116,6 +115,22 @@ impl<T: FlowTransporter> FlowTransporter for Settings<T> {
} }
} }
impl<T> ControlSettings for Settings<T>{
fn update_local_settings(&mut self, local: frame::SettingSet) -> Result<(), ConnectionError> {
self.local = local;
self.is_local_dirty = true;
Ok(())
}
fn local_settings(&self) -> &SettingSet {
&self.local
}
fn remote_settings(&self) -> &SettingSet {
&self.remote
}
}
impl<T, U> Stream for Settings<T> impl<T, U> Stream for Settings<T>
where T: Stream<Item = Frame, Error = ConnectionError>, where T: Stream<Item = Frame, Error = ConnectionError>,
T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>, T: Sink<SinkItem = Frame<U>, SinkError = ConnectionError>,

View File

@@ -2,7 +2,7 @@ use Peer;
use error::ConnectionError; use error::ConnectionError;
use error::Reason::*; use error::Reason::*;
use error::User::*; use error::User::*;
use proto::{FlowController, WindowSize, WindowUnderflow}; use proto::{FlowControlState, WindowSize, WindowUnderflow};
/// Represents the state of an H2 stream /// Represents the state of an H2 stream
/// ///
@@ -78,7 +78,7 @@ impl StreamState {
if eos { if eos {
*self = HalfClosedRemote(local); *self = HalfClosedRemote(local);
} else { } else {
*self = Open { local, remote: Data(FlowController::new(initial_recv_window_size)) }; *self = Open { local, remote: Data(FlowControlState::new(initial_recv_window_size)) };
} }
Ok(true) Ok(true)
} }
@@ -98,7 +98,7 @@ impl StreamState {
if eos { if eos {
*self = Closed; *self = Closed;
} else { } else {
*self = HalfClosedLocal(Data(FlowController::new(initial_recv_window_size))); *self = HalfClosedLocal(Data(FlowControlState::new(initial_recv_window_size)));
}; };
Ok(false) Ok(false)
} }
@@ -155,7 +155,7 @@ impl StreamState {
HalfClosedLocal(Headers) HalfClosedLocal(Headers)
} else { } else {
Open { Open {
local: Data(FlowController::new(initial_window_size)), local: Data(FlowControlState::new(initial_window_size)),
remote: Headers, remote: Headers,
} }
}; };
@@ -169,7 +169,7 @@ impl StreamState {
*self = if eos { *self = if eos {
HalfClosedLocal(remote) HalfClosedLocal(remote)
} else { } else {
let local = Data(FlowController::new(initial_window_size)); let local = Data(FlowControlState::new(initial_window_size));
Open { local, remote } Open { local, remote }
}; };
@@ -182,7 +182,7 @@ impl StreamState {
*self = if eos { *self = if eos {
Closed Closed
} else { } else {
HalfClosedRemote(Data(FlowController::new(initial_window_size))) HalfClosedRemote(Data(FlowControlState::new(initial_window_size)))
}; };
Ok(false) Ok(false)
@@ -357,8 +357,8 @@ impl Default for StreamState {
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub enum PeerState { pub enum PeerState {
Headers, Headers,
/// Contains a FlowController representing the _receiver_ of this this data stream. /// Contains a FlowControlState representing the _receiver_ of this this data stream.
Data(FlowController), Data(FlowControlState),
} }
impl PeerState { impl PeerState {

View File

@@ -41,7 +41,7 @@ impl<T, P, U> StreamTracker<T, P>
} }
} }
impl<T, P> StreamTransporter for StreamTracker<T, P> { impl<T, P> ControlStreams for StreamTracker<T, P> {
#[inline] #[inline]
fn streams(&self) -> &StreamMap { fn streams(&self) -> &StreamMap {
&self.streams &self.streams