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 <FMEThirdPartyWarningsDisable.h>
11#include <boost/asio/ip/tcp.hpp>
12#include <memory>
13#include <FMEThirdPartyWarningsRestore.h>
14
15
16namespace asio {
17
35 // Note: inline this class completely or export base class and do NOT export a class whose base classes is not.
36 // Whether it is a realy problem here is still unclear, but this should be handled. At least in cases of same problem with
37 // derivations of std::runtime_error this would cause subtle problems.
39 {
40 public:
41
42 TcpSocket(std::shared_ptr<Context> context);
43
44 ~TcpSocket() override;
45
46 template<typename ConnectCompletionHandler>
47 void async_connect(const std::string& host, const unsigned short port, ConnectCompletionHandler&& handler)
48 {
49 using DnsQuery = boost::asio::ip::tcp::resolver::query;
50 using TcpResolver = boost::asio::ip::tcp::resolver;
51 using EndpointIterator = TcpResolver::iterator;
52
53 auto query = DnsQuery(host == "localhost" ? "127.0.0.1" : host, std::to_string(port));
54#if defined(MLAB_CMAKE_BUILDSYSTEM)
55// See https://stackoverflow.com/questions/67281460/boostasio-get-io-service-alternative-in-boost-1-70/67773642#67773642
56// for resolution of deprecation of get_io_service().
57#if BOOST_VERSION >= 107000
58#define GET_IO_SERVICE(s) ((boost::asio::io_context&)(s).get_executor().context())
59#else
60#define GET_IO_SERVICE(s) ((s).get_io_service())
61#endif
62
63 auto resolver = std::make_shared<TcpResolver>(GET_IO_SERVICE(socket));
64#else
65 auto resolver = std::make_shared<TcpResolver>(socket.get_io_service());
66#endif
67
68
69 resolver->async_resolve(query,
70 [this, handler, resolver](const ErrorCode& ec_resolve, const EndpointIterator& iterator) {
71 if (!ec_resolve) {
72 socket.async_connect(iterator->endpoint(), handler);
73 }
74 else {
75 handler(ec_resolve);
76 }
77 });
78 }
79
80 bool is_open() const;
81
82 bool is_valid() const override;
83
84 void async_read(void* buffer, const std::size_t buffer_size, const CompletionHandler& handler) override;
85
86 void async_write(const void* buffer, const std::size_t buffer_size, const CompletionHandler& handler) override;
87
88 void close() override;
89
90 TcpSocket(const TcpSocket&) = delete;
91
92 TcpSocket& operator=(const TcpSocket&) = delete;
93
94 protected:
95
96 friend class TcpAcceptor;
97
98 std::shared_ptr<Context> context;
99
100 boost::asio::ip::tcp::socket socket;
101 };
102
103}
#define ASYNCHRONOUSIO_EXPORT
Definition System.h:10
Abstract base class for an asynchronous channel supporting read and write of binary data.
Definition Socket.h:24
std::function< void(const ErrorCode &, const std::size_t)> CompletionHandler
Definition Socket.h:27
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:39
std::shared_ptr< Context > context
Definition TcpSocket.h:98
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:47
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:100
~TcpSocket() override
TcpSocket & operator=(const TcpSocket &)=delete
TcpSocket(std::shared_ptr< Context > context)
bool is_valid() const override
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
boost::system::error_code ErrorCode
Definition ErrorCodes.h:13