2017-07-11 23:59:14 +01:00
|
|
|
#include "connection.h"
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#define NOMCX
|
|
|
|
#define NOSERVICE
|
|
|
|
#define NOIME
|
|
|
|
#include <windows.h>
|
|
|
|
|
2017-07-20 22:58:23 +01:00
|
|
|
int GetProcessId()
|
|
|
|
{
|
|
|
|
return ::GetCurrentProcessId();
|
|
|
|
}
|
|
|
|
|
2017-07-17 17:28:54 +01:00
|
|
|
struct BaseConnectionWin : public BaseConnection {
|
2017-07-11 23:59:14 +01:00
|
|
|
HANDLE pipe{INVALID_HANDLE_VALUE};
|
|
|
|
};
|
|
|
|
|
2017-07-17 17:28:54 +01:00
|
|
|
static BaseConnectionWin Connection;
|
2017-07-25 17:27:48 +01:00
|
|
|
// static const wchar_t* PipeName = L"\\\\?\\pipe\\discord-ipc";
|
2017-07-20 23:59:50 +01:00
|
|
|
static const wchar_t* PipeName = L"\\\\?\\pipe\\discord-ipc-0";
|
2017-07-11 23:59:14 +01:00
|
|
|
|
2017-07-17 17:28:54 +01:00
|
|
|
/*static*/ BaseConnection* BaseConnection::Create()
|
2017-07-11 23:59:14 +01:00
|
|
|
{
|
2017-07-17 17:28:54 +01:00
|
|
|
return &Connection;
|
2017-07-11 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-07-17 17:28:54 +01:00
|
|
|
/*static*/ void BaseConnection::Destroy(BaseConnection*& c)
|
2017-07-11 23:59:14 +01:00
|
|
|
{
|
2017-07-17 17:28:54 +01:00
|
|
|
auto self = reinterpret_cast<BaseConnectionWin*>(c);
|
|
|
|
self->Close();
|
2017-07-11 23:59:14 +01:00
|
|
|
c = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-07-17 17:28:54 +01:00
|
|
|
bool BaseConnection::Open()
|
2017-07-11 23:59:14 +01:00
|
|
|
{
|
2017-07-17 17:28:54 +01:00
|
|
|
auto self = reinterpret_cast<BaseConnectionWin*>(this);
|
2017-07-11 23:59:14 +01:00
|
|
|
for (;;) {
|
2017-07-25 17:27:48 +01:00
|
|
|
self->pipe = ::CreateFileW(
|
|
|
|
PipeName, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
|
2017-07-11 23:59:14 +01:00
|
|
|
if (self->pipe != INVALID_HANDLE_VALUE) {
|
2017-07-17 17:28:54 +01:00
|
|
|
return true;
|
2017-07-11 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (GetLastError() != ERROR_PIPE_BUSY) {
|
2017-07-17 17:28:54 +01:00
|
|
|
return false;
|
2017-07-11 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!WaitNamedPipeW(PipeName, 10000)) {
|
2017-07-17 17:28:54 +01:00
|
|
|
return false;
|
2017-07-11 23:59:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 17:28:54 +01:00
|
|
|
bool BaseConnection::Close()
|
2017-07-11 23:59:14 +01:00
|
|
|
{
|
2017-07-17 17:28:54 +01:00
|
|
|
auto self = reinterpret_cast<BaseConnectionWin*>(this);
|
2017-07-11 23:59:14 +01:00
|
|
|
::CloseHandle(self->pipe);
|
|
|
|
self->pipe = INVALID_HANDLE_VALUE;
|
2017-07-17 17:28:54 +01:00
|
|
|
return true;
|
2017-07-11 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-07-17 17:28:54 +01:00
|
|
|
bool BaseConnection::Write(const void* data, size_t length)
|
2017-07-11 23:59:14 +01:00
|
|
|
{
|
2017-07-17 17:28:54 +01:00
|
|
|
auto self = reinterpret_cast<BaseConnectionWin*>(this);
|
2017-07-17 22:49:31 +01:00
|
|
|
return ::WriteFile(self->pipe, data, length, nullptr, nullptr) == TRUE;
|
2017-07-11 23:59:14 +01:00
|
|
|
}
|
|
|
|
|
2017-07-17 17:28:54 +01:00
|
|
|
bool BaseConnection::Read(void* data, size_t length)
|
2017-07-11 23:59:14 +01:00
|
|
|
{
|
2017-07-17 17:28:54 +01:00
|
|
|
auto self = reinterpret_cast<BaseConnectionWin*>(this);
|
|
|
|
DWORD bytesAvailable = 0;
|
|
|
|
if (::PeekNamedPipe(self->pipe, nullptr, 0, nullptr, &bytesAvailable, nullptr)) {
|
|
|
|
if (bytesAvailable >= length) {
|
|
|
|
if (::ReadFile(self->pipe, data, length, nullptr, nullptr) == TRUE) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2017-07-11 23:59:14 +01:00
|
|
|
}
|