/
asio_include.hpp
228 lines (183 loc) · 5.57 KB
/
asio_include.hpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
restinio
*/
/*!
Selective include of asio/boost::asio.
*/
#pragma once
#if !defined(RESTINIO_USE_BOOST_ASIO)
// RESTinio uses stand-alone version of asio.
#include <asio.hpp>
// Define added to not have to distinguish between boost and non-boost asio in
// other code.
#define RESTINIO_ASIO_VERSION ASIO_VERSION
namespace restinio
{
namespace asio_ns = ::asio;
//! @name Adoptation functions to cover differences between snad-alone and beast asio.
///@{
inline bool
error_is_operation_aborted( const asio_ns::error_code & ec ) noexcept
{
return ec == asio_ns::error::operation_aborted;
}
inline bool
error_is_eof( const asio_ns::error_code & ec ) noexcept
{
return ec == asio_ns::error::eof;
}
///@}
namespace asio_ec
{
constexpr auto eof = asio_ns::error::eof;
inline const auto & system_category() { return asio_ns::system_category(); }
} /* namespace err */
//! \}
//! An alias for base class of error category entity.
using error_category_base_t = asio_ns::error_category;
// Define a proxy macro for having the same name for asio stand-alone and boost.
#define RESTINIO_ERROR_CATEGORY_NAME_NOEXCEPT ASIO_ERROR_CATEGORY_NOEXCEPT
} /* namespace restinio */
#if defined(ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
// Define feature macro with the same name for stand-alone and boost asio.
#define RESTINIO_ASIO_HAS_WINDOWS_OVERLAPPED_PTR
#endif
#else
// RESTinio uses boost::asio.
#include <boost/asio.hpp>
// Define added to not have to distinguish between boost and non-boost asio in
// other code.
#define RESTINIO_ASIO_VERSION BOOST_ASIO_VERSION
namespace restinio
{
namespace asio_ns
{
using namespace ::boost::asio;
using error_code = ::boost::system::error_code;
} /* namespace asio_ns */
//! @name Adoptation functions to cover differences between snad-alone and beast asio.
///@{
inline bool error_is_operation_aborted( const asio_ns::error_code & ec )
{
return ec == asio_ns::error::basic_errors::operation_aborted;
}
inline bool error_is_eof( const asio_ns::error_code & ec )
{
return ec == asio_ns::error::misc_errors::eof;
}
///@}
namespace asio_ec
{
constexpr auto eof = asio_ns::error::misc_errors::eof;
inline const auto & system_category() { return ::boost::system::system_category(); }
} /* namespace err */
//! An alias for base class of error category entity.
using error_category_base_t = ::boost::system::error_category;
// Define a proxy macro for having the same name for asio stand-alone and boost.
#define RESTINIO_ERROR_CATEGORY_NAME_NOEXCEPT BOOST_SYSTEM_NOEXCEPT
} /* namespace restinio */
#if defined(BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR)
// Define feature macro with the same name for stand-alone and boost asio.
#define RESTINIO_ASIO_HAS_WINDOWS_OVERLAPPED_PTR
#endif
#endif
namespace restinio
{
//! Enum for restinio errors that must presented as asio_ns::error_code value.
/*
@since v.0.4.8
*/
enum class asio_convertible_error_t : int
{
// Notificators error.
//! After write notificator error: data was not sent,
//! connection closed (or aborted) before a given piece of data.
write_was_not_executed = 100,
//! After write notificator error: a notificator was set for a write_group_t
//! but no external invokation happened, so write_group_t destructor
//! calls it with error.
write_group_destroyed_passively,
//! A call to async_write failed.
//! The corresponding write operation wasn't done.
/*!
* @since v.0.6.0
*/
async_write_call_failed,
//! A call to async_read_some_at failed.
//! The corresponding sendfile operation wasn't done.
/*!
* @since v.0.6.0
*/
async_read_some_at_call_failed
};
namespace impl
{
//! Error category for asio compatible error codes.
/*
@since v.0.4.8
*/
class restinio_err_category_t : public error_category_base_t
{
public:
virtual const char*
name() const RESTINIO_ERROR_CATEGORY_NAME_NOEXCEPT override
{
return "restinio";
}
virtual std::string
message( int value ) const override
{
std::string result{};
switch( static_cast< asio_convertible_error_t >( value ) )
{
case asio_convertible_error_t::write_was_not_executed:
result.assign( "write operation was not" );
break;
case asio_convertible_error_t::write_group_destroyed_passively:
result.assign(
"write group destroyed without external notificato invokation" );
break;
case asio_convertible_error_t::async_write_call_failed:
result.assign(
"a call to async_write() failed" );
break;
case asio_convertible_error_t::async_read_some_at_call_failed:
result.assign(
"a call to async_read_some_at_call_failed() failed" );
break;
}
return result;
}
};
} /* namespace impl */
//! Get restinio error category.
/*
@since v.0.4.8
*/
inline const error_category_base_t &
restinio_err_category()
{
static impl::restinio_err_category_t instance;
return instance;
}
//! Make restinio error_code compatible with asio_ns::error_code.
/*
@since v.0.4.8
*/
inline asio_ns::error_code
make_asio_compaible_error( asio_convertible_error_t err ) noexcept
{
return asio_ns::error_code{ static_cast< int >( err ), restinio_err_category() };
}
// Since Asio 1.17 the usage of asio::executor requires
// a special define ASIO_USE_TS_EXECUTOR_AS_DEFAULT (otherwise the
// code won't compile).
// A new name any_io_executor is introduced in Asio 1.17.
// We'll use that name for Asio 1.17 or newer.
// Old name asio::executor will be used for older versions of Asio.
#if RESTINIO_ASIO_VERSION >= 101700
using default_asio_executor = asio_ns::any_io_executor;
#else
using default_asio_executor = asio_ns::executor;
#endif
} /* namespace restinio */