Skip to content

Commit 27c9ba3

Browse files
authored
Merge pull request #971 from bugdea1er/io-context
Remove usage of deprecated Asio API
2 parents ccb1fa8 + a8a8b62 commit 27c9ba3

File tree

7 files changed

+105
-105
lines changed

7 files changed

+105
-105
lines changed

include/crow/http_connection.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ namespace crow
5050

5151
public:
5252
Connection(
53-
asio::io_service& io_service,
53+
asio::io_context& io_context,
5454
Handler* handler,
5555
const std::string& server_name,
5656
std::tuple<Middlewares...>* middlewares,
5757
std::function<std::string()>& get_cached_date_str_f,
5858
detail::task_timer& task_timer,
5959
typename Adaptor::context* adaptor_ctx_,
6060
std::atomic<unsigned int>& queue_length):
61-
adaptor_(io_service, adaptor_ctx_),
61+
adaptor_(io_context, adaptor_ctx_),
6262
handler_(handler),
6363
parser_(this),
6464
req_(parser_.req),
@@ -143,7 +143,7 @@ namespace crow
143143
ctx_ = detail::context<Middlewares...>();
144144
req_.middleware_context = static_cast<void*>(&ctx_);
145145
req_.middleware_container = static_cast<void*>(middlewares_);
146-
req_.io_service = &adaptor_.get_io_service();
146+
req_.io_context = &adaptor_.get_io_context();
147147

148148
req_.remote_ip_address = adaptor_.remote_endpoint().address().to_string();
149149

include/crow/http_request.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
4848

4949
void* middleware_context{};
5050
void* middleware_container{};
51-
asio::io_service* io_service{};
51+
asio::io_context* io_context{};
5252

5353
/// Construct an empty request. (sets the method to `GET`)
5454
request():
@@ -88,14 +88,14 @@ namespace crow // NOTE: Already documented in "crow/app.h"
8888
template<typename CompletionHandler>
8989
void post(CompletionHandler handler)
9090
{
91-
io_service->post(handler);
91+
asio::post(io_context, handler);
9292
}
9393

9494
/// Send data to whoever made this request with a completion handler.
9595
template<typename CompletionHandler>
9696
void dispatch(CompletionHandler handler)
9797
{
98-
io_service->dispatch(handler);
98+
asio::dispatch(io_context, handler);
9999
}
100100
};
101101
} // namespace crow

include/crow/http_server.h

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ namespace crow // NOTE: Already documented in "crow/app.h"
4343
{
4444
public:
4545
Server(Handler* handler, std::string bindaddr, uint16_t port, std::string server_name = std::string("Crow/") + VERSION, std::tuple<Middlewares...>* middlewares = nullptr, uint16_t concurrency = 1, uint8_t timeout = 5, typename Adaptor::context* adaptor_ctx = nullptr):
46-
acceptor_(io_service_, tcp::endpoint(asio::ip::address::from_string(bindaddr), port)),
47-
signals_(io_service_),
48-
tick_timer_(io_service_),
46+
acceptor_(io_context_, tcp::endpoint(asio::ip::make_address(bindaddr), port)),
47+
signals_(io_context_),
48+
tick_timer_(io_context_),
4949
handler_(handler),
5050
concurrency_(concurrency),
5151
timeout_(timeout),
@@ -78,7 +78,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
7878
{
7979
uint16_t worker_thread_count = concurrency_ - 1;
8080
for (int i = 0; i < worker_thread_count; i++)
81-
io_service_pool_.emplace_back(new asio::io_service());
81+
io_context_pool_.emplace_back(new asio::io_context());
8282
get_cached_date_str_pool_.resize(worker_thread_count);
8383
task_timer_pool_.resize(worker_thread_count);
8484

@@ -116,7 +116,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
116116
};
117117

118118
// initializing task timers
119-
detail::task_timer task_timer(*io_service_pool_[i]);
119+
detail::task_timer task_timer(*io_context_pool_[i]);
120120
task_timer.set_default_timeout(timeout_);
121121
task_timer_pool_[i] = &task_timer;
122122
task_queue_length_pool_[i] = 0;
@@ -126,7 +126,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
126126
{
127127
try
128128
{
129-
if (io_service_pool_[i]->run() == 0)
129+
if (io_context_pool_[i]->run() == 0)
130130
{
131131
// when io_service.run returns 0, there are no more works to do.
132132
break;
@@ -170,7 +170,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
170170
std::thread(
171171
[this] {
172172
notify_start();
173-
io_service_.run();
173+
io_context_.run();
174174
CROW_LOG_INFO << "Exiting.";
175175
})
176176
.join();
@@ -179,17 +179,17 @@ namespace crow // NOTE: Already documented in "crow/app.h"
179179
void stop()
180180
{
181181
shutting_down_ = true; // Prevent the acceptor from taking new connections
182-
for (auto& io_service : io_service_pool_)
182+
for (auto& io_context : io_context_pool_)
183183
{
184-
if (io_service != nullptr)
184+
if (io_context != nullptr)
185185
{
186-
CROW_LOG_INFO << "Closing IO service " << &io_service;
187-
io_service->stop(); // Close all io_services (and HTTP connections)
186+
CROW_LOG_INFO << "Closing IO service " << &io_context;
187+
io_context->stop(); // Close all io_services (and HTTP connections)
188188
}
189189
}
190190

191-
CROW_LOG_INFO << "Closing main IO service (" << &io_service_ << ')';
192-
io_service_.stop(); // Close main io_service
191+
CROW_LOG_INFO << "Closing main IO service (" << &io_context_ << ')';
192+
io_context_.stop(); // Close main io_service
193193
}
194194

195195
uint16_t port(){
@@ -215,7 +215,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
215215
}
216216

217217
private:
218-
uint16_t pick_io_service_idx()
218+
uint16_t pick_io_context_idx()
219219
{
220220
uint16_t min_queue_idx = 0;
221221

@@ -235,29 +235,29 @@ namespace crow // NOTE: Already documented in "crow/app.h"
235235
{
236236
if (!shutting_down_)
237237
{
238-
uint16_t service_idx = pick_io_service_idx();
239-
asio::io_service& is = *io_service_pool_[service_idx];
240-
task_queue_length_pool_[service_idx]++;
241-
CROW_LOG_DEBUG << &is << " {" << service_idx << "} queue length: " << task_queue_length_pool_[service_idx];
238+
uint16_t context_idx = pick_io_context_idx();
239+
asio::io_context& ic = *io_context_pool_[context_idx];
240+
task_queue_length_pool_[context_idx]++;
241+
CROW_LOG_DEBUG << &ic << " {" << context_idx << "} queue length: " << task_queue_length_pool_[context_idx];
242242

243243
auto p = std::make_shared<Connection<Adaptor, Handler, Middlewares...>>(
244-
is, handler_, server_name_, middlewares_,
245-
get_cached_date_str_pool_[service_idx], *task_timer_pool_[service_idx], adaptor_ctx_, task_queue_length_pool_[service_idx]);
244+
ic, handler_, server_name_, middlewares_,
245+
get_cached_date_str_pool_[context_idx], *task_timer_pool_[context_idx], adaptor_ctx_, task_queue_length_pool_[context_idx]);
246246

247247
acceptor_.async_accept(
248248
p->socket(),
249-
[this, p, &is, service_idx](error_code ec) {
249+
[this, p, &ic, context_idx](error_code ec) {
250250
if (!ec)
251251
{
252-
is.post(
252+
asio::post(ic,
253253
[p] {
254254
p->start();
255255
});
256256
}
257257
else
258258
{
259-
task_queue_length_pool_[service_idx]--;
260-
CROW_LOG_DEBUG << &is << " {" << service_idx << "} queue length: " << task_queue_length_pool_[service_idx];
259+
task_queue_length_pool_[context_idx]--;
260+
CROW_LOG_DEBUG << &ic << " {" << context_idx << "} queue length: " << task_queue_length_pool_[context_idx];
261261
}
262262
do_accept();
263263
});
@@ -273,8 +273,8 @@ namespace crow // NOTE: Already documented in "crow/app.h"
273273
}
274274

275275
private:
276-
std::vector<std::unique_ptr<asio::io_service>> io_service_pool_;
277-
asio::io_service io_service_;
276+
std::vector<std::unique_ptr<asio::io_context>> io_context_pool_;
277+
asio::io_context io_context_;
278278
std::vector<detail::task_timer*> task_timer_pool_;
279279
std::vector<std::function<std::string()>> get_cached_date_str_pool_;
280280
tcp::acceptor acceptor_;

include/crow/socket_adaptors.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#include "crow/settings.h"
2020

2121
#if (CROW_USE_BOOST && BOOST_VERSION >= 107000) || (ASIO_VERSION >= 101300)
22-
#define GET_IO_SERVICE(s) ((asio::io_context&)(s).get_executor().context())
22+
#define GET_IO_CONTEXT(s) ((asio::io_context&)(s).get_executor().context())
2323
#else
24-
#define GET_IO_SERVICE(s) ((s).get_io_service())
24+
#define GET_IO_CONTEXT(s) ((s).get_io_service())
2525
#endif
2626

2727
namespace crow
@@ -38,13 +38,13 @@ namespace crow
3838
struct SocketAdaptor
3939
{
4040
using context = void;
41-
SocketAdaptor(asio::io_service& io_service, context*):
42-
socket_(io_service)
41+
SocketAdaptor(asio::io_context& io_context, context*):
42+
socket_(io_context)
4343
{}
4444

45-
asio::io_service& get_io_service()
45+
asio::io_context& get_io_context()
4646
{
47-
return GET_IO_SERVICE(socket_);
47+
return GET_IO_CONTEXT(socket_);
4848
}
4949

5050
/// Get the TCP socket handling data trasfers, regardless of what layer is handling transfers on top of the socket.
@@ -107,8 +107,8 @@ namespace crow
107107
{
108108
using context = asio::ssl::context;
109109
using ssl_socket_t = asio::ssl::stream<tcp::socket>;
110-
SSLAdaptor(asio::io_service& io_service, context* ctx):
111-
ssl_socket_(new ssl_socket_t(io_service, *ctx))
110+
SSLAdaptor(asio::io_context& io_context, context* ctx):
111+
ssl_socket_(new ssl_socket_t(io_context, *ctx))
112112
{}
113113

114114
asio::ssl::stream<tcp::socket>& socket()
@@ -168,9 +168,9 @@ namespace crow
168168
}
169169
}
170170

171-
asio::io_service& get_io_service()
171+
asio::io_context& get_io_context()
172172
{
173-
return GET_IO_SERVICE(raw_socket());
173+
return GET_IO_CONTEXT(raw_socket());
174174
}
175175

176176
template<typename F>

include/crow/task_timer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ namespace crow
4242
using clock_type = std::chrono::steady_clock;
4343
using time_type = clock_type::time_point;
4444
public:
45-
task_timer(asio::io_service& io_service,
45+
task_timer(asio::io_context& io_context,
4646
const std::chrono::milliseconds tick_length =
4747
std::chrono::seconds(1)) :
48-
io_service_(io_service), timer_(io_service_),
48+
io_context_(io_context), timer_(io_context_),
4949
tick_length_ms_(tick_length)
5050
{
5151
timer_.expires_after(tick_length_ms_);
@@ -155,7 +155,7 @@ namespace crow
155155
}
156156

157157
private:
158-
asio::io_service& io_service_;
158+
asio::io_context& io_context_;
159159
asio::basic_waitable_timer<clock_type> timer_;
160160
std::map<identifier_type, std::pair<time_type, task_type>> tasks_;
161161

include/crow/websocket.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
202202
template<typename CompletionHandler>
203203
void dispatch(CompletionHandler&& handler)
204204
{
205-
asio::dispatch(adaptor_.get_io_service(),
205+
asio::dispatch(adaptor_.get_io_context(),
206206
WeakWrappedMessage<typename std::decay<CompletionHandler>::type>{
207207
std::forward<CompletionHandler>(handler), anchor_});
208208
}
@@ -211,7 +211,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
211211
template<typename CompletionHandler>
212212
void post(CompletionHandler&& handler)
213213
{
214-
asio::post(adaptor_.get_io_service(),
214+
asio::post(adaptor_.get_io_context(),
215215
WeakWrappedMessage<typename std::decay<CompletionHandler>::type>{
216216
std::forward<CompletionHandler>(handler), anchor_});
217217
}

0 commit comments

Comments
 (0)