| 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 |
#ifndef AGENT_H |
| 22 |
#define AGENT_H |
| 23 |
|
| 24 |
#include <windows.h> |
| 25 |
#include <stdint.h> |
| 26 |
|
| 27 |
#include <memory> |
| 28 |
#include <string> |
| 29 |
|
| 30 |
#include "DsrSender.h" |
| 31 |
#include "EventLoop.h" |
| 32 |
#include "Win32Console.h" |
| 33 |
|
| 34 |
class ConsoleInput; |
| 35 |
class NamedPipe; |
| 36 |
class ReadBuffer; |
| 37 |
class Scraper; |
| 38 |
class WriteBuffer; |
| 39 |
class Win32ConsoleBuffer; |
| 40 |
|
| 41 |
class Agent : public EventLoop, public DsrSender |
| 42 |
{ |
| 43 |
public: |
| 44 |
Agent(LPCWSTR controlPipeName, |
| 45 |
uint64_t agentFlags, |
| 46 |
int mouseMode, |
| 47 |
int initialCols, |
| 48 |
int initialRows); |
| 49 |
virtual ~Agent(); |
| 50 |
void sendDsr() override; |
| 51 |
|
| 52 |
private: |
| 53 |
NamedPipe &connectToControlPipe(LPCWSTR pipeName); |
| 54 |
NamedPipe &createDataServerPipe(bool write, const wchar_t *kind); |
| 55 |
|
| 56 |
private: |
| 57 |
void pollControlPipe(); |
| 58 |
void handlePacket(ReadBuffer &packet); |
| 59 |
void writePacket(WriteBuffer &packet); |
| 60 |
void handleStartProcessPacket(ReadBuffer &packet); |
| 61 |
void handleSetSizePacket(ReadBuffer &packet); |
| 62 |
void handleGetConsoleProcessListPacket(ReadBuffer &packet); |
| 63 |
void pollConinPipe(); |
| 64 |
|
| 65 |
protected: |
| 66 |
virtual void onPollTimeout() override; |
| 67 |
virtual void onPipeIo(NamedPipe &namedPipe) override; |
| 68 |
|
| 69 |
private: |
| 70 |
void autoClosePipesForShutdown(); |
| 71 |
std::unique_ptr<Win32ConsoleBuffer> openPrimaryBuffer(); |
| 72 |
void resizeWindow(int cols, int rows); |
| 73 |
void scrapeBuffers(); |
| 74 |
void syncConsoleTitle(); |
| 75 |
|
| 76 |
private: |
| 77 |
const bool m_useConerr; |
| 78 |
const bool m_plainMode; |
| 79 |
const int m_mouseMode; |
| 80 |
Win32Console m_console; |
| 81 |
std::unique_ptr<Scraper> m_primaryScraper; |
| 82 |
std::unique_ptr<Scraper> m_errorScraper; |
| 83 |
std::unique_ptr<Win32ConsoleBuffer> m_errorBuffer; |
| 84 |
NamedPipe *m_controlPipe = nullptr; |
| 85 |
NamedPipe *m_coninPipe = nullptr; |
| 86 |
NamedPipe *m_conoutPipe = nullptr; |
| 87 |
NamedPipe *m_conerrPipe = nullptr; |
| 88 |
bool m_autoShutdown = false; |
| 89 |
bool m_exitAfterShutdown = false; |
| 90 |
bool m_closingOutputPipes = false; |
| 91 |
std::unique_ptr<ConsoleInput> m_consoleInput; |
| 92 |
HANDLE m_childProcess = nullptr; |
| 93 |
|
| 94 |
// If the title is initialized to the empty string, then cmd.exe will |
| 95 |
// sometimes print this error: |
| 96 |
// Not enough storage is available to process this command. |
| 97 |
// It happens on Windows 7 when logged into a Cygwin SSH session, for |
| 98 |
// example. Using a title of a single space character avoids the problem. |
| 99 |
// See https://github.com/rprichard/winpty/issues/74. |
| 100 |
std::wstring m_currentTitle = L" "; |
| 101 |
}; |
| 102 |
|
| 103 |
#endif // AGENT_H |