2013-06-15 00:55:05 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Publisher/Subscriber Pattern
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-06-15 21:32:13 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2013-06-15 00:55:05 +04:00
|
|
|
#include <winpr/collections.h>
|
|
|
|
|
2013-06-18 08:39:48 +04:00
|
|
|
/**
|
|
|
|
* Events (C# Programming Guide)
|
|
|
|
* http://msdn.microsoft.com/en-us/library/awbftdfh.aspx
|
|
|
|
*/
|
|
|
|
|
2013-06-15 00:55:05 +04:00
|
|
|
/**
|
|
|
|
* Properties
|
|
|
|
*/
|
|
|
|
|
2013-06-19 05:23:26 +04:00
|
|
|
wEventType* PubSub_GetEventTypes(wPubSub* pubSub, int* count)
|
2013-06-15 00:55:05 +04:00
|
|
|
{
|
2013-06-15 21:32:13 +04:00
|
|
|
if (count)
|
|
|
|
*count = pubSub->count;
|
|
|
|
|
|
|
|
return pubSub->events;
|
2013-06-15 00:55:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Methods
|
|
|
|
*/
|
|
|
|
|
2013-08-02 14:07:05 +04:00
|
|
|
void PubSub_Lock(wPubSub* pubSub)
|
2013-06-16 02:18:02 +04:00
|
|
|
{
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&pubSub->lock);
|
2013-06-16 02:18:02 +04:00
|
|
|
}
|
|
|
|
|
2013-08-02 14:07:05 +04:00
|
|
|
void PubSub_Unlock(wPubSub* pubSub)
|
2013-06-16 02:18:02 +04:00
|
|
|
{
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&pubSub->lock);
|
2013-06-16 02:18:02 +04:00
|
|
|
}
|
|
|
|
|
2013-06-19 05:23:26 +04:00
|
|
|
wEventType* PubSub_FindEventType(wPubSub* pubSub, const char* EventName)
|
2013-06-15 21:32:13 +04:00
|
|
|
{
|
|
|
|
int index;
|
2013-06-19 05:23:26 +04:00
|
|
|
wEventType* event = NULL;
|
2013-06-15 21:32:13 +04:00
|
|
|
|
2015-03-11 13:33:17 +03:00
|
|
|
for (index = 0; index < pubSub->count; index++)
|
2013-06-15 21:32:13 +04:00
|
|
|
{
|
|
|
|
if (strcmp(pubSub->events[index].EventName, EventName) == 0)
|
|
|
|
{
|
|
|
|
event = &(pubSub->events[index]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
2013-06-19 05:23:26 +04:00
|
|
|
void PubSub_AddEventTypes(wPubSub* pubSub, wEventType* events, int count)
|
2013-06-15 21:32:13 +04:00
|
|
|
{
|
2013-06-16 02:18:02 +04:00
|
|
|
if (pubSub->synchronized)
|
|
|
|
PubSub_Lock(pubSub);
|
|
|
|
|
2013-06-15 21:32:13 +04:00
|
|
|
while (pubSub->count + count >= pubSub->size)
|
|
|
|
{
|
2015-03-11 09:59:56 +03:00
|
|
|
int new_size;
|
|
|
|
wEventType *new_event;
|
|
|
|
|
|
|
|
new_size = pubSub->size * 2;
|
2017-09-25 14:28:16 +03:00
|
|
|
new_event = (wEventType*) realloc(pubSub->events, new_size * sizeof(wEventType));
|
2015-03-11 09:59:56 +03:00
|
|
|
if (!new_event)
|
|
|
|
return;
|
|
|
|
pubSub->size = new_size;
|
|
|
|
pubSub->events = new_event;
|
2013-06-15 21:32:13 +04:00
|
|
|
}
|
|
|
|
|
2013-06-19 05:23:26 +04:00
|
|
|
CopyMemory(&pubSub->events[pubSub->count], events, count * sizeof(wEventType));
|
2013-06-15 21:32:13 +04:00
|
|
|
pubSub->count += count;
|
2013-06-16 02:18:02 +04:00
|
|
|
|
|
|
|
if (pubSub->synchronized)
|
|
|
|
PubSub_Unlock(pubSub);
|
2013-06-15 21:32:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int PubSub_Subscribe(wPubSub* pubSub, const char* EventName, pEventHandler EventHandler)
|
|
|
|
{
|
2013-06-19 05:23:26 +04:00
|
|
|
wEventType* event;
|
2013-06-15 21:32:13 +04:00
|
|
|
int status = -1;
|
|
|
|
|
2013-06-16 02:18:02 +04:00
|
|
|
if (pubSub->synchronized)
|
|
|
|
PubSub_Lock(pubSub);
|
|
|
|
|
2013-06-19 05:23:26 +04:00
|
|
|
event = PubSub_FindEventType(pubSub, EventName);
|
2013-06-15 21:32:13 +04:00
|
|
|
|
|
|
|
if (event)
|
|
|
|
{
|
|
|
|
status = 0;
|
2013-06-16 02:18:02 +04:00
|
|
|
|
|
|
|
if (event->EventHandlerCount <= MAX_EVENT_HANDLERS)
|
|
|
|
{
|
|
|
|
event->EventHandlers[event->EventHandlerCount] = EventHandler;
|
|
|
|
event->EventHandlerCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = -1;
|
|
|
|
}
|
2013-06-15 21:32:13 +04:00
|
|
|
}
|
|
|
|
|
2013-06-16 02:18:02 +04:00
|
|
|
if (pubSub->synchronized)
|
|
|
|
PubSub_Unlock(pubSub);
|
|
|
|
|
2013-06-15 21:32:13 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PubSub_Unsubscribe(wPubSub* pubSub, const char* EventName, pEventHandler EventHandler)
|
2013-06-15 00:55:05 +04:00
|
|
|
{
|
2013-06-16 02:18:02 +04:00
|
|
|
int index;
|
2013-06-19 05:23:26 +04:00
|
|
|
wEventType* event;
|
2013-06-15 21:32:13 +04:00
|
|
|
int status = -1;
|
|
|
|
|
2013-06-16 02:18:02 +04:00
|
|
|
if (pubSub->synchronized)
|
|
|
|
PubSub_Lock(pubSub);
|
|
|
|
|
2013-06-19 05:23:26 +04:00
|
|
|
event = PubSub_FindEventType(pubSub, EventName);
|
2013-06-15 00:55:05 +04:00
|
|
|
|
2013-06-15 21:32:13 +04:00
|
|
|
if (event)
|
|
|
|
{
|
|
|
|
status = 0;
|
2013-06-16 02:18:02 +04:00
|
|
|
|
|
|
|
for (index = 0; index < event->EventHandlerCount; index++)
|
|
|
|
{
|
|
|
|
if (event->EventHandlers[index] == EventHandler)
|
|
|
|
{
|
|
|
|
event->EventHandlers[index] = NULL;
|
|
|
|
event->EventHandlerCount--;
|
|
|
|
MoveMemory(&event->EventHandlers[index], &event->EventHandlers[index + 1],
|
2017-09-25 14:28:16 +03:00
|
|
|
(MAX_EVENT_HANDLERS - index - 1) * sizeof(pEventHandler));
|
2013-06-16 02:18:02 +04:00
|
|
|
status = 1;
|
|
|
|
}
|
|
|
|
}
|
2013-06-15 21:32:13 +04:00
|
|
|
}
|
|
|
|
|
2013-06-16 02:18:02 +04:00
|
|
|
if (pubSub->synchronized)
|
|
|
|
PubSub_Unlock(pubSub);
|
|
|
|
|
2013-06-15 21:32:13 +04:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PubSub_OnEvent(wPubSub* pubSub, const char* EventName, void* context, wEventArgs* e)
|
|
|
|
{
|
2013-06-16 02:18:02 +04:00
|
|
|
int index;
|
2013-06-19 05:23:26 +04:00
|
|
|
wEventType* event;
|
2013-06-15 21:32:13 +04:00
|
|
|
int status = -1;
|
|
|
|
|
2013-06-16 02:18:02 +04:00
|
|
|
if (pubSub->synchronized)
|
|
|
|
PubSub_Lock(pubSub);
|
|
|
|
|
2013-06-19 05:23:26 +04:00
|
|
|
event = PubSub_FindEventType(pubSub, EventName);
|
2013-06-15 21:32:13 +04:00
|
|
|
|
2013-06-27 22:16:50 +04:00
|
|
|
if (pubSub->synchronized)
|
|
|
|
PubSub_Unlock(pubSub);
|
|
|
|
|
2013-06-15 21:32:13 +04:00
|
|
|
if (event)
|
|
|
|
{
|
2013-06-16 02:18:02 +04:00
|
|
|
status = 0;
|
|
|
|
|
|
|
|
for (index = 0; index < event->EventHandlerCount; index++)
|
2013-06-15 21:32:13 +04:00
|
|
|
{
|
2013-06-16 02:18:02 +04:00
|
|
|
if (event->EventHandlers[index])
|
|
|
|
{
|
|
|
|
event->EventHandlers[index](context, e);
|
2013-06-18 08:39:48 +04:00
|
|
|
status++;
|
2013-06-16 02:18:02 +04:00
|
|
|
}
|
2013-06-15 21:32:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2013-06-15 00:55:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construction, Destruction
|
|
|
|
*/
|
|
|
|
|
|
|
|
wPubSub* PubSub_New(BOOL synchronized)
|
|
|
|
{
|
|
|
|
wPubSub* pubSub = NULL;
|
|
|
|
|
|
|
|
pubSub = (wPubSub*) malloc(sizeof(wPubSub));
|
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!pubSub)
|
|
|
|
return NULL;
|
2013-06-15 00:55:05 +04:00
|
|
|
|
2015-03-25 19:37:46 +03:00
|
|
|
pubSub->synchronized = synchronized;
|
2015-03-23 19:25:23 +03:00
|
|
|
|
2015-03-25 19:37:46 +03:00
|
|
|
if (pubSub->synchronized && !InitializeCriticalSectionAndSpinCount(&pubSub->lock, 4000))
|
|
|
|
{
|
|
|
|
free(pubSub);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-06-15 21:32:13 +04:00
|
|
|
|
2015-03-25 19:37:46 +03:00
|
|
|
pubSub->count = 0;
|
|
|
|
pubSub->size = 64;
|
2013-06-15 21:32:13 +04:00
|
|
|
|
2017-05-30 11:46:43 +03:00
|
|
|
pubSub->events = (wEventType*) calloc(pubSub->size, sizeof(wEventType));
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!pubSub->events)
|
|
|
|
{
|
|
|
|
if (pubSub->synchronized)
|
|
|
|
DeleteCriticalSection(&pubSub->lock);
|
|
|
|
free(pubSub);
|
|
|
|
return NULL;
|
2013-06-15 00:55:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return pubSub;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PubSub_Free(wPubSub* pubSub)
|
|
|
|
{
|
|
|
|
if (pubSub)
|
|
|
|
{
|
|
|
|
if (pubSub->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
DeleteCriticalSection(&pubSub->lock);
|
2013-06-15 00:55:05 +04:00
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
free(pubSub->events);
|
2013-06-15 00:55:05 +04:00
|
|
|
free(pubSub);
|
|
|
|
}
|
|
|
|
}
|