Skip to content

Commit

Permalink
- removed all asio includes from header files
Browse files Browse the repository at this point in the history
  • Loading branch information
TimGroeneboom committed Sep 13, 2022
1 parent fe7e6b0 commit 4c43c26
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 48 deletions.
2 changes: 1 addition & 1 deletion modules/napudp/src/udpadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
14 changes: 9 additions & 5 deletions modules/napudp/src/udpadapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
#pragma once

// Nap includes
#include <nap/resource.h>
#include <nap/resourceptr.h>
#include <udpthread.h>

// STD includes
#include <system_error>

namespace nap
{
//////////////////////////////////////////////////////////////////////////
// forward declares
class UDPThread;

/**
* Base class of specific UDP client and server resources.
Expand All @@ -23,8 +28,6 @@ namespace nap

RTTI_ENABLE(Resource)
public:
ResourcePtr<UDPThread> mThread = nullptr; ///< Property: 'Thread' the udp thread the adapter registers itself to

/**
* Initialization
* @param error contains error information
Expand All @@ -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<UDPThread> 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);
};
}
44 changes: 37 additions & 7 deletions modules/napudp/src/udpclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,69 @@

#include <thread>

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<UDPClientASIO>();

// 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))
Expand All @@ -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());
Expand Down Expand Up @@ -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)
{
Expand Down
27 changes: 16 additions & 11 deletions modules/napudp/src/udpclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
#include <queue>
#include <mutex>

// ASIO includes
#include <asio/ts/buffer.hpp>
#include <asio/ts/internet.hpp>
#include <asio/io_service.hpp>
#include <asio/system_error.hpp>

// NAP includes
#include <utility/threading.h>
#include <concurrentqueue.h>
Expand All @@ -27,13 +21,26 @@ namespace nap
{
//////////////////////////////////////////////////////////////////////////

// forward declares
class UDPClientASIO;

/**
* The UDP Client class is used to send UDP Packets to an endpoint.
*/
class NAPAPI UDPClient final : public UDPAdapter
{
RTTI_ENABLE(UDPAdapter)
public:
/**
* Constructor
*/
UDPClient();

/**
* Destructor
*/
virtual ~UDPClient();

/**
* Initializes the UDP client
* @param error contains error information
Expand Down Expand Up @@ -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<nap::uint8> mBuffer;
asio::ip::udp::endpoint mRemoteEndpoint;
std::unique_ptr<UDPClientASIO> mASIO;
std::vector<nap::uint8> mBuffer;

// Threading
moodycamel::ConcurrentQueue<UDPPacket> mQueue;
Expand Down
45 changes: 37 additions & 8 deletions modules/napudp/src/udpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include "udppacket.h"
#include "udpthread.h"

// ASIO Includes
#include <asio/ip/udp.hpp>
#include <asio/io_service.hpp>
#include <asio/ts/buffer.hpp>
#include <asio/ts/internet.hpp>

// External includes
#include <asio/ts/buffer.hpp>
#include <asio/ts/internet.hpp>
Expand All @@ -15,28 +21,51 @@

#include <thread>

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<UDPServerASIO>();

// 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;

Expand All @@ -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;

Expand All @@ -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)
{
Expand All @@ -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<uint8> 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)
{
Expand Down
24 changes: 14 additions & 10 deletions modules/napudp/src/udpserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
#include <concurrentqueue.h>
#include <nap/signalslot.h>

// ASIO includes
#include <asio/ts/buffer.hpp>
#include <asio/ts/internet.hpp>
#include <asio/io_service.hpp>
#include <asio/system_error.hpp>

// Local includes
#include "udpadapter.h"
#include "udppacket.h"
Expand All @@ -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.
Expand All @@ -37,6 +34,16 @@ namespace nap
{
RTTI_ENABLE(UDPAdapter)
public:
/**
* Constructor
*/
UDPServer();

/*
* Destructor
*/
virtual ~UDPServer();

/**
* initialization
* @param error contains error information
Expand All @@ -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<UDPServerASIO> mASIO;
};
}
8 changes: 8 additions & 0 deletions modules/napudp/src/udpthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <asio/ts/buffer.hpp>
#include <asio/ts/internet.hpp>
#include <asio/io_service.hpp>
#include <asio/system_error.hpp>

// NAP includes
#include <nap/logger.h>

using asio::ip::address;
Expand Down
6 changes: 0 additions & 6 deletions modules/napudp/src/udpthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
#include <concurrentqueue.h>
#include <rtti/factory.h>

// ASIO includes
#include <asio/ts/buffer.hpp>
#include <asio/ts/internet.hpp>
#include <asio/io_service.hpp>
#include <asio/system_error.hpp>

namespace nap
{
//////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 4c43c26

Please sign in to comment.