use std::task::{self, Poll};
use bytes::Buf;
pub use crate::proto::stream::{InvalidStreamId, StreamId};
pub use crate::stream::WriteBuf;
pub trait Error: std::error::Error + Send + Sync {
fn is_timeout(&self) -> bool;
fn err_code(&self) -> Option<u64>;
}
impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
fn from(err: E) -> Box<dyn Error + 'a> {
Box::new(err)
}
}
pub trait Connection<B: Buf> {
type BidiStream: SendStream<B> + RecvStream;
type SendStream: SendStream<B>;
type RecvStream: RecvStream;
type OpenStreams: OpenStreams<B>;
type Error: Into<Box<dyn Error>>;
fn poll_accept_recv(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Option<Self::RecvStream>, Self::Error>>;
fn poll_accept_bidi(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Option<Self::BidiStream>, Self::Error>>;
fn poll_open_bidi(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Self::BidiStream, Self::Error>>;
fn poll_open_send(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Self::SendStream, Self::Error>>;
fn opener(&self) -> Self::OpenStreams;
fn close(&mut self, code: crate::error::Code, reason: &[u8]);
}
pub trait OpenStreams<B: Buf> {
type BidiStream: SendStream<B> + RecvStream;
type SendStream: SendStream<B>;
type RecvStream: RecvStream;
type Error: Into<Box<dyn Error>>;
fn poll_open_bidi(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Self::BidiStream, Self::Error>>;
fn poll_open_send(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Self::SendStream, Self::Error>>;
fn close(&mut self, code: crate::error::Code, reason: &[u8]);
}
pub trait SendStream<B: Buf> {
type Error: Into<Box<dyn Error>>;
fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>>;
fn send_data<T: Into<WriteBuf<B>>>(&mut self, data: T) -> Result<(), Self::Error>;
fn poll_finish(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>>;
fn reset(&mut self, reset_code: u64);
fn id(&self) -> StreamId;
}
pub trait RecvStream {
type Buf: Buf;
type Error: Into<Box<dyn Error>>;
fn poll_data(
&mut self,
cx: &mut task::Context<'_>,
) -> Poll<Result<Option<Self::Buf>, Self::Error>>;
fn stop_sending(&mut self, error_code: u64);
}
pub trait BidiStream<B: Buf>: SendStream<B> + RecvStream {
type SendStream: SendStream<B>;
type RecvStream: RecvStream;
fn split(self) -> (Self::SendStream, Self::RecvStream);
}