From e278c18fc22faa2e5099f3f43a813942144c7560 Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Wed, 13 Mar 2019 16:23:37 -0700 Subject: update ASIO interfaces This commit does 2 things. 1. Upgrades and prepares bmcweb for boost 1.70. 2. Allows us to compile with BOOST_AIO_NO_DEPRECATED Tested: Compiled against 1.69 and 1.70. All changes should be no-op. Change-Id: I557ecd840fe2b88c0fa01978a1b666b40ccccca4 Signed-off-by: Ed Tanous --- crow/include/crow/http_connection.h | 73 +++++++++++++++++++++++++------------ crow/include/crow/http_server.h | 69 ++++++++++++++++++++++++----------- crow/include/crow/websocket.h | 2 +- 3 files changed, 97 insertions(+), 47 deletions(-) (limited to 'crow') diff --git a/crow/include/crow/http_connection.h b/crow/include/crow/http_connection.h index c02f4ec1e3..ba2bcdd98c 100644 --- a/crow/include/crow/http_connection.h +++ b/crow/include/crow/http_connection.h @@ -8,7 +8,11 @@ #include #include #include +#if BOOST_VERSION >= 107000 +#include +#else #include +#endif #include #include #include @@ -323,30 +327,22 @@ class Connection } } - std::string epName; - boost::system::error_code ec; - tcp::endpoint ep = adaptor.lowest_layer().remote_endpoint(ec); - if (!ec) - { - epName = boost::lexical_cast(ep); - } - - BMCWEB_LOG_INFO << "Request: " << epName << " " << this << " HTTP/" - << req->version() / 10 << "." << req->version() % 10 - << ' ' << req->methodString() << " " << req->target(); + BMCWEB_LOG_INFO << "Request: " + << " " << this << " HTTP/" << req->version() / 10 << "." + << req->version() % 10 << ' ' << req->methodString() + << " " << req->target(); needToCallAfterHandlers = false; if (!isInvalidRequest) { res.completeRequestHandler = [] {}; - res.isAliveHelper = [this]() -> bool { - return adaptor.lowest_layer().is_open(); - }; + res.isAliveHelper = [this]() -> bool { return isAlive(); }; ctx = detail::Context(); - req->middlewareContext = (void*)&ctx; - req->ioService = &adaptor.get_executor().context(); + req->middlewareContext = static_cast(&ctx); + req->ioService = static_castioService)>( + &adaptor.get_executor().context()); detail::middlewareCallHelper< 0, decltype(ctx), decltype(*middlewares), Middlewares...>( *middlewares, *req, res, ctx); @@ -378,6 +374,35 @@ class Connection } } + bool isAlive() + { + + if constexpr (std::is_same_v>) + { + return adaptor.next_layer().is_open(); + } + else + { + return adaptor.is_open(); + } + } + void close() + { + + if constexpr (std::is_same_v>) + { + adaptor.next_layer().close(); + } + else + { + adaptor.close(); + } + } + void completeRequest() { BMCWEB_LOG_INFO << "Response: " << this << ' ' << req->url << ' ' @@ -397,7 +422,7 @@ class Connection // auto self = this->shared_from_this(); res.completeRequestHandler = res.completeRequestHandler = [] {}; - if (!adaptor.lowest_layer().is_open()) + if (!isAlive()) { // BMCWEB_LOG_DEBUG << this << " delete (socket is closed) " << // isReading @@ -456,7 +481,7 @@ class Connection { // if the adaptor isn't open anymore, and wasn't handed to a // websocket, treat as an error - if (!adaptor.lowest_layer().is_open() && !req->isUpgrade()) + if (!isAlive() && !req->isUpgrade()) { errorWhileReading = true; } @@ -465,7 +490,7 @@ class Connection if (errorWhileReading) { cancelDeadlineTimer(); - adaptor.lowest_layer().close(); + close(); BMCWEB_LOG_DEBUG << this << " from read(1)"; checkDestroy(); return; @@ -505,7 +530,7 @@ class Connection } else { - if (!adaptor.lowest_layer().is_open()) + if (!isAlive()) { errorWhileReading = true; } @@ -513,7 +538,7 @@ class Connection if (errorWhileReading) { cancelDeadlineTimer(); - adaptor.lowest_layer().close(); + close(); BMCWEB_LOG_DEBUG << this << " from read(1)"; checkDestroy(); return; @@ -545,7 +570,7 @@ class Connection } if (!res.keepAlive()) { - adaptor.lowest_layer().close(); + close(); BMCWEB_LOG_DEBUG << this << " from write(1)"; checkDestroy(); return; @@ -587,11 +612,11 @@ class Connection cancelDeadlineTimer(); timerCancelKey = timerQueue.add([this] { - if (!adaptor.lowest_layer().is_open()) + if (!isAlive()) { return; } - adaptor.lowest_layer().close(); + close(); }); BMCWEB_LOG_DEBUG << this << " timer added: " << &timerQueue << ' ' << timerCancelKey; diff --git a/crow/include/crow/http_server.h b/crow/include/crow/http_server.h index 75b49a463f..31b8ec2d04 100644 --- a/crow/include/crow/http_server.h +++ b/crow/include/crow/http_server.h @@ -2,10 +2,16 @@ #include #include +#include #include #include #include +#if BOOST_VERSION >= 107000 +#include +#else #include +#endif + #include #include #include @@ -47,9 +53,8 @@ class Server std::make_shared()) : Server(handler, std::make_unique( - *io, - tcp::endpoint( - boost::asio::ip::address::from_string(bindaddr), port)), + *io, tcp::endpoint(boost::asio::ip::make_address(bindaddr), + port)), middlewares, adaptor_ctx, io) { } @@ -168,30 +173,50 @@ class Server boost::asio::ip::tcp::socket>>::value) { adaptorTemp = Adaptor(*ioService, *adaptorCtx); + Connection* p = + new Connection( + *ioService, handler, serverName, middlewares, + getCachedDateStr, timerQueue, + std::move(adaptorTemp.value())); + + acceptor->async_accept(p->socket().next_layer(), + [this, p](boost::system::error_code ec) { + if (!ec) + { + boost::asio::post( + *this->ioService, + [p] { p->start(); }); + } + else + { + delete p; + } + doAccept(); + }); } else { adaptorTemp = Adaptor(*ioService); + Connection* p = + new Connection( + *ioService, handler, serverName, middlewares, + getCachedDateStr, timerQueue, + std::move(adaptorTemp.value())); + + acceptor->async_accept( + p->socket(), [this, p](boost::system::error_code ec) { + if (!ec) + { + boost::asio::post(*this->ioService, + [p] { p->start(); }); + } + else + { + delete p; + } + doAccept(); + }); } - - Connection* p = - new Connection( - *ioService, handler, serverName, middlewares, getCachedDateStr, - timerQueue, std::move(adaptorTemp.value())); - - acceptor->async_accept(p->socket().lowest_layer(), - [this, p](boost::system::error_code ec) { - if (!ec) - { - this->ioService->post( - [p] { p->start(); }); - } - else - { - delete p; - } - doAccept(); - }); } private: diff --git a/crow/include/crow/websocket.h b/crow/include/crow/websocket.h index 6901eb108f..778a29707b 100644 --- a/crow/include/crow/websocket.h +++ b/crow/include/crow/websocket.h @@ -66,7 +66,7 @@ template class ConnectionImpl : public Connection boost::asio::io_context& get_io_context() override { - return ws.get_executor().context(); + return (boost::asio::io_context&)ws.get_executor().context(); } void start() -- cgit v1.2.3