Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-17 08:39:20

0001 //
0002 // Copyright (c) 2024 Klemens Morgenstern (klemens.morgenstern@gmx.net)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 
0008 #ifndef BOOST_COBALT_IOSOCKET_HPP
0009 #define BOOST_COBALT_IOSOCKET_HPP
0010 
0011 #include <boost/cobalt/io/detail/config.hpp>
0012 #include <boost/cobalt/io/endpoint.hpp>
0013 #include <boost/cobalt/io/ops.hpp>
0014 #include <boost/asio/socket_base.hpp>
0015 #include <boost/asio/basic_socket.hpp>
0016 
0017 
0018 namespace boost::cobalt::io
0019 {
0020 
0021 struct BOOST_SYMBOL_VISIBLE socket
0022 {
0023   [[nodiscard]] system::result<void> open(protocol_type prot = protocol_type {});
0024   [[nodiscard]] system::result<void> close();
0025   [[nodiscard]] system::result<void> cancel();
0026   [[nodiscard]] bool is_open() const;
0027 
0028   // asio acceptor compatibility
0029   template<typename T>
0030   struct rebind_executor {using other = socket;};
0031 
0032   using shutdown_type      = asio::socket_base::shutdown_type;
0033   using wait_type          = asio::socket_base::wait_type;
0034   using message_flags      = asio::socket_base::message_flags;
0035   constexpr static int message_peek          = asio::socket_base::message_peek;
0036   constexpr static int message_out_of_band   = asio::socket_base::message_out_of_band;
0037   constexpr static int message_do_not_route  = asio::socket_base::message_do_not_route;
0038   constexpr static int message_end_of_record = asio::socket_base::message_end_of_record;
0039 
0040   using native_handle_type = asio::basic_socket<protocol_type, executor>::native_handle_type;
0041   BOOST_COBALT_IO_DECL native_handle_type native_handle();
0042 
0043   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> shutdown(shutdown_type = shutdown_type::shutdown_both);
0044 
0045   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<endpoint> local_endpoint() const;
0046   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<endpoint> remote_endpoint() const;
0047 
0048 
0049   BOOST_COBALT_IO_DECL system::result<void> assign(protocol_type protocol, native_handle_type native_handle);
0050   BOOST_COBALT_IO_DECL system::result<native_handle_type> release();
0051 
0052   /// copied from what asio does
0053   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<std::size_t> bytes_readable();
0054 
0055   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> set_debug(bool debug);
0056   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<bool> get_debug() const;
0057 
0058   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> set_do_not_route(bool do_not_route);
0059   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<bool> get_do_not_route() const;
0060 
0061   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> set_enable_connection_aborted(bool enable_connection_aborted);
0062   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<bool> get_enable_connection_aborted() const;
0063 
0064   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> set_keep_alive(bool keep_alive);
0065   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<bool> get_keep_alive() const;
0066 
0067   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> set_linger(bool linger, int timeout);
0068   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<std::pair<bool, int>> get_linger() const;
0069 
0070   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void>        set_receive_buffer_size(int receive_buffer_size);
0071   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<int> get_receive_buffer_size() const;
0072 
0073   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void>        set_send_buffer_size(int send_buffer_size);
0074   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<int> get_send_buffer_size() const;
0075 
0076   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void>        set_receive_low_watermark(int receive_low_watermark);
0077   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<int> get_receive_low_watermark() const;
0078 
0079   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void>        set_send_low_watermark(int send_low_watermark);
0080   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<int> get_send_low_watermark() const;
0081 
0082   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> set_reuse_address(bool reuse_address);
0083   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<bool> get_reuse_address() const;
0084 
0085   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<void> set_no_delay(bool reuse_address);
0086   [[nodiscard]] BOOST_COBALT_IO_DECL system::result<bool> get_no_delay() const;
0087 
0088   struct BOOST_COBALT_IO_DECL wait_op final : op<system::error_code>
0089   {
0090     wait_type wt;
0091     void ready(boost::cobalt::handler<system::error_code>) final;
0092     void initiate(boost::cobalt::completion_handler<system::error_code>) final;
0093 
0094     wait_op(wait_type wt, socket & sock) :
0095         wt(wt), sock_(sock) {}
0096     ~wait_op() = default;
0097    private:
0098     socket & sock_;
0099 
0100   };
0101   [[nodiscard]]  wait_op wait(wait_type wt = wait_type::wait_read)
0102   {
0103     return {wt, *this};
0104   }
0105 
0106   struct BOOST_COBALT_IO_DECL connect_op final : op<system::error_code>
0107   {
0108     struct endpoint endpoint;
0109 
0110     void initiate(boost::cobalt::completion_handler<system::error_code>) final;
0111 
0112     connect_op(struct endpoint endpoint, socket & socket) :
0113         endpoint(endpoint), sock_(socket) {}
0114     ~connect_op() = default;
0115    private:
0116     socket & sock_;
0117   };
0118 
0119   [[nodiscard]] connect_op connect(endpoint ep)
0120   {
0121     return {ep, *this};
0122   }
0123 
0124 
0125   struct BOOST_COBALT_IO_DECL ranged_connect_op final : op<system::error_code, endpoint>
0126   {
0127     endpoint_sequence endpoints;
0128 
0129     void initiate(boost::cobalt::completion_handler<system::error_code, endpoint>) final;
0130 
0131     ranged_connect_op(endpoint_sequence eps, socket & socket) :
0132         endpoints(eps), sock_(socket) {}
0133     ~ranged_connect_op() = default;
0134    private:
0135     socket & sock_;
0136   };
0137   [[nodiscard]] ranged_connect_op connect(endpoint_sequence ep)
0138   {
0139     return {std::move(ep), *this};
0140   }
0141 
0142 
0143  protected:
0144   virtual void adopt_endpoint_(endpoint & ) {}
0145   socket(asio::basic_socket<protocol_type, executor> & socket) : socket_(socket) {}
0146  private:
0147 
0148   friend struct acceptor;
0149   asio::basic_socket<protocol_type, executor> & socket_;
0150 };
0151 
0152 BOOST_COBALT_IO_DECL system::result<void> connect_pair(protocol_type protocol, socket & socket1, socket & socket2);
0153 
0154 
0155 }
0156 
0157 #endif //BOOST_COBALT_IOSOCKET_HPP