use std::{cmp::min, ffi::c_int, time::Duration};
use crate::{IPPROTO_TCP, OptRMarker, OptWMarker, SockOptMarker};
#[cfg(not(any(
target_os = "tvos",
target_os = "ios",
target_os = "macos",
target_os = "openbsd",
)))]
use crate::TCP_KEEPIDLE;
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
))]
use crate::TCP_KEEPALIVE;
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct IpProtoKeepIdleAliveR(c_int);
impl SockOptMarker for IpProtoKeepIdleAliveR
{
const SO_LEVEL: c_int = IPPROTO_TCP;
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
))]
const SO_OPTNAME: c_int = TCP_KEEPALIVE;
#[cfg(not(any(
target_os = "tvos",
target_os = "ios",
target_os = "macos",
target_os = "openbsd",
)))]
const SO_OPTNAME: c_int = TCP_KEEPIDLE;
type DataType = Duration;
type InputType = c_int;
#[inline]
fn from(value: Self::InputType) -> Self
{
return Self(value);
}
#[inline]
fn get(self) -> Self::DataType
{
return Duration::from_secs(self.0 as u64);
}
#[inline]
fn from_user(_sz: Self::DataType) -> Self where Self: Sized
{
panic!("IpProtoKeepIdleAliveR cannot write!");
}
}
impl OptRMarker for IpProtoKeepIdleAliveR {}
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct IpProtoKeepIdleAliveW(c_int);
impl SockOptMarker for IpProtoKeepIdleAliveW
{
const SO_LEVEL: c_int = IPPROTO_TCP;
#[cfg(any(
target_os = "ios",
target_os = "macos",
target_os = "tvos",
))]
const SO_OPTNAME: c_int = TCP_KEEPALIVE;
#[cfg(not(any(
target_os = "tvos",
target_os = "ios",
target_os = "macos",
target_os = "openbsd",
)))]
const SO_OPTNAME: c_int = TCP_KEEPIDLE;
type DataType = Duration;
type InputType = c_int;
#[inline]
fn from(_value: Self::InputType) -> Self
{
panic!("IpProtoKeepIdleAliveR cannot read!");
}
#[inline]
fn get(self) -> Self::DataType
{
panic!("IpProtoKeepIdleAliveR cannot read!");
}
#[inline]
fn from_user(sz: Self::DataType) -> Self where Self: Sized
{
return Self(min(sz.as_secs(), c_int::MAX as u64) as Self::InputType);
}
}
impl OptWMarker for IpProtoKeepIdleAliveW {}