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 <boost/asio.hpp>
14#include <boost/asio/ip/basic_resolver.hpp>
15#include <boost/asio/ip/tcp.hpp>
16#include <boost/asio/query.hpp>
17#include <iostream>
18#include <FMEThirdPartyWarningsRestore.h>
19
20
21namespace asio {
22
40 // Note: inline this class completely or export base class and do NOT export a class whose base classes is not.
41 // Whether it is a realy problem here is still unclear, but this should be handled. At least in cases of same problem with
42 // derivations of std::runtime_error this would cause subtle problems.
44 {
45 public:
46
47 TcpSocket(std::shared_ptr<Context> context);
48
49 ~TcpSocket() override;
50
51 template<typename ConnectCompletionHandler>
52 void async_connect(const std::string& host, const unsigned short port, ConnectCompletionHandler&& handler)
53 {
54 using TcpResolver = boost::asio::ip::tcp::resolver;
55 typedef boost::asio::ip::tcp::resolver::results_type ResolverResultsType;
56
57 auto resolver = std::make_shared<TcpResolver>( socket.get_executor() );
58 auto hostStr = (host == "localhost" ? "127.0.0.1" : host);
59 resolver->async_resolve(hostStr, std::to_string(port),
60 [this, handler, resolver](const ErrorCode& ec_resolve, const ResolverResultsType& iterator ) {
61 if (!ec_resolve) {
62 socket.async_connect(*iterator.begin(), handler);
63 }
64 else {
65 handler(ec_resolve);
66 }
67 });
68 }
69
70 bool is_open() const;
71
72 bool is_valid() const override;
73
74 void async_read(void* buffer, const std::size_t buffer_size, const CompletionHandler& handler) override;
75
76 void async_write(const void* buffer, const std::size_t buffer_size, const CompletionHandler& handler) override;
77
78 void close() override;
79
80 TcpSocket(const TcpSocket&) = delete;
81
82 TcpSocket& operator=(const TcpSocket&) = delete;
83
84 protected:
85
86 friend class TcpAcceptor;
87
88 std::shared_ptr<Context> context;
89
90 boost::asio::ip::tcp::socket socket;
91 };
92
93}
#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:44
std::shared_ptr< Context > context
Definition TcpSocket.h:88
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:52
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:90
~TcpSocket() override
TcpSocket & operator=(const TcpSocket &)=delete
TcpSocket(std::shared_ptr< Context > context)
bool is_valid() const override
boost::system::error_code ErrorCode
Definition ErrorCodes.h:13