installed pty
[VSoRC/.git] / node_modules / node-pty / deps / winpty / src / unix-adapter / OutputHandler.cc
1 // Copyright (c) 2011-2015 Ryan Prichard
2 //
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:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
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
19 // IN THE SOFTWARE.
20
21 #include "OutputHandler.h"
22
23 #include <assert.h>
24 #include <errno.h>
25 #include <sys/select.h>
26 #include <unistd.h>
27
28 #include <algorithm>
29 #include <vector>
30
31 #include "../shared/DebugClient.h"
32 #include "Util.h"
33 #include "WakeupFd.h"
34
35 OutputHandler::OutputHandler(
36         HANDLE conout, int outputfd, WakeupFd &completionWakeup) :
37     m_conout(conout),
38     m_outputfd(outputfd),
39     m_completionWakeup(completionWakeup),
40     m_threadHasBeenJoined(false),
41     m_threadCompleted(0)
42 {
43     pthread_create(&m_thread, NULL, OutputHandler::threadProcS, this);
44 }
45
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;
51     }
52 }
53
54 void OutputHandler::threadProc() {
55     std::vector<char> buffer(4096);
56     while (true) {
57         DWORD numRead = 0;
58         BOOL ret = ReadFile(m_conout,
59                             &buffer[0], buffer.size(),
60                             &numRead, NULL);
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));
65             } else {
66                 trace("OutputHandler: read failed: "
67                     "ret=%d lastError=0x%x numRead=%u",
68                     ret,
69                     static_cast<unsigned int>(GetLastError()),
70                     static_cast<unsigned int>(numRead));
71             }
72             break;
73         }
74         if (!writeAll(m_outputfd, &buffer[0], numRead)) {
75             break;
76         }
77     }
78     m_threadCompleted = 1;
79     m_completionWakeup.set();
80 }