tweak
This commit is contained in:
@@ -6,6 +6,35 @@ use bytes::{BufMut, Bytes, BytesMut};
|
||||
use num_enum::{FromPrimitive, IntoPrimitive};
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||
|
||||
/// Represent kind of application data being sent in Transport mode
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, IntoPrimitive, FromPrimitive, Hash)]
|
||||
#[repr(u16)]
|
||||
pub enum LpFrameKind {
|
||||
Opaque = 0,
|
||||
Registration = 1,
|
||||
Forward = 2,
|
||||
SphinxStream = 3,
|
||||
FragmentedSphinxPacket = 4,
|
||||
FragmentedOutfoxPacket = 5,
|
||||
|
||||
#[num_enum(catch_all)]
|
||||
Unknown(u16),
|
||||
}
|
||||
|
||||
impl LpFrameKind {
|
||||
// Indicate if the frame attributes can be parsed as fragments attributess
|
||||
pub fn is_fragmented(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::FragmentedSphinxPacket | Self::FragmentedOutfoxPacket
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Raw 14-byte frame attributes field in every [`LpFrameHeader`].
|
||||
/// Interpretation depends on the [`LpFrameKind`].
|
||||
pub type LpFrameAttributes = [u8; 14];
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct LpFrameHeader {
|
||||
pub kind: LpFrameKind,
|
||||
@@ -15,10 +44,10 @@ pub struct LpFrameHeader {
|
||||
impl LpFrameHeader {
|
||||
pub const SIZE: usize = 16; // message_kind(2) + message_attributes(14)
|
||||
|
||||
pub fn new(kind: LpFrameKind, frame_attributes: LpFrameAttributes) -> Self {
|
||||
pub fn new(kind: LpFrameKind, frame_attributes: impl Into<LpFrameAttributes>) -> Self {
|
||||
Self {
|
||||
kind,
|
||||
frame_attributes,
|
||||
frame_attributes: frame_attributes.into(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +108,7 @@ impl LpFrame {
|
||||
content: impl Into<Bytes>,
|
||||
) -> Self {
|
||||
Self {
|
||||
header: LpFrameHeader::new(kind, attrs.into()),
|
||||
header: LpFrameHeader::new(kind, attrs),
|
||||
content: content.into(),
|
||||
}
|
||||
}
|
||||
@@ -127,31 +156,6 @@ impl LpFrame {
|
||||
}
|
||||
}
|
||||
|
||||
/// Represent kind of application data being sent in Transport mode
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, IntoPrimitive, FromPrimitive, Hash)]
|
||||
#[repr(u16)]
|
||||
pub enum LpFrameKind {
|
||||
Opaque = 0,
|
||||
Registration = 1,
|
||||
Forward = 2,
|
||||
SphinxStream = 3,
|
||||
FragmentedSphinxPacket = 4,
|
||||
FragmentedOutfoxPacket = 5,
|
||||
|
||||
#[num_enum(catch_all)]
|
||||
Unknown(u16),
|
||||
}
|
||||
|
||||
impl LpFrameKind {
|
||||
// Indicate if the frame attributes can be parsed as fragments attributess
|
||||
pub fn is_fragmented(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::FragmentedSphinxPacket | Self::FragmentedOutfoxPacket
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Message type within a `LpFrameKind::SphinxStream` frame.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
@@ -178,10 +182,6 @@ pub struct SphinxStreamFrameAttributes {
|
||||
pub sequence_num: u32,
|
||||
}
|
||||
|
||||
/// Raw 14-byte frame attributes field in every [`LpFrameHeader`].
|
||||
/// Interpretation depends on the [`LpFrameKind`].
|
||||
pub type LpFrameAttributes = [u8; 14];
|
||||
|
||||
impl SphinxStreamFrameAttributes {
|
||||
pub fn encode(&self) -> LpFrameAttributes {
|
||||
let mut buf = [0u8; 14];
|
||||
|
||||
Reference in New Issue
Block a user