MeVisLab Toolbox Reference
TcpSocket.h
Go to the documentation of this file.
1 // Copyright (c) Fraunhofer MEVIS, Germany. All rights reserved.
2 // **InsertLicense** code
3 
4 #pragma once
5 
6 #include <asio/System.h>
7 #include <asio/Context.h>
8 #include <asio/Socket.h>
9 
10 #include <ThirdPartyWarningsDisable.h>
11 #include <boost/asio/ip/tcp.hpp>
12 #include <memory>
13 #include <ThirdPartyWarningsRestore.h>
14 
15 
16 namespace asio {
17 
36  {
37  public:
38 
39  TcpSocket(std::shared_ptr<Context> context);
40 
41  ~TcpSocket() override;
42 
43  template<typename ConnectCompletionHandler>
44  void async_connect(const std::string& host, const unsigned short port, ConnectCompletionHandler&& handler)
45  {
46  using DnsQuery = boost::asio::ip::tcp::resolver::query;
47  using TcpResolver = boost::asio::ip::tcp::resolver;
48  using EndpointIterator = TcpResolver::iterator;
49 
50  auto query = DnsQuery(host == "localhost" ? "127.0.0.1" : host, std::to_string(port));
51 #if defined(MLAB_CMAKE_BUILDSYSTEM)
52 // See https://stackoverflow.com/questions/67281460/boostasio-get-io-service-alternative-in-boost-1-70/67773642#67773642
53 // for resolution of deprecation of get_io_service().
54 #if BOOST_VERSION >= 107000
55 #define GET_IO_SERVICE(s) ((boost::asio::io_context&)(s).get_executor().context())
56 #else
57 #define GET_IO_SERVICE(s) ((s).get_io_service())
58 #endif
59 
60  auto resolver = std::make_shared<TcpResolver>(GET_IO_SERVICE(socket));
61 #else
62  auto resolver = std::make_shared<TcpResolver>(socket.get_io_service());
63 #endif
64 
65 
66  resolver->async_resolve(query,
67  [this, handler, resolver](const ErrorCode& ec_resolve, const EndpointIterator& iterator) {
68  if (!ec_resolve) {
69  socket.async_connect(iterator->endpoint(), handler);
70  }
71  else {
72  handler(ec_resolve);
73  }
74  });
75  }
76 
77  bool is_open() const;
78 
79  bool is_valid() const override;
80 
81  void async_read(void* buffer, const std::size_t buffer_size, const CompletionHandler& handler) override;
82 
83  void async_write(const void* buffer, const std::size_t buffer_size, const CompletionHandler& handler) override;
84 
85  void close() override;
86 
87  TcpSocket(const TcpSocket&) = delete;
88 
89  TcpSocket& operator=(const TcpSocket&) = delete;
90 
91  protected:
92 
93  friend class TcpAcceptor;
94 
95  std::shared_ptr<Context> context;
96 
97  boost::asio::ip::tcp::socket socket;
98  };
99 
100 }
#define ASYNCHRONOUSIO_EXPORT
Definition: System.h:9
Abstract base class for an asynchronous channel supporting read and write of binary data.
Definition: Socket.h:23
std::function< void(const ErrorCode &, const std::size_t)> CompletionHandler
Definition: Socket.h:26
A TCP acceptor listening on a given port and interface address.
Definition: TcpAcceptor.h:40
A TCP socket class implementing the asio::Socket interface.
Definition: TcpSocket.h:36
std::shared_ptr< Context > context
Definition: TcpSocket.h:95
void async_write(const void *buffer, const std::size_t buffer_size, const CompletionHandler &handler) override
void async_connect(const std::string &host, const unsigned short port, ConnectCompletionHandler &&handler)
Definition: TcpSocket.h:44
void close() override
TcpSocket(const TcpSocket &)=delete
void async_read(void *buffer, const std::size_t buffer_size, const CompletionHandler &handler) override
bool is_open() const
boost::asio::ip::tcp::socket socket
Definition: TcpSocket.h:97
~TcpSocket() override
TcpSocket(std::shared_ptr< Context > context)
bool is_valid() const override
TcpSocket & operator=(const TcpSocket &)=delete
boost::system::error_code ErrorCode
Definition: ErrorCodes.h:13