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
optional = true
[dependencies.socket2]
version = "0.5.4"
features = ["all"]
optional = true
[dependencies.spacepackets]
version = "0.7.0-beta.1"
# path = "../../spacepackets"
@ -93,7 +98,8 @@ std = [
"serde/std",
"spacepackets/std",
"num_enum/std",
"thiserror"
"thiserror",
"socket2"
]
alloc = [
"serde/alloc",

View File

@ -1,8 +1,9 @@
//! Generic TCP TMTC servers with different TMTC format flavours.
use alloc::vec;
use alloc::{boxed::Box, vec::Vec};
use socket2::{Domain, Socket, Type};
use std::net::SocketAddr;
use std::net::{TcpListener, ToSocketAddrs};
use std::net::TcpListener;
use crate::tmtc::{ReceivesTc, TmPacketSource};
use thiserror::Error;
@ -40,15 +41,24 @@ pub(crate) struct TcpTmtcServerBase<TcError, TmError> {
}
impl<TcError, TmError> TcpTmtcServerBase<TcError, TmError> {
pub(crate) fn new<A: ToSocketAddrs>(
addr: A,
pub(crate) fn new(
addr: &SocketAddr,
reuse_addr: bool,
reuse_port: bool,
tm_buffer_size: usize,
tm_source: Box<dyn TmPacketSource<Error = TmError> + Send>,
tc_buffer_size: usize,
tc_receiver: Box<dyn ReceivesTc<Error = TcError> + Send>,
) -> 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 {
listener: TcpListener::bind(addr)?,
listener: socket.into(),
tm_source,
tm_buffer: vec![0; tm_buffer_size],
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> {
self.listener.local_addr()
}

View File

@ -6,7 +6,7 @@ use cobs::max_encoding_length;
use std::io::Read;
use std::io::Write;
use std::net::SocketAddr;
use std::net::ToSocketAddrs;
use std::net::TcpListener;
use std::vec::Vec;
use crate::hal::std::tcp_server::TcpTmtcServerBase;
@ -44,6 +44,10 @@ impl<TcError: 'static, TmError: 'static> TcpTmtcInCobsServer<TcError, TmError> {
/// ## Parameter
///
/// * `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
/// 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
@ -58,8 +62,10 @@ impl<TcError: 'static, TmError: 'static> TcpTmtcInCobsServer<TcError, TmError> {
/// calculate this size.
/// * `tc_receiver` - Any received telecommand which was decoded successfully will be forwarded
/// to this TC receiver.
pub fn new<A: ToSocketAddrs>(
addr: A,
pub fn new(
addr: &SocketAddr,
reuse_addr: bool,
reuse_port: bool,
tm_buffer_size: usize,
tm_source: Box<dyn TmPacketSource<Error = TmError> + Send>,
tc_buffer_size: usize,
@ -68,6 +74,8 @@ impl<TcError: 'static, TmError: 'static> TcpTmtcInCobsServer<TcError, TmError> {
Ok(Self {
base: TcpTmtcServerBase::new(
addr,
reuse_addr,
reuse_port,
tm_buffer_size,
tm_source,
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
/// useful if using the port number 0 for OS auto-assignment.
pub fn local_addr(&self) -> std::io::Result<SocketAddr> {
@ -464,6 +477,8 @@ mod tests {
) -> TcpTmtcInCobsServer<(), ()> {
TcpTmtcInCobsServer::new(
addr,
false,
false,
1024,
Box::new(tm_source),
1024,

View File

@ -20,7 +20,7 @@ use std::vec::Vec;
/// ```
/// use std::net::{IpAddr, Ipv4Addr, SocketAddr, UdpSocket};
/// 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 spacepackets::SpHeader;
/// use spacepackets::ecss::tc::PusTcCreator;