2012-03-23 10:11:34 +00:00
|
|
|
// Copyright (c) 2011-2012 Ryan Prichard
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
|
2011-10-31 09:19:18 +00:00
|
|
|
#ifndef AGENT_H
|
|
|
|
#define AGENT_H
|
|
|
|
|
2014-05-12 09:53:44 +00:00
|
|
|
#include <string>
|
2011-10-31 09:19:18 +00:00
|
|
|
#include <windows.h>
|
2012-03-14 05:14:39 +00:00
|
|
|
#include "EventLoop.h"
|
2012-03-20 09:04:46 +00:00
|
|
|
#include "DsrSender.h"
|
2011-10-31 09:19:18 +00:00
|
|
|
|
2011-11-17 11:00:02 +00:00
|
|
|
class Win32Console;
|
2012-03-20 04:30:16 +00:00
|
|
|
class ConsoleInput;
|
2011-11-22 10:31:47 +00:00
|
|
|
class Terminal;
|
2012-02-20 09:23:46 +00:00
|
|
|
class ReadBuffer;
|
2012-03-14 05:14:39 +00:00
|
|
|
class NamedPipe;
|
2011-10-31 09:19:18 +00:00
|
|
|
|
2011-11-19 10:26:37 +00:00
|
|
|
const int BUFFER_LINE_COUNT = 3000; // TODO: Use something like 9000.
|
|
|
|
const int MAX_CONSOLE_WIDTH = 500;
|
|
|
|
|
2012-03-20 09:04:46 +00:00
|
|
|
class Agent : public EventLoop, public DsrSender
|
2011-10-31 09:19:18 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-03-14 05:14:39 +00:00
|
|
|
Agent(LPCWSTR controlPipeName,
|
|
|
|
LPCWSTR dataPipeName,
|
|
|
|
int initialCols,
|
|
|
|
int initialRows);
|
2012-03-20 09:04:46 +00:00
|
|
|
virtual ~Agent();
|
|
|
|
void sendDsr();
|
2011-10-31 09:19:18 +00:00
|
|
|
|
2011-11-21 10:03:40 +00:00
|
|
|
private:
|
2012-03-14 05:14:39 +00:00
|
|
|
NamedPipe *makeSocket(LPCWSTR pipeName);
|
2011-11-22 10:31:47 +00:00
|
|
|
void resetConsoleTracking(bool sendClear = true);
|
2011-11-21 10:03:40 +00:00
|
|
|
|
2012-03-14 05:39:14 +00:00
|
|
|
private:
|
2012-03-14 06:27:21 +00:00
|
|
|
void pollControlSocket();
|
2012-02-20 09:23:46 +00:00
|
|
|
void handlePacket(ReadBuffer &packet);
|
2012-02-20 11:30:43 +00:00
|
|
|
int handleStartProcessPacket(ReadBuffer &packet);
|
|
|
|
int handleSetSizePacket(ReadBuffer &packet);
|
2012-03-14 06:27:21 +00:00
|
|
|
void pollDataSocket();
|
2012-03-14 05:14:39 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void onPollTimeout();
|
2012-03-14 06:27:21 +00:00
|
|
|
virtual void onPipeIo(NamedPipe *namedPipe);
|
2011-11-16 11:25:03 +00:00
|
|
|
|
|
|
|
private:
|
Record a high-water-mark of "dirty" lines in the console screen buffer
and avoid sending lines in the window that aren't dirty yet.
The idea is that the bottom of the console screen commonly has blank lines
that do not need to be sent to the client, because nothing has been written
there yet. For example, when the console first appears, there is typically
a command-line prompt at the top of the window. When a console is expanded
vertically, blank lines commonly appear at the bottom.
Other misc changes to the output:
- When we send one later, also send all following lines.
- When we save a line into m_bufferData, fill in the rest of the line with
spaces. This way, it's possible to expand the console horizontally
without the agent resending all the lines.
- If nothing on the console has changed, then leave the cursor alone.
Don't hide, show, or move it.
2011-11-21 08:25:14 +00:00
|
|
|
void markEntireWindowDirty();
|
|
|
|
void scanForDirtyLines();
|
2011-11-17 11:00:02 +00:00
|
|
|
void resizeWindow(int cols, int rows);
|
2011-11-12 09:42:33 +00:00
|
|
|
void scrapeOutput();
|
2011-11-19 10:26:37 +00:00
|
|
|
void freezeConsole();
|
|
|
|
void unfreezeConsole();
|
|
|
|
void syncMarkerText(CHAR_INFO *output);
|
|
|
|
int findSyncMarker();
|
|
|
|
void createSyncMarker(int row);
|
|
|
|
|
2011-10-31 09:19:18 +00:00
|
|
|
private:
|
2011-11-17 11:00:02 +00:00
|
|
|
Win32Console *m_console;
|
2012-03-14 05:14:39 +00:00
|
|
|
NamedPipe *m_controlSocket;
|
|
|
|
NamedPipe *m_dataSocket;
|
2012-03-14 06:27:21 +00:00
|
|
|
bool m_closingDataSocket;
|
2011-11-22 10:31:47 +00:00
|
|
|
Terminal *m_terminal;
|
2012-03-20 04:30:16 +00:00
|
|
|
ConsoleInput *m_consoleInput;
|
2012-02-20 11:30:43 +00:00
|
|
|
HANDLE m_childProcess;
|
|
|
|
int m_childExitCode;
|
2011-11-26 02:34:33 +00:00
|
|
|
|
2011-11-19 10:26:37 +00:00
|
|
|
int m_syncRow;
|
|
|
|
int m_syncCounter;
|
|
|
|
|
|
|
|
int m_scrapedLineCount;
|
|
|
|
int m_scrolledCount;
|
|
|
|
int m_maxBufferedLine;
|
|
|
|
CHAR_INFO (*m_bufferData)[MAX_CONSOLE_WIDTH];
|
Record a high-water-mark of "dirty" lines in the console screen buffer
and avoid sending lines in the window that aren't dirty yet.
The idea is that the bottom of the console screen commonly has blank lines
that do not need to be sent to the client, because nothing has been written
there yet. For example, when the console first appears, there is typically
a command-line prompt at the top of the window. When a console is expanded
vertically, blank lines commonly appear at the bottom.
Other misc changes to the output:
- When we send one later, also send all following lines.
- When we save a line into m_bufferData, fill in the rest of the line with
spaces. This way, it's possible to expand the console horizontally
without the agent resending all the lines.
- If nothing on the console has changed, then leave the cursor alone.
Don't hide, show, or move it.
2011-11-21 08:25:14 +00:00
|
|
|
int m_dirtyWindowTop;
|
|
|
|
int m_dirtyLineCount;
|
2014-05-12 09:53:44 +00:00
|
|
|
|
|
|
|
std::wstring m_currentTitle;
|
2011-10-31 09:19:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // AGENT_H
|