fiber_socket.h
1 // Copyright 2018, Beeri 15. All rights reserved.
2 // Author: Roman Gershman (romange@gmail.com)
3 //
4 #pragma once
5 
6 #include <boost/asio/ip/tcp.hpp>
7 
8 #include "util/asio/asio_utils.h"
9 #include "util/asio/detail/fiber_socket_impl.h"
10 
11 namespace util {
12 
13 class IoContext;
14 
16  public:
17  using error_code = ::boost::system::error_code;
18  using next_layer_type = ::boost::asio::ip::tcp::socket;
19  using lowest_layer_type = next_layer_type::lowest_layer_type;
20 
21  // C'tor can be called from any thread.
22  FiberSyncSocket(next_layer_type&& sock, size_t rbuf_size = 1 << 12)
23  : impl_(new detail::FiberSocketImpl{std::move(sock), rbuf_size}) {}
24 
26  FiberSyncSocket(const std::string& hname, const std::string& port, IoContext* cntx,
27  size_t rbuf_size = 1 << 12)
28  : impl_(new detail::FiberSocketImpl{hname, port, cntx, rbuf_size}) {}
29 
30  // FiberSyncSocket can not be moveable due to attached fiber.
31  FiberSyncSocket(FiberSyncSocket&& other) : impl_(std::move(other.impl_)) {}
32 
33  ~FiberSyncSocket() {}
34 
35  // Waits for client socket to become connected. Can be called from any thread.
36  // Please note that connection status might be stale if called from a foreigh thread.
37  error_code ClientWaitToConnect(uint32_t ms) {
38  return impl_->ClientWaitToConnect(ms);
39  }
40 
41  // Read/Write functions should be called from IoContext thread.
42  // (fiber) SyncRead interface:
43  // https://www.boost.org/doc/libs/1_69_0/doc/html/boost_asio/reference/SyncReadStream.html
44  template <typename MBS> size_t read_some(const MBS& bufs, error_code& ec) {
45  return impl_->read_some(bufs, ec);
46  }
47 
48  // To calm SyncReadStream compile-checker we provide exception-enabled interface without
49  // implementing it.
50  template <typename MBS> size_t read_some(const MBS& bufs);
51 
52  // SyncWrite interface:
53  // https://www.boost.org/doc/libs/1_69_0/doc/html/boost_asio/reference/SyncWriteStream.html
54  template <typename BS> size_t write_some(const BS& bufs, error_code& ec) {
55  return impl_->write_some(bufs, ec);
56  }
57 
58  // To calm SyncWriteStream compile-checker we provide exception-enabled interface without
59  // implementing it.
60  template <typename BS> size_t write_some(const BS& bufs);
61 
62  auto native_handle() { return impl_->native_handle(); }
63 
64  bool is_open() const { return impl_ && impl_->is_open(); }
65 
66  // Closes the socket and shuts down its background processes if needed.
67  // For client socket it's thread-safe but for non-client it should be called
68  // from the socket thread.
69  void Shutdown(error_code& ec) {
70  impl_->Shutdown(ec);
71  }
72 
73  next_layer_type::endpoint_type remote_endpoint(error_code& ec) const {
74  return impl_->remote_endpoint(ec);
75  }
76 
77  error_code status() const { return impl_->status(); }
78 
79  // To support socket requirements.
80  next_layer_type& next_layer() { return impl_->next_layer(); }
81  lowest_layer_type& lowest_layer() { return impl_->next_layer().lowest_layer(); }
82 
83  // For debugging/testing.
84  IoContext& context() { return impl_->context(); }
85 
86  bool keep_alive() const { return impl_->keep_alive(); }
87  void set_keep_alive(bool flag) { impl_->set_keep_alive(flag); }
88 
89  private:
90  std::unique_ptr<detail::FiberSocketImpl> impl_;
91 };
92 
93 static_assert(std::is_move_constructible<FiberSyncSocket>::value, "");
94 
95 } // namespace util
FiberSyncSocket(const std::string &hname, const std::string &port, IoContext *cntx, size_t rbuf_size=1<< 12)
Client socket constructor.
Definition: fiber_socket.h:26