Improve read stdout

This commit is contained in:
Oleg Korshul
2025-10-28 23:46:35 +03:00
parent 03498026a2
commit ebe2925239

View File

@ -187,15 +187,43 @@ namespace NSProcesses
)
{
std::string lineBuf;
char buf[256]; DWORD n;
while (m_running.load() &&
#ifdef _WIN32
ReadFile(handle, buf, sizeof(buf), &n, nullptr) && n > 0
#else
(n = read(fd, buf, sizeof(buf))) > 0
char buf[4096];
DWORD n;
#ifndef _WIN32
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
#endif
)
while (m_running.load())
{
#ifdef _WIN32
DWORD available = 0;
PeekNamedPipe(handle, nullptr, 0, nullptr, &available, nullptr);
if (available == 0)
{
NSThreads::Sleep(100);
continue;
}
if (!ReadFile(handle, buf, sizeof(buf), &n, nullptr) || n == 0)
break;
#else
n = read(fd, buf, sizeof(buf));
if (n < 0)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
{
NSThreads::Sleep(100);
continue;
}
break;
}
if (n == 0)
break;
#endif
lineBuf.append(buf, n);
size_t pos;
while ((pos = lineBuf.find('\n')) != std::string::npos)
@ -203,9 +231,8 @@ namespace NSProcesses
m_callback->process_callback(m_id, type, lineBuf.substr(0, pos));
lineBuf.erase(0, pos + 1);
}
NSThreads::Sleep(500);
}
if (!lineBuf.empty())
m_callback->process_callback(m_id, type, lineBuf);
}