-
Notifications
You must be signed in to change notification settings - Fork 3
/
socket.h
192 lines (160 loc) · 6.47 KB
/
socket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright (c) 2022 Novemus Band. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
#pragma once
#include <logger.h>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio/socket_base.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <boost/date_time/posix_time/posix_time_duration.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
namespace plexus { namespace network {
template<typename socket_impl, int64_t timeout_ms> struct asio_socket : public socket_impl
{
typedef typename socket_impl::lowest_layer_type::endpoint_type endpoint_type;
template<typename protocol_type, typename ...arguments>
asio_socket(const protocol_type& protocol, boost::asio::io_service& io, arguments&... args)
: socket_impl(io, args...), m_io(io)
{
socket_impl::lowest_layer().open(protocol);
static const size_t SOCKET_BUFFER_SIZE = 1048576;
socket_impl::lowest_layer().non_blocking(true);
socket_impl::lowest_layer().set_option(boost::asio::socket_base::reuse_address(true));
socket_impl::lowest_layer().set_option(boost::asio::socket_base::send_buffer_size(SOCKET_BUFFER_SIZE));
socket_impl::lowest_layer().set_option(boost::asio::socket_base::receive_buffer_size(SOCKET_BUFFER_SIZE));
}
template<typename ...arguments>
asio_socket(const endpoint_type& remote, boost::asio::io_service& io, arguments&... args)
: asio_socket(remote.protocol(), io, args...)
{
m_remote = remote;
}
void wait(boost::asio::socket_base::wait_type type, boost::asio::yield_context yield, int64_t timeout = timeout_ms) noexcept(false)
{
execute([&]() {
socket_impl::lowest_layer().async_wait(type, yield);
return 0;
}, timeout);
}
void connect(boost::asio::yield_context yield, int64_t timeout = timeout_ms) noexcept(false)
{
execute([&]() {
socket_impl::lowest_layer().async_connect(m_remote, yield);
return 0;
}, timeout);
}
void shutdown() noexcept(true)
{
if (socket_impl::lowest_layer().is_open())
{
boost::system::error_code ec;
socket_impl::lowest_layer().shutdown(boost::asio::socket_base::shutdown_both, ec);
socket_impl::lowest_layer().close(ec);
}
}
template <typename handshake_type>
void handshake(handshake_type type, boost::asio::yield_context yield, int64_t timeout = timeout_ms) noexcept(false)
{
execute([&]() {
socket_impl::async_handshake(type, yield);
return 0;
}, timeout);
}
template <typename const_buffer_type>
size_t write_some(const const_buffer_type& buffer, boost::asio::yield_context yield, int64_t timeout = timeout_ms) noexcept(false)
{
return execute([&]() {
return socket_impl::async_write_some(buffer, yield);
}, timeout);
}
template <typename mutable_buffer_type>
size_t read_some(const mutable_buffer_type& buffer, boost::asio::yield_context yield, int64_t timeout = timeout_ms) noexcept(false)
{
return execute([&]() {
return socket_impl::async_read_some(buffer, yield);
}, timeout);
}
template <typename const_buffer_type>
size_t write(const const_buffer_type& buffer, boost::asio::yield_context yield, int64_t timeout = timeout_ms) noexcept(false)
{
return execute([&]() {
return boost::asio::async_write(static_cast<socket_impl&>(*this), buffer, yield);
}, timeout);
}
template <typename mutable_buffer_type>
size_t read(const mutable_buffer_type& buffer, boost::asio::yield_context yield, int64_t timeout = timeout_ms) noexcept(false)
{
return execute([&]() {
return boost::asio::async_read(static_cast<socket_impl&>(*this), buffer, yield);
}, timeout);
}
template <typename const_buffer_type>
size_t send_to(const const_buffer_type& buffer, const endpoint_type& endpoint, boost::asio::yield_context yield, int64_t timeout = timeout_ms) noexcept(false)
{
return execute([&]() {
return socket_impl::async_send_to(buffer, endpoint, yield);
}, timeout);
}
template <typename mutable_buffer_type>
size_t receive_from(const mutable_buffer_type& buffer, const endpoint_type& endpoint, boost::asio::yield_context yield, int64_t timeout = timeout_ms) noexcept(false)
{
auto timer = [start = boost::posix_time::microsec_clock::universal_time()]()
{
return boost::posix_time::microsec_clock::universal_time() - start;
};
while (timer().total_milliseconds() < timeout)
{
endpoint_type source;
size_t size = execute([&]() {
return socket_impl::async_receive_from(buffer, source, yield);
}, timeout - timer().total_milliseconds());
if (is_matched(source, endpoint))
return size;
}
throw boost::system::error_code(boost::asio::error::operation_aborted);
}
private:
static bool is_matched(const endpoint_type& source, const endpoint_type& match)
{
return (match.address().is_unspecified() || match.address() == source.address()) && (match.port() == 0 || match.port() == source.port());
}
size_t execute(const std::function<size_t()>& function, int64_t timeout = timeout_ms) noexcept(false)
{
boost::asio::deadline_timer timer(m_io);
if (timeout > 0)
{
timer.expires_from_now(boost::posix_time::milliseconds(timeout));
timer.async_wait([&](const boost::system::error_code& error)
{
if(error)
{
if (error == boost::asio::error::operation_aborted)
return;
_err_ << error.message();
}
try
{
socket_impl::lowest_layer().cancel();
}
catch (const std::exception &ex)
{
_err_ << ex.what();
}
});
}
size_t res = function();
timer.cancel();
return res;
}
boost::asio::io_service& m_io;
endpoint_type m_remote;
};
}}