-
Notifications
You must be signed in to change notification settings - Fork 1
/
Reactor.cpp
64 lines (48 loc) · 1.37 KB
/
Reactor.cpp
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
// Reactor.cpp: implementation of the CReactor class.
//
//////////////////////////////////////////////////////////////////////
#include "Reactor.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CReactor::~CReactor()
{
}
void CReactor::HandleEvents()
{
while(!bTerminated)
{
DWORD dwRetval = ::WaitForMultipleObjectsEx(m_dwHandlesNum,
m_handles, FALSE, INFINITE, TRUE);
if(dwRetval >= WAIT_OBJECT_0 && dwRetval < WAIT_OBJECT_0 + m_dwHandlesNum)
//(dwRetval >= WAIT_ADANDONED_0 && dwRetval < WAIT_ADANDONED_0 + m_dwHandlesNum)
{
DWORD dwIndex = dwRetval - WAIT_OBJECT_0;
GetHandler(dwIndex)->HandleEvent(m_handles[dwIndex]);
}
}
}
BOOL CReactor::RegisterHandler(CEventHandler* eh)
{
CThreadMutexGuard Guard(m_Lock);
if (m_dwHandlesNum == m_dwMaxHandles)
return FALSE;
m_eh[m_dwHandlesNum] = eh;
m_handles[m_dwHandlesNum] = eh->GetHandle();
++m_dwHandlesNum;
return TRUE;
}
BOOL CReactor::RemoveHandler(HANDLE h)
{
CThreadMutexGuard Guard(m_Lock);
for (DWORD i = 0; i < m_dwHandlesNum; ++i)
{
if (h == m_handles[i])
{
for (DWORD j = i; j < m_dwHandlesNum; ++j)
m_handles[j] = m_handles[j + 1];
return TRUE;
}
}
return FALSE;
}