From caef2b7b871c951e68b4fd858f407c566decbe7c Mon Sep 17 00:00:00 2001 From: badaix Date: Mon, 31 Jan 2022 21:07:34 +0100 Subject: [PATCH] Clean up asio includes --- client/client_connection.cpp | 58 ++++++++++++++++++++++++ client/client_connection.hpp | 56 ++++------------------- client/player/alsa_player.hpp | 2 + client/player/file_player.hpp | 1 + client/player/player.hpp | 2 +- server/control_server.hpp | 3 +- server/control_session.hpp | 1 - server/control_session_tcp.cpp | 4 ++ server/control_session_tcp.hpp | 5 +- server/control_session_ws.cpp | 4 +- server/control_session_ws.hpp | 1 + server/publishZeroConf/publish_avahi.hpp | 1 + server/publishZeroConf/publish_mdns.hpp | 2 +- server/server.hpp | 5 +- server/stream_server.hpp | 3 +- server/stream_session.hpp | 6 +-- server/stream_session_tcp.hpp | 1 + server/stream_session_ws.hpp | 7 ++- server/streamreader/alsa_stream.hpp | 6 ++- server/streamreader/asio_stream.hpp | 6 ++- server/streamreader/pcm_stream.hpp | 5 +- server/streamreader/stream_control.cpp | 3 ++ server/streamreader/stream_control.hpp | 3 +- server/streamreader/tcp_stream.hpp | 5 +- server/streamreader/watchdog.hpp | 4 +- 25 files changed, 120 insertions(+), 74 deletions(-) diff --git a/client/client_connection.cpp b/client/client_connection.cpp index 429d9857..20031844 100644 --- a/client/client_connection.cpp +++ b/client/client_connection.cpp @@ -25,6 +25,11 @@ #include "common/str_compat.hpp" #include "message/hello.hpp" +// 3rd party headers +#include +#include +#include + // standard headers #include #include @@ -34,6 +39,59 @@ using namespace std; static constexpr auto LOG_TAG = "Connection"; +PendingRequest::PendingRequest(const boost::asio::strand& strand, uint16_t reqId, const MessageHandler& handler) + : id_(reqId), timer_(strand), strand_(strand), handler_(handler) +{ +} + +PendingRequest::~PendingRequest() +{ + handler_ = nullptr; + timer_.cancel(); +} + +void PendingRequest::setValue(std::unique_ptr value) +{ + boost::asio::post(strand_, [this, self = shared_from_this(), val = std::move(value)]() mutable { + timer_.cancel(); + if (handler_) + handler_({}, std::move(val)); + }); +} + +uint16_t PendingRequest::id() const +{ + return id_; +} + +void PendingRequest::startTimer(const chronos::usec& timeout) +{ + timer_.expires_after(timeout); + timer_.async_wait([this, self = shared_from_this()](boost::system::error_code ec) { + if (!handler_) + return; + if (!ec) + { + // !ec => expired => timeout + handler_(boost::asio::error::timed_out, nullptr); + handler_ = nullptr; + } + else if (ec != boost::asio::error::operation_aborted) + { + // ec != aborted => not cancelled (in setValue) + // => should not happen, but who knows => pass the error to the handler + handler_(ec, nullptr); + } + }); +} + +bool PendingRequest::operator<(const PendingRequest& other) const +{ + return (id_ < other.id()); +} + + + ClientConnection::ClientConnection(boost::asio::io_context& io_context, const ClientSettings::Server& server) : io_context_(io_context), strand_(boost::asio::make_strand(io_context_.get_executor())), resolver_(strand_), socket_(strand_), reqId_(1), server_(server) { diff --git a/client/client_connection.hpp b/client/client_connection.hpp index 30c5f35f..bd107934 100644 --- a/client/client_connection.hpp +++ b/client/client_connection.hpp @@ -26,7 +26,10 @@ #include "message/message.hpp" // 3rd party headers -#include +#include +#include +#include +#include // standard headers #include @@ -51,61 +54,22 @@ using MessageHandler = std::function { public: - PendingRequest(const boost::asio::strand& strand, uint16_t reqId, const MessageHandler& handler) - : id_(reqId), timer_(strand), strand_(strand), handler_(handler){}; - - virtual ~PendingRequest() - { - handler_ = nullptr; - timer_.cancel(); - } + PendingRequest(const boost::asio::strand& strand, uint16_t reqId, const MessageHandler& handler); + virtual ~PendingRequest(); /// Set the response for the pending request and passes it to the handler /// @param value the response message - void setValue(std::unique_ptr value) - { - boost::asio::post(strand_, [this, self = shared_from_this(), val = std::move(value)]() mutable { - timer_.cancel(); - if (handler_) - handler_({}, std::move(val)); - }); - } + void setValue(std::unique_ptr value); /// @return the id of the request - uint16_t id() const - { - return id_; - } + uint16_t id() const; /// Start the timer for the request /// @param timeout the timeout to wait for the reception of the response - void startTimer(const chronos::usec& timeout) - { - timer_.expires_after(timeout); - timer_.async_wait([this, self = shared_from_this()](boost::system::error_code ec) { - if (!handler_) - return; - if (!ec) - { - // !ec => expired => timeout - handler_(boost::asio::error::timed_out, nullptr); - handler_ = nullptr; - } - else if (ec != boost::asio::error::operation_aborted) - { - // ec != aborted => not cancelled (in setValue) - // => should not happen, but who knows => pass the error to the handler - handler_(ec, nullptr); - } - }); - } + void startTimer(const chronos::usec& timeout); /// Needed to put the requests in a container - bool operator<(const PendingRequest& other) const - { - return (id_ < other.id()); - } - + bool operator<(const PendingRequest& other) const; private: uint16_t id_; diff --git a/client/player/alsa_player.hpp b/client/player/alsa_player.hpp index fefc2354..f043457e 100644 --- a/client/player/alsa_player.hpp +++ b/client/player/alsa_player.hpp @@ -24,6 +24,8 @@ // 3rd party headers #include +#include +#include // standard headers #include diff --git a/client/player/file_player.hpp b/client/player/file_player.hpp index e8599eef..a8e2ecb5 100644 --- a/client/player/file_player.hpp +++ b/client/player/file_player.hpp @@ -23,6 +23,7 @@ #include "player.hpp" // 3rd party headers +#include // standard headers #include diff --git a/client/player/player.hpp b/client/player/player.hpp index 7226d99f..e437b304 100644 --- a/client/player/player.hpp +++ b/client/player/player.hpp @@ -26,7 +26,7 @@ #include "stream.hpp" // 3rd party headers -#include +#include // standard headers #include diff --git a/server/control_server.hpp b/server/control_server.hpp index bfd2b211..79d1abee 100644 --- a/server/control_server.hpp +++ b/server/control_server.hpp @@ -29,7 +29,8 @@ #include "server_settings.hpp" // 3rd party headers -#include +#include +#include // standard headers #include diff --git a/server/control_session.hpp b/server/control_session.hpp index ae7ada68..ed2a0b49 100644 --- a/server/control_session.hpp +++ b/server/control_session.hpp @@ -25,7 +25,6 @@ #include "server_settings.hpp" // 3rd party headers -#include // standard headers #include diff --git a/server/control_session_tcp.cpp b/server/control_session_tcp.cpp index b395d973..1774375d 100644 --- a/server/control_session_tcp.cpp +++ b/server/control_session_tcp.cpp @@ -19,6 +19,10 @@ // prototype/interface header file #include "control_session_tcp.hpp" +// 3rd party headers +#include +#include + // local headers #include "common/aixlog.hpp" #include "message/pcm_chunk.hpp" diff --git a/server/control_session_tcp.hpp b/server/control_session_tcp.hpp index 07af3691..e9f98208 100644 --- a/server/control_session_tcp.hpp +++ b/server/control_session_tcp.hpp @@ -23,7 +23,10 @@ #include "control_session.hpp" // 3rd party headers -#include +#include +#include +#include +#include // standard headers #include diff --git a/server/control_session_ws.cpp b/server/control_session_ws.cpp index 905790f8..60dc04d0 100644 --- a/server/control_session_ws.cpp +++ b/server/control_session_ws.cpp @@ -16,8 +16,10 @@ along with this program. If not, see . ***/ -// local headers +// prototype/interface header file #include "control_session_ws.hpp" + +// local headers #include "common/aixlog.hpp" #include "message/pcm_chunk.hpp" diff --git a/server/control_session_ws.hpp b/server/control_session_ws.hpp index 86d155b8..87c496d5 100644 --- a/server/control_session_ws.hpp +++ b/server/control_session_ws.hpp @@ -23,6 +23,7 @@ #include "control_session.hpp" // 3rd party headers +#include #include #include diff --git a/server/publishZeroConf/publish_avahi.hpp b/server/publishZeroConf/publish_avahi.hpp index 4d8023fe..8a8e2dfd 100644 --- a/server/publishZeroConf/publish_avahi.hpp +++ b/server/publishZeroConf/publish_avahi.hpp @@ -30,6 +30,7 @@ #include #include #include +#include // standard headers #include diff --git a/server/publishZeroConf/publish_mdns.hpp b/server/publishZeroConf/publish_mdns.hpp index 54798cab..83fd542e 100644 --- a/server/publishZeroConf/publish_mdns.hpp +++ b/server/publishZeroConf/publish_mdns.hpp @@ -20,7 +20,7 @@ #define PUBLISH_MDNS_HPP // 3rd party headers -#include +#include // standard headers #include diff --git a/server/server.hpp b/server/server.hpp index 6f6b4006..e916c6c5 100644 --- a/server/server.hpp +++ b/server/server.hpp @@ -1,6 +1,6 @@ /*** This file is part of snapcast - Copyright (C) 2014-2021 Johannes Pohl + Copyright (C) 2014-2022 Johannes Pohl This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -33,7 +33,8 @@ #include "streamreader/stream_manager.hpp" // 3rd party headers -#include +#include +#include // standard headers #include diff --git a/server/stream_server.hpp b/server/stream_server.hpp index 95a06449..5b6eec94 100644 --- a/server/stream_server.hpp +++ b/server/stream_server.hpp @@ -32,7 +32,8 @@ #include "streamreader/stream_manager.hpp" // 3rd party headers -#include +#include +#include // standard headers #include diff --git a/server/stream_session.hpp b/server/stream_session.hpp index 85f7a385..9c71bb55 100644 --- a/server/stream_session.hpp +++ b/server/stream_session.hpp @@ -25,7 +25,9 @@ #include "streamreader/stream_manager.hpp" // 3rd party headers -#include +#include +#include +#include // standard headers #include @@ -39,8 +41,6 @@ #include -namespace net = boost::asio; - class StreamSession; diff --git a/server/stream_session_tcp.hpp b/server/stream_session_tcp.hpp index bc85a7a9..b53675de 100644 --- a/server/stream_session_tcp.hpp +++ b/server/stream_session_tcp.hpp @@ -23,6 +23,7 @@ #include "stream_session.hpp" // 3rd party headers +#include // standard headers diff --git a/server/stream_session_ws.hpp b/server/stream_session_ws.hpp index 76a16732..773f1be2 100644 --- a/server/stream_session_ws.hpp +++ b/server/stream_session_ws.hpp @@ -1,6 +1,6 @@ /*** This file is part of snapcast - Copyright (C) 2014-2021 Johannes Pohl + Copyright (C) 2014-2022 Johannes Pohl This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -24,10 +24,9 @@ #include #include -namespace beast = boost::beast; // from -namespace http = beast::http; // from +namespace beast = boost::beast; // from +// namespace http = beast::http; // from namespace websocket = beast::websocket; // from -namespace net = boost::asio; // from /// Endpoint for a connected control client. diff --git a/server/streamreader/alsa_stream.hpp b/server/streamreader/alsa_stream.hpp index e2608d7d..9d74d9ac 100644 --- a/server/streamreader/alsa_stream.hpp +++ b/server/streamreader/alsa_stream.hpp @@ -1,6 +1,6 @@ /*** This file is part of snapcast - Copyright (C) 2014-2021 Johannes Pohl + Copyright (C) 2014-2022 Johannes Pohl This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -24,7 +24,9 @@ // 3rd party headers #include -#include +#include +#include + namespace streamreader { diff --git a/server/streamreader/asio_stream.hpp b/server/streamreader/asio_stream.hpp index 0f424b2e..6dd8efa8 100644 --- a/server/streamreader/asio_stream.hpp +++ b/server/streamreader/asio_stream.hpp @@ -1,6 +1,6 @@ /*** This file is part of snapcast - Copyright (C) 2014-2021 Johannes Pohl + Copyright (C) 2014-2022 Johannes Pohl This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -25,7 +25,9 @@ #include "pcm_stream.hpp" // 3rd party headers -#include +#include +#include +#include // standard headers #include diff --git a/server/streamreader/pcm_stream.hpp b/server/streamreader/pcm_stream.hpp index 3c12df37..d33a27be 100644 --- a/server/streamreader/pcm_stream.hpp +++ b/server/streamreader/pcm_stream.hpp @@ -1,6 +1,6 @@ /*** This file is part of snapcast - Copyright (C) 2014-2021 Johannes Pohl + Copyright (C) 2014-2022 Johannes Pohl This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -43,7 +43,6 @@ #include -namespace net = boost::asio; using json = nlohmann::json; @@ -180,7 +179,7 @@ protected: /// Send request to stream control script void sendRequest(const std::string& method, const jsonrpcpp::Parameter& params, ResultHandler handler); - net::strand strand_; + boost::asio::strand strand_; std::chrono::time_point tvEncodedChunk_; std::vector pcmListeners_; StreamUri uri_; diff --git a/server/streamreader/stream_control.cpp b/server/streamreader/stream_control.cpp index 772c123d..8393bdde 100644 --- a/server/streamreader/stream_control.cpp +++ b/server/streamreader/stream_control.cpp @@ -27,6 +27,9 @@ #include "common/utils/string_utils.hpp" #include "encoder/encoder_factory.hpp" +// 3rd party headers +#include + // standard headers #include diff --git a/server/streamreader/stream_control.hpp b/server/streamreader/stream_control.hpp index 6e4e340f..4793d595 100644 --- a/server/streamreader/stream_control.hpp +++ b/server/streamreader/stream_control.hpp @@ -33,7 +33,7 @@ #pragma GCC diagnostic ignored "-Wc++11-narrowing" #include #pragma GCC diagnostic pop -#include +#include // standard headers #include @@ -41,7 +41,6 @@ namespace bp = boost::process; -namespace net = boost::asio; using json = nlohmann::json; diff --git a/server/streamreader/tcp_stream.hpp b/server/streamreader/tcp_stream.hpp index 3d26ae8a..cb2c9da8 100644 --- a/server/streamreader/tcp_stream.hpp +++ b/server/streamreader/tcp_stream.hpp @@ -1,6 +1,6 @@ /*** This file is part of snapcast - Copyright (C) 2014-2021 Johannes Pohl + Copyright (C) 2014-2022 Johannes Pohl This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,6 +22,9 @@ // local headers #include "asio_stream.hpp" +// 3rd party headers +#include + using boost::asio::ip::tcp; namespace streamreader diff --git a/server/streamreader/watchdog.hpp b/server/streamreader/watchdog.hpp index 6c990ff3..a6eefe2e 100644 --- a/server/streamreader/watchdog.hpp +++ b/server/streamreader/watchdog.hpp @@ -20,12 +20,12 @@ #define WATCH_DOG_HPP // 3rd party headers -#include +#include +#include // standard headers #include -namespace net = boost::asio; namespace streamreader {