any_stream.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // Copyright (c) 2019-2023 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BHO_MYSQL_DETAIL_ANY_STREAM_HPP
  8. #define BHO_MYSQL_DETAIL_ANY_STREAM_HPP
  9. #include <asio2/bho/mysql/error_code.hpp>
  10. #include <asio/any_completion_handler.hpp>
  11. #include <asio/any_io_executor.hpp>
  12. #include <asio/buffer.hpp>
  13. #include <cstddef>
  14. namespace bho {
  15. namespace mysql {
  16. namespace detail {
  17. class any_stream
  18. {
  19. public:
  20. any_stream(bool supports_ssl) noexcept
  21. : ssl_state_(supports_ssl ? ssl_state::inactive : ssl_state::unsupported)
  22. {
  23. }
  24. bool ssl_active() const noexcept { return ssl_state_ == ssl_state::active; }
  25. void reset_ssl_active() noexcept
  26. {
  27. if (ssl_state_ == ssl_state::active)
  28. ssl_state_ = ssl_state::inactive;
  29. }
  30. void set_ssl_active() noexcept
  31. {
  32. BHO_ASSERT(ssl_state_ != ssl_state::unsupported);
  33. ssl_state_ = ssl_state::active;
  34. }
  35. bool supports_ssl() const noexcept { return ssl_state_ != ssl_state::unsupported; }
  36. using executor_type = asio::any_io_executor;
  37. virtual ~any_stream() {}
  38. virtual executor_type get_executor() = 0;
  39. // SSL
  40. virtual void handshake(error_code& ec) = 0;
  41. virtual void async_handshake(asio::any_completion_handler<void(error_code)>) = 0;
  42. virtual void shutdown(error_code& ec) = 0;
  43. virtual void async_shutdown(asio::any_completion_handler<void(error_code)>) = 0;
  44. // Reading
  45. virtual std::size_t read_some(asio::mutable_buffer, error_code& ec) = 0;
  46. virtual void async_read_some(asio::mutable_buffer, asio::any_completion_handler<void(error_code, std::size_t)>) = 0;
  47. // Writing
  48. virtual std::size_t write_some(asio::const_buffer, error_code& ec) = 0;
  49. virtual void async_write_some(asio::const_buffer, asio::any_completion_handler<void(error_code, std::size_t)>) = 0;
  50. // Connect and close - these apply only to SocketStream's
  51. virtual void connect(const void* endpoint, error_code& ec) = 0;
  52. virtual void async_connect(const void* endpoint, asio::any_completion_handler<void(error_code)>) = 0;
  53. virtual void close(error_code& ec) = 0;
  54. virtual bool is_open() const noexcept = 0;
  55. private:
  56. enum class ssl_state
  57. {
  58. inactive,
  59. active,
  60. unsupported
  61. } ssl_state_;
  62. };
  63. } // namespace detail
  64. } // namespace mysql
  65. } // namespace bho
  66. #endif