mqtt_recv_connect_op.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_RECV_CONNECT_OP_HPP__
  11. #define __ASIO2_RECV_CONNECT_OP_HPP__
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. #pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <asio2/base/iopool.hpp>
  16. #include <asio2/base/define.hpp>
  17. #include <asio2/base/detail/util.hpp>
  18. #include <asio2/mqtt/core.hpp>
  19. namespace asio2::detail
  20. {
  21. template<class SocketT, class HandlerT>
  22. class mqtt_recv_connect_op : public asio::coroutine
  23. {
  24. ASIO2_CLASS_FRIEND_DECLARE_BASE;
  25. ASIO2_CLASS_FRIEND_DECLARE_TCP_BASE;
  26. ASIO2_CLASS_FRIEND_DECLARE_TCP_SERVER;
  27. ASIO2_CLASS_FRIEND_DECLARE_TCP_SESSION;
  28. ASIO2_CLASS_FRIEND_DECLARE_TCP_CLIENT;
  29. public:
  30. asio::io_context & context_;
  31. SocketT& socket_;
  32. HandlerT handler_;
  33. std::unique_ptr<asio::streambuf> stream{ std::make_unique<asio::streambuf>() };
  34. template<class SKT, class H>
  35. mqtt_recv_connect_op(asio::io_context& context, SKT& skt, H&& h)
  36. : context_(context)
  37. , socket_ (skt)
  38. , handler_(std::forward<H>(h))
  39. {
  40. (*this)();
  41. }
  42. template<typename = void>
  43. void operator()(error_code ec = {}, std::size_t bytes_transferred = 0)
  44. {
  45. detail::ignore_unused(ec, bytes_transferred);
  46. // There is no need to use a timeout timer because there is already has
  47. // connect_timeout_cp
  48. ASIO_CORO_REENTER(*this)
  49. {
  50. // The client connects to the server, and sends a connect message
  51. // The server wait for recv the connect message
  52. ASIO_CORO_YIELD
  53. {
  54. asio::streambuf& strbuf = *stream;
  55. asio::async_read_until(socket_, strbuf, mqtt::mqtt_match_role, std::move(*this));
  56. }
  57. handler_(ec, std::move(stream));
  58. }
  59. }
  60. };
  61. // C++17 class template argument deduction guides
  62. template<class SKT, class H>
  63. mqtt_recv_connect_op(asio::io_context&, SKT&, H)->mqtt_recv_connect_op<SKT, H>;
  64. }
  65. #endif // !__ASIO2_RECV_CONNECT_OP_HPP__