From 20ad7e4ced70c7b36f51397b3a450af84f598d73 Mon Sep 17 00:00:00 2001 From: Chris Marsh Date: Thu, 27 Jul 2017 15:09:05 -0700 Subject: [PATCH] OSX/linux version initial attempt --- src/CMakeLists.txt | 2 + src/connection_unix.cpp | 91 +++++++++++++++++++++++++++++------------ 2 files changed, 66 insertions(+), 27 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3316346..621a655 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,8 @@ endif(WIN32) if(UNIX) add_library(discord-rpc STATIC ${BASE_RPC_SRC} connection_unix.cpp) + target_link_libraries(discord-rpc PUBLIC pthread) + target_compile_options(discord-rpc PRIVATE -g -Wall) endif(UNIX) target_include_directories(discord-rpc PRIVATE ${RAPIDJSON}/include) diff --git a/src/connection_unix.cpp b/src/connection_unix.cpp index ff7f30f..38c5fe2 100644 --- a/src/connection_unix.cpp +++ b/src/connection_unix.cpp @@ -1,6 +1,11 @@ #include "connection.h" +#include +#include +#include +#include #include +#include #include int GetProcessId() @@ -8,52 +13,84 @@ int GetProcessId() return ::getpid(); } -const int RpcVersion = 1; -const int NumFrames = 4; - -struct RpcConnectionUnix : public RpcConnection { - int pipe{-1}; - RpcMessageFrame frames[NumFrames]; - int nextFrame{0}; +struct BaseConnectionUnix : public BaseConnection { + int sock{-1}; }; -/*static*/ RpcConnection* RpcConnection::Create(const char* applicationId) +static BaseConnectionUnix Connection; +sockaddr_un PipeAddr{}; + +static const char* GetTempPath() { - return new RpcConnectionUnix; + const char* temp = getenv("XDG_RUNTIME_DIR"); + temp = temp ? temp : getenv("TMPDIR"); + temp = temp ? temp : getenv("TMP"); + temp = temp ? temp : getenv("TEMP"); + temp = temp ? temp : "/tmp"; + return temp; } -/*static*/ void RpcConnection::Destroy(RpcConnection*& c) +/*static*/ BaseConnection* BaseConnection::Create() { - auto self = reinterpret_cast(c); - delete self; + snprintf(PipeAddr.sun_path, sizeof(PipeAddr.sun_path), "%s/discord-ipc-0", GetTempPath()); + PipeAddr.sun_family = AF_UNIX; + return &Connection; +} + +/*static*/ void BaseConnection::Destroy(BaseConnection*& c) +{ + auto self = reinterpret_cast(c); + self->Close(); c = nullptr; } -void RpcConnection::Open() +bool BaseConnection::Open() { + auto self = reinterpret_cast(this); + self->sock = socket(AF_UNIX, SOCK_STREAM, 0); + if (self->sock == -1) { + return false; + } + fcntl(self->sock, F_SETFL, O_NONBLOCK); + int err = connect(self->sock, (const sockaddr*)&PipeAddr, sizeof(PipeAddr)); + if (err != 0) { + self->Close(); + return false; + } + return true; } -void RpcConnection::Close() +bool BaseConnection::Close() { + auto self = reinterpret_cast(this); + if (self->sock == -1) { + return false; + } + close(self->sock); + self->sock = -1; + return true; } -void RpcConnection::Write(const void* data, size_t length) +bool BaseConnection::Write(const void* data, size_t length) { + auto self = reinterpret_cast(this); + + if (self->sock == -1) { + return false; + } + + ssize_t sentBytes = send(self->sock, data, length, 0); + return sentBytes == (ssize_t)length; } -RpcMessageFrame* RpcConnection::Read() +bool BaseConnection::Read(void* data, size_t length) { - return nullptr; -} + auto self = reinterpret_cast(this); -RpcMessageFrame* RpcConnection::GetNextFrame() -{ - auto self = reinterpret_cast(this); - auto result = &(self->frames[self->nextFrame]); - self->nextFrame = (self->nextFrame + 1) % NumFrames; - return result; -} + if (self->sock == -1) { + return false; + } -void RpcConnection::WriteFrame(RpcMessageFrame* frame) -{ + int res = recv(self->sock, data, length, 0); + return res == (int)length; }