181 lines
5.0 KiB
C++
181 lines
5.0 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/Windows/EventManager.h
|
|
*
|
|
* @brief Header file for Windows event manager.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_WINDOWS_EVENT_MANAGER_H)
|
|
#define ACTIV_BASE_WINDOWS_EVENT_MANAGER_H
|
|
|
|
#include "ActivMiddleware/ActivBase/External.h"
|
|
#include "ActivMiddleware/ActivBase/EventManagee.h"
|
|
#include "ActivMiddleware/CxxYZ/atomic.h"
|
|
#include "ActivMiddleware/CxxYZ/memory.h"
|
|
#include "ActivMiddleware/CxxYZ/vector.h"
|
|
#include "ActivMiddleware/Misc/ScopedHelper.h"
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
class IMutex;
|
|
class TraceBufferBase;
|
|
|
|
//! Event manager class.
|
|
class EventManager
|
|
{
|
|
public:
|
|
//! Settings.
|
|
class Settings
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param hasMutex
|
|
*/
|
|
ACTIV_BASE_API Settings(const bool hasMutex);
|
|
|
|
bool m_hasMutex; ///< Whether to create a mutex.
|
|
};
|
|
|
|
/**
|
|
* @brief Get the underlying notification handle, if available.
|
|
*
|
|
* @param handle reference to receive the handle.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_NOT_SUPPORTED the notification type for this EventManager does not support an exportable handle.
|
|
*/
|
|
ACTIV_BASE_API StatusCode GetNotificationHandle(HANDLE &handle) const;
|
|
|
|
/**
|
|
* @brief Wait for events and distribute.
|
|
*
|
|
* Should only be called from a single thread. It is safe to add/remove EventManagees from other threads,
|
|
* as long as Settings::m_hasMutex is true.
|
|
*
|
|
* @param timeoutPeriod in ms.
|
|
* @param pTraceBuffer optional trace buffer to record timestamp to.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_SHUTTING_DOWN user has requested thread exit.
|
|
* @retval STATUS_CODE_TIMEOUT
|
|
*/
|
|
ACTIV_BASE_API StatusCode Distribute(const TimeoutPeriod timeoutPeriod, TraceBufferBase *pTraceBuffer);
|
|
|
|
/**
|
|
* @brief Initiate a shutdown of the manager's thread(s).
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval ...
|
|
*/
|
|
ACTIV_BASE_API StatusCode Shutdown();
|
|
|
|
/**
|
|
* @brief Create an EventManager using the most suitable underlying mechanism for this platform.
|
|
*
|
|
* @param settings
|
|
*
|
|
* @return Pointer to an EventManager.
|
|
*
|
|
* @throw Exception.
|
|
*/
|
|
ACTIV_BASE_API static CxxYZ::unique_ptr<EventManager> Create(const Settings &settings);
|
|
|
|
private:
|
|
class CallbackInfo
|
|
{
|
|
public:
|
|
CallbackInfo(EventManagee * const pEventManagee, const SOCKET socket);
|
|
|
|
EventManagee * const m_pEventManagee;
|
|
const SOCKET m_socket;
|
|
};
|
|
|
|
class NotificationEventManagee ACTIV_FINAL : public EventManagee
|
|
{
|
|
public:
|
|
NotificationEventManagee(EventManager &eventManager);
|
|
|
|
protected:
|
|
virtual void OnEvents(const Events events);
|
|
};
|
|
|
|
typedef CxxYZ::shared_ptr<CallbackInfo> CallbackInfoPtr;
|
|
typedef CxxYZ::vector<CallbackInfoPtr> CallbackInfoList;
|
|
typedef CxxYZ::vector<WSAEVENT> WsaEventList;
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param settings
|
|
*/
|
|
EventManager(const Settings &settings);
|
|
|
|
/**
|
|
* @brief Add a handle to the manager.
|
|
*
|
|
* @param handle the handle to add.
|
|
* @param events the set of events to wait on, initially.
|
|
* @param pCallback callback to receive event notifications through.
|
|
* @param socket (optional) socket associated with @a handle.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval ...
|
|
*/
|
|
StatusCode AddFd(const HANDLE handle, const EventManagee::Events events, EventManagee * const pCallback,
|
|
const SOCKET socket = INVALID_SOCKET);
|
|
|
|
/**
|
|
* @brief Remove a file description from the manager.
|
|
*
|
|
* @param handle the handle to remove.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_NOT_FOUND @a handle is not being managed.
|
|
*/
|
|
StatusCode RemoveFd(const HANDLE handle);
|
|
|
|
/**
|
|
* @brief Modify the set of events for an socket already managed.
|
|
*
|
|
* @param handle the handle to modify.
|
|
* @param events the new set of events to wait on.
|
|
* @param pCallback callback to receive event notifications through.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval ...
|
|
*/
|
|
StatusCode ModifyEvents(const HANDLE handle, const EventManagee::Events events, EventManagee * const pCallback);
|
|
|
|
void ProcessEvent(const size_t index, CallbackInfo &callbackInfo);
|
|
void ProcessNotificationEvent();
|
|
StatusCode InternalModifyEvents(const CallbackInfo &callbackInfo, const EventManagee::Events events);
|
|
bool FindIndex(const HANDLE handle, size_t &index);
|
|
StatusCode SetNotificationEvent();
|
|
|
|
const Settings m_settings;
|
|
const CxxYZ::unique_ptr<IMutex> m_pMutex;
|
|
CxxYZ::atomic_bool m_shutdownPosted; ///< Set when user thread initiates a shutdown.
|
|
bool m_shutdownReceived;
|
|
|
|
ScopedHandle m_notificationEvent;
|
|
NotificationEventManagee m_notificationEventManagee;
|
|
CallbackInfoList m_callbackInfoList;
|
|
WsaEventList m_wsaEventList;
|
|
CxxYZ::atomic<uint32_t> m_version; ///< Incremented when the set of events managed changes.
|
|
|
|
// Used in Distribute().
|
|
CallbackInfoList m_distributeCallbackInfoList;
|
|
WsaEventList m_distributeWsaEventList;
|
|
size_t m_distributeStartIndex;
|
|
uint32_t m_distributeVersion;
|
|
|
|
friend class EventManagee;
|
|
};
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_WINDOWS_EVENT_MANAGER_H)
|