diff --git a/satrs-core/Cargo.toml b/satrs-core/Cargo.toml index 7b6697f..9ee7cc1 100644 --- a/satrs-core/Cargo.toml +++ b/satrs-core/Cargo.toml @@ -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", diff --git a/satrs-core/src/hal/std/tcp_server.rs b/satrs-core/src/hal/std/tcp_server.rs index f8cd16a..c98ab9d 100644 --- a/satrs-core/src/hal/std/tcp_server.rs +++ b/satrs-core/src/hal/std/tcp_server.rs @@ -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 { } impl TcpTmtcServerBase { - pub(crate) fn new( - addr: A, + pub(crate) fn new( + addr: &SocketAddr, + reuse_addr: bool, + reuse_port: bool, tm_buffer_size: usize, tm_source: Box + Send>, tc_buffer_size: usize, tc_receiver: Box + Send>, ) -> Result { + // 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 TcpTmtcServerBase { }) } + pub(crate) fn listener(&mut self) -> &mut TcpListener { + &mut self.listener + } + pub(crate) fn local_addr(&self) -> std::io::Result { self.listener.local_addr() } diff --git a/satrs-core/src/hal/std/tcp_with_cobs_server.rs b/satrs-core/src/hal/std/tcp_with_cobs_server.rs index 0d1248c..605595f 100644 --- a/satrs-core/src/hal/std/tcp_with_cobs_server.rs +++ b/satrs-core/src/hal/std/tcp_with_cobs_server.rs @@ -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 TcpTmtcInCobsServer { /// ## 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 TcpTmtcInCobsServer { /// calculate this size. /// * `tc_receiver` - Any received telecommand which was decoded successfully will be forwarded /// to this TC receiver. - pub fn new( - addr: A, + pub fn new( + addr: &SocketAddr, + reuse_addr: bool, + reuse_port: bool, tm_buffer_size: usize, tm_source: Box + Send>, tc_buffer_size: usize, @@ -68,6 +74,8 @@ impl TcpTmtcInCobsServer { Ok(Self { base: TcpTmtcServerBase::new( addr, + reuse_addr, + reuse_port, tm_buffer_size, tm_source, tc_buffer_size, @@ -77,6 +85,11 @@ impl TcpTmtcInCobsServer { }) } + /// 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 { @@ -464,6 +477,8 @@ mod tests { ) -> TcpTmtcInCobsServer<(), ()> { TcpTmtcInCobsServer::new( addr, + false, + false, 1024, Box::new(tm_source), 1024, diff --git a/satrs-core/src/hal/std/udp_server.rs b/satrs-core/src/hal/std/udp_server.rs index 931da1c..28e0328 100644 --- a/satrs-core/src/hal/std/udp_server.rs +++ b/satrs-core/src/hal/std/udp_server.rs @@ -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;