wss_server.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #if defined(ASIO2_ENABLE_SSL) || defined(ASIO2_USE_SSL)
  11. #ifndef __ASIO2_WSS_SERVER_HPP__
  12. #define __ASIO2_WSS_SERVER_HPP__
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. #pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <asio2/base/detail/push_options.hpp>
  17. #include <asio2/tcp/tcps_server.hpp>
  18. #include <asio2/http/wss_session.hpp>
  19. namespace asio2::detail
  20. {
  21. ASIO2_CLASS_FORWARD_DECLARE_BASE;
  22. ASIO2_CLASS_FORWARD_DECLARE_TCP_BASE;
  23. ASIO2_CLASS_FORWARD_DECLARE_TCP_SERVER;
  24. template<class derived_t, class session_t>
  25. class wss_server_impl_t : public tcps_server_impl_t<derived_t, session_t>
  26. {
  27. ASIO2_CLASS_FRIEND_DECLARE_BASE;
  28. ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE;
  29. ASIO2_CLASS_FRIEND_DECLARE_TCP_SERVER;
  30. public:
  31. using super = tcps_server_impl_t<derived_t, session_t>;
  32. using self = wss_server_impl_t <derived_t, session_t>;
  33. using session_type = session_t;
  34. public:
  35. /**
  36. * @brief constructor
  37. */
  38. template<class... Args>
  39. explicit wss_server_impl_t(
  40. asio::ssl::context::method method,
  41. Args&&... args
  42. )
  43. : super(method, std::forward<Args>(args)...)
  44. {
  45. }
  46. /**
  47. * @brief constructor
  48. */
  49. template<class... Args>
  50. explicit wss_server_impl_t(Args&&... args)
  51. : super(ASIO2_DEFAULT_SSL_METHOD, std::forward<Args>(args)...)
  52. {
  53. }
  54. /**
  55. * @brief constructor
  56. */
  57. explicit wss_server_impl_t()
  58. : super(ASIO2_DEFAULT_SSL_METHOD)
  59. {
  60. }
  61. /**
  62. * @brief destructor
  63. */
  64. ~wss_server_impl_t()
  65. {
  66. this->stop();
  67. }
  68. /**
  69. * @brief start the server
  70. * @param host - A string identifying a location. May be a descriptive name or
  71. * a numeric address string.
  72. * @param service - A string identifying the requested service. This may be a
  73. * descriptive name or a numeric string corresponding to a port number.
  74. */
  75. template<typename String, typename StrOrInt, typename... Args>
  76. bool start(String&& host, StrOrInt&& service, Args&&... args)
  77. {
  78. return this->derived()._do_start(
  79. std::forward<String>(host), std::forward<StrOrInt>(service),
  80. ecs_helper::make_ecs('0', std::forward<Args>(args)...));
  81. }
  82. public:
  83. /**
  84. * @brief bind websocket upgrade listener
  85. * @param fun - a user defined callback function.
  86. * Function signature : void(std::shared_ptr<asio2::wss_session>& session_ptr)
  87. */
  88. template<class F, class ...C>
  89. inline derived_t & bind_upgrade(F&& fun, C&&... obj)
  90. {
  91. this->listener_.bind(event_type::upgrade, observer_t<std::shared_ptr<session_t>&>
  92. (std::forward<F>(fun), std::forward<C>(obj)...));
  93. return (this->derived());
  94. }
  95. protected:
  96. };
  97. }
  98. namespace asio2
  99. {
  100. template<class derived_t, class session_t>
  101. using wss_server_impl_t = detail::wss_server_impl_t<derived_t, session_t>;
  102. template<class session_t>
  103. class wss_server_t : public detail::wss_server_impl_t<wss_server_t<session_t>, session_t>
  104. {
  105. public:
  106. using detail::wss_server_impl_t<wss_server_t<session_t>, session_t>::wss_server_impl_t;
  107. };
  108. using wss_server = wss_server_t<wss_session>;
  109. }
  110. #if defined(ASIO2_INCLUDE_RATE_LIMIT)
  111. #include <asio2/tcp/tcp_stream.hpp>
  112. namespace asio2
  113. {
  114. template<class session_t>
  115. class wss_rate_server_t : public asio2::wss_server_impl_t<wss_rate_server_t<session_t>, session_t>
  116. {
  117. public:
  118. using asio2::wss_server_impl_t<wss_rate_server_t<session_t>, session_t>::wss_server_impl_t;
  119. };
  120. using wss_rate_server = wss_rate_server_t<wss_rate_session>;
  121. }
  122. #endif
  123. #include <asio2/base/detail/pop_options.hpp>
  124. #endif // !__ASIO2_WSS_SERVER_HPP__
  125. #endif