2012-03-14 05:14:39 +00:00
|
|
|
#include "EventLoop.h"
|
|
|
|
#include "NamedPipe.h"
|
2012-03-14 06:27:21 +00:00
|
|
|
#include "AgentAssert.h"
|
2012-03-14 05:14:39 +00:00
|
|
|
#include "../Shared/DebugClient.h"
|
|
|
|
|
|
|
|
EventLoop::EventLoop() : m_exiting(false), m_pollInterval(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EventLoop::~EventLoop()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_pipes.size(); ++i)
|
|
|
|
delete m_pipes[i];
|
|
|
|
}
|
|
|
|
|
2012-03-14 06:27:21 +00:00
|
|
|
// Enter the event loop. Runs until the I/O or timeout handler calls exit().
|
2012-03-14 05:14:39 +00:00
|
|
|
void EventLoop::run()
|
|
|
|
{
|
|
|
|
std::vector<HANDLE> waitHandles;
|
2012-03-14 06:27:21 +00:00
|
|
|
DWORD lastTime = GetTickCount();
|
2012-03-14 05:14:39 +00:00
|
|
|
while (!m_exiting) {
|
2012-03-14 06:27:21 +00:00
|
|
|
bool didSomething = false;
|
|
|
|
|
|
|
|
// Attempt to make progress with the pipes.
|
2012-03-14 05:14:39 +00:00
|
|
|
waitHandles.clear();
|
|
|
|
for (size_t i = 0; i < m_pipes.size(); ++i) {
|
2012-03-14 06:27:21 +00:00
|
|
|
if (m_pipes[i]->serviceIo(&waitHandles)) {
|
|
|
|
onPipeIo(m_pipes[i]);
|
|
|
|
didSomething = true;
|
|
|
|
}
|
2012-03-14 05:14:39 +00:00
|
|
|
}
|
2012-03-14 06:27:21 +00:00
|
|
|
|
|
|
|
// Call the timeout if enough time has elapsed.
|
2012-03-14 05:14:39 +00:00
|
|
|
if (m_pollInterval > 0) {
|
2012-03-14 06:27:21 +00:00
|
|
|
int elapsed = GetTickCount() - lastTime;
|
2012-03-14 05:14:39 +00:00
|
|
|
if (elapsed >= m_pollInterval) {
|
|
|
|
onPollTimeout();
|
2012-03-14 06:27:21 +00:00
|
|
|
lastTime = GetTickCount();
|
|
|
|
didSomething = true;
|
2012-03-14 05:14:39 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-14 06:27:21 +00:00
|
|
|
|
|
|
|
if (didSomething)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If there's nothing to do, wait.
|
|
|
|
DWORD timeout = INFINITE;
|
|
|
|
if (m_pollInterval > 0)
|
|
|
|
timeout = std::max(0, (int)(lastTime + m_pollInterval - GetTickCount()));
|
|
|
|
if (waitHandles.size() == 0) {
|
|
|
|
ASSERT(timeout != INFINITE);
|
|
|
|
if (timeout > 0)
|
|
|
|
Sleep(timeout);
|
|
|
|
} else {
|
|
|
|
DWORD result = WaitForMultipleObjects(waitHandles.size(),
|
|
|
|
waitHandles.data(),
|
|
|
|
FALSE,
|
|
|
|
timeout);
|
|
|
|
ASSERT(result != WAIT_FAILED);
|
|
|
|
}
|
2012-03-14 05:14:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NamedPipe *EventLoop::createNamedPipe()
|
|
|
|
{
|
|
|
|
NamedPipe *ret = new NamedPipe();
|
|
|
|
m_pipes.push_back(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventLoop::setPollInterval(int ms)
|
|
|
|
{
|
|
|
|
m_pollInterval = ms;
|
|
|
|
}
|
|
|
|
|
2012-03-14 06:27:21 +00:00
|
|
|
void EventLoop::shutdown()
|
2012-03-14 05:14:39 +00:00
|
|
|
{
|
|
|
|
m_exiting = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventLoop::onPollTimeout()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-03-14 06:27:21 +00:00
|
|
|
void EventLoop::onPipeIo(NamedPipe *namedPipe)
|
2012-03-14 05:14:39 +00:00
|
|
|
{
|
|
|
|
}
|