Improvements to {Read,Write}Buffer classes and NamedPipe
* Avoid std::stringstream because it's inefficient/bloated. * Explicitly handle decode errors, by throwing ReadBuffer::DecodeError. For the time being, this exception can only be thrown in the agent, but I anticipate it being thrown in the libwinpty code eventually. * Catch encoding errors a bit better. * Be more precise about the integer types used for length. e.g. Use 64 bits on the wire, but otherwise use size_t/uint64_t where appropriate. * Replace memcpy with std::copy to make MSVC's /SDL security checks happy. It's a somewhat silly/arbitrary change. I don't think it will affect performance, but I haven't benchmarked it. * This commit changes the RPC protocol that libwinpty uses to communicate with winpty-agent.
This commit is contained in:
parent
51893bad84
commit
9839b8581a
@ -242,27 +242,36 @@ void Agent::pollControlSocket()
|
||||
}
|
||||
|
||||
while (true) {
|
||||
int32_t packetSize;
|
||||
int size = m_controlSocket->peek((char*)&packetSize, sizeof(int32_t));
|
||||
if (size < (int)sizeof(int32_t))
|
||||
break;
|
||||
int totalSize = sizeof(int32_t) + packetSize;
|
||||
if (m_controlSocket->bytesAvailable() < totalSize) {
|
||||
if (m_controlSocket->readBufferSize() < totalSize)
|
||||
m_controlSocket->setReadBufferSize(totalSize);
|
||||
uint64_t packetSize = 0;
|
||||
const auto amt1 =
|
||||
m_controlSocket->peek(&packetSize, sizeof(packetSize));
|
||||
if (amt1 < sizeof(packetSize)) {
|
||||
break;
|
||||
}
|
||||
std::string packetData = m_controlSocket->read(totalSize);
|
||||
ASSERT((int)packetData.size() == totalSize);
|
||||
ReadBuffer buffer(packetData);
|
||||
buffer.getInt(); // Discard the size.
|
||||
handlePacket(buffer);
|
||||
ASSERT(packetSize >= sizeof(packetSize) && packetSize <= SIZE_MAX);
|
||||
if (m_controlSocket->bytesAvailable() < packetSize) {
|
||||
if (m_controlSocket->readBufferSize() < packetSize) {
|
||||
m_controlSocket->setReadBufferSize(packetSize);
|
||||
}
|
||||
break;
|
||||
}
|
||||
std::vector<char> packetData;
|
||||
packetData.resize(packetSize);
|
||||
const auto amt2 = m_controlSocket->read(packetData.data(), packetSize);
|
||||
ASSERT(amt2 == packetSize);
|
||||
try {
|
||||
ReadBuffer buffer(std::move(packetData));
|
||||
buffer.getRawValue<uint64_t>(); // Discard the size.
|
||||
handlePacket(buffer);
|
||||
} catch (const ReadBuffer::DecodeError &error) {
|
||||
ASSERT(false && "Decode error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Agent::handlePacket(ReadBuffer &packet)
|
||||
{
|
||||
int type = packet.getInt();
|
||||
int type = packet.getInt32();
|
||||
int32_t result = -1;
|
||||
switch (type) {
|
||||
case AgentMsg::Ping:
|
||||
@ -280,24 +289,24 @@ void Agent::handlePacket(ReadBuffer &packet)
|
||||
result = handleSetSizePacket(packet);
|
||||
break;
|
||||
case AgentMsg::GetExitCode:
|
||||
ASSERT(packet.eof());
|
||||
packet.assertEof();
|
||||
result = m_childExitCode;
|
||||
break;
|
||||
case AgentMsg::GetProcessId:
|
||||
ASSERT(packet.eof());
|
||||
packet.assertEof();
|
||||
if (m_childProcess == NULL)
|
||||
result = -1;
|
||||
else
|
||||
result = GetProcessId(m_childProcess);
|
||||
break;
|
||||
case AgentMsg::SetConsoleMode:
|
||||
m_terminal->setConsoleMode(packet.getInt());
|
||||
m_terminal->setConsoleMode(packet.getInt32());
|
||||
result = 0;
|
||||
break;
|
||||
default:
|
||||
trace("Unrecognized message, id:%d", type);
|
||||
}
|
||||
m_controlSocket->write((char*)&result, sizeof(result));
|
||||
m_controlSocket->write(&result, sizeof(result));
|
||||
}
|
||||
|
||||
int Agent::handleStartProcessPacket(ReadBuffer &packet)
|
||||
@ -310,7 +319,7 @@ int Agent::handleStartProcessPacket(ReadBuffer &packet)
|
||||
std::wstring cwd = packet.getWString();
|
||||
std::wstring env = packet.getWString();
|
||||
std::wstring desktop = packet.getWString();
|
||||
ASSERT(packet.eof());
|
||||
packet.assertEof();
|
||||
|
||||
LPCWSTR programArg = program.empty() ? NULL : program.c_str();
|
||||
std::vector<wchar_t> cmdlineCopy;
|
||||
@ -352,16 +361,16 @@ int Agent::handleStartProcessPacket(ReadBuffer &packet)
|
||||
|
||||
int Agent::handleSetSizePacket(ReadBuffer &packet)
|
||||
{
|
||||
int cols = packet.getInt();
|
||||
int rows = packet.getInt();
|
||||
ASSERT(packet.eof());
|
||||
int cols = packet.getInt32();
|
||||
int rows = packet.getInt32();
|
||||
packet.assertEof();
|
||||
resizeWindow(cols, rows);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Agent::pollDataSocket()
|
||||
{
|
||||
const std::string newData = m_dataSocket->readAll();
|
||||
const std::string newData = m_dataSocket->readAllToString();
|
||||
if (hasDebugFlag("input_separated_bytes")) {
|
||||
// This debug flag is intended to help with testing incomplete escape
|
||||
// sequences and multibyte UTF-8 encodings. (I wonder if the normal
|
||||
|
@ -43,25 +43,30 @@ NamedPipe::~NamedPipe()
|
||||
// Returns true if anything happens (data received, data sent, pipe error).
|
||||
bool NamedPipe::serviceIo(std::vector<HANDLE> *waitHandles)
|
||||
{
|
||||
if (m_handle == NULL)
|
||||
const auto kError = ServiceResult::Error;
|
||||
const auto kProgress = ServiceResult::Progress;
|
||||
if (m_handle == NULL) {
|
||||
return false;
|
||||
int readBytes = m_inputWorker->service();
|
||||
int writeBytes = m_outputWorker->service();
|
||||
if (readBytes == -1 || writeBytes == -1) {
|
||||
}
|
||||
const auto readProgress = m_inputWorker->service();
|
||||
const auto writeProgress = m_outputWorker->service();
|
||||
if (readProgress == kError || writeProgress == kError) {
|
||||
closePipe();
|
||||
return true;
|
||||
}
|
||||
if (m_inputWorker->getWaitEvent() != NULL)
|
||||
if (m_inputWorker->getWaitEvent() != NULL) {
|
||||
waitHandles->push_back(m_inputWorker->getWaitEvent());
|
||||
if (m_outputWorker->getWaitEvent() != NULL)
|
||||
}
|
||||
if (m_outputWorker->getWaitEvent() != NULL) {
|
||||
waitHandles->push_back(m_outputWorker->getWaitEvent());
|
||||
return readBytes > 0 || writeBytes > 0;
|
||||
}
|
||||
return readProgress == kProgress || writeProgress == kProgress;
|
||||
}
|
||||
|
||||
NamedPipe::IoWorker::IoWorker(NamedPipe *namedPipe) :
|
||||
m_namedPipe(namedPipe),
|
||||
m_pending(false),
|
||||
m_currentIoSize(-1)
|
||||
m_currentIoSize(0)
|
||||
{
|
||||
m_event = CreateEventW(NULL, TRUE, FALSE, NULL);
|
||||
ASSERT(m_event != NULL);
|
||||
@ -72,11 +77,11 @@ NamedPipe::IoWorker::~IoWorker()
|
||||
CloseHandle(m_event);
|
||||
}
|
||||
|
||||
int NamedPipe::IoWorker::service()
|
||||
NamedPipe::ServiceResult NamedPipe::IoWorker::service()
|
||||
{
|
||||
int progress = 0;
|
||||
ServiceResult progress = ServiceResult::NoProgress;
|
||||
if (m_pending) {
|
||||
DWORD actual;
|
||||
DWORD actual = 0;
|
||||
BOOL ret = GetOverlappedResult(m_namedPipe->m_handle, &m_over, &actual, FALSE);
|
||||
if (!ret) {
|
||||
if (GetLastError() == ERROR_IO_INCOMPLETE) {
|
||||
@ -84,17 +89,17 @@ int NamedPipe::IoWorker::service()
|
||||
return progress;
|
||||
} else {
|
||||
// Pipe error.
|
||||
return -1;
|
||||
return ServiceResult::Error;
|
||||
}
|
||||
}
|
||||
ResetEvent(m_event);
|
||||
m_pending = false;
|
||||
completeIo(actual);
|
||||
m_currentIoSize = -1;
|
||||
progress += actual;
|
||||
m_currentIoSize = 0;
|
||||
progress = ServiceResult::Progress;
|
||||
}
|
||||
int nextSize;
|
||||
bool isRead;
|
||||
DWORD nextSize = 0;
|
||||
bool isRead = false;
|
||||
while (shouldIssueIo(&nextSize, &isRead)) {
|
||||
m_currentIoSize = nextSize;
|
||||
DWORD actual = 0;
|
||||
@ -110,13 +115,13 @@ int NamedPipe::IoWorker::service()
|
||||
return progress;
|
||||
} else {
|
||||
// Pipe error.
|
||||
return -1;
|
||||
return ServiceResult::Error;
|
||||
}
|
||||
}
|
||||
ResetEvent(m_event);
|
||||
completeIo(actual);
|
||||
m_currentIoSize = -1;
|
||||
progress += actual;
|
||||
m_currentIoSize = 0;
|
||||
progress = ServiceResult::Progress;
|
||||
}
|
||||
return progress;
|
||||
}
|
||||
@ -138,17 +143,17 @@ HANDLE NamedPipe::IoWorker::getWaitEvent()
|
||||
return m_pending ? m_event : NULL;
|
||||
}
|
||||
|
||||
void NamedPipe::InputWorker::completeIo(int size)
|
||||
void NamedPipe::InputWorker::completeIo(DWORD size)
|
||||
{
|
||||
m_namedPipe->m_inQueue.append(m_buffer, size);
|
||||
}
|
||||
|
||||
bool NamedPipe::InputWorker::shouldIssueIo(int *size, bool *isRead)
|
||||
bool NamedPipe::InputWorker::shouldIssueIo(DWORD *size, bool *isRead)
|
||||
{
|
||||
*isRead = true;
|
||||
if (m_namedPipe->isClosed()) {
|
||||
return false;
|
||||
} else if ((int)m_namedPipe->m_inQueue.size() < m_namedPipe->readBufferSize()) {
|
||||
} else if (m_namedPipe->m_inQueue.size() < m_namedPipe->readBufferSize()) {
|
||||
*size = kIoSize;
|
||||
return true;
|
||||
} else {
|
||||
@ -156,18 +161,19 @@ bool NamedPipe::InputWorker::shouldIssueIo(int *size, bool *isRead)
|
||||
}
|
||||
}
|
||||
|
||||
void NamedPipe::OutputWorker::completeIo(int size)
|
||||
void NamedPipe::OutputWorker::completeIo(DWORD size)
|
||||
{
|
||||
ASSERT(size == m_currentIoSize);
|
||||
}
|
||||
|
||||
bool NamedPipe::OutputWorker::shouldIssueIo(int *size, bool *isRead)
|
||||
bool NamedPipe::OutputWorker::shouldIssueIo(DWORD *size, bool *isRead)
|
||||
{
|
||||
*isRead = false;
|
||||
if (!m_namedPipe->m_outQueue.empty()) {
|
||||
int writeSize = std::min((int)m_namedPipe->m_outQueue.size(), (int)kIoSize);
|
||||
memcpy(m_buffer, m_namedPipe->m_outQueue.data(), writeSize);
|
||||
m_namedPipe->m_outQueue.erase(0, writeSize);
|
||||
auto &out = m_namedPipe->m_outQueue;
|
||||
const DWORD writeSize = std::min<size_t>(out.size(), kIoSize);
|
||||
std::copy(&out[0], &out[writeSize], m_buffer);
|
||||
out.erase(0, writeSize);
|
||||
*size = writeSize;
|
||||
return true;
|
||||
} else {
|
||||
@ -175,7 +181,7 @@ bool NamedPipe::OutputWorker::shouldIssueIo(int *size, bool *isRead)
|
||||
}
|
||||
}
|
||||
|
||||
int NamedPipe::OutputWorker::getPendingIoSize()
|
||||
DWORD NamedPipe::OutputWorker::getPendingIoSize()
|
||||
{
|
||||
return m_pending ? m_currentIoSize : 0;
|
||||
}
|
||||
@ -199,17 +205,18 @@ bool NamedPipe::connectToServer(LPCWSTR pipeName)
|
||||
return true;
|
||||
}
|
||||
|
||||
int NamedPipe::bytesToSend()
|
||||
size_t NamedPipe::bytesToSend()
|
||||
{
|
||||
int ret = m_outQueue.size();
|
||||
if (m_outputWorker != NULL)
|
||||
auto ret = m_outQueue.size();
|
||||
if (m_outputWorker != NULL) {
|
||||
ret += m_outputWorker->getPendingIoSize();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void NamedPipe::write(const void *data, int size)
|
||||
void NamedPipe::write(const void *data, size_t size)
|
||||
{
|
||||
m_outQueue.append((const char*)data, size);
|
||||
m_outQueue.append(reinterpret_cast<const char*>(data), size);
|
||||
}
|
||||
|
||||
void NamedPipe::write(const char *text)
|
||||
@ -217,37 +224,45 @@ void NamedPipe::write(const char *text)
|
||||
write(text, strlen(text));
|
||||
}
|
||||
|
||||
int NamedPipe::readBufferSize()
|
||||
size_t NamedPipe::readBufferSize()
|
||||
{
|
||||
return m_readBufferSize;
|
||||
}
|
||||
|
||||
void NamedPipe::setReadBufferSize(int size)
|
||||
void NamedPipe::setReadBufferSize(size_t size)
|
||||
{
|
||||
m_readBufferSize = size;
|
||||
}
|
||||
|
||||
int NamedPipe::bytesAvailable()
|
||||
size_t NamedPipe::bytesAvailable()
|
||||
{
|
||||
return m_inQueue.size();
|
||||
}
|
||||
|
||||
int NamedPipe::peek(void *data, int size)
|
||||
size_t NamedPipe::peek(void *data, size_t size)
|
||||
{
|
||||
int ret = std::min(size, (int)m_inQueue.size());
|
||||
memcpy(data, m_inQueue.data(), ret);
|
||||
const auto out = reinterpret_cast<char*>(data);
|
||||
const size_t ret = std::min(size, m_inQueue.size());
|
||||
std::copy(&m_inQueue[0], &m_inQueue[size], out);
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string NamedPipe::read(int size)
|
||||
size_t NamedPipe::read(void *data, size_t size)
|
||||
{
|
||||
int retSize = std::min(size, (int)m_inQueue.size());
|
||||
size_t ret = peek(data, size);
|
||||
m_inQueue.erase(0, ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string NamedPipe::readToString(size_t size)
|
||||
{
|
||||
size_t retSize = std::min(size, m_inQueue.size());
|
||||
std::string ret = m_inQueue.substr(0, retSize);
|
||||
m_inQueue.erase(0, retSize);
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string NamedPipe::readAll()
|
||||
std::string NamedPipe::readAllToString()
|
||||
{
|
||||
std::string ret = m_inQueue;
|
||||
m_inQueue.clear();
|
||||
|
@ -36,25 +36,27 @@ private:
|
||||
~NamedPipe();
|
||||
bool serviceIo(std::vector<HANDLE> *waitHandles);
|
||||
|
||||
enum class ServiceResult { NoProgress, Error, Progress };
|
||||
|
||||
private:
|
||||
class IoWorker
|
||||
{
|
||||
public:
|
||||
IoWorker(NamedPipe *namedPipe);
|
||||
virtual ~IoWorker();
|
||||
int service();
|
||||
ServiceResult service();
|
||||
void waitForCanceledIo();
|
||||
HANDLE getWaitEvent();
|
||||
protected:
|
||||
NamedPipe *m_namedPipe;
|
||||
bool m_pending;
|
||||
int m_currentIoSize;
|
||||
DWORD m_currentIoSize;
|
||||
HANDLE m_event;
|
||||
OVERLAPPED m_over;
|
||||
enum { kIoSize = 64 * 1024 };
|
||||
char m_buffer[kIoSize];
|
||||
virtual void completeIo(int size) = 0;
|
||||
virtual bool shouldIssueIo(int *size, bool *isRead) = 0;
|
||||
virtual void completeIo(DWORD size) = 0;
|
||||
virtual bool shouldIssueIo(DWORD *size, bool *isRead) = 0;
|
||||
};
|
||||
|
||||
class InputWorker : public IoWorker
|
||||
@ -62,37 +64,38 @@ private:
|
||||
public:
|
||||
InputWorker(NamedPipe *namedPipe) : IoWorker(namedPipe) {}
|
||||
protected:
|
||||
virtual void completeIo(int size);
|
||||
virtual bool shouldIssueIo(int *size, bool *isRead);
|
||||
virtual void completeIo(DWORD size);
|
||||
virtual bool shouldIssueIo(DWORD *size, bool *isRead);
|
||||
};
|
||||
|
||||
class OutputWorker : public IoWorker
|
||||
{
|
||||
public:
|
||||
OutputWorker(NamedPipe *namedPipe) : IoWorker(namedPipe) {}
|
||||
int getPendingIoSize();
|
||||
DWORD getPendingIoSize();
|
||||
protected:
|
||||
virtual void completeIo(int size);
|
||||
virtual bool shouldIssueIo(int *size, bool *isRead);
|
||||
virtual void completeIo(DWORD size);
|
||||
virtual bool shouldIssueIo(DWORD *size, bool *isRead);
|
||||
};
|
||||
|
||||
public:
|
||||
bool connectToServer(LPCWSTR pipeName);
|
||||
int bytesToSend();
|
||||
void write(const void *data, int size);
|
||||
size_t bytesToSend();
|
||||
void write(const void *data, size_t size);
|
||||
void write(const char *text);
|
||||
int readBufferSize();
|
||||
void setReadBufferSize(int size);
|
||||
int bytesAvailable();
|
||||
int peek(void *data, int size);
|
||||
std::string read(int size);
|
||||
std::string readAll();
|
||||
size_t readBufferSize();
|
||||
void setReadBufferSize(size_t size);
|
||||
size_t bytesAvailable();
|
||||
size_t peek(void *data, size_t size);
|
||||
size_t read(void *data, size_t size);
|
||||
std::string readToString(size_t size);
|
||||
std::string readAllToString();
|
||||
void closePipe();
|
||||
bool isClosed();
|
||||
|
||||
private:
|
||||
// Input/output buffers
|
||||
int m_readBufferSize;
|
||||
size_t m_readBufferSize;
|
||||
std::string m_inQueue;
|
||||
std::string m_outQueue;
|
||||
HANDLE m_handle;
|
||||
|
@ -313,7 +313,7 @@ void Terminal::sendLine(int64_t line, const CHAR_INFO *lineData, int width)
|
||||
moveTerminalToLine(line);
|
||||
|
||||
m_termLine.clear();
|
||||
int trimmedLineLength = 0;
|
||||
size_t trimmedLineLength = 0;
|
||||
bool alreadyErasedLine = false;
|
||||
|
||||
int cellCount = 1;
|
||||
|
@ -36,6 +36,7 @@ AGENT_OBJECTS = \
|
||||
build/agent/agent/Terminal.o \
|
||||
build/agent/agent/Win32Console.o \
|
||||
build/agent/agent/main.o \
|
||||
build/agent/shared/Buffer.o \
|
||||
build/agent/shared/DebugClient.o \
|
||||
build/agent/shared/WinptyAssert.o \
|
||||
build/agent/shared/WinptyVersion.o \
|
||||
|
@ -24,6 +24,7 @@ $(eval $(call def_mingw_target,libwinpty,-DCOMPILING_WINPTY_DLL))
|
||||
|
||||
LIBWINPTY_OBJECTS = \
|
||||
build/libwinpty/libwinpty/winpty.o \
|
||||
build/libwinpty/shared/Buffer.o \
|
||||
build/libwinpty/shared/DebugClient.o \
|
||||
build/libwinpty/shared/WinptyAssert.o
|
||||
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <limits>
|
||||
#include "../shared/DebugClient.h"
|
||||
#include "../shared/AgentMsg.h"
|
||||
#include "../shared/Buffer.h"
|
||||
@ -114,15 +115,22 @@ static bool connectNamedPipe(HANDLE handle, bool overlapped)
|
||||
return success;
|
||||
}
|
||||
|
||||
static void writePacket(winpty_t *pc, const WriteBuffer &packet)
|
||||
static inline WriteBuffer newPacket()
|
||||
{
|
||||
std::string payload = packet.str();
|
||||
int32_t payloadSize = payload.size();
|
||||
DWORD actual;
|
||||
BOOL success = WriteFile(pc->controlPipe, &payloadSize, sizeof(int32_t), &actual, NULL);
|
||||
assert(success && actual == sizeof(int32_t));
|
||||
success = WriteFile(pc->controlPipe, payload.c_str(), payloadSize, &actual, NULL);
|
||||
assert(success && (int32_t)actual == payloadSize);
|
||||
WriteBuffer packet;
|
||||
packet.putRawValue<uint64_t>(0); // Reserve space for size.
|
||||
return packet;
|
||||
}
|
||||
|
||||
static void writePacket(winpty_t *pc, WriteBuffer &packet)
|
||||
{
|
||||
packet.replaceRawValue<uint64_t>(0, packet.buf().size());
|
||||
const auto &buf = packet.buf();
|
||||
DWORD actual = 0;
|
||||
ASSERT(buf.size() <= std::numeric_limits<DWORD>::max());
|
||||
const BOOL success = WriteFile(pc->controlPipe, buf.data(), buf.size(),
|
||||
&actual, nullptr);
|
||||
ASSERT(success && actual == buf.size());
|
||||
}
|
||||
|
||||
static int32_t readInt32(winpty_t *pc)
|
||||
@ -338,8 +346,8 @@ WINPTY_API winpty_t *winpty_open(int cols, int rows)
|
||||
// a dummy message on the control pipe, we should confirm that something
|
||||
// trusted (i.e. the agent we just started) successfully connected and wrote
|
||||
// to one of our pipes.
|
||||
WriteBuffer packet;
|
||||
packet.putInt(AgentMsg::Ping);
|
||||
auto packet = newPacket();
|
||||
packet.putInt32(AgentMsg::Ping);
|
||||
writePacket(pc, packet);
|
||||
if (readInt32(pc) != 0) {
|
||||
delete pc;
|
||||
@ -387,8 +395,8 @@ WINPTY_API int winpty_start_process(winpty_t *pc,
|
||||
const wchar_t *cwd,
|
||||
const wchar_t *env)
|
||||
{
|
||||
WriteBuffer packet;
|
||||
packet.putInt(AgentMsg::StartProcess);
|
||||
auto packet = newPacket();
|
||||
packet.putInt32(AgentMsg::StartProcess);
|
||||
packet.putWString(appname ? appname : L"");
|
||||
packet.putWString(cmdline ? cmdline : L"");
|
||||
packet.putWString(cwd ? cwd : L"");
|
||||
@ -413,16 +421,16 @@ WINPTY_API int winpty_start_process(winpty_t *pc,
|
||||
|
||||
WINPTY_API int winpty_get_exit_code(winpty_t *pc)
|
||||
{
|
||||
WriteBuffer packet;
|
||||
packet.putInt(AgentMsg::GetExitCode);
|
||||
auto packet = newPacket();
|
||||
packet.putInt32(AgentMsg::GetExitCode);
|
||||
writePacket(pc, packet);
|
||||
return readInt32(pc);
|
||||
}
|
||||
|
||||
WINPTY_API int winpty_get_process_id(winpty_t *pc)
|
||||
{
|
||||
WriteBuffer packet;
|
||||
packet.putInt(AgentMsg::GetProcessId);
|
||||
auto packet = newPacket();
|
||||
packet.putInt32(AgentMsg::GetProcessId);
|
||||
writePacket(pc, packet);
|
||||
return readInt32(pc);
|
||||
}
|
||||
@ -434,10 +442,10 @@ WINPTY_API HANDLE winpty_get_data_pipe(winpty_t *pc)
|
||||
|
||||
WINPTY_API int winpty_set_size(winpty_t *pc, int cols, int rows)
|
||||
{
|
||||
WriteBuffer packet;
|
||||
packet.putInt(AgentMsg::SetSize);
|
||||
packet.putInt(cols);
|
||||
packet.putInt(rows);
|
||||
auto packet = newPacket();
|
||||
packet.putInt32(AgentMsg::SetSize);
|
||||
packet.putInt32(cols);
|
||||
packet.putInt32(rows);
|
||||
writePacket(pc, packet);
|
||||
return readInt32(pc);
|
||||
}
|
||||
@ -451,9 +459,9 @@ WINPTY_API void winpty_close(winpty_t *pc)
|
||||
|
||||
WINPTY_API int winpty_set_console_mode(winpty_t *pc, int mode)
|
||||
{
|
||||
WriteBuffer packet;
|
||||
packet.putInt(AgentMsg::SetConsoleMode);
|
||||
packet.putInt(mode);
|
||||
auto packet = newPacket();
|
||||
packet.putInt32(AgentMsg::SetConsoleMode);
|
||||
packet.putInt32(mode);
|
||||
writePacket(pc, packet);
|
||||
return readInt32(pc);
|
||||
}
|
||||
|
103
src/shared/Buffer.cc
Executable file
103
src/shared/Buffer.cc
Executable file
@ -0,0 +1,103 @@
|
||||
// Copyright (c) 2011-2016 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.
|
||||
|
||||
#include "Buffer.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "DebugClient.h"
|
||||
#include "WinptyAssert.h"
|
||||
|
||||
// Define the READ_BUFFER_CHECK() macro. It *must* evaluate its condition,
|
||||
// exactly once.
|
||||
#define READ_BUFFER_CHECK(cond) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
trace("decode error: %s", #cond); \
|
||||
throw DecodeError(); \
|
||||
} \
|
||||
} while (false)
|
||||
|
||||
enum class Piece : uint8_t { Int32, Int64, WString };
|
||||
|
||||
void WriteBuffer::putRawData(const void *data, size_t len) {
|
||||
const auto p = reinterpret_cast<const char*>(data);
|
||||
m_buf.insert(m_buf.end(), p, p + len);
|
||||
}
|
||||
|
||||
void WriteBuffer::replaceRawData(size_t pos, const void *data, size_t len) {
|
||||
ASSERT(pos <= m_buf.size() && len <= m_buf.size() - pos);
|
||||
const auto p = reinterpret_cast<const char*>(data);
|
||||
std::copy(p, p + len, m_buf.begin());
|
||||
}
|
||||
|
||||
void WriteBuffer::putInt32(int32_t i) {
|
||||
putRawValue(Piece::Int32);
|
||||
putRawValue(i);
|
||||
}
|
||||
|
||||
void WriteBuffer::putInt64(int64_t i) {
|
||||
putRawValue(Piece::Int64);
|
||||
putRawValue(i);
|
||||
}
|
||||
|
||||
// len is in characters, excluding NUL, i.e. the number of wchar_t elements
|
||||
void WriteBuffer::putWString(const wchar_t *str, size_t len) {
|
||||
putRawValue(Piece::WString);
|
||||
putRawValue(static_cast<uint64_t>(len));
|
||||
putRawData(str, sizeof(wchar_t) * len);
|
||||
}
|
||||
|
||||
void ReadBuffer::getRawData(void *data, size_t len) {
|
||||
ASSERT(m_off <= m_buf.size());
|
||||
READ_BUFFER_CHECK(len <= m_buf.size() - m_off);
|
||||
const char *const inp = &m_buf[m_off];
|
||||
std::copy(inp, inp + len, reinterpret_cast<char*>(data));
|
||||
m_off += len;
|
||||
}
|
||||
|
||||
int32_t ReadBuffer::getInt32() {
|
||||
READ_BUFFER_CHECK(getRawValue<Piece>() == Piece::Int32);
|
||||
return getRawValue<int32_t>();
|
||||
}
|
||||
|
||||
int64_t ReadBuffer::getInt64() {
|
||||
READ_BUFFER_CHECK(getRawValue<Piece>() == Piece::Int64);
|
||||
return getRawValue<int64_t>();
|
||||
}
|
||||
|
||||
std::wstring ReadBuffer::getWString() {
|
||||
READ_BUFFER_CHECK(getRawValue<Piece>() == Piece::WString);
|
||||
const uint64_t charLen = getRawValue<uint64_t>();
|
||||
READ_BUFFER_CHECK(charLen <= SIZE_MAX / sizeof(wchar_t));
|
||||
// To be strictly conforming, we can't use the convenient wstring
|
||||
// constructor, because the string in m_buf mightn't be aligned.
|
||||
std::wstring ret;
|
||||
if (charLen > 0) {
|
||||
const size_t byteLen = charLen * sizeof(wchar_t);
|
||||
ret.resize(charLen);
|
||||
getRawData(&ret[0], byteLen);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ReadBuffer::assertEof() {
|
||||
READ_BUFFER_CHECK(m_off == m_buf.size());
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
// Copyright (c) 2011-2012 Ryan Prichard
|
||||
// Copyright (c) 2011-2016 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
|
||||
@ -21,79 +21,83 @@
|
||||
#ifndef BUFFER_H
|
||||
#define BUFFER_H
|
||||
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
class WriteBuffer
|
||||
{
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "WinptyException.h"
|
||||
|
||||
class WriteBuffer {
|
||||
private:
|
||||
std::stringstream ss;
|
||||
std::vector<char> m_buf;
|
||||
|
||||
public:
|
||||
void putInt(int i);
|
||||
void putWString(const std::wstring &str);
|
||||
void putWString(const wchar_t *str);
|
||||
std::string str() const;
|
||||
WriteBuffer() {}
|
||||
|
||||
template <typename T> void putRawValue(const T &t) {
|
||||
putRawData(&t, sizeof(t));
|
||||
}
|
||||
template <typename T> void replaceRawValue(size_t pos, const T &t) {
|
||||
replaceRawData(pos, &t, sizeof(t));
|
||||
}
|
||||
|
||||
void putRawData(const void *data, size_t len);
|
||||
void replaceRawData(size_t pos, const void *data, size_t len);
|
||||
void putInt32(int32_t i);
|
||||
void putInt64(int64_t i);
|
||||
void putWString(const wchar_t *str, size_t len);
|
||||
void putWString(const wchar_t *str) { putWString(str, wcslen(str)); }
|
||||
void putWString(const std::wstring &str) { putWString(str.data(), str.size()); }
|
||||
std::vector<char> &buf() { return m_buf; }
|
||||
|
||||
// MSVC 2013 does not generate these automatically, so help it out.
|
||||
WriteBuffer(WriteBuffer &&other) : m_buf(std::move(other.m_buf)) {}
|
||||
WriteBuffer &operator=(WriteBuffer &&other) {
|
||||
m_buf = std::move(other.m_buf);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
inline void WriteBuffer::putInt(int i)
|
||||
{
|
||||
ss.write((const char*)&i, sizeof(i));
|
||||
}
|
||||
|
||||
inline void WriteBuffer::putWString(const std::wstring &str)
|
||||
{
|
||||
putInt(str.size());
|
||||
ss.write((const char*)str.c_str(), sizeof(wchar_t) * str.size());
|
||||
}
|
||||
|
||||
inline void WriteBuffer::putWString(const wchar_t *str)
|
||||
{
|
||||
int len = wcslen(str);
|
||||
putInt(len);
|
||||
ss.write((const char*)str, sizeof(wchar_t) * len);
|
||||
}
|
||||
|
||||
inline std::string WriteBuffer::str() const
|
||||
{
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
class ReadBuffer
|
||||
{
|
||||
private:
|
||||
std::stringstream ss;
|
||||
class ReadBuffer {
|
||||
public:
|
||||
ReadBuffer(const std::string &packet);
|
||||
int getInt();
|
||||
class DecodeError : public WinptyException {
|
||||
virtual const wchar_t *what() {
|
||||
return L"DecodeError: RPC message decoding error";
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
std::vector<char> m_buf;
|
||||
size_t m_off = 0;
|
||||
|
||||
public:
|
||||
ReadBuffer(std::vector<char> &&buf) : m_buf(std::move(buf)) {}
|
||||
|
||||
template <typename T> T getRawValue() {
|
||||
T ret = {};
|
||||
getRawData(&ret, sizeof(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
void getRawData(void *data, size_t len);
|
||||
int32_t getRawInt32() { return getRawValue<int32_t>(); }
|
||||
int32_t getInt32();
|
||||
int64_t getInt64();
|
||||
std::wstring getWString();
|
||||
bool eof();
|
||||
void assertEof();
|
||||
|
||||
// MSVC 2013 does not generate these automatically, so help it out.
|
||||
ReadBuffer(ReadBuffer &&other) :
|
||||
m_buf(std::move(other.m_buf)), m_off(other.m_off) {}
|
||||
ReadBuffer &operator=(ReadBuffer &&other) {
|
||||
m_buf = std::move(other.m_buf);
|
||||
m_off = other.m_off;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
inline ReadBuffer::ReadBuffer(const std::string &packet) : ss(packet)
|
||||
{
|
||||
}
|
||||
|
||||
inline int ReadBuffer::getInt()
|
||||
{
|
||||
int i;
|
||||
ss.read((char*)&i, sizeof(i));
|
||||
return i;
|
||||
}
|
||||
|
||||
inline std::wstring ReadBuffer::getWString()
|
||||
{
|
||||
int len = getInt();
|
||||
wchar_t *tmp = new wchar_t[len];
|
||||
ss.read((char*)tmp, sizeof(wchar_t) * len);
|
||||
std::wstring ret(tmp, len);
|
||||
delete [] tmp;
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline bool ReadBuffer::eof()
|
||||
{
|
||||
ss.peek();
|
||||
return ss.eof();
|
||||
}
|
||||
|
||||
#endif /* BUFFER_H */
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include <wchar.h>
|
||||
|
||||
#include <array>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <new>
|
||||
#include <string>
|
||||
|
30
src/shared/WinptyException.h
Executable file
30
src/shared/WinptyException.h
Executable file
@ -0,0 +1,30 @@
|
||||
// Copyright (c) 2016 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.
|
||||
|
||||
#ifndef WINPTY_EXCEPTION_H
|
||||
#define WINPTY_EXCEPTION_H
|
||||
|
||||
class WinptyException {
|
||||
public:
|
||||
virtual const wchar_t *what() = 0;
|
||||
~WinptyException() {}
|
||||
};
|
||||
|
||||
#endif // WINPTY_EXCEPTION_H
|
@ -73,12 +73,14 @@
|
||||
'agent/main.cc',
|
||||
'shared/AgentMsg.h',
|
||||
'shared/Buffer.h',
|
||||
'shared/Buffer.cc',
|
||||
'shared/DebugClient.h',
|
||||
'shared/DebugClient.cc',
|
||||
'shared/OsModule.h',
|
||||
'shared/UnixCtrlChars.h',
|
||||
'shared/WinptyAssert.h',
|
||||
'shared/WinptyAssert.cc',
|
||||
'shared/WinptyException.h',
|
||||
'shared/WinptyVersion.h',
|
||||
'shared/WinptyVersion.cc',
|
||||
'shared/winpty_snprintf.h',
|
||||
@ -103,10 +105,12 @@
|
||||
'libwinpty/winpty.cc',
|
||||
'shared/AgentMsg.h',
|
||||
'shared/Buffer.h',
|
||||
'shared/Buffer.cc',
|
||||
'shared/DebugClient.h',
|
||||
'shared/DebugClient.cc',
|
||||
'shared/WinptyAssert.h',
|
||||
'shared/WinptyAssert.cc',
|
||||
'shared/WinptyException.h',
|
||||
'shared/winpty_snprintf.h',
|
||||
],
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user