pub mod buffer;
pub mod state;
pub use buffer::{RecvBuffer, SendBuffer};
pub use state::{StateMachine, StreamState};
use crate::flow_control::FlowControl;
use crate::frame::StreamId;
use crate::hpack::HeaderField;
#[derive(Debug)]
pub struct Stream {
id: StreamId,
state: StateMachine,
flow_control: FlowControl,
headers: Vec<HeaderField>,
recv_buffer: RecvBuffer,
send_buffer: SendBuffer,
pending_end_stream: bool,
initial_headers_received: bool,
expected_content_length: Option<u64>,
received_content_length: u64,
connect_established: bool,
request_method: Option<Vec<u8>>,
has_protocol: bool,
protocol: Option<Vec<u8>>,
no_content: bool,
final_response_sent: bool,
}
impl Stream {
#[must_use]
pub fn new(id: StreamId, send_initial_window_size: u32, recv_initial_window_size: u32) -> Self {
Self {
id,
state: StateMachine::new(),
flow_control: FlowControl::with_separate_windows(
send_initial_window_size,
recv_initial_window_size,
),
headers: Vec::new(),
recv_buffer: RecvBuffer::new(recv_initial_window_size as usize),
send_buffer: SendBuffer::new(send_initial_window_size as usize),
pending_end_stream: false,
initial_headers_received: false,
expected_content_length: None,
received_content_length: 0,
connect_established: false,
request_method: None,
has_protocol: false,
protocol: None,
no_content: false,
final_response_sent: false,
}
}
#[must_use]
pub const fn id(&self) -> StreamId {
self.id
}
#[must_use]
pub const fn state(&self) -> StreamState {
self.state.state()
}
#[must_use]
pub const fn state_machine(&self) -> &StateMachine {
&self.state
}
pub fn state_machine_mut(&mut self) -> &mut StateMachine {
&mut self.state
}
#[must_use]
pub const fn flow_control(&self) -> &FlowControl {
&self.flow_control
}
pub fn flow_control_mut(&mut self) -> &mut FlowControl {
&mut self.flow_control
}
pub fn set_headers(&mut self, headers: Vec<HeaderField>) {
self.headers = headers;
}
#[must_use]
pub fn headers(&self) -> &[HeaderField] {
&self.headers
}
#[must_use]
pub const fn recv_buffer(&self) -> &RecvBuffer {
&self.recv_buffer
}
pub fn recv_buffer_mut(&mut self) -> &mut RecvBuffer {
&mut self.recv_buffer
}
#[must_use]
pub const fn send_buffer(&self) -> &SendBuffer {
&self.send_buffer
}
pub fn send_buffer_mut(&mut self) -> &mut SendBuffer {
&mut self.send_buffer
}
#[must_use]
pub const fn pending_end_stream(&self) -> bool {
self.pending_end_stream
}
pub fn set_pending_end_stream(&mut self, value: bool) {
self.pending_end_stream = value;
}
#[must_use]
pub const fn is_open(&self) -> bool {
self.state.state().is_open()
}
#[must_use]
pub const fn is_closed(&self) -> bool {
self.state.state().is_closed()
}
#[must_use]
pub const fn initial_headers_received(&self) -> bool {
self.initial_headers_received
}
pub fn set_initial_headers_received(&mut self, received: bool) {
self.initial_headers_received = received;
}
pub fn set_expected_content_length(&mut self, length: Option<u64>) {
self.expected_content_length = length;
}
#[must_use]
pub const fn expected_content_length(&self) -> Option<u64> {
self.expected_content_length
}
#[must_use]
pub const fn received_content_length(&self) -> u64 {
self.received_content_length
}
pub fn add_received_content_length(&mut self, length: u64) {
self.received_content_length += length;
}
#[must_use]
pub const fn connect_established(&self) -> bool {
self.connect_established
}
pub fn set_connect_established(&mut self, established: bool) {
self.connect_established = established;
}
#[must_use]
pub fn request_method(&self) -> Option<&[u8]> {
self.request_method.as_deref()
}
pub fn set_request_method(&mut self, method: Vec<u8>) {
self.request_method = Some(method);
}
#[must_use]
pub const fn has_protocol(&self) -> bool {
self.has_protocol
}
pub fn set_has_protocol(&mut self, has_protocol: bool) {
self.has_protocol = has_protocol;
}
#[must_use]
pub fn protocol(&self) -> Option<&[u8]> {
self.protocol.as_deref()
}
pub fn set_protocol(&mut self, protocol: Vec<u8>) {
self.protocol = Some(protocol);
}
#[must_use]
pub const fn no_content(&self) -> bool {
self.no_content
}
pub fn set_no_content(&mut self, no_content: bool) {
self.no_content = no_content;
}
#[must_use]
pub const fn final_response_sent(&self) -> bool {
self.final_response_sent
}
pub fn set_final_response_sent(&mut self, sent: bool) {
self.final_response_sent = sent;
}
}