ws_server.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_WS_SERVER_HPP__
  11. #define __ASIO2_WS_SERVER_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/push_options.hpp>
  16. #include <asio2/tcp/tcp_server.hpp>
  17. #include <asio2/http/ws_session.hpp>
  18. namespace asio2::detail
  19. {
  20. ASIO2_CLASS_FORWARD_DECLARE_BASE;
  21. ASIO2_CLASS_FORWARD_DECLARE_TCP_BASE;
  22. ASIO2_CLASS_FORWARD_DECLARE_TCP_SERVER;
  23. template<class derived_t, class session_t>
  24. class ws_server_impl_t : public tcp_server_impl_t<derived_t, session_t>
  25. {
  26. ASIO2_CLASS_FRIEND_DECLARE_BASE;
  27. ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE;
  28. ASIO2_CLASS_FRIEND_DECLARE_TCP_SERVER;
  29. public:
  30. using super = tcp_server_impl_t<derived_t, session_t>;
  31. using self = ws_server_impl_t <derived_t, session_t>;
  32. using session_type = session_t;
  33. public:
  34. /**
  35. * @brief constructor
  36. */
  37. template<class ...Args>
  38. explicit ws_server_impl_t(Args&&... args)
  39. : super(std::forward<Args>(args)...)
  40. {
  41. }
  42. /**
  43. * @brief destructor
  44. */
  45. ~ws_server_impl_t()
  46. {
  47. this->stop();
  48. }
  49. /**
  50. * @brief start the server
  51. * @param host - A string identifying a location. May be a descriptive name or
  52. * a numeric address string.
  53. * @param service - A string identifying the requested service. This may be a
  54. * descriptive name or a numeric string corresponding to a port number.
  55. */
  56. template<typename String, typename StrOrInt, typename... Args>
  57. bool start(String&& host, StrOrInt&& service, Args&&... args)
  58. {
  59. return this->derived()._do_start(
  60. std::forward<String>(host), std::forward<StrOrInt>(service),
  61. ecs_helper::make_ecs('0', std::forward<Args>(args)...));
  62. }
  63. public:
  64. /**
  65. * @brief bind websocket upgrade listener
  66. * @param fun - a user defined callback function.
  67. * Function signature : void(std::shared_ptr<asio2::ws_session>& session_ptr)
  68. */
  69. template<class F, class ...C>
  70. inline derived_t & bind_upgrade(F&& fun, C&&... obj)
  71. {
  72. this->listener_.bind(event_type::upgrade, observer_t<std::shared_ptr<session_t>&>
  73. (std::forward<F>(fun), std::forward<C>(obj)...));
  74. return (this->derived());
  75. }
  76. protected:
  77. };
  78. }
  79. namespace asio2
  80. {
  81. template<class derived_t, class session_t>
  82. using ws_server_impl_t = detail::ws_server_impl_t<derived_t, session_t>;
  83. template<class session_t>
  84. class ws_server_t : public detail::ws_server_impl_t<ws_server_t<session_t>, session_t>
  85. {
  86. public:
  87. using detail::ws_server_impl_t<ws_server_t<session_t>, session_t>::ws_server_impl_t;
  88. };
  89. using ws_server = ws_server_t<ws_session>;
  90. }
  91. #if defined(ASIO2_INCLUDE_RATE_LIMIT)
  92. #include <asio2/tcp/tcp_stream.hpp>
  93. namespace asio2
  94. {
  95. template<class session_t>
  96. class ws_rate_server_t : public asio2::ws_server_impl_t<ws_rate_server_t<session_t>, session_t>
  97. {
  98. public:
  99. using asio2::ws_server_impl_t<ws_rate_server_t<session_t>, session_t>::ws_server_impl_t;
  100. };
  101. using ws_rate_server = ws_rate_server_t<ws_rate_session>;
  102. }
  103. #endif
  104. #include <asio2/base/detail/pop_options.hpp>
  105. #endif // !__ASIO2_WS_SERVER_HPP__