sat-rs/satrs-core/src/hal/std/tcp_server.rs

128 lines
4.5 KiB
Rust
Raw Normal View History

2023-09-16 21:28:22 +02:00
//! Generic TCP TMTC servers with different TMTC format flavours.
2023-09-15 15:37:57 +02:00
use alloc::vec;
2023-09-15 19:15:26 +02:00
use alloc::{boxed::Box, vec::Vec};
2023-09-17 02:31:02 +02:00
use core::time::Duration;
2023-09-17 01:32:18 +02:00
use socket2::{Domain, Socket, Type};
2023-09-15 19:15:26 +02:00
use std::net::SocketAddr;
2023-09-17 01:32:18 +02:00
use std::net::TcpListener;
2023-09-15 19:15:26 +02:00
use crate::tmtc::{ReceivesTc, TmPacketSource};
2023-09-15 18:34:05 +02:00
use thiserror::Error;
2023-09-15 15:37:57 +02:00
2023-09-15 19:15:26 +02:00
// Re-export the TMTC in COBS server.
2023-09-16 21:28:22 +02:00
pub use crate::hal::std::tcp_with_cobs_server::{
2023-09-15 19:22:12 +02:00
parse_buffer_for_cobs_encoded_packets, TcpTmtcInCobsServer,
};
2023-09-15 18:34:05 +02:00
2023-09-17 02:35:08 +02:00
/// TCP configuration struct.
///
/// ## Parameters
///
/// * `addr` - Address of the TCP server.
/// * `inner_loop_delay` - If a client connects for a longer period, but no TC is received or
/// no TM needs to be sent, the TCP server will delay for the specified amount of time
/// to reduce CPU load.
/// * `tm_buffer_size` - Size of the TM buffer used to read TM from the [TmPacketSource] and
/// encoding of that data. This buffer should at large enough to hold the maximum expected
/// TM size in addition to the COBS encoding overhead. You can use
/// [cobs::max_encoding_length] to calculate this size.
/// * `tc_buffer_size` - Size of the TC buffer used to read encoded telecommands sent from
/// the client. It is recommended to make this buffer larger to allow reading multiple
/// consecutive packets as well, for example by using 4096 or 8192 byte. The buffer should
/// at the very least be large enough to hold the maximum expected telecommand size in
/// addition to its COBS encoding overhead. You can use [cobs::max_encoding_length] to
/// calculate this size.
/// * `reuse_addr` - Can be used to set the `SO_REUSEADDR` option on the raw socket. This is
/// especially useful if the address and port are static for the server. Set to false by
/// default.
/// * `reuse_port` - Can be used to set the `SO_REUSEPORT` option on the raw socket. This is
/// especially useful if the address and port are static for the server. Set to false by
/// default.
#[derive(Debug, Copy, Clone)]
pub struct ServerConfig {
pub addr: SocketAddr,
pub inner_loop_delay: Duration,
pub tm_buffer_size: usize,
pub tc_buffer_size: usize,
pub reuse_addr: bool,
pub reuse_port: bool,
}
impl ServerConfig {
pub fn new(
addr: SocketAddr,
inner_loop_delay: Duration,
tm_buffer_size: usize,
tc_buffer_size: usize,
) -> Self {
Self {
addr,
inner_loop_delay,
tm_buffer_size,
tc_buffer_size,
reuse_addr: false,
reuse_port: false,
}
}
}
2023-09-15 18:34:05 +02:00
#[derive(Error, Debug)]
2023-09-16 16:23:42 +02:00
pub enum TcpTmtcError<TmError, TcError> {
2023-09-15 18:34:05 +02:00
#[error("TM retrieval error: {0}")]
TmError(TmError),
#[error("TC retrieval error: {0}")]
TcError(TcError),
#[error("io error: {0}")]
Io(#[from] std::io::Error),
2023-09-15 15:37:57 +02:00
}
2023-09-15 19:15:26 +02:00
/// Result of one connection attempt. Contains the client address if a connection was established,
/// in addition to the number of telecommands and telemetry packets exchanged.
#[derive(Debug, Default)]
pub struct ConnectionResult {
pub addr: Option<SocketAddr>,
pub num_received_tcs: u32,
pub num_sent_tms: u32,
}
2023-09-18 00:11:01 +02:00
pub(crate) struct TcpTmtcServerBase<TmError, TcError> {
2023-09-15 19:22:12 +02:00
pub(crate) listener: TcpListener,
2023-09-17 02:31:02 +02:00
pub(crate) inner_loop_delay: Duration,
2023-09-16 16:23:42 +02:00
pub(crate) tm_source: Box<dyn TmPacketSource<Error = TmError> + Send>,
2023-09-15 19:22:12 +02:00
pub(crate) tm_buffer: Vec<u8>,
2023-09-16 16:23:42 +02:00
pub(crate) tc_receiver: Box<dyn ReceivesTc<Error = TcError> + Send>,
2023-09-15 19:22:12 +02:00
pub(crate) tc_buffer: Vec<u8>,
2023-09-15 19:15:26 +02:00
}
2023-09-18 00:11:01 +02:00
impl<TmError, TcError> TcpTmtcServerBase<TmError, TcError> {
2023-09-17 01:32:18 +02:00
pub(crate) fn new(
2023-09-18 00:11:01 +02:00
cfg: ServerConfig,
2023-09-16 16:23:42 +02:00
tm_source: Box<dyn TmPacketSource<Error = TmError> + Send>,
tc_receiver: Box<dyn ReceivesTc<Error = TcError> + Send>,
2023-09-15 15:37:57 +02:00
) -> Result<Self, std::io::Error> {
2023-09-17 01:32:18 +02:00
// Create a TCP listener bound to two addresses.
let socket = Socket::new(Domain::IPV4, Type::STREAM, None)?;
2023-09-18 00:11:01 +02:00
socket.set_reuse_address(cfg.reuse_addr)?;
socket.set_reuse_port(cfg.reuse_port)?;
let addr = (cfg.addr).into();
2023-09-17 01:32:18 +02:00
socket.bind(&addr)?;
socket.listen(128)?;
2023-09-15 18:34:05 +02:00
Ok(Self {
2023-09-17 01:32:18 +02:00
listener: socket.into(),
2023-09-18 00:11:01 +02:00
inner_loop_delay: cfg.inner_loop_delay,
2023-09-15 18:34:05 +02:00
tm_source,
2023-09-18 00:11:01 +02:00
tm_buffer: vec![0; cfg.tm_buffer_size],
2023-09-15 15:37:57 +02:00
tc_receiver,
2023-09-18 00:11:01 +02:00
tc_buffer: vec![0; cfg.tc_buffer_size],
2023-09-15 15:37:57 +02:00
})
}
2023-09-16 22:19:48 +02:00
2023-09-17 01:32:18 +02:00
pub(crate) fn listener(&mut self) -> &mut TcpListener {
&mut self.listener
}
2023-09-16 22:19:48 +02:00
pub(crate) fn local_addr(&self) -> std::io::Result<SocketAddr> {
2023-09-16 21:51:06 +02:00
self.listener.local_addr()
}
2023-09-14 23:51:17 +02:00
}