first version of InternalUDPService

This commit is contained in:
Henry Winkel
2022-11-12 14:23:12 +01:00
parent 3006f79883
commit 84305eb7fa
16 changed files with 1924 additions and 136 deletions

View File

@@ -1,69 +1,53 @@
#pragma once
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/.
*/
#ifndef SAFE_QUEUE
#define SAFE_QUEUE
/**
* @file
* @copyright MPLv2
*/
#include <condition_variable>
#include <mutex>
#include <queue>
#include <memory>
#include <condition_variable>
#include <string>
namespace WHISPER{
/**
* @class BasicMessageQueue
*
* This class encapsulates shared storage which is a queue and protect it
* with a mutex. In Addition there is a condition variable to notify other
* threads if there is new data in the queue.
*/
template<class T>
namespace WHISPER {
// A threadsafe-queue.
template <class T>
class threadSafeQueue
{
private:
std::queue<std::unique_ptr<T>> q;
std::mutex mx;
std::condition_variable condVar;
public:
/**
* @brief default constructor for class BasicMessageQueue
*/
threadSafeQueue();
threadSafeQueue() : q(), m(), c() {}
/**
* @brief appends given message to queue
*
* The storage is protected by a mutex. Makes a notify_one call to inform
* in waiting thread that there is a new message in the queue.
*
* @param msg - incoming message
*/
void addElement( std::unique_ptr<T> msg );
~threadSafeQueue() {}
/**
* @brief gets the fron message from the queue
*
* This method gets the front message in the queue and deletes it from the
* queue. The storage is protected by a mutex. If the queue is empty the
* function throws an exception.
*
* @result msg - returns the front message from the queue
* @throws length_error - if queue is empty this method throws length_error exception
*/
std::unique_ptr<T> getElement();
// Add an element to the queue.
void addElement(T t)
{
std::lock_guard<std::mutex> lock(m);
q.push(t);
c.notify_one();
}
/**
* @brief method size
*
* @result size - current size of the message queue
*/
unsigned int size();
// Get the front element.
// If the queue is empty, wait till a element is avaiable.
T get(void)
{
std::unique_lock<std::mutex> lock(m);
while (q.empty())
{
// release lock as long as the wait and reaquire it afterwards.
c.wait(lock);
}
T val = q.front();
q.pop();
return val;
}
unsigned int size()
{
std::unique_lock<std::mutex> lock(m);
return q.size();
}
}; //BasicMessageQueue
}; // namespace BC
private:
std::queue<T> q;
mutable std::mutex m;
std::condition_variable c;
};
}
#endif