From 4c43c2605e681a17925c02990100549d0f5f146f Mon Sep 17 00:00:00 2001 From: Tim Groeneboom Date: Mon, 12 Sep 2022 16:12:25 +0200 Subject: [PATCH] - removed all asio includes from header files --- modules/napudp/src/udpadapter.cpp | 2 +- modules/napudp/src/udpadapter.h | 14 ++++++---- modules/napudp/src/udpclient.cpp | 44 +++++++++++++++++++++++++----- modules/napudp/src/udpclient.h | 27 +++++++++++-------- modules/napudp/src/udpserver.cpp | 45 +++++++++++++++++++++++++------ modules/napudp/src/udpserver.h | 24 ++++++++++------- modules/napudp/src/udpthread.cpp | 8 ++++++ modules/napudp/src/udpthread.h | 6 ----- 8 files changed, 122 insertions(+), 48 deletions(-) diff --git a/modules/napudp/src/udpadapter.cpp b/modules/napudp/src/udpadapter.cpp index 9ff541f32a..962e78cad2 100644 --- a/modules/napudp/src/udpadapter.cpp +++ b/modules/napudp/src/udpadapter.cpp @@ -34,7 +34,7 @@ namespace nap } - bool UDPAdapter::handleAsioError(const asio::error_code& errorCode, utility::ErrorState& errorState, bool& success) + bool UDPAdapter::handleAsioError(const std::error_code& errorCode, utility::ErrorState& errorState, bool& success) { if(errorCode) { diff --git a/modules/napudp/src/udpadapter.h b/modules/napudp/src/udpadapter.h index ce16add944..4cfa94654f 100644 --- a/modules/napudp/src/udpadapter.h +++ b/modules/napudp/src/udpadapter.h @@ -5,12 +5,17 @@ #pragma once // Nap includes +#include #include -#include + +// STD includes +#include namespace nap { ////////////////////////////////////////////////////////////////////////// + // forward declares + class UDPThread; /** * Base class of specific UDP client and server resources. @@ -23,8 +28,6 @@ namespace nap RTTI_ENABLE(Resource) public: - ResourcePtr mThread = nullptr; ///< Property: 'Thread' the udp thread the adapter registers itself to - /** * Initialization * @param error contains error information @@ -38,13 +41,14 @@ namespace nap virtual void onDestroy() override; public: // Properties - bool mAllowFailure = false; ///< Property: 'AllowFailure' if binding to socket is allowed to fail on initialization + ResourcePtr mThread; ///< Property: 'Thread' the udp thread the adapter registers itself to + bool mAllowFailure = false; ///< Property: 'AllowFailure' if binding to socket is allowed to fail on initialization protected: /** * called by a UDPThread */ virtual void process() = 0; - bool handleAsioError(const asio::error_code& errorCode, utility::ErrorState& errorState, bool& success); + bool handleAsioError(const std::error_code& errorCode, utility::ErrorState& errorState, bool& success); }; } diff --git a/modules/napudp/src/udpclient.cpp b/modules/napudp/src/udpclient.cpp index dfe7117c9b..9b3ce09602 100644 --- a/modules/napudp/src/udpclient.cpp +++ b/modules/napudp/src/udpclient.cpp @@ -15,39 +15,69 @@ #include -using asio::ip::address; -using asio::ip::udp; - RTTI_BEGIN_CLASS(nap::UDPClient) RTTI_PROPERTY("Endpoint", &nap::UDPClient::mRemoteIp, nap::rtti::EPropertyMetaData::Default) + RTTI_PROPERTY("Broadcast", &nap::UDPClient::mBroadcast, nap::rtti::EPropertyMetaData::Default) RTTI_PROPERTY("Port", &nap::UDPClient::mPort, nap::rtti::EPropertyMetaData::Default) RTTI_PROPERTY("MaxQueueSize", &nap::UDPClient::mMaxPacketQueueSize, nap::rtti::EPropertyMetaData::Default) RTTI_PROPERTY("StopOnMaxQueueSizeExceeded", &nap::UDPClient::mStopOnMaxQueueSizeExceeded, nap::rtti::EPropertyMetaData::Default) RTTI_END_CLASS +using asio::ip::address; +using asio::ip::udp; + namespace nap { + ////////////////////////////////////////////////////////////////////////// + // UDPClientASIO + ////////////////////////////////////////////////////////////////////////// + + class UDPClientASIO + { + public: + // ASIO + asio::io_context mIOService; + asio::ip::udp::endpoint mRemoteEndpoint; + asio::ip::udp::socket mSocket{ mIOService }; + }; + ////////////////////////////////////////////////////////////////////////// // UDPClient ////////////////////////////////////////////////////////////////////////// + UDPClient::UDPClient() : UDPAdapter() + {} + + + UDPClient::~UDPClient() + {} + + bool UDPClient::init(utility::ErrorState& errorState) { + // create asio implementation + mASIO = std::make_unique(); + // when asio error occurs, init_success indicates whether initialization should fail or succeed bool init_success = false; // try to open socket asio::error_code asio_error_code; - mSocket.open(udp::v4(), asio_error_code); + mASIO->mSocket.open(udp::v4(), asio_error_code); if(handleAsioError(asio_error_code, errorState, init_success)) return init_success; + // enable/disable broadcast + mASIO->mSocket.set_option(asio::socket_base::broadcast(mBroadcast), asio_error_code); + if(handleAsioError(asio_error_code, errorState, init_success)) + return init_success; + // create address from string auto address = address::from_string(mRemoteIp, asio_error_code); if(handleAsioError(asio_error_code, errorState, init_success)) return init_success; - mRemoteEndpoint = udp::endpoint(address, mPort); + mASIO->mRemoteEndpoint = udp::endpoint(address, mPort); // init UDPAdapter, registering the client to an UDPThread if (!UDPAdapter::init(errorState)) @@ -62,7 +92,7 @@ namespace nap UDPAdapter::onDestroy(); asio::error_code err; - mSocket.close(err); + mASIO->mSocket.close(err); if (err) { nap::Logger::error(*this, "error closing socket : %s", err.message().c_str()); @@ -117,7 +147,7 @@ namespace nap while(mQueue.try_dequeue(packet_to_send)) { asio::error_code err; - mSocket.send_to(asio::buffer(&packet_to_send.data()[0], packet_to_send.size()), mRemoteEndpoint, 0, err); + mASIO->mSocket.send_to(asio::buffer(&packet_to_send.data()[0], packet_to_send.size()), mASIO->mRemoteEndpoint, 0, err); if(err) { diff --git a/modules/napudp/src/udpclient.h b/modules/napudp/src/udpclient.h index 89c2395cc9..d1269d322b 100644 --- a/modules/napudp/src/udpclient.h +++ b/modules/napudp/src/udpclient.h @@ -9,12 +9,6 @@ #include #include -// ASIO includes -#include -#include -#include -#include - // NAP includes #include #include @@ -27,6 +21,9 @@ namespace nap { ////////////////////////////////////////////////////////////////////////// + // forward declares + class UDPClientASIO; + /** * The UDP Client class is used to send UDP Packets to an endpoint. */ @@ -34,6 +31,16 @@ namespace nap { RTTI_ENABLE(UDPAdapter) public: + /** + * Constructor + */ + UDPClient(); + + /** + * Destructor + */ + virtual ~UDPClient(); + /** * Initializes the UDP client * @param error contains error information @@ -67,17 +74,15 @@ namespace nap std::string mRemoteIp = "10.8.0.3"; ///< Property: 'Endpoint' the ip address the client socket binds to int mMaxPacketQueueSize = 1000; ///< Property: 'MaxQueueSize' maximum of queued packets bool mStopOnMaxQueueSizeExceeded = true; ///< Property: 'StopOnMaxQueueSizeExceeded' stop adding packets when queue size is exceed + bool mBroadcast = false; ///< Property: 'Broadcast' set option to broadcast protected: /** * The process function */ void process() override; private: - // ASIO - asio::io_service mIOService; - asio::ip::udp::socket mSocket{mIOService}; - std::vector mBuffer; - asio::ip::udp::endpoint mRemoteEndpoint; + std::unique_ptr mASIO; + std::vector mBuffer; // Threading moodycamel::ConcurrentQueue mQueue; diff --git a/modules/napudp/src/udpserver.cpp b/modules/napudp/src/udpserver.cpp index d58fea9c87..d1db33276d 100644 --- a/modules/napudp/src/udpserver.cpp +++ b/modules/napudp/src/udpserver.cpp @@ -6,6 +6,12 @@ #include "udppacket.h" #include "udpthread.h" +// ASIO Includes +#include +#include +#include +#include + // External includes #include #include @@ -15,28 +21,51 @@ #include -using asio::ip::address; -using asio::ip::udp; - RTTI_BEGIN_CLASS(nap::UDPServer) RTTI_PROPERTY("Port", &nap::UDPServer::mPort, nap::rtti::EPropertyMetaData::Default) RTTI_PROPERTY("IP Address", &nap::UDPServer::mIPAddress, nap::rtti::EPropertyMetaData::Default) RTTI_END_CLASS +using namespace asio::ip; + namespace nap { + ////////////////////////////////////////////////////////////////////////// + // UDPServerASIO + ////////////////////////////////////////////////////////////////////////// + + class UDPServerASIO + { + public: + // ASIO + asio::io_context mIOService; + asio::ip::udp::endpoint mRemoteEndpoint; + asio::ip::udp::socket mSocket{ mIOService }; + }; + ////////////////////////////////////////////////////////////////////////// // UDPServer ////////////////////////////////////////////////////////////////////////// + UDPServer::UDPServer() : UDPAdapter() + {} + + + UDPServer::~UDPServer() + {} + + bool UDPServer::init(utility::ErrorState& errorState) { + // create asio implementation + mASIO = std::make_unique(); + // when asio error occurs, init_success indicates whether initialization should fail or succeed bool init_success = false; // try to open socket asio::error_code asio_error_code; - mSocket.open(udp::v4(), asio_error_code); + mASIO->mSocket.open(udp::v4(), asio_error_code); if(handleAsioError(asio_error_code, errorState, init_success)) return init_success; @@ -56,7 +85,7 @@ namespace nap // try to bind socket nap::Logger::info(*this, "Listening at port %i", mPort); - mSocket.bind(udp::endpoint(address, mPort), asio_error_code); + mASIO->mSocket.bind(udp::endpoint(address, mPort), asio_error_code); if(handleAsioError(asio_error_code, errorState, init_success)) return init_success; @@ -73,7 +102,7 @@ namespace nap UDPAdapter::onDestroy(); asio::error_code asio_error_code; - mSocket.close(asio_error_code); + mASIO->mSocket.close(asio_error_code); if(asio_error_code) { @@ -85,13 +114,13 @@ namespace nap void UDPServer::process() { asio::error_code asio_error; - size_t available_bytes = mSocket.available(asio_error); + size_t available_bytes = mASIO->mSocket.available(asio_error); if(available_bytes > 0) { // fill buffer std::vector buffer; buffer.resize(available_bytes); - mSocket.receive(asio::buffer(buffer), 0, asio_error); + mASIO->mSocket.receive(asio::buffer(buffer), 0, asio_error); if (!asio_error) { diff --git a/modules/napudp/src/udpserver.h b/modules/napudp/src/udpserver.h index dd0f157e8d..14977abcb7 100644 --- a/modules/napudp/src/udpserver.h +++ b/modules/napudp/src/udpserver.h @@ -14,12 +14,6 @@ #include #include -// ASIO includes -#include -#include -#include -#include - // Local includes #include "udpadapter.h" #include "udppacket.h" @@ -28,6 +22,9 @@ namespace nap { ////////////////////////////////////////////////////////////////////////// + // forward declares + class UDPServerASIO; + /** * The UDP Server connects to an endpoint and receives any UDP packets send to the endpoint. * The server will invoke the packetReceived signal when packets are received. @@ -37,6 +34,16 @@ namespace nap { RTTI_ENABLE(UDPAdapter) public: + /** + * Constructor + */ + UDPServer(); + + /* + * Destructor + */ + virtual ~UDPServer(); + /** * initialization * @param error contains error information @@ -63,9 +70,6 @@ namespace nap */ void process() override; private: - // ASIO - asio::io_service mIOService; - asio::ip::udp::socket mSocket{mIOService}; - asio::ip::udp::endpoint mRemoteEndpoint; + std::unique_ptr mASIO; }; } diff --git a/modules/napudp/src/udpthread.cpp b/modules/napudp/src/udpthread.cpp index 6242e74e03..a85b7593da 100644 --- a/modules/napudp/src/udpthread.cpp +++ b/modules/napudp/src/udpthread.cpp @@ -2,10 +2,18 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +// local includes #include "udpthread.h" #include "udpadapter.h" #include "udpservice.h" +// ASIO includes +#include +#include +#include +#include + +// NAP includes #include using asio::ip::address; diff --git a/modules/napudp/src/udpthread.h b/modules/napudp/src/udpthread.h index b51ec90dca..b3d6d3aa52 100644 --- a/modules/napudp/src/udpthread.h +++ b/modules/napudp/src/udpthread.h @@ -15,12 +15,6 @@ #include #include -// ASIO includes -#include -#include -#include -#include - namespace nap { //////////////////////////////////////////////////////////////////////////