some more improvements

This commit is contained in:
Robin Müller 2023-09-17 01:32:18 +02:00
parent d582ce212e
commit de690b3eed
Signed by: muellerr
GPG Key ID: A649FB78196E3849
4 changed files with 44 additions and 9 deletions

View File

@ -60,6 +60,11 @@ version = "1"
default-features = false default-features = false
optional = true optional = true
[dependencies.socket2]
version = "0.5.4"
features = ["all"]
optional = true
[dependencies.spacepackets] [dependencies.spacepackets]
version = "0.7.0-beta.1" version = "0.7.0-beta.1"
# path = "../../spacepackets" # path = "../../spacepackets"
@ -93,7 +98,8 @@ std = [
"serde/std", "serde/std",
"spacepackets/std", "spacepackets/std",
"num_enum/std", "num_enum/std",
"thiserror" "thiserror",
"socket2"
] ]
alloc = [ alloc = [
"serde/alloc", "serde/alloc",

View File

@ -1,8 +1,9 @@
//! Generic TCP TMTC servers with different TMTC format flavours. //! Generic TCP TMTC servers with different TMTC format flavours.
use alloc::vec; use alloc::vec;
use alloc::{boxed::Box, vec::Vec}; use alloc::{boxed::Box, vec::Vec};
use socket2::{Domain, Socket, Type};
use std::net::SocketAddr; use std::net::SocketAddr;
use std::net::{TcpListener, ToSocketAddrs}; use std::net::TcpListener;
use crate::tmtc::{ReceivesTc, TmPacketSource}; use crate::tmtc::{ReceivesTc, TmPacketSource};
use thiserror::Error; use thiserror::Error;
@ -40,15 +41,24 @@ pub(crate) struct TcpTmtcServerBase<TcError, TmError> {
} }
impl<TcError, TmError> TcpTmtcServerBase<TcError, TmError> { impl<TcError, TmError> TcpTmtcServerBase<TcError, TmError> {
pub(crate) fn new<A: ToSocketAddrs>( pub(crate) fn new(
addr: A, addr: &SocketAddr,
reuse_addr: bool,
reuse_port: bool,
tm_buffer_size: usize, tm_buffer_size: usize,
tm_source: Box<dyn TmPacketSource<Error = TmError> + Send>, tm_source: Box<dyn TmPacketSource<Error = TmError> + Send>,
tc_buffer_size: usize, tc_buffer_size: usize,
tc_receiver: Box<dyn ReceivesTc<Error = TcError> + Send>, tc_receiver: Box<dyn ReceivesTc<Error = TcError> + Send>,
) -> Result<Self, std::io::Error> { ) -> Result<Self, std::io::Error> {
// Create a TCP listener bound to two addresses.
let socket = Socket::new(Domain::IPV4, Type::STREAM, None)?;
socket.set_reuse_address(reuse_addr)?;
socket.set_reuse_port(reuse_port)?;
let addr = (*addr).into();
socket.bind(&addr)?;
socket.listen(128)?;
Ok(Self { Ok(Self {
listener: TcpListener::bind(addr)?, listener: socket.into(),
tm_source, tm_source,
tm_buffer: vec![0; tm_buffer_size], tm_buffer: vec![0; tm_buffer_size],
tc_receiver, tc_receiver,
@ -56,6 +66,10 @@ impl<TcError, TmError> TcpTmtcServerBase<TcError, TmError> {
}) })
} }
pub(crate) fn listener(&mut self) -> &mut TcpListener {
&mut self.listener
}
pub(crate) fn local_addr(&self) -> std::io::Result<SocketAddr> { pub(crate) fn local_addr(&self) -> std::io::Result<SocketAddr> {
self.listener.local_addr() self.listener.local_addr()
} }

View File

@ -6,7 +6,7 @@ use cobs::max_encoding_length;
use std::io::Read; use std::io::Read;
use std::io::Write; use std::io::Write;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::net::ToSocketAddrs; use std::net::TcpListener;
use std::vec::Vec; use std::vec::Vec;
use crate::hal::std::tcp_server::TcpTmtcServerBase; use crate::hal::std::tcp_server::TcpTmtcServerBase;
@ -44,6 +44,10 @@ impl<TcError: 'static, TmError: 'static> TcpTmtcInCobsServer<TcError, TmError> {
/// ## Parameter /// ## Parameter
/// ///
/// * `addr` - Address of the TCP server. /// * `addr` - Address of the TCP server.
/// * `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.
/// * `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.
/// * `tm_buffer_size` - Size of the TM buffer used to read TM from the [TmPacketSource] and /// * `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 /// 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 /// TM size in addition to the COBS encoding overhead. You can use
@ -58,8 +62,10 @@ impl<TcError: 'static, TmError: 'static> TcpTmtcInCobsServer<TcError, TmError> {
/// calculate this size. /// calculate this size.
/// * `tc_receiver` - Any received telecommand which was decoded successfully will be forwarded /// * `tc_receiver` - Any received telecommand which was decoded successfully will be forwarded
/// to this TC receiver. /// to this TC receiver.
pub fn new<A: ToSocketAddrs>( pub fn new(
addr: A, addr: &SocketAddr,
reuse_addr: bool,
reuse_port: bool,
tm_buffer_size: usize, tm_buffer_size: usize,
tm_source: Box<dyn TmPacketSource<Error = TmError> + Send>, tm_source: Box<dyn TmPacketSource<Error = TmError> + Send>,
tc_buffer_size: usize, tc_buffer_size: usize,
@ -68,6 +74,8 @@ impl<TcError: 'static, TmError: 'static> TcpTmtcInCobsServer<TcError, TmError> {
Ok(Self { Ok(Self {
base: TcpTmtcServerBase::new( base: TcpTmtcServerBase::new(
addr, addr,
reuse_addr,
reuse_port,
tm_buffer_size, tm_buffer_size,
tm_source, tm_source,
tc_buffer_size, tc_buffer_size,
@ -77,6 +85,11 @@ impl<TcError: 'static, TmError: 'static> TcpTmtcInCobsServer<TcError, TmError> {
}) })
} }
/// Retrieve the internal [TcpListener] class.
pub fn listener(&mut self) -> &mut TcpListener {
self.base.listener()
}
/// Can be used to retrieve the local assigned address of the TCP server. This is especially /// Can be used to retrieve the local assigned address of the TCP server. This is especially
/// useful if using the port number 0 for OS auto-assignment. /// useful if using the port number 0 for OS auto-assignment.
pub fn local_addr(&self) -> std::io::Result<SocketAddr> { pub fn local_addr(&self) -> std::io::Result<SocketAddr> {
@ -464,6 +477,8 @@ mod tests {
) -> TcpTmtcInCobsServer<(), ()> { ) -> TcpTmtcInCobsServer<(), ()> {
TcpTmtcInCobsServer::new( TcpTmtcInCobsServer::new(
addr, addr,
false,
false,
1024, 1024,
Box::new(tm_source), Box::new(tm_source),
1024, 1024,

View File

@ -20,7 +20,7 @@ use std::vec::Vec;
/// ``` /// ```
/// use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket}; /// use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
/// use spacepackets::ecss::SerializablePusPacket; /// use spacepackets::ecss::SerializablePusPacket;
/// use satrs_core::hal::host::udp_server::UdpTcServer; /// use satrs_core::hal::std::udp_server::UdpTcServer;
/// use satrs_core::tmtc::{ReceivesTc, ReceivesTcCore}; /// use satrs_core::tmtc::{ReceivesTc, ReceivesTcCore};
/// use spacepackets::SpHeader; /// use spacepackets::SpHeader;
/// use spacepackets::ecss::tc::PusTcCreator; /// use spacepackets::ecss::tc::PusTcCreator;