| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : winprocess.h |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
#ifndef __winprocess__ |
| 26 |
#define __winprocess__ |
| 27 |
|
| 28 |
#ifdef __WXMSW__ |
| 29 |
|
| 30 |
#include "asyncprocess.h" |
| 31 |
#include "codelite_exports.h" |
| 32 |
#include "wx/msw/wrapwin.h" // includes windows.h |
| 33 |
#include <thread> |
| 34 |
#include <unordered_set> |
| 35 |
#include <wx/string.h> |
| 36 |
|
| 37 |
class ProcessReaderThread; |
| 38 |
class WinWriterThread; |
| 39 |
|
| 40 |
#define BUFFER_SIZE 16 * 1024 |
| 41 |
|
| 42 |
class WXDLLIMPEXP_CL WinProcessImpl : public IProcess |
| 43 |
{ |
| 44 |
char m_buffer[BUFFER_SIZE]; |
| 45 |
WinWriterThread* m_writerThread = nullptr; |
| 46 |
std::unordered_set<long> m_initialChildren; |
| 47 |
|
| 48 |
protected: |
| 49 |
void StartReaderThread(); |
| 50 |
bool DoReadFromPipe(HANDLE pipe, wxString& buff); |
| 51 |
|
| 52 |
public: |
| 53 |
WinProcessImpl(wxEvtHandler* parent); |
| 54 |
virtual ~WinProcessImpl(); |
| 55 |
|
| 56 |
// Create process asynchronously and return a process object |
| 57 |
static IProcess* Execute(wxEvtHandler* parent, const wxString& cmd, size_t flags = IProcessCreateDefault, |
| 58 |
const wxString& workingDir = wxEmptyString, IProcessCallback* cb = NULL); |
| 59 |
|
| 60 |
// Create process asynchronously and return a process object |
| 61 |
static IProcess* Execute(wxEvtHandler* parent, const wxArrayString& args, size_t flags = IProcessCreateDefault, |
| 62 |
const wxString& workingDir = wxEmptyString, IProcessCallback* cb = NULL); |
| 63 |
|
| 64 |
/** |
| 65 |
* @brief read data from stdout and error |
| 66 |
* @param buff check the buffer when true is returned |
| 67 |
* @return return true on success or timeout, flase otherwise, incase of false the reader thread will terminate |
| 68 |
*/ |
| 69 |
virtual bool Read(wxString& buff, wxString& buffErr); |
| 70 |
|
| 71 |
// Write to the process stdin |
| 72 |
virtual bool Write(const wxString& buff); |
| 73 |
virtual bool Write(const std::string& buff); |
| 74 |
virtual bool WriteRaw(const wxString& buff); |
| 75 |
virtual bool WriteRaw(const std::string& buff); |
| 76 |
virtual bool WriteToConsole(const wxString& buff); |
| 77 |
|
| 78 |
// Return true if the process is still alive |
| 79 |
virtual bool IsAlive(); |
| 80 |
|
| 81 |
// Clean the process resources and kill the process if it is |
| 82 |
// still alive |
| 83 |
virtual void Cleanup(); |
| 84 |
virtual void Terminate(); |
| 85 |
virtual void Detach(); |
| 86 |
virtual void Signal(wxSignal sig); |
| 87 |
|
| 88 |
private: |
| 89 |
// Creating process related handles |
| 90 |
HANDLE hChildStdinRd, hChildStdinWr, hChildStdinWrDup, hChildStdoutRd, hChildStdoutWr, hChildStdoutRdDup, |
| 91 |
hChildStderrRd, hChildStderrWr, hChildStderrRdDup, hSaveStdin, hSaveStdout, hSaveStderr; |
| 92 |
|
| 93 |
// Child process id & information |
| 94 |
DWORD dwProcessId; |
| 95 |
PROCESS_INFORMATION piProcInfo; |
| 96 |
}; |
| 97 |
|
| 98 |
#endif |
| 99 |
|
| 100 |
#endif // __winprocess__ |