| 1 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 2 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 4 |
// option. This file may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
use std::fmt::{self, Display, Formatter}; |
| 8 |
|
| 9 |
use neqo_common::qdebug; |
| 10 |
use neqo_transport::{Connection, StreamId}; |
| 11 |
|
| 12 |
use crate::{ |
| 13 |
frames::{FrameReader, HFrame, StreamReaderConnectionWrapper}, |
| 14 |
CloseType, Error, Http3StreamType, ReceiveOutput, RecvStream, Res, Stream, |
| 15 |
}; |
| 16 |
|
| 17 |
/// The remote control stream is responsible only for reading frames. The frames are handled by |
| 18 |
/// `Http3Connection`. |
| 19 |
#[derive(Debug)] |
| 20 |
pub struct ControlStreamRemote { |
| 21 |
stream_id: StreamId, |
| 22 |
frame_reader: FrameReader, |
| 23 |
} |
| 24 |
|
| 25 |
impl Display for ControlStreamRemote { |
| 26 |
fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 27 |
write!(f, "Http3 remote control stream {:?}", self.stream_id) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
impl ControlStreamRemote { |
| 32 |
pub fn new(stream_id: StreamId) -> Self { |
| 33 |
Self { |
| 34 |
stream_id, |
| 35 |
frame_reader: FrameReader::new(), |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/// Check if a stream is the control stream and read received data. |
| 40 |
pub fn receive_single(&mut self, conn: &mut Connection) -> Res<Option<HFrame>> { |
| 41 |
qdebug!("[{self}] Receiving data"); |
| 42 |
match self |
| 43 |
.frame_reader |
| 44 |
.receive(&mut StreamReaderConnectionWrapper::new( |
| 45 |
conn, |
| 46 |
self.stream_id, |
| 47 |
))? { |
| 48 |
(_, true) => Err(Error::HttpClosedCriticalStream), |
| 49 |
(s, false) => { |
| 50 |
qdebug!("[{self}] received {s:?}"); |
| 51 |
Ok(s) |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
impl Stream for ControlStreamRemote { |
| 58 |
fn stream_type(&self) -> Http3StreamType { |
| 59 |
Http3StreamType::Control |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
impl RecvStream for ControlStreamRemote { |
| 64 |
fn reset(&mut self, _close_type: CloseType) -> Res<()> { |
| 65 |
Err(Error::HttpClosedCriticalStream) |
| 66 |
} |
| 67 |
|
| 68 |
fn receive(&mut self, conn: &mut Connection) -> Res<(ReceiveOutput, bool)> { |
| 69 |
let mut control_frames = Vec::new(); |
| 70 |
|
| 71 |
loop { |
| 72 |
if let Some(f) = self.receive_single(conn)? { |
| 73 |
control_frames.push(f); |
| 74 |
} else { |
| 75 |
return Ok((ReceiveOutput::ControlFrames(control_frames), false)); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
} |