tests pass
This commit is contained in:
		| @@ -72,6 +72,7 @@ impl<T, P, B> Connection<T, P, B> | ||||
|                      end_of_stream: bool) | ||||
|         -> sink::Send<Self> | ||||
|     { | ||||
|         trace!("send_data: id={:?}", id); | ||||
|         self.send(Frame::Data { | ||||
|             id, | ||||
|             data, | ||||
|   | ||||
| @@ -407,12 +407,12 @@ impl<T: ControlStreams> ControlStreams for FlowControl<T> { | ||||
|         self.inner.send_flow_controller(id) | ||||
|     } | ||||
|  | ||||
|     fn check_can_send_data(&mut self, id: StreamId) -> Result<(), ConnectionError> { | ||||
|         self.inner.check_can_send_data(id) | ||||
|     fn can_send_data(&mut self, id: StreamId) -> bool { | ||||
|         self.inner.can_send_data(id) | ||||
|     } | ||||
|  | ||||
|     fn check_can_recv_data(&mut self, id: StreamId) -> Result<(), ConnectionError>  { | ||||
|         self.inner.check_can_recv_data(id) | ||||
|     fn can_recv_data(&mut self, id: StreamId) -> bool  { | ||||
|         self.inner.can_recv_data(id) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -155,7 +155,7 @@ impl<T, B> Sink for FramedWrite<T, B> | ||||
|     } | ||||
|  | ||||
|     fn poll_complete(&mut self) -> Poll<(), ConnectionError> { | ||||
|         trace!("FramedWrite::poll_complete"); | ||||
|         trace!("poll_complete"); | ||||
|  | ||||
|         // TODO: implement | ||||
|         match self.next { | ||||
| @@ -165,6 +165,7 @@ impl<T, B> Sink for FramedWrite<T, B> | ||||
|  | ||||
|         // As long as there is data to write, try to write it! | ||||
|         while !self.is_empty() { | ||||
|             trace!("writing {}", self.buf.remaining()); | ||||
|             try_ready!(self.inner.write_buf(&mut self.buf)); | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -1,6 +1,5 @@ | ||||
| use ConnectionError; | ||||
| use error::Reason::*; | ||||
| use error::User::*; | ||||
| use proto::{FlowControlState, WindowSize}; | ||||
|  | ||||
| /// Represents the state of an H2 stream | ||||
| @@ -156,40 +155,24 @@ impl StreamState { | ||||
|         Ok(true) | ||||
|     } | ||||
|  | ||||
|     pub fn check_can_send_data(&self) -> Result<(), ConnectionError> { | ||||
|     pub fn can_send_data(&self) -> bool { | ||||
|         use self::StreamState::*; | ||||
|         match self { | ||||
|             &Open { ref remote, .. } => { | ||||
|                 try!(remote.check_streaming(UnexpectedFrameType.into())); | ||||
|                 Ok(()) | ||||
|             } | ||||
|             &Idle | &Closed | &HalfClosedRemote(..) => false, | ||||
|  | ||||
|             &HalfClosedLocal(ref remote) => { | ||||
|                 try!(remote.check_streaming(UnexpectedFrameType.into())); | ||||
|                 Ok(()) | ||||
|             } | ||||
|  | ||||
|             &Idle | &Closed | &HalfClosedRemote(..) => { | ||||
|                 Err(UnexpectedFrameType.into()) | ||||
|             } | ||||
|             &Open { ref remote, .. } | | ||||
|             &HalfClosedLocal(ref remote) => remote.is_streaming(), | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     pub fn check_can_recv_data(&self) -> Result<(), ConnectionError> { | ||||
|     pub fn can_recv_data(&self) -> bool { | ||||
|         use self::StreamState::*; | ||||
|         match self { | ||||
|             &Open { ref local, .. } => { | ||||
|                 try!(local.check_streaming(ProtocolError.into())); | ||||
|                 Ok(()) | ||||
|             } | ||||
|             &Idle | &Closed | &HalfClosedLocal(..) => false, | ||||
|  | ||||
|             &Open { ref local, .. } | | ||||
|             &HalfClosedRemote(ref local) => { | ||||
|                 try!(local.check_streaming(ProtocolError.into())); | ||||
|                 Ok(()) | ||||
|             } | ||||
|  | ||||
|             &Idle | &Closed | &HalfClosedLocal(..) => { | ||||
|                 Err(ProtocolError.into()) | ||||
|                 local.is_streaming() | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| @@ -288,11 +271,11 @@ impl PeerState { | ||||
|     } | ||||
|  | ||||
|     #[inline] | ||||
|     fn check_streaming(&self, err: ConnectionError) -> Result<(), ConnectionError> { | ||||
|     fn is_streaming(&self) -> bool { | ||||
|         use self::PeerState::*; | ||||
|         match self { | ||||
|             &Streaming(..) => Ok(()), | ||||
|             _ => Err(err), | ||||
|             &Streaming(..) => true, | ||||
|             _ => false, | ||||
|         } | ||||
|     } | ||||
|  | ||||
|   | ||||
| @@ -157,12 +157,12 @@ impl<T: ControlStreams> ControlStreams for StreamRecvClose<T> { | ||||
|         self.inner.send_flow_controller(id) | ||||
|     } | ||||
|  | ||||
|     fn check_can_send_data(&mut self, id: StreamId) -> Result<(), ConnectionError> { | ||||
|         self.inner.check_can_send_data(id) | ||||
|     fn can_send_data(&mut self, id: StreamId) -> bool { | ||||
|         self.inner.can_send_data(id) | ||||
|     } | ||||
|  | ||||
|     fn check_can_recv_data(&mut self, id: StreamId) -> Result<(), ConnectionError>  { | ||||
|         self.inner.check_can_recv_data(id) | ||||
|     fn can_recv_data(&mut self, id: StreamId) -> bool  { | ||||
|         self.inner.can_recv_data(id) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -170,7 +170,9 @@ impl<T, U> Stream for StreamRecvOpen<T> | ||||
|  | ||||
|             if let &Data(..) = &frame { | ||||
|                 // Ensures we've already received headers for this stream. | ||||
|                 self.inner.check_can_recv_data(id)?; | ||||
|                 if !self.inner.can_recv_data(id) { | ||||
|                     return Err(ProtocolError.into()); | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             // If the frame ends the stream, it will be handled in | ||||
| @@ -400,12 +402,12 @@ impl<T: ControlStreams> ControlStreams for StreamRecvOpen<T> { | ||||
|         self.inner.send_flow_controller(id) | ||||
|     } | ||||
|  | ||||
|     fn check_can_send_data(&mut self, id: StreamId) -> Result<(), ConnectionError> { | ||||
|         self.inner.check_can_send_data(id) | ||||
|     fn can_send_data(&mut self, id: StreamId) -> bool { | ||||
|         self.inner.can_send_data(id) | ||||
|     } | ||||
|  | ||||
|     fn check_can_recv_data(&mut self, id: StreamId) -> Result<(), ConnectionError>  { | ||||
|         self.inner.check_can_recv_data(id) | ||||
|     fn can_recv_data(&mut self, id: StreamId) -> bool  { | ||||
|         self.inner.can_recv_data(id) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -162,12 +162,12 @@ impl<T: ControlStreams> ControlStreams for StreamSendClose<T> { | ||||
|         self.inner.send_flow_controller(id) | ||||
|     } | ||||
|  | ||||
|     fn check_can_send_data(&mut self, id: StreamId) -> Result<(), ConnectionError> { | ||||
|         self.inner.check_can_send_data(id) | ||||
|     fn can_send_data(&mut self, id: StreamId) -> bool { | ||||
|         self.inner.can_send_data(id) | ||||
|     } | ||||
|  | ||||
|     fn check_can_recv_data(&mut self, id: StreamId) -> Result<(), ConnectionError>  { | ||||
|         self.inner.check_can_recv_data(id) | ||||
|     fn can_recv_data(&mut self, id: StreamId) -> bool  { | ||||
|         self.inner.can_recv_data(id) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -1,5 +1,5 @@ | ||||
| use ConnectionError; | ||||
| use error::User::{InvalidStreamId, StreamReset, Rejected}; | ||||
| use error::User::{InactiveStreamId, InvalidStreamId, StreamReset, Rejected, UnexpectedFrameType}; | ||||
| use frame::{Frame, SettingSet}; | ||||
| use proto::*; | ||||
|  | ||||
| @@ -103,7 +103,11 @@ impl<T, U> Sink for StreamSendOpen<T> | ||||
|         } | ||||
|  | ||||
|         if T::local_valid_id(id) { | ||||
|             if !self.inner.is_local_active(id) { | ||||
|             if self.inner.is_local_active(id) { | ||||
|                 if !self.inner.can_send_data(id) { | ||||
|                     return Err(InactiveStreamId.into()); | ||||
|                 } | ||||
|             } else { | ||||
|                 if !T::local_can_open() { | ||||
|                     return Err(InvalidStreamId.into()); | ||||
|                 } | ||||
| @@ -114,8 +118,11 @@ impl<T, U> Sink for StreamSendOpen<T> | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|                 trace!("creating new local stream"); | ||||
|                 self.inner.local_open(id, self.initial_window_size)?; | ||||
|                 if let &Frame::Headers(..) = &frame { | ||||
|                     self.inner.local_open(id, self.initial_window_size)?; | ||||
|                 } else { | ||||
|                     return Err(InactiveStreamId.into()); | ||||
|                 } | ||||
|             } | ||||
|         } else { | ||||
|             // If the frame is part of a remote stream, it MUST already exist. | ||||
| @@ -130,7 +137,9 @@ impl<T, U> Sink for StreamSendOpen<T> | ||||
|  | ||||
|         if let &Frame::Data(..) = &frame { | ||||
|             // Ensures we've already sent headers for this stream. | ||||
|             self.inner.check_can_send_data(id)?; | ||||
|             if !self.inner.can_send_data(id) { | ||||
|                 return Err(InactiveStreamId.into()); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         trace!("sending frame..."); | ||||
| @@ -230,12 +239,12 @@ impl<T: ControlStreams> ControlStreams for StreamSendOpen<T> { | ||||
|         self.inner.send_flow_controller(id) | ||||
|     } | ||||
|  | ||||
|     fn check_can_send_data(&mut self, id: StreamId) -> Result<(), ConnectionError> { | ||||
|         self.inner.check_can_send_data(id) | ||||
|     fn can_send_data(&mut self, id: StreamId) -> bool { | ||||
|         self.inner.can_send_data(id) | ||||
|     } | ||||
|  | ||||
|     fn check_can_recv_data(&mut self, id: StreamId) -> Result<(), ConnectionError>  { | ||||
|         self.inner.check_can_recv_data(id) | ||||
|     fn can_recv_data(&mut self, id: StreamId) -> bool  { | ||||
|         self.inner.can_recv_data(id) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -76,8 +76,8 @@ pub trait ControlStreams { | ||||
|     fn update_inital_recv_window_size(&mut self, old_sz: u32, new_sz: u32); | ||||
|     fn update_inital_send_window_size(&mut self, old_sz: u32, new_sz: u32); | ||||
|  | ||||
|     fn check_can_send_data(&mut self, id: StreamId) -> Result<(), ConnectionError>; | ||||
|     fn check_can_recv_data(&mut self, id: StreamId) -> Result<(), ConnectionError>; | ||||
|     fn can_send_data(&mut self, id: StreamId) -> bool; | ||||
|     fn can_recv_data(&mut self, id: StreamId) -> bool; | ||||
| } | ||||
|  | ||||
| /// Holds the underlying stream state to be accessed by upper layers. | ||||
| @@ -366,18 +366,18 @@ impl<T, P: Peer> ControlStreams for StreamStore<T, P> { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     fn check_can_send_data(&mut self, id: StreamId) -> Result<(), ConnectionError> { | ||||
|         if let Some(s) = self.get_active(id) { | ||||
|             return s.check_can_send_data(); | ||||
|     fn can_send_data(&mut self, id: StreamId) -> bool { | ||||
|         match self.get_active(id) { | ||||
|             Some(s) => s.can_send_data(), | ||||
|             None => false, | ||||
|         } | ||||
|         Err(ProtocolError.into()) | ||||
|     } | ||||
|  | ||||
|     fn check_can_recv_data(&mut self, id: StreamId) -> Result<(), ConnectionError>  { | ||||
|         if let Some(s) = self.get_active(id) { | ||||
|             return s.check_can_recv_data(); | ||||
|     fn can_recv_data(&mut self, id: StreamId) -> bool  { | ||||
|         match self.get_active(id) { | ||||
|             Some(s) => s.can_recv_data(), | ||||
|             None => false, | ||||
|         } | ||||
|         Err(ProtocolError.into()) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user