tcp_keepalive_cp.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * email : 37792738@qq.com
  6. *
  7. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. */
  10. #ifndef __ASIO2_TCP_KEEPALIVE_COMPONENT_HPP__
  11. #define __ASIO2_TCP_KEEPALIVE_COMPONENT_HPP__
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. #pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <asio2/base/detail/keepalive_options.hpp>
  16. namespace asio2::detail
  17. {
  18. template<class derived_t, class args_t>
  19. class tcp_keepalive_cp
  20. {
  21. public:
  22. tcp_keepalive_cp() noexcept {}
  23. ~tcp_keepalive_cp() noexcept {}
  24. /**
  25. * @brief set tcp socket keep alive options
  26. * @param onoff - Turn keepalive on or off.
  27. * @param idle - How many seconds after the connection is idle, start sending keepalives.
  28. * @param interval - How many seconds later to send again when no reply is received.
  29. * @param count - How many times to resend when no reply is received.
  30. * @li on macOS Catalina 10.15.5 (19F101), the default value is:
  31. * onoff - false, idle - 7200, interval - 75, count - 8
  32. */
  33. bool set_keep_alive_options(
  34. bool onoff = true,
  35. unsigned int idle = 60,
  36. unsigned int interval = 3,
  37. unsigned int count = 3
  38. ) noexcept
  39. {
  40. derived_t& derive = static_cast<derived_t&>(*this);
  41. return detail::set_keepalive_options(derive.socket(), onoff, idle, interval, count);
  42. }
  43. };
  44. }
  45. #endif // !__ASIO2_TCP_KEEPALIVE_COMPONENT_HPP__