use crate::{Buf, BufError, BufMut, BufResult, Codec, Cursor, quicv1::VarInt};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u64)]
pub enum FrameType {
Padding = 0x00,
Ping = 0x01,
Ack = 0x02,
ResetStream = 0x04,
StopSending = 0x05,
Crypto = 0x06,
NewToken = 0x07,
Stream = 0x08,
MaxData = 0x10,
MaxStreamData = 0x11,
MaxStreams = 0x12,
DataBlocked = 0x14,
StreamDataBlocked = 0x15,
StreamsBlocked = 0x16,
NewConnectionId = 0x18,
RetireConnectionId = 0x19,
PathChallenge = 0x1a,
PathResponse = 0x1b,
ConnectionClose = 0x1c,
HandshakeDone = 0x1e,
}
impl Codec for FrameType {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
(*self as u8).encode(writer, ())
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
match u8::decode(reader, ())? {
x if x == (Self::Padding as u8) => Ok(Self::Padding),
_ => Err(BufError::UnexpectedValue),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Frame {
pub r#type: VarInt,
pub frame_payload: Vec<u8>,
}
impl Codec for Frame {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
self.r#type.encode(writer, ())?;
self.frame_payload.encode(writer, ())
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
let r#type = VarInt::decode(reader, ())?;
let _length = VarInt::decode(reader, ())?;
let frame_payload = Vec::decode(reader, ())?;
Ok(Self {
r#type,
frame_payload,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DataFrame {
pub r#type: VarInt,
pub frame_payload: Vec<u8>,
}
impl Codec for DataFrame {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
self.r#type.encode(writer, ())?;
self.frame_payload.encode(writer, ())
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
let r#type = VarInt::decode(reader, ())?;
let _length = VarInt::decode(reader, ())?;
let frame_payload = Vec::decode(reader, ())?;
Ok(Self {
r#type,
frame_payload,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct HeadersFrame {
pub r#type: VarInt,
pub frame_payload: Vec<u8>,
}
impl Codec for HeadersFrame {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
self.r#type.encode(writer, ())?;
self.frame_payload.encode(writer, ())
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
let r#type = VarInt::decode(reader, ())?;
let _length = VarInt::decode(reader, ())?;
let frame_payload = Vec::decode(reader, ())?;
Ok(Self {
r#type,
frame_payload,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CancelPush {
pub r#type: VarInt,
pub frame_payload: Vec<u8>,
}
impl Codec for CancelPush {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
self.r#type.encode(writer, ())?;
self.frame_payload.encode(writer, ())
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
let r#type = VarInt::decode(reader, ())?;
let _length = VarInt::decode(reader, ())?;
let frame_payload = Vec::decode(reader, ())?;
Ok(Self {
r#type,
frame_payload,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SettingsFrame {
pub r#type: VarInt,
pub frame_payload: Vec<u8>,
}
impl Codec for SettingsFrame {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
self.r#type.encode(writer, ())?;
self.frame_payload.encode(writer, ())
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
let r#type = VarInt::decode(reader, ())?;
let _length = VarInt::decode(reader, ())?;
let frame_payload = Vec::decode(reader, ())?;
Ok(Self {
r#type,
frame_payload,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PushPromise {
pub r#type: VarInt,
pub frame_payload: Vec<u8>,
}
impl Codec for PushPromise {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
self.r#type.encode(writer, ())?;
self.frame_payload.encode(writer, ())
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
let r#type = VarInt::decode(reader, ())?;
let _length = VarInt::decode(reader, ())?;
let frame_payload = Vec::decode(reader, ())?;
Ok(Self {
r#type,
frame_payload,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Goaway {
pub r#type: VarInt,
pub frame_payload: Vec<u8>,
}
impl Codec for Goaway {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
self.r#type.encode(writer, ())?;
self.frame_payload.encode(writer, ())
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
let r#type = VarInt::decode(reader, ())?;
let _length = VarInt::decode(reader, ())?;
let frame_payload = Vec::decode(reader, ())?;
Ok(Self {
r#type,
frame_payload,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MaxPushId {
pub r#type: VarInt,
pub frame_payload: Vec<u8>,
}
impl Codec for MaxPushId {
fn encode<W: BufMut>(&self, writer: &mut Cursor<W>, _: ()) -> BufResult<()> {
self.r#type.encode(writer, ())?;
self.frame_payload.encode(writer, ())
}
fn decode<R: Buf>(reader: &mut Cursor<R>, _: ()) -> BufResult<Self> {
let r#type = VarInt::decode(reader, ())?;
let _length = VarInt::decode(reader, ())?;
let frame_payload = Vec::decode(reader, ())?;
Ok(Self {
r#type,
frame_payload,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u64)]
pub enum ErrorCode {
H3NoError = 0x0100,
H3GeneralProtocolError = 0x0101,
H3InternalError = 0x0102,
FlowControlError = 0x0103,
StreamLimitError = 0x0104,
StreamStateError = 0x0105,
FinalSizeError = 0x0106,
FrameEncodingError = 0x0107,
TransportParameterError = 0x0108,
ConnectionIdLimitError = 0x0109,
ProtocolViolation = 0x010a,
InvalidToken = 0x010b,
ApplicationError = 0x010c,
CryptoBufferExceeded = 0x010d,
KeyUpdateError = 0x010e,
AeadLimitReached = 0x010f,
NoViablePath = 0x0110,
}
impl From<ErrorCode> for VarInt {
fn from(value: ErrorCode) -> Self {
Self(value as u64)
}
}
impl TryFrom<VarInt> for ErrorCode {
type Error = BufError;
fn try_from(value: VarInt) -> Result<Self, Self::Error> {
Self::try_from(value.into_inner())
}
}
impl From<ErrorCode> for u64 {
fn from(value: ErrorCode) -> Self {
value as u64
}
}
impl TryFrom<u64> for ErrorCode {
type Error = BufError;
fn try_from(value: u64) -> Result<Self, Self::Error> {
match value {
x if x == Self::H3NoError as u64 => Ok(Self::H3NoError),
x if x == Self::H3GeneralProtocolError as u64 => Ok(Self::H3GeneralProtocolError),
x if x == Self::H3InternalError as u64 => Ok(Self::H3InternalError),
x if x == Self::FlowControlError as u64 => Ok(Self::FlowControlError),
x if x == Self::StreamLimitError as u64 => Ok(Self::StreamLimitError),
x if x == Self::StreamStateError as u64 => Ok(Self::StreamStateError),
x if x == Self::FinalSizeError as u64 => Ok(Self::FinalSizeError),
x if x == Self::FrameEncodingError as u64 => Ok(Self::FrameEncodingError),
x if x == Self::TransportParameterError as u64 => Ok(Self::TransportParameterError),
x if x == Self::ConnectionIdLimitError as u64 => Ok(Self::ConnectionIdLimitError),
x if x == Self::ProtocolViolation as u64 => Ok(Self::ProtocolViolation),
x if x == Self::InvalidToken as u64 => Ok(Self::InvalidToken),
x if x == Self::ApplicationError as u64 => Ok(Self::ApplicationError),
x if x == Self::CryptoBufferExceeded as u64 => Ok(Self::CryptoBufferExceeded),
x if x == Self::KeyUpdateError as u64 => Ok(Self::KeyUpdateError),
x if x == Self::AeadLimitReached as u64 => Ok(Self::AeadLimitReached),
x if x == Self::NoViablePath as u64 => Ok(Self::NoViablePath),
_ => Err(BufError::UnexpectedValue),
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn test() {}
}