-
Notifications
You must be signed in to change notification settings - Fork 1
/
Reactor.h
71 lines (51 loc) · 1.42 KB
/
Reactor.h
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
// Reactor.h: interface for the CReactor class.
//
//////////////////////////////////////////////////////////////////////
#ifndef REACTOR_H
#define REACTOR_H
#pragma once
#include "common.h"
#include "EventHandler.h"
#include "ThreadMutex.h"
class CReactor
{
private:
HANDLE* m_handles;
CEventHandler** m_eh;
DWORD m_dwMaxHandles;
DWORD m_dwHandlesNum;
HANDLE m_hThread;
BOOL bTerminated;
CThreadMutex m_Lock;
inline CEventHandler* GetHandler(DWORD dwIndex)
{
CThreadMutexGuard Guard(m_Lock);
return m_eh[dwIndex];
}
static DWORD WINAPI SvcRun(PVOID pvArgs)
{
CReactor* this_obj = static_cast<CReactor*>(pvArgs);
this_obj->HandleEvents();
}
public:
CReactor(DWORD dwMaxHandles): bTerminated(FALSE),
m_dwHandlesNum(0)
{
if (dwMaxHandles == 0 || dwMaxHandles > MAXIMUM_WAIT_OBJECTS)
m_dwMaxHandles = MAXIMUM_WAIT_OBJECTS;
else
m_dwMaxHandles = dwMaxHandles;
m_handles = new HANDLE[dwMaxHandles];
m_eh = new CEventHandler*[dwMaxHandles];
DWORD dwThreadId;
m_hThread = CreateThread(NULL, 0, CReactor::SvcRun, this, 0, &dwThreadId);
assert(m_hThread != NULL);
TRACE(TEXT("Creato Reactor Thread: Handle 0x%x, Id %i"), m_hThread, dwThreadId);
CloseHandle(m_hThread);
}
virtual ~CReactor();
void HandleEvents();
BOOL RegisterHandler(CEventHandler* eh);
BOOL RemoveHandler(HANDLE h);
};
#endif //REACTOR_H