diff --git a/examples/socket_client_with_options.rs b/examples/socket_client_with_options.rs index 065cd5c..1dda861 100644 --- a/examples/socket_client_with_options.rs +++ b/examples/socket_client_with_options.rs @@ -2,13 +2,20 @@ mod async_helpers; use std::convert::TryFrom; use std::error::Error; -use zeromq::util::PeerIdentity; +use zeromq::util::{PeerIdentity, TcpKeepalive}; use zeromq::{Socket, SocketOptions, SocketRecv, SocketSend}; #[async_helpers::main] async fn main() -> Result<(), Box> { let mut options = SocketOptions::default(); - options.peer_identity(PeerIdentity::try_from(Vec::from("SomeCustomId")).unwrap()); + options + .peer_identity(PeerIdentity::try_from(Vec::from("SomeCustomId")).unwrap()) + .tcp_keepalive( + TcpKeepalive::default() + .set_idle(1) + .set_count(5) + .set_interval(10), + ); let mut socket = zeromq::ReqSocket::with_options(options); socket diff --git a/src/lib.rs b/src/lib.rs index 8a3b8e1..50152ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ pub use message::*; use crate::codec::*; use crate::transport::AcceptStopHandle; -use util::PeerIdentity; +use util::{PeerIdentity, TcpKeepalive}; #[macro_use] extern crate enum_primitive_derive; @@ -129,6 +129,7 @@ pub enum SocketEvent { #[derive(Default)] pub struct SocketOptions { pub(crate) peer_id: Option, + pub(crate) tcp_keepalive: Option, } impl SocketOptions { @@ -136,6 +137,11 @@ impl SocketOptions { self.peer_id = Some(peer_id); self } + + pub fn tcp_keepalive(&mut self, tcp_keepalive_opt: TcpKeepalive) -> &mut Self { + self.tcp_keepalive = Some(tcp_keepalive_opt); + self + } } #[async_trait] diff --git a/src/util.rs b/src/util.rs index a5c3a3c..a5703fb 100644 --- a/src/util.rs +++ b/src/util.rs @@ -216,6 +216,50 @@ pub(crate) async fn connect_forever(endpoint: Endpoint) -> ZmqResult<(FramedIo, } } +#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Clone)] +pub struct TcpKeepalive { + pub idle: Option, + pub count: Option, + pub interval: Option, +} + +impl TcpKeepalive { + pub const fn new() -> Self { + TcpKeepalive { + idle: None, + count: None, + interval: None, + } + } + + pub const fn set_idle(self, idle: u64) -> Self { + Self { + idle: Some(idle), + ..self + } + } + + pub const fn set_count(self, count: u64) -> Self { + Self { + count: Some(count), + ..self + } + } + + pub const fn set_interval(self, interval: u64) -> Self { + Self { + interval: Some(interval), + ..self + } + } +} + +impl Default for TcpKeepalive { + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] pub(crate) mod tests { use super::*;