stream_socket.hpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // Copyright (c) 2024 Klemens Morgenstern (klemens.morgenstern@gmx.net)
  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 BOOST_COBALT_IO_STREAM_SOCKET_HPP
  8. #define BOOST_COBALT_IO_STREAM_SOCKET_HPP
  9. #include <boost/cobalt/io/detail/config.hpp>
  10. #include <boost/cobalt/io/endpoint.hpp>
  11. #include <boost/cobalt/io/socket.hpp>
  12. #include <boost/cobalt/io/stream.hpp>
  13. #include <boost/asio/generic/datagram_protocol.hpp>
  14. #include <boost/asio/basic_stream_socket.hpp>
  15. namespace boost::cobalt::io
  16. {
  17. struct BOOST_SYMBOL_VISIBLE stream_socket final : socket, stream
  18. {
  19. BOOST_COBALT_IO_DECL stream_socket(const cobalt::executor & executor = this_thread::get_executor());
  20. BOOST_COBALT_IO_DECL stream_socket(stream_socket && lhs);
  21. BOOST_COBALT_IO_DECL stream_socket(native_handle_type h, protocol_type protocol = protocol_type(),
  22. const cobalt::executor & executor = this_thread::get_executor());
  23. BOOST_COBALT_IO_DECL stream_socket(endpoint ep,
  24. const cobalt::executor & executor = this_thread::get_executor());
  25. [[nodiscard]] write_op write_some(const_buffer_sequence buffer) override
  26. {
  27. return {buffer, this, initiate_write_some_};
  28. }
  29. [[nodiscard]] read_op read_some(mutable_buffer_sequence buffer) override
  30. {
  31. return {buffer, this, initiate_read_some_};
  32. }
  33. public:
  34. BOOST_COBALT_IO_DECL void adopt_endpoint_(endpoint & ep) override;
  35. BOOST_COBALT_IO_DECL static void initiate_read_some_ (void *, mutable_buffer_sequence, boost::cobalt::completion_handler<system::error_code, std::size_t>);
  36. BOOST_COBALT_IO_DECL static void initiate_write_some_(void *, const_buffer_sequence, boost::cobalt::completion_handler<system::error_code, std::size_t>);
  37. asio::basic_stream_socket<protocol_type, executor> stream_socket_;
  38. friend struct ssl_stream;
  39. };
  40. inline system::result<std::pair<stream_socket, stream_socket>> make_pair(decltype(local_stream) protocol)
  41. {
  42. std::pair<stream_socket, stream_socket> res;
  43. auto c = connect_pair(protocol, res.first, res.second);
  44. if (c)
  45. return res;
  46. else
  47. return c.error();
  48. }
  49. }
  50. #endif //BOOST_COBALT_IOSTREAM_SOCKET_HPP