2012-03-23 10:11:34 +00:00
|
|
|
// Copyright (c) 2011-2012 Ryan Prichard
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
|
2012-03-14 05:14:39 +00:00
|
|
|
#include "EventLoop.h"
|
2015-10-16 04:40:12 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2012-03-14 05:14:39 +00:00
|
|
|
#include "NamedPipe.h"
|
2012-03-14 06:27:21 +00:00
|
|
|
#include "AgentAssert.h"
|
2012-04-01 09:13:21 +00:00
|
|
|
#include "../shared/DebugClient.h"
|
2012-03-14 05:14:39 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|