1 // Copyright (c) 2011-2015 Ryan Prichard
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 #include "OutputHandler.h"
25 #include <sys/select.h>
31 #include "../shared/DebugClient.h"
35 OutputHandler::OutputHandler(
36 HANDLE conout, int outputfd, WakeupFd &completionWakeup) :
39 m_completionWakeup(completionWakeup),
40 m_threadHasBeenJoined(false),
43 pthread_create(&m_thread, NULL, OutputHandler::threadProcS, this);
46 void OutputHandler::shutdown() {
47 if (!m_threadHasBeenJoined) {
48 int ret = pthread_join(m_thread, NULL);
49 assert(ret == 0 && "pthread_join failed");
50 m_threadHasBeenJoined = true;
54 void OutputHandler::threadProc() {
55 std::vector<char> buffer(4096);
58 BOOL ret = ReadFile(m_conout,
59 &buffer[0], buffer.size(),
61 if (!ret || numRead == 0) {
62 if (!ret && GetLastError() == ERROR_BROKEN_PIPE) {
63 trace("OutputHandler: pipe closed: numRead=%u",
64 static_cast<unsigned int>(numRead));
66 trace("OutputHandler: read failed: "
67 "ret=%d lastError=0x%x numRead=%u",
69 static_cast<unsigned int>(GetLastError()),
70 static_cast<unsigned int>(numRead));
74 if (!writeAll(m_outputfd, &buffer[0], numRead)) {
78 m_threadCompleted = 1;
79 m_completionWakeup.set();