Handle the remote returning a protocol error

This commit is contained in:
Carl Lerche
2017-08-07 22:35:29 -07:00
parent 71da8d121f
commit 441a8416c6
7 changed files with 98 additions and 155 deletions

View File

@@ -66,7 +66,7 @@ enum Inner {
HalfClosedLocal(Peer), // TODO: explicitly name this value
HalfClosedRemote(Peer),
// When reset, a reason is provided
Closed(Option<Reason>),
Closed(Option<Cause>),
}
#[derive(Debug, Copy, Clone)]
@@ -76,6 +76,12 @@ enum Peer {
Streaming(FlowControl),
}
#[derive(Debug, Copy, Clone)]
enum Cause {
Proto(Reason),
Io,
}
impl State {
/// Opens the send-half of a stream if it is not already open.
pub fn send_open(&mut self, sz: WindowSize, eos: bool) -> Result<(), ConnectionError> {
@@ -178,6 +184,19 @@ impl State {
}
}
pub fn recv_err(&mut self, err: &ConnectionError) {
match self.inner {
Closed(..) => {}
_ => {
self.inner = Closed(match *err {
ConnectionError::Proto(reason) => Some(Cause::Proto(reason)),
ConnectionError::Io(..) => Some(Cause::Io),
_ => panic!("cannot terminate stream with user error"),
});
}
}
}
/// Indicates that the local side will not send more data to the local.
pub fn send_close(&mut self) -> Result<(), ConnectionError> {
match self.inner {
@@ -225,6 +244,21 @@ impl State {
_ => None,
}
}
pub fn ensure_recv_open(&self) -> Result<(), ConnectionError> {
use std::io;
// TODO: Is this correct?
match self.inner {
Closed(Some(Cause::Proto(reason))) => {
Err(ConnectionError::Proto(reason))
}
Closed(Some(Cause::Io)) => {
Err(ConnectionError::Io(io::ErrorKind::BrokenPipe.into()))
}
_ => Ok(()),
}
}
}
impl Default for State {