summaryrefslogtreecommitdiff
path: root/plugins/MirFox/src/lib/MirFoxCommons_messageQueueBySM.h
blob: 513b720ee727bb4c8709a408dffe74da27ecbafb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef _WSX22_IPC_MESSAGEQUEUEBYSM
#define _WSX22_IPC_MESSAGEQUEUEBYSM



//message queue by shared memory related definitions
#define MQCONST_MQSM_TEXT_SIZEC	2042	//number of wchar_t chars in message - now used sharedmemory mode

#define MQTHREAD_TICK_TIME		100		// 100ms - check exit every this time
#define MQTHREAD_FIRSTRUN_TIME	0		// MQTHREAD_FIRSTRUN_TIME * MQTHREAD_TICK_TIME = 0s  - time to first run of checking message queue in mq thread (after thread start)
#define MQTHREAD_NEXTRUN_TIME	1		// MQTHREAD_NEXTRUN_TIME * MQTHREAD_TICK_TIME = 0s  - time to next run of checking message queue in mq thread



#include "MirFoxCommons_logger.h"






/**
 * MessageQueue Utils
 *
 * based on Boost Shared Memory, beacouse boost message queue doesn't work between 32bit and 64bit processes (at boost 1.46)
 *
 * Singleton pattern based on
 * http://www.codeproject.com/KB/threads/SingletonThreadSafety.aspx
 */
class MessageQueueUtils
{

public:

	//constructor
	MessageQueueUtils();
	//destructor
	~MessageQueueUtils();



	std::string getMqName(uint16_t processId);

	//return 0 if success, >0 if error
	int createMessageQueue(std::string mqName);

	void unloadMessageQueue(uint16_t unloadedMQProcessId);

	//wchar_t*& - pointer by reference
	bool tryReceiveMessage (char& menuItemType, char& userActionType, char& userButton, uint64_t& targetHandle, wchar_t*& userActionSelection, size_t uasBuffCSize);

	void sendMessage(int clientRecordId, char menuItemType, char userActionType, char userButton, uint64_t targetHandle, std::wstring userActionSelection);

	uint16_t volatile unloadedMQProcessId;


	//static method that returns only instance of SharedMemoryUtils
	static MessageQueueUtils * getInstance()
	{
		//initialized always from one thread at a time
		if (m_pOnlyOneInstance == NULL) {
			if (m_pOnlyOneInstance == NULL) {
				m_pOnlyOneInstance = new MessageQueueUtils();
			}
		}
		return m_pOnlyOneInstance;
	}


private:

	boost::interprocess::windows_shared_memory* volatile mqMirSm;

	std::size_t getMqMirSmTotalSize();

	//CRITICAL_SECTION smCs;
	HANDLE smMutex;

	//holds one and only object of MySingleton
	static MessageQueueUtils * m_pOnlyOneInstance;

	MFLogger* logger;

};


#endif //#ifndef _WSX22_IPC_MESSAGEQUEUEBYSM