TCP Server #77
@ -162,8 +162,8 @@ impl<
|
|||||||
cfg: ServerConfig,
|
cfg: ServerConfig,
|
||||||
tc_parser: TcParser,
|
tc_parser: TcParser,
|
||||||
tm_sender: TmSender,
|
tm_sender: TmSender,
|
||||||
tm_source: Box<dyn TmPacketSource<Error = TmError> + Send>,
|
tm_source: Box<dyn TmPacketSource<Error = TmError>>,
|
||||||
tc_receiver: Box<dyn ReceivesTc<Error = TcError> + Send>,
|
tc_receiver: Box<dyn ReceivesTc<Error = TcError>>,
|
||||||
) -> Result<TcpTmtcGenericServer<TmError, TcError, TmSender, TcParser>, std::io::Error> {
|
) -> Result<TcpTmtcGenericServer<TmError, TcError, TmSender, TcParser>, std::io::Error> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
base: TcpTmtcServerBase::new(cfg, tm_source, tc_receiver)?,
|
base: TcpTmtcServerBase::new(cfg, tm_source, tc_receiver)?,
|
||||||
@ -280,17 +280,17 @@ impl<
|
|||||||
pub(crate) struct TcpTmtcServerBase<TmError, TcError> {
|
pub(crate) struct TcpTmtcServerBase<TmError, TcError> {
|
||||||
pub(crate) listener: TcpListener,
|
pub(crate) listener: TcpListener,
|
||||||
pub(crate) inner_loop_delay: Duration,
|
pub(crate) inner_loop_delay: Duration,
|
||||||
pub(crate) tm_source: Box<dyn TmPacketSource<Error = TmError> + Send>,
|
pub(crate) tm_source: Box<dyn TmPacketSource<Error = TmError>>,
|
||||||
pub(crate) tm_buffer: Vec<u8>,
|
pub(crate) tm_buffer: Vec<u8>,
|
||||||
pub(crate) tc_receiver: Box<dyn ReceivesTc<Error = TcError> + Send>,
|
pub(crate) tc_receiver: Box<dyn ReceivesTc<Error = TcError>>,
|
||||||
pub(crate) tc_buffer: Vec<u8>,
|
pub(crate) tc_buffer: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<TmError, TcError> TcpTmtcServerBase<TmError, TcError> {
|
impl<TmError, TcError> TcpTmtcServerBase<TmError, TcError> {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
cfg: ServerConfig,
|
cfg: ServerConfig,
|
||||||
tm_source: Box<dyn TmPacketSource<Error = TmError> + Send>,
|
tm_source: Box<dyn TmPacketSource<Error = TmError>>,
|
||||||
tc_receiver: Box<dyn ReceivesTc<Error = TcError> + Send>,
|
tc_receiver: Box<dyn ReceivesTc<Error = TcError>>,
|
||||||
) -> Result<Self, std::io::Error> {
|
) -> Result<Self, std::io::Error> {
|
||||||
// Create a TCP listener bound to two addresses.
|
// Create a TCP listener bound to two addresses.
|
||||||
let socket = Socket::new(Domain::IPV4, Type::STREAM, None)?;
|
let socket = Socket::new(Domain::IPV4, Type::STREAM, None)?;
|
||||||
|
@ -20,7 +20,7 @@ use crate::hal::std::tcp_server::{
|
|||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct CobsTcParser {}
|
pub struct CobsTcParser {}
|
||||||
|
|
||||||
impl<TmError, TcError> TcpTcParser<TmError, TcError> for CobsTcParser {
|
impl<TmError, TcError: 'static> TcpTcParser<TmError, TcError> for CobsTcParser {
|
||||||
fn handle_tc_parsing(
|
fn handle_tc_parsing(
|
||||||
&mut self,
|
&mut self,
|
||||||
tc_buffer: &mut [u8],
|
tc_buffer: &mut [u8],
|
||||||
@ -32,7 +32,7 @@ impl<TmError, TcError> TcpTcParser<TmError, TcError> for CobsTcParser {
|
|||||||
// Reader vec full, need to parse for packets.
|
// Reader vec full, need to parse for packets.
|
||||||
conn_result.num_received_tcs += parse_buffer_for_cobs_encoded_packets(
|
conn_result.num_received_tcs += parse_buffer_for_cobs_encoded_packets(
|
||||||
&mut tc_buffer[..current_write_idx],
|
&mut tc_buffer[..current_write_idx],
|
||||||
tc_receiver,
|
tc_receiver.upcast_mut(),
|
||||||
next_write_idx,
|
next_write_idx,
|
||||||
)
|
)
|
||||||
.map_err(|e| TcpTmtcError::TcError(e))?;
|
.map_err(|e| TcpTmtcError::TcError(e))?;
|
||||||
@ -105,7 +105,7 @@ impl<TmError, TcError> TcpTmSender<TmError, TcError> for CobsTmSender {
|
|||||||
/// packets even from a data stream which is split up. The server wil use the
|
/// packets even from a data stream which is split up. The server wil use the
|
||||||
/// [parse_buffer_for_cobs_encoded_packets] function to parse for packets and pass them to a
|
/// [parse_buffer_for_cobs_encoded_packets] function to parse for packets and pass them to a
|
||||||
/// generic TC receiver.
|
/// generic TC receiver.
|
||||||
pub struct TcpTmtcInCobsServer<TmError, TcError> {
|
pub struct TcpTmtcInCobsServer<TmError, TcError: 'static> {
|
||||||
generic_server: TcpTmtcGenericServer<TmError, TcError, CobsTmSender, CobsTcParser>,
|
generic_server: TcpTmtcGenericServer<TmError, TcError, CobsTmSender, CobsTcParser>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -122,8 +122,8 @@ impl<TmError: 'static, TcError: 'static> TcpTmtcInCobsServer<TmError, TcError> {
|
|||||||
/// forwarded to this TC receiver.
|
/// forwarded to this TC receiver.
|
||||||
pub fn new(
|
pub fn new(
|
||||||
cfg: ServerConfig,
|
cfg: ServerConfig,
|
||||||
tm_source: Box<dyn TmPacketSource<Error = TmError> + Send>,
|
tm_source: Box<dyn TmPacketSource<Error = TmError>>,
|
||||||
tc_receiver: Box<dyn ReceivesTc<Error = TcError> + Send>,
|
tc_receiver: Box<dyn ReceivesTc<Error = TcError>>,
|
||||||
) -> Result<Self, TcpTmtcError<TmError, TcError>> {
|
) -> Result<Self, TcpTmtcError<TmError, TcError>> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
generic_server: TcpTmtcGenericServer::new(
|
generic_server: TcpTmtcGenericServer::new(
|
||||||
@ -168,7 +168,7 @@ mod tests {
|
|||||||
use crate::{
|
use crate::{
|
||||||
hal::std::tcp_server::ServerConfig,
|
hal::std::tcp_server::ServerConfig,
|
||||||
parsers::tests::{INVERTED_PACKET, SIMPLE_PACKET},
|
parsers::tests::{INVERTED_PACKET, SIMPLE_PACKET},
|
||||||
tmtc::{ReceivesTcCore, TmPacketSource},
|
tmtc::{ReceivesTcCore, TmPacketSourceCore},
|
||||||
};
|
};
|
||||||
use alloc::{boxed::Box, collections::VecDeque, sync::Arc, vec::Vec};
|
use alloc::{boxed::Box, collections::VecDeque, sync::Arc, vec::Vec};
|
||||||
use cobs::encode;
|
use cobs::encode;
|
||||||
@ -201,7 +201,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TmPacketSource for SyncTmSource {
|
impl TmPacketSourceCore for SyncTmSource {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
|
|
||||||
fn retrieve_packet(&mut self, buffer: &mut [u8]) -> Result<usize, Self::Error> {
|
fn retrieve_packet(&mut self, buffer: &mut [u8]) -> Result<usize, Self::Error> {
|
||||||
|
@ -4,7 +4,7 @@ use alloc::vec::Vec;
|
|||||||
use hashbrown::HashSet;
|
use hashbrown::HashSet;
|
||||||
use spacepackets::PacketId;
|
use spacepackets::PacketId;
|
||||||
|
|
||||||
use crate::tmtc::ReceivesTc;
|
use crate::tmtc::ReceivesTcCore;
|
||||||
|
|
||||||
pub trait PacketIdLookup {
|
pub trait PacketIdLookup {
|
||||||
fn validate(&self, packet_id: u16) -> bool;
|
fn validate(&self, packet_id: u16) -> bool;
|
||||||
@ -68,7 +68,7 @@ impl PacketIdLookup for &[PacketId] {
|
|||||||
pub fn parse_buffer_for_ccsds_space_packets<E>(
|
pub fn parse_buffer_for_ccsds_space_packets<E>(
|
||||||
buf: &mut [u8],
|
buf: &mut [u8],
|
||||||
packet_id_lookup: &dyn PacketIdLookup,
|
packet_id_lookup: &dyn PacketIdLookup,
|
||||||
tc_receiver: &mut dyn ReceivesTc<Error = E>,
|
tc_receiver: &mut dyn ReceivesTcCore<Error = E>,
|
||||||
next_write_idx: &mut usize,
|
next_write_idx: &mut usize,
|
||||||
) -> Result<u32, E> {
|
) -> Result<u32, E> {
|
||||||
let packets_found = 0;
|
let packets_found = 0;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::tmtc::ReceivesTc;
|
use crate::tmtc::ReceivesTcCore;
|
||||||
use cobs::decode_in_place;
|
use cobs::decode_in_place;
|
||||||
|
|
||||||
/// This function parses a given buffer for COBS encoded packets. The packet structure is
|
/// This function parses a given buffer for COBS encoded packets. The packet structure is
|
||||||
@ -13,7 +13,7 @@ use cobs::decode_in_place;
|
|||||||
/// The parser will write all packets which were decoded successfully to the given `tc_receiver`.
|
/// The parser will write all packets which were decoded successfully to the given `tc_receiver`.
|
||||||
pub fn parse_buffer_for_cobs_encoded_packets<E>(
|
pub fn parse_buffer_for_cobs_encoded_packets<E>(
|
||||||
buf: &mut [u8],
|
buf: &mut [u8],
|
||||||
tc_receiver: &mut dyn ReceivesTc<Error = E>,
|
tc_receiver: &mut dyn ReceivesTcCore<Error = E>,
|
||||||
next_write_idx: &mut usize,
|
next_write_idx: &mut usize,
|
||||||
) -> Result<u32, E> {
|
) -> Result<u32, E> {
|
||||||
let mut start_index_packet = 0;
|
let mut start_index_packet = 0;
|
||||||
|
@ -72,12 +72,33 @@ pub trait ReceivesTcCore {
|
|||||||
/// Extension trait of [ReceivesTcCore] which allows downcasting by implementing [Downcast] and
|
/// Extension trait of [ReceivesTcCore] which allows downcasting by implementing [Downcast] and
|
||||||
/// is also sendable.
|
/// is also sendable.
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
pub trait ReceivesTc: ReceivesTcCore + Downcast + Send {}
|
pub trait ReceivesTc: ReceivesTcCore + Downcast + Send {
|
||||||
|
// Remove this once trait upcasting coercion has been implemented.
|
||||||
|
// Tracking issue: https://github.com/rust-lang/rust/issues/65991
|
||||||
|
fn upcast(&self) -> &dyn ReceivesTcCore<Error = Self::Error>;
|
||||||
|
// Remove this once trait upcasting coercion has been implemented.
|
||||||
|
// Tracking issue: https://github.com/rust-lang/rust/issues/65991
|
||||||
|
fn upcast_mut(&mut self) -> &mut dyn ReceivesTcCore<Error = Self::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
/// Blanket implementation to automatically implement [ReceivesTc] when the [alloc] feature
|
/// Blanket implementation to automatically implement [ReceivesTc] when the [alloc] feature
|
||||||
/// is enabled.
|
/// is enabled.
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
impl<T> ReceivesTc for T where T: ReceivesTcCore + Send + 'static {}
|
impl<T> ReceivesTc for T
|
||||||
|
where
|
||||||
|
T: ReceivesTcCore + Send + 'static,
|
||||||
|
{
|
||||||
|
// Remove this once trait upcasting coercion has been implemented.
|
||||||
|
// Tracking issue: https://github.com/rust-lang/rust/issues/65991
|
||||||
|
fn upcast(&self) -> &dyn ReceivesTcCore<Error = Self::Error> {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
// Remove this once trait upcasting coercion has been implemented.
|
||||||
|
// Tracking issue: https://github.com/rust-lang/rust/issues/65991
|
||||||
|
fn upcast_mut(&mut self) -> &mut dyn ReceivesTcCore<Error = Self::Error> {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
impl_downcast!(ReceivesTc assoc Error);
|
impl_downcast!(ReceivesTc assoc Error);
|
||||||
@ -95,7 +116,38 @@ pub trait ReceivesCcsdsTc {
|
|||||||
|
|
||||||
/// Generic trait for a TM packet source, with no restrictions on the type of TM.
|
/// Generic trait for a TM packet source, with no restrictions on the type of TM.
|
||||||
/// Implementors write the telemetry into the provided buffer and return the size of the telemetry.
|
/// Implementors write the telemetry into the provided buffer and return the size of the telemetry.
|
||||||
pub trait TmPacketSource {
|
pub trait TmPacketSourceCore {
|
||||||
type Error;
|
type Error;
|
||||||
fn retrieve_packet(&mut self, buffer: &mut [u8]) -> Result<usize, Self::Error>;
|
fn retrieve_packet(&mut self, buffer: &mut [u8]) -> Result<usize, Self::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extension trait of [TmPacketSourceCore] which allows downcasting by implementing [Downcast] and
|
||||||
|
/// is also sendable.
|
||||||
|
#[cfg(feature = "alloc")]
|
||||||
|
pub trait TmPacketSource: TmPacketSourceCore + Downcast + Send {
|
||||||
|
// Remove this once trait upcasting coercion has been implemented.
|
||||||
|
// Tracking issue: https://github.com/rust-lang/rust/issues/65991
|
||||||
|
fn upcast(&self) -> &dyn TmPacketSourceCore<Error = Self::Error>;
|
||||||
|
// Remove this once trait upcasting coercion has been implemented.
|
||||||
|
// Tracking issue: https://github.com/rust-lang/rust/issues/65991
|
||||||
|
fn upcast_mut(&mut self) -> &mut dyn TmPacketSourceCore<Error = Self::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Blanket implementation to automatically implement [ReceivesTc] when the [alloc] feature
|
||||||
|
/// is enabled.
|
||||||
|
#[cfg(feature = "alloc")]
|
||||||
|
impl<T> TmPacketSource for T
|
||||||
|
where
|
||||||
|
T: TmPacketSourceCore + Send + 'static,
|
||||||
|
{
|
||||||
|
// Remove this once trait upcasting coercion has been implemented.
|
||||||
|
// Tracking issue: https://github.com/rust-lang/rust/issues/65991
|
||||||
|
fn upcast(&self) -> &dyn TmPacketSourceCore<Error = Self::Error> {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
// Remove this once trait upcasting coercion has been implemented.
|
||||||
|
// Tracking issue: https://github.com/rust-lang/rust/issues/65991
|
||||||
|
fn upcast_mut(&mut self) -> &mut dyn TmPacketSourceCore<Error = Self::Error> {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user