mirror of
https://github.com/gnif/LookingGlass.git
synced 2026-08-09 08:41:31 +00:00
[idd] ipc: connect LGInput to LGIdd
Move the reusable named-pipe endpoint and shared support code into the LGCommon static library. Run a dedicated server in LGIdd and a reconnecting client in LGInput, with device-lifecycle handling and report framing. Refactor the helper pipe to use the same endpoint implementation.
This commit is contained in:
585
idd/LGCommon/CPipeEndpoint.cpp
Normal file
585
idd/LGCommon/CPipeEndpoint.cpp
Normal file
@@ -0,0 +1,585 @@
|
||||
/**
|
||||
* Looking Glass
|
||||
* Copyright © 2017-2026 The Looking Glass Authors
|
||||
* https://looking-glass.io
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 2 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
||||
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include "CPipeEndpoint.h"
|
||||
|
||||
#include "CDebug.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
|
||||
const DWORD CPipeEndpoint::CLIENT_RETRY_INITIAL_MS = 100;
|
||||
const DWORD CPipeEndpoint::CLIENT_RETRY_MAX_MS = 2000;
|
||||
const DWORD CPipeEndpoint::SERVER_RETRY_MS = 1000;
|
||||
const DWORD CPipeEndpoint::WRITE_TIMEOUT_MS = 250;
|
||||
const DWORD CPipeEndpoint::WAIT_FIRST_OBJECT_VALUE = 0;
|
||||
|
||||
bool CPipeEndpoint::IsDisconnectedError(DWORD error)
|
||||
{
|
||||
return error == ERROR_BROKEN_PIPE ||
|
||||
error == ERROR_NO_DATA ||
|
||||
error == ERROR_PIPE_NOT_CONNECTED;
|
||||
}
|
||||
|
||||
CPipeEndpoint::PipeIoResult CPipeEndpoint::WaitForOverlapped(
|
||||
HANDLE pipe,
|
||||
HANDLE ioEvent,
|
||||
OVERLAPPED * overlapped,
|
||||
DWORD * transferred,
|
||||
DWORD timeoutMs)
|
||||
{
|
||||
const HANDLE waitHandles[] = { ioEvent, m_stopEvent };
|
||||
const DWORD waitResult = WaitForMultipleObjects(
|
||||
_countof(waitHandles), waitHandles, FALSE, timeoutMs);
|
||||
|
||||
if (waitResult == WAIT_TIMEOUT)
|
||||
{
|
||||
CancelIoEx(pipe, overlapped);
|
||||
if (GetOverlappedResult(pipe, overlapped, transferred, TRUE))
|
||||
return PipeIoResult::Success;
|
||||
DEBUG_WARN("Named pipe write timed out");
|
||||
return PipeIoResult::Error;
|
||||
}
|
||||
|
||||
if (waitResult == WAIT_FIRST_OBJECT_VALUE + 1)
|
||||
{
|
||||
CancelIoEx(pipe, overlapped);
|
||||
GetOverlappedResult(pipe, overlapped, transferred, TRUE);
|
||||
return PipeIoResult::Stopped;
|
||||
}
|
||||
|
||||
if (waitResult != WAIT_FIRST_OBJECT_VALUE)
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to wait for named pipe I/O");
|
||||
CancelIoEx(pipe, overlapped);
|
||||
GetOverlappedResult(pipe, overlapped, transferred, TRUE);
|
||||
return PipeIoResult::Error;
|
||||
}
|
||||
|
||||
if (GetOverlappedResult(pipe, overlapped, transferred, FALSE))
|
||||
return PipeIoResult::Success;
|
||||
|
||||
const DWORD error = GetLastError();
|
||||
if (error == ERROR_OPERATION_ABORTED &&
|
||||
WaitForSingleObject(m_stopEvent, 0) == WAIT_FIRST_OBJECT_VALUE)
|
||||
return PipeIoResult::Stopped;
|
||||
|
||||
if (IsDisconnectedError(error))
|
||||
return PipeIoResult::Disconnected;
|
||||
|
||||
DEBUG_WARN_HR(error, "Named pipe I/O failed");
|
||||
return PipeIoResult::Error;
|
||||
}
|
||||
|
||||
CPipeEndpoint::PipeIoResult CPipeEndpoint::ReadMessage(
|
||||
HANDLE pipe,
|
||||
HANDLE ioEvent,
|
||||
void * message,
|
||||
DWORD messageSize,
|
||||
DWORD * bytesRead)
|
||||
{
|
||||
ResetEvent(ioEvent);
|
||||
OVERLAPPED overlapped = {};
|
||||
overlapped.hEvent = ioEvent;
|
||||
|
||||
if (ReadFile(pipe, message, messageSize, bytesRead, &overlapped))
|
||||
return PipeIoResult::Success;
|
||||
|
||||
const DWORD error = GetLastError();
|
||||
if (error == ERROR_IO_PENDING)
|
||||
return WaitForOverlapped(
|
||||
pipe, ioEvent, &overlapped, bytesRead);
|
||||
|
||||
if (error == ERROR_OPERATION_ABORTED &&
|
||||
WaitForSingleObject(m_stopEvent, 0) == WAIT_FIRST_OBJECT_VALUE)
|
||||
return PipeIoResult::Stopped;
|
||||
|
||||
if (IsDisconnectedError(error))
|
||||
return PipeIoResult::Disconnected;
|
||||
|
||||
if (error == ERROR_MORE_DATA)
|
||||
{
|
||||
DEBUG_ERROR("Named pipe message exceeds the negotiated frame size");
|
||||
return PipeIoResult::Error;
|
||||
}
|
||||
|
||||
DEBUG_WARN_HR(error, "Failed to read from named pipe");
|
||||
return PipeIoResult::Error;
|
||||
}
|
||||
|
||||
CPipeEndpoint::PipeIoResult CPipeEndpoint::WriteMessage(
|
||||
HANDLE pipe,
|
||||
const void * message,
|
||||
DWORD messageSize)
|
||||
{
|
||||
HANDLE ioEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
||||
if (!ioEvent)
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create named pipe write event");
|
||||
return PipeIoResult::Error;
|
||||
}
|
||||
|
||||
OVERLAPPED overlapped = {};
|
||||
overlapped.hEvent = ioEvent;
|
||||
DWORD bytesWritten = 0;
|
||||
PipeIoResult result = PipeIoResult::Success;
|
||||
|
||||
if (!WriteFile(pipe, message, messageSize, &bytesWritten, &overlapped))
|
||||
{
|
||||
const DWORD error = GetLastError();
|
||||
if (error == ERROR_IO_PENDING)
|
||||
result = WaitForOverlapped(
|
||||
pipe,
|
||||
ioEvent,
|
||||
&overlapped,
|
||||
&bytesWritten,
|
||||
WRITE_TIMEOUT_MS);
|
||||
else if (IsDisconnectedError(error))
|
||||
result = PipeIoResult::Disconnected;
|
||||
else if (error == ERROR_OPERATION_ABORTED &&
|
||||
WaitForSingleObject(m_stopEvent, 0) ==
|
||||
WAIT_FIRST_OBJECT_VALUE)
|
||||
result = PipeIoResult::Stopped;
|
||||
else
|
||||
{
|
||||
DEBUG_WARN_HR(error, "Failed to write to named pipe");
|
||||
result = PipeIoResult::Error;
|
||||
}
|
||||
}
|
||||
|
||||
if (result == PipeIoResult::Success && bytesWritten != messageSize)
|
||||
{
|
||||
DEBUG_ERROR(
|
||||
"Short named pipe write, expected %lu bytes, wrote %lu bytes",
|
||||
messageSize,
|
||||
bytesWritten);
|
||||
result = PipeIoResult::Error;
|
||||
}
|
||||
|
||||
CloseHandle(ioEvent);
|
||||
return result;
|
||||
}
|
||||
|
||||
CPipeEndpoint::~CPipeEndpoint()
|
||||
{
|
||||
Stop();
|
||||
}
|
||||
|
||||
bool CPipeEndpoint::Start(
|
||||
const wchar_t * pipeName,
|
||||
Mode mode,
|
||||
size_t messageSize)
|
||||
{
|
||||
Stop();
|
||||
|
||||
if (!pipeName || !*pipeName || !messageSize || messageSize > MAXDWORD)
|
||||
return false;
|
||||
|
||||
m_pipeName = pipeName;
|
||||
m_mode = mode;
|
||||
m_messageSize = messageSize;
|
||||
m_stopEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
||||
if (!m_stopEvent)
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create named pipe stop event");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_mode == Mode::Server)
|
||||
{
|
||||
HANDLE pipe = CreateServerPipe();
|
||||
if (pipe == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
CloseHandle(m_stopEvent);
|
||||
m_stopEvent = nullptr;
|
||||
return false;
|
||||
}
|
||||
PublishPipe(pipe);
|
||||
}
|
||||
|
||||
m_running.store(true);
|
||||
m_thread = CreateThread(nullptr, 0, ThreadProc, this, 0, nullptr);
|
||||
if (!m_thread)
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create named pipe thread");
|
||||
m_running.store(false);
|
||||
|
||||
AcquireSRWLockExclusive(&m_pipeLock);
|
||||
if (m_pipe != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
CloseHandle(m_pipe);
|
||||
m_pipe = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&m_pipeLock);
|
||||
|
||||
CloseHandle(m_stopEvent);
|
||||
m_stopEvent = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void CPipeEndpoint::Stop()
|
||||
{
|
||||
m_running.store(false);
|
||||
if (m_stopEvent)
|
||||
SetEvent(m_stopEvent);
|
||||
|
||||
AcquireSRWLockShared(&m_pipeLock);
|
||||
if (m_pipe != INVALID_HANDLE_VALUE)
|
||||
CancelIoEx(m_pipe, nullptr);
|
||||
ReleaseSRWLockShared(&m_pipeLock);
|
||||
|
||||
if (m_thread)
|
||||
{
|
||||
WaitForSingleObject(m_thread, INFINITE);
|
||||
CloseHandle(m_thread);
|
||||
m_thread = nullptr;
|
||||
}
|
||||
|
||||
AcquireSRWLockExclusive(&m_pipeLock);
|
||||
if (m_pipe != INVALID_HANDLE_VALUE)
|
||||
{
|
||||
CloseHandle(m_pipe);
|
||||
m_pipe = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&m_pipeLock);
|
||||
|
||||
if (m_stopEvent)
|
||||
{
|
||||
CloseHandle(m_stopEvent);
|
||||
m_stopEvent = nullptr;
|
||||
}
|
||||
|
||||
m_connected.store(false);
|
||||
}
|
||||
|
||||
bool CPipeEndpoint::Send(const void * message, size_t size)
|
||||
{
|
||||
if (!message || size != m_messageSize || !IsRunning() || !IsConnected())
|
||||
return false;
|
||||
|
||||
bool success = false;
|
||||
AcquireSRWLockExclusive(&m_pipeLock);
|
||||
if (m_pipe != INVALID_HANDLE_VALUE && IsConnected())
|
||||
{
|
||||
const PipeIoResult result = WriteMessage(
|
||||
m_pipe,
|
||||
message,
|
||||
static_cast<DWORD>(size));
|
||||
success = result == PipeIoResult::Success;
|
||||
if (!success)
|
||||
{
|
||||
m_connected.store(false);
|
||||
CancelIoEx(m_pipe, nullptr);
|
||||
}
|
||||
}
|
||||
ReleaseSRWLockExclusive(&m_pipeLock);
|
||||
return success;
|
||||
}
|
||||
|
||||
DWORD WINAPI CPipeEndpoint::ThreadProc(void * context)
|
||||
{
|
||||
static_cast<CPipeEndpoint *>(context)->Thread();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void CPipeEndpoint::Thread()
|
||||
{
|
||||
if (m_mode == Mode::Server)
|
||||
RunServer();
|
||||
else
|
||||
RunClient();
|
||||
|
||||
m_running.store(false);
|
||||
m_connected.store(false);
|
||||
}
|
||||
|
||||
HANDLE CPipeEndpoint::CreateServerPipe()
|
||||
{
|
||||
const DWORD bufferSize = static_cast<DWORD>(
|
||||
std::max<size_t>(m_messageSize, 1024));
|
||||
|
||||
HANDLE pipe = CreateNamedPipeW(
|
||||
m_pipeName.c_str(),
|
||||
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
|
||||
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
|
||||
1,
|
||||
bufferSize,
|
||||
bufferSize,
|
||||
0,
|
||||
nullptr);
|
||||
if (pipe == INVALID_HANDLE_VALUE)
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create named pipe %ls", m_pipeName.c_str());
|
||||
return pipe;
|
||||
}
|
||||
|
||||
void CPipeEndpoint::RunServer()
|
||||
{
|
||||
HANDLE ioEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
||||
if (!ioEvent)
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create named pipe I/O event");
|
||||
AcquireSRWLockShared(&m_pipeLock);
|
||||
const HANDLE pipe = m_pipe;
|
||||
ReleaseSRWLockShared(&m_pipeLock);
|
||||
if (pipe != INVALID_HANDLE_VALUE)
|
||||
ClosePipe(pipe);
|
||||
return;
|
||||
}
|
||||
|
||||
HANDLE pipe = INVALID_HANDLE_VALUE;
|
||||
AcquireSRWLockShared(&m_pipeLock);
|
||||
pipe = m_pipe;
|
||||
ReleaseSRWLockShared(&m_pipeLock);
|
||||
|
||||
while (IsRunning())
|
||||
{
|
||||
if (pipe == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
pipe = CreateServerPipe();
|
||||
if (pipe == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
if (!WaitForRetry(SERVER_RETRY_MS))
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
PublishPipe(pipe);
|
||||
}
|
||||
|
||||
ResetEvent(ioEvent);
|
||||
OVERLAPPED overlapped = {};
|
||||
overlapped.hEvent = ioEvent;
|
||||
DWORD transferred = 0;
|
||||
PipeIoResult connectResult = PipeIoResult::Success;
|
||||
|
||||
if (!ConnectNamedPipe(pipe, &overlapped))
|
||||
{
|
||||
const DWORD error = GetLastError();
|
||||
if (error == ERROR_PIPE_CONNECTED)
|
||||
connectResult = PipeIoResult::Success;
|
||||
else if (error == ERROR_IO_PENDING)
|
||||
connectResult = WaitForOverlapped(
|
||||
pipe, ioEvent, &overlapped, &transferred);
|
||||
else if (error == ERROR_OPERATION_ABORTED && !IsRunning())
|
||||
connectResult = PipeIoResult::Stopped;
|
||||
else
|
||||
{
|
||||
DEBUG_WARN_HR(error, "Failed to accept named pipe client");
|
||||
connectResult = PipeIoResult::Error;
|
||||
}
|
||||
}
|
||||
|
||||
if (connectResult != PipeIoResult::Success)
|
||||
{
|
||||
ClosePipe(pipe);
|
||||
pipe = INVALID_HANDLE_VALUE;
|
||||
if (connectResult == PipeIoResult::Stopped || !IsRunning())
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!IsRunning())
|
||||
break;
|
||||
|
||||
if (!IsRunning() ||
|
||||
WaitForSingleObject(m_stopEvent, 0) == WAIT_FIRST_OBJECT_VALUE)
|
||||
break;
|
||||
|
||||
m_connected.store(true);
|
||||
DEBUG_INFO("Named pipe client connected: %ls", m_pipeName.c_str());
|
||||
if (m_handler)
|
||||
m_handler->OnPipeConnected();
|
||||
|
||||
ReadMessages(pipe);
|
||||
|
||||
m_connected.store(false);
|
||||
if (m_handler)
|
||||
m_handler->OnPipeDisconnected();
|
||||
DEBUG_INFO("Named pipe client disconnected: %ls", m_pipeName.c_str());
|
||||
|
||||
if (!DisconnectNamedPipe(pipe))
|
||||
{
|
||||
const DWORD error = GetLastError();
|
||||
if (error != ERROR_PIPE_NOT_CONNECTED)
|
||||
{
|
||||
DEBUG_WARN_HR(error, "Failed to disconnect named pipe client");
|
||||
ClosePipe(pipe);
|
||||
pipe = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pipe != INVALID_HANDLE_VALUE)
|
||||
ClosePipe(pipe);
|
||||
CloseHandle(ioEvent);
|
||||
}
|
||||
|
||||
void CPipeEndpoint::RunClient()
|
||||
{
|
||||
DWORD retryDelay = CLIENT_RETRY_INITIAL_MS;
|
||||
DWORD lastConnectError = ERROR_SUCCESS;
|
||||
|
||||
while (IsRunning())
|
||||
{
|
||||
if (m_handler && !m_handler->ShouldReconnect())
|
||||
break;
|
||||
|
||||
HANDLE pipe = CreateFileW(
|
||||
m_pipeName.c_str(),
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
0,
|
||||
nullptr,
|
||||
OPEN_EXISTING,
|
||||
FILE_FLAG_OVERLAPPED,
|
||||
nullptr);
|
||||
|
||||
if (pipe == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
const DWORD error = GetLastError();
|
||||
if (error != lastConnectError)
|
||||
{
|
||||
DEBUG_TRACE_HR(
|
||||
error, "Named pipe is not available yet: %ls", m_pipeName.c_str());
|
||||
lastConnectError = error;
|
||||
}
|
||||
|
||||
if (!WaitForRetry(retryDelay))
|
||||
break;
|
||||
retryDelay = (std::min)(retryDelay * 2, CLIENT_RETRY_MAX_MS);
|
||||
continue;
|
||||
}
|
||||
|
||||
DWORD mode = PIPE_READMODE_MESSAGE;
|
||||
if (!SetNamedPipeHandleState(pipe, &mode, nullptr, nullptr))
|
||||
{
|
||||
DEBUG_WARN_HR(GetLastError(), "Failed to set named pipe message mode");
|
||||
CloseHandle(pipe);
|
||||
if (!WaitForRetry(retryDelay))
|
||||
break;
|
||||
retryDelay = (std::min)(retryDelay * 2, CLIENT_RETRY_MAX_MS);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!IsRunning() ||
|
||||
WaitForSingleObject(m_stopEvent, 0) == WAIT_FIRST_OBJECT_VALUE)
|
||||
{
|
||||
CloseHandle(pipe);
|
||||
break;
|
||||
}
|
||||
|
||||
PublishPipe(pipe);
|
||||
m_connected.store(true);
|
||||
retryDelay = CLIENT_RETRY_INITIAL_MS;
|
||||
lastConnectError = ERROR_SUCCESS;
|
||||
DEBUG_INFO("Named pipe connected: %ls", m_pipeName.c_str());
|
||||
if (m_handler)
|
||||
m_handler->OnPipeConnected();
|
||||
|
||||
ReadMessages(pipe);
|
||||
|
||||
m_connected.store(false);
|
||||
if (m_handler)
|
||||
m_handler->OnPipeDisconnected();
|
||||
DEBUG_INFO("Named pipe disconnected: %ls", m_pipeName.c_str());
|
||||
ClosePipe(pipe);
|
||||
|
||||
if (!WaitForRetry(retryDelay))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool CPipeEndpoint::ReadMessages(HANDLE pipe)
|
||||
{
|
||||
HANDLE ioEvent = CreateEventW(nullptr, TRUE, FALSE, nullptr);
|
||||
if (!ioEvent)
|
||||
{
|
||||
DEBUG_ERROR_HR(GetLastError(), "Failed to create named pipe read event");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> message(m_messageSize);
|
||||
bool success = true;
|
||||
while (IsRunning() && IsConnected())
|
||||
{
|
||||
DWORD bytesRead = 0;
|
||||
const PipeIoResult result = ReadMessage(
|
||||
pipe,
|
||||
ioEvent,
|
||||
message.data(),
|
||||
static_cast<DWORD>(message.size()),
|
||||
&bytesRead);
|
||||
if (result != PipeIoResult::Success)
|
||||
{
|
||||
success = result == PipeIoResult::Disconnected ||
|
||||
result == PipeIoResult::Stopped;
|
||||
break;
|
||||
}
|
||||
|
||||
if (bytesRead != message.size())
|
||||
{
|
||||
DEBUG_ERROR(
|
||||
"Invalid named pipe frame size, expected %llu bytes, received %lu",
|
||||
static_cast<unsigned long long>(message.size()),
|
||||
bytesRead);
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!IsRunning() ||
|
||||
WaitForSingleObject(m_stopEvent, 0) == WAIT_FIRST_OBJECT_VALUE)
|
||||
break;
|
||||
|
||||
if (m_handler && !m_handler->OnPipeMessage(
|
||||
message.data(), message.size()))
|
||||
{
|
||||
DEBUG_ERROR("Named pipe peer sent an invalid message");
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CloseHandle(ioEvent);
|
||||
return success;
|
||||
}
|
||||
|
||||
bool CPipeEndpoint::WaitForRetry(DWORD delayMs)
|
||||
{
|
||||
return IsRunning() &&
|
||||
WaitForSingleObject(m_stopEvent, delayMs) == WAIT_TIMEOUT;
|
||||
}
|
||||
|
||||
void CPipeEndpoint::PublishPipe(HANDLE pipe)
|
||||
{
|
||||
AcquireSRWLockExclusive(&m_pipeLock);
|
||||
m_pipe = pipe;
|
||||
ReleaseSRWLockExclusive(&m_pipeLock);
|
||||
}
|
||||
|
||||
void CPipeEndpoint::ClosePipe(HANDLE pipe)
|
||||
{
|
||||
AcquireSRWLockExclusive(&m_pipeLock);
|
||||
if (m_pipe == pipe)
|
||||
m_pipe = INVALID_HANDLE_VALUE;
|
||||
CloseHandle(pipe);
|
||||
ReleaseSRWLockExclusive(&m_pipeLock);
|
||||
}
|
||||
131
idd/LGCommon/CPipeEndpoint.h
Normal file
131
idd/LGCommon/CPipeEndpoint.h
Normal file
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* Looking Glass
|
||||
* Copyright © 2017-2026 The Looking Glass Authors
|
||||
* https://looking-glass.io
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 2 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
||||
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <stddef.h>
|
||||
#include <string>
|
||||
|
||||
class IPipeEndpointHandler
|
||||
{
|
||||
public:
|
||||
virtual ~IPipeEndpointHandler() = default;
|
||||
|
||||
virtual void OnPipeConnected() {}
|
||||
virtual void OnPipeDisconnected() {}
|
||||
virtual bool ShouldReconnect() { return true; }
|
||||
virtual bool OnPipeMessage(
|
||||
_In_reads_bytes_(size) const void * message,
|
||||
_In_ size_t size) = 0;
|
||||
};
|
||||
|
||||
class CPipeEndpoint
|
||||
{
|
||||
public:
|
||||
enum class Mode
|
||||
{
|
||||
Server,
|
||||
Client,
|
||||
};
|
||||
|
||||
CPipeEndpoint() = default;
|
||||
~CPipeEndpoint();
|
||||
|
||||
CPipeEndpoint(const CPipeEndpoint &) = delete;
|
||||
CPipeEndpoint & operator=(const CPipeEndpoint &) = delete;
|
||||
|
||||
bool Start(
|
||||
_In_z_ const wchar_t * pipeName,
|
||||
_In_ Mode mode,
|
||||
_In_ size_t messageSize);
|
||||
void Stop();
|
||||
|
||||
bool Send(
|
||||
_In_reads_bytes_(size) const void * message,
|
||||
_In_ size_t size);
|
||||
|
||||
bool IsRunning() const { return m_running.load(); }
|
||||
bool IsConnected() const { return m_connected.load(); }
|
||||
|
||||
void SetHandler(_In_opt_ IPipeEndpointHandler * handler)
|
||||
{
|
||||
m_handler = handler;
|
||||
}
|
||||
|
||||
private:
|
||||
enum class PipeIoResult
|
||||
{
|
||||
Success,
|
||||
Disconnected,
|
||||
Stopped,
|
||||
Error,
|
||||
};
|
||||
|
||||
static const DWORD CLIENT_RETRY_INITIAL_MS;
|
||||
static const DWORD CLIENT_RETRY_MAX_MS;
|
||||
static const DWORD SERVER_RETRY_MS;
|
||||
static const DWORD WRITE_TIMEOUT_MS;
|
||||
static const DWORD WAIT_FIRST_OBJECT_VALUE;
|
||||
|
||||
static bool IsDisconnectedError(_In_ DWORD error);
|
||||
PipeIoResult WaitForOverlapped(
|
||||
_In_ HANDLE pipe,
|
||||
_In_ HANDLE ioEvent,
|
||||
_Inout_ OVERLAPPED * overlapped,
|
||||
_Out_ DWORD * transferred,
|
||||
_In_ DWORD timeoutMs = INFINITE);
|
||||
PipeIoResult ReadMessage(
|
||||
_In_ HANDLE pipe,
|
||||
_In_ HANDLE ioEvent,
|
||||
_Out_writes_bytes_(messageSize) void * message,
|
||||
_In_ DWORD messageSize,
|
||||
_Out_ DWORD * bytesRead);
|
||||
PipeIoResult WriteMessage(
|
||||
_In_ HANDLE pipe,
|
||||
_In_reads_bytes_(messageSize) const void * message,
|
||||
_In_ DWORD messageSize);
|
||||
|
||||
static DWORD WINAPI ThreadProc(_In_ void * context);
|
||||
|
||||
void Thread();
|
||||
void RunServer();
|
||||
void RunClient();
|
||||
bool ReadMessages(_In_ HANDLE pipe);
|
||||
HANDLE CreateServerPipe();
|
||||
bool WaitForRetry(_In_ DWORD delayMs);
|
||||
void PublishPipe(_In_ HANDLE pipe);
|
||||
void ClosePipe(_In_ HANDLE pipe);
|
||||
|
||||
std::wstring m_pipeName;
|
||||
size_t m_messageSize = 0;
|
||||
Mode m_mode = Mode::Client;
|
||||
IPipeEndpointHandler * m_handler = nullptr;
|
||||
|
||||
std::atomic<bool> m_running { false };
|
||||
std::atomic<bool> m_connected { false };
|
||||
|
||||
SRWLOCK m_pipeLock = SRWLOCK_INIT;
|
||||
HANDLE m_pipe = INVALID_HANDLE_VALUE;
|
||||
HANDLE m_thread = nullptr;
|
||||
HANDLE m_stopEvent = nullptr;
|
||||
};
|
||||
51
idd/LGCommon/InputPipeProtocol.h
Normal file
51
idd/LGCommon/InputPipeProtocol.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Looking Glass
|
||||
* Copyright © 2017-2026 The Looking Glass Authors
|
||||
* https://looking-glass.io
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 2 of the License, or (at your option)
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc., 59
|
||||
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static constexpr wchar_t LG_INPUT_PIPE_NAME[] =
|
||||
L"\\\\.\\pipe\\LookingGlassIDDInput";
|
||||
|
||||
static constexpr uint32_t LG_INPUT_PIPE_MAGIC = 0x5049474c;
|
||||
static constexpr uint16_t LG_INPUT_PIPE_VERSION = 1;
|
||||
static constexpr size_t LG_INPUT_PIPE_MAX_REPORT_SIZE = 64;
|
||||
|
||||
enum LGInputPipeMessageType : uint16_t
|
||||
{
|
||||
LG_INPUT_PIPE_MESSAGE_REPORT = 1,
|
||||
};
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct LGInputPipeMessage
|
||||
{
|
||||
uint32_t magic;
|
||||
uint16_t version;
|
||||
uint16_t type;
|
||||
uint32_t payloadSize;
|
||||
uint64_t sequence;
|
||||
uint8_t payload[LG_INPUT_PIPE_MAX_REPORT_SIZE];
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
static_assert(sizeof(LGInputPipeMessage) == 84,
|
||||
"LGInputPipeMessage wire layout changed");
|
||||
83
idd/LGCommon/LGCommon.vcxproj
Normal file
83
idd/LGCommon/LGCommon.vcxproj
Normal file
@@ -0,0 +1,83 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" ToolsVersion="12.0"
|
||||
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{ACB90E34-01CA-4B86-813B-3D20904994C6}</ProjectGuid>
|
||||
<RootNamespace>LGCommon</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0.26100.0</WindowsTargetPlatformVersion>
|
||||
<UMDF_VERSION_MAJOR>2</UMDF_VERSION_MAJOR>
|
||||
<Configuration Condition="'$(Configuration)' == ''">Debug</Configuration>
|
||||
<Platform Condition="'$(Platform)' == ''">Win32</Platform>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
<PlatformToolset>WindowsUserModeDriver10.0</PlatformToolset>
|
||||
<TargetVersion>Windows10</TargetVersion>
|
||||
<DriverTargetPlatform>Universal</DriverTargetPlatform>
|
||||
<DriverType>UMDF</DriverType>
|
||||
<UMDF_VERSION_MINOR>25</UMDF_VERSION_MINOR>
|
||||
<UMDF_MINIMUM_VERSION_REQUIRED>25</UMDF_MINIMUM_VERSION_REQUIRED>
|
||||
<_NT_TARGET_VERSION>0xA000005</_NT_TARGET_VERSION>
|
||||
<Driver_SpectreMitigation>Spectre</Driver_SpectreMitigation>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)' == 'Debug'" Label="Configuration">
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)' == 'Release'" Label="Configuration">
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings" />
|
||||
<ImportGroup Label="PropertySheets">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props"
|
||||
Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')"
|
||||
Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup>
|
||||
<TargetName>LGCommon</TargetName>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup>
|
||||
<ClCompile>
|
||||
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
|
||||
<PreprocessorDefinitions>_ITERATOR_DEBUG_LEVEL=0;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<AdditionalOptions>/EHsc /D_ATL_NO_WIN_SUPPORT %(AdditionalOptions)</AdditionalOptions>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="CDebug.cpp" />
|
||||
<ClCompile Include="CPipeEndpoint.cpp" />
|
||||
<ClCompile Include="RefreshRate.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="CDebug.h" />
|
||||
<ClInclude Include="CPipeEndpoint.h" />
|
||||
<ClInclude Include="CSRWLock.h" />
|
||||
<ClInclude Include="DefaultDisplayModes.h" />
|
||||
<ClInclude Include="InputPipeProtocol.h" />
|
||||
<ClInclude Include="PipeMsg.h" />
|
||||
<ClInclude Include="RefreshRate.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets" />
|
||||
</Project>
|
||||
48
idd/LGCommon/LGCommon.vcxproj.filters
Normal file
48
idd/LGCommon/LGCommon.vcxproj.filters
Normal file
@@ -0,0 +1,48 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0"
|
||||
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cxx;cc</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4B04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="CDebug.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CPipeEndpoint.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="RefreshRate.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="CDebug.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CPipeEndpoint.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="CSRWLock.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="DefaultDisplayModes.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="InputPipeProtocol.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="PipeMsg.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="RefreshRate.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define LG_PIPE_NAME "\\\\.\\pipe\\LookingGlassIDD"
|
||||
static constexpr wchar_t LG_PIPE_NAME[] = L"\\\\.\\pipe\\LookingGlassIDD";
|
||||
|
||||
struct LGPipeMsg
|
||||
{
|
||||
@@ -68,3 +68,5 @@ struct LGPipeMsg
|
||||
resolutionRejected;
|
||||
};
|
||||
};
|
||||
|
||||
static_assert(sizeof(LGPipeMsg) == 20, "LGPipeMsg wire layout changed");
|
||||
|
||||
Reference in New Issue
Block a user