forked from CobaltFusion/DebugViewPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipeReader.cpp
More file actions
71 lines (59 loc) · 2.07 KB
/
Copy pathPipeReader.cpp
File metadata and controls
71 lines (59 loc) · 2.07 KB
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
// (C) Copyright Gert-Jan de Vos and Jan Wilmans 2013.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "Win32/Win32Lib.h"
#include "CobaltFusion/stringbuilder.h"
#include "DebugViewppLib/PipeReader.h"
#include "DebugViewppLib/LineBuffer.h"
#include <array>
namespace fusion {
namespace debugviewpp {
PipeReader::PipeReader(Timer& timer, ILineBuffer& linebuffer, HANDLE hPipe, DWORD pid, const std::string& processName, long pollFrequency) :
PolledLogSource(timer, SourceType::Pipe, linebuffer, pollFrequency),
m_hPipe(hPipe),
m_pid(pid),
m_process(processName)
{
SetDescription(wstringbuilder() << L"Piped from " << processName);
StartThread();
}
PipeReader::~PipeReader() = default;
bool PipeReader::AtEnd() const
{
return LogSource::AtEnd() || PeekNamedPipe(m_hPipe, nullptr, 0, nullptr, nullptr, nullptr) == FALSE;
}
void PipeReader::Poll()
{
Poll(*this);
}
void PipeReader::Poll(PolledLogSource& logsource)
{
std::array<char, 4096> buf;
// copy m_buffer into 'buf', set 'start' to the position one past the last element copied
char* start = std::copy(m_buffer.data(), m_buffer.data() + m_buffer.size(), buf.data());
DWORD avail = 0;
while ((PeekNamedPipe(m_hPipe, nullptr, 0, nullptr, &avail, nullptr) != 0) && avail > 0)
{
auto size = static_cast<DWORD>(buf.data() + sizeof(buf) - start);
DWORD read = 0;
ReadFile(m_hPipe, start, size, &read, nullptr);
char* begin = buf.data();
char* end = start + read;
char* p = start;
while (p != end)
{
if (*p == '\0' || *p == '\n' || p - begin > 4000)
{
logsource.AddMessage(m_pid, m_process, std::string(begin, p));
begin = p + 1;
}
++p;
}
start = std::copy(begin, end, buf.data());
}
// keep remainer of line for next poll()
m_buffer = std::string(buf.data(), start);
}
} // namespace debugviewpp
} // namespace fusion