Choose pipe number on initialize (#250)

* Choose pipe number on initialize

* Get pipe from base connection instance

* UE4 support

* Warnings as errors yelling

* Fix windows connection

* Oops all variables

* maybe this fixes it

* This one actually works!!!!

* Fix double function declaration
This commit is contained in:
Mason Sciotti 2019-01-14 00:16:22 -08:00 committed by GitHub
parent 4e53fa0392
commit 4824b20f28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 25 additions and 19 deletions

View file

@ -78,7 +78,7 @@ public class DiscordRpc
Ignore = 2
}
public static void Initialize(string applicationId, ref EventHandlers handlers, bool autoRegister, string optionalSteamId)
public static void Initialize(string applicationId, ref EventHandlers handlers, bool autoRegister, string optionalSteamId, int pipe = 0)
{
Callbacks = handlers;
@ -90,11 +90,11 @@ public class DiscordRpc
staticEventHandlers.spectateCallback += DiscordRpc.SpectateCallback;
staticEventHandlers.requestCallback += DiscordRpc.RequestCallback;
InitializeInternal(applicationId, ref staticEventHandlers, autoRegister, optionalSteamId);
InitializeInternal(applicationId, ref staticEventHandlers, autoRegister, optionalSteamId, pipe);
}
[DllImport("discord-rpc", EntryPoint = "Discord_Initialize", CallingConvention = CallingConvention.Cdecl)]
static extern void InitializeInternal(string applicationId, ref EventHandlers handlers, bool autoRegister, string optionalSteamId);
static extern void InitializeInternal(string applicationId, ref EventHandlers handlers, bool autoRegister, string optionalSteamId, int pipe);
[DllImport("discord-rpc", EntryPoint = "Discord_Shutdown", CallingConvention = CallingConvention.Cdecl)]
public static extern void Shutdown();

View file

@ -129,7 +129,7 @@ static void discordInit()
handlers.joinGame = handleDiscordJoin;
handlers.spectateGame = handleDiscordSpectate;
handlers.joinRequest = handleDiscordJoinRequest;
Discord_Initialize(APPLICATION_ID, &handlers, 1, NULL);
Discord_Initialize(APPLICATION_ID, &handlers, 1, NULL, 0);
}
static void gameLoop()

View file

@ -82,7 +82,8 @@ static void JoinRequestHandler(const DiscordUser* request)
void UDiscordRpc::Initialize(const FString& applicationId,
bool autoRegister,
const FString& optionalSteamId)
const FString& optionalSteamId,
int pipe)
{
self = this;
IsConnected = false;
@ -102,7 +103,7 @@ void UDiscordRpc::Initialize(const FString& applicationId,
auto appId = StringCast<ANSICHAR>(*applicationId);
auto steamId = StringCast<ANSICHAR>(*optionalSteamId);
Discord_Initialize(
(const char*)appId.Get(), &handlers, autoRegister, (const char*)steamId.Get());
(const char*)appId.Get(), &handlers, autoRegister, (const char*)steamId.Get(), pipe);
}
void UDiscordRpc::Shutdown()

View file

@ -99,7 +99,8 @@ public:
Category = "Discord")
void Initialize(const FString& applicationId,
bool autoRegister,
const FString& optionalSteamId);
const FString& optionalSteamId,
int optionalPipeNumber);
UFUNCTION(BlueprintCallable,
meta = (DisplayName = "Shut down connection", Keywords = "Discord rpc"),

View file

@ -64,7 +64,8 @@ typedef struct DiscordEventHandlers {
DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
DiscordEventHandlers* handlers,
int autoRegister,
const char* optionalSteamId);
const char* optionalSteamId,
int optionalPipeNumber);
DISCORD_EXPORT void Discord_Shutdown(void);
/* checks for incoming messages, dispatches callbacks */

View file

@ -12,7 +12,7 @@ struct BaseConnection {
static BaseConnection* Create();
static void Destroy(BaseConnection*&);
bool isOpen{false};
bool Open();
bool Open(int pipe);
bool Close();
bool Write(const void* data, size_t length);
bool Read(void* data, size_t length);

View file

@ -49,7 +49,7 @@ static const char* GetTempPath()
c = nullptr;
}
bool BaseConnection::Open()
bool BaseConnection::Open(int pipe)
{
const char* tempPath = GetTempPath();
auto self = reinterpret_cast<BaseConnectionUnix*>(this);
@ -62,8 +62,7 @@ bool BaseConnection::Open()
int optval = 1;
setsockopt(self->sock, SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval));
#endif
for (int pipeNum = 0; pipeNum < 10; ++pipeNum) {
for (int pipeNum = pipe; pipeNum < 10; ++pipeNum) {
snprintf(
PipeAddr.sun_path, sizeof(PipeAddr.sun_path), "%s/discord-ipc-%d", tempPath, pipeNum);
int err = connect(self->sock, (const sockaddr*)&PipeAddr, sizeof(PipeAddr));

View file

@ -6,6 +6,7 @@
#define NOIME
#include <assert.h>
#include <windows.h>
#include <sstream>
int GetProcessId()
{
@ -30,11 +31,11 @@ static BaseConnectionWin Connection;
c = nullptr;
}
bool BaseConnection::Open()
bool BaseConnection::Open(int pipe)
{
wchar_t pipeName[]{L"\\\\?\\pipe\\discord-ipc-0"};
const size_t pipeDigit = sizeof(pipeName) / sizeof(wchar_t) - 2;
pipeName[pipeDigit] = L'0';
pipeName[pipeDigit] += pipe;
auto self = reinterpret_cast<BaseConnectionWin*>(this);
for (;;) {
self->pipe = ::CreateFileW(

View file

@ -273,7 +273,8 @@ static bool DeregisterForEvent(const char* evtName)
extern "C" DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
DiscordEventHandlers* handlers,
int autoRegister,
const char* optionalSteamId)
const char* optionalSteamId,
int pipe)
{
IoThread = new (std::nothrow) IoThreadHolder();
if (IoThread == nullptr) {
@ -308,7 +309,7 @@ extern "C" DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
return;
}
Connection = RpcConnection::Create(applicationId);
Connection = RpcConnection::Create(applicationId, pipe);
Connection->onConnect = [](JsonDocument& readyMessage) {
Discord_UpdateHandlers(&QueuedHandlers);
if (QueuedPresence.length > 0) {

View file

@ -6,10 +6,11 @@
static const int RpcVersion = 1;
static RpcConnection Instance;
/*static*/ RpcConnection* RpcConnection::Create(const char* applicationId)
/*static*/ RpcConnection* RpcConnection::Create(const char* applicationId, int pipe)
{
Instance.connection = BaseConnection::Create();
StringCopy(Instance.appId, applicationId);
Instance.pipe = pipe;
return &Instance;
}
@ -26,7 +27,7 @@ void RpcConnection::Open()
return;
}
if (state == State::Disconnected && !connection->Open()) {
if (state == State::Disconnected && !connection->Open(Instance.pipe)) {
return;
}

View file

@ -43,11 +43,12 @@ struct RpcConnection {
void (*onConnect)(JsonDocument& message){nullptr};
void (*onDisconnect)(int errorCode, const char* message){nullptr};
char appId[64]{};
int pipe;
int lastErrorCode{0};
char lastErrorMessage[256]{};
RpcConnection::MessageFrame sendFrame;
static RpcConnection* Create(const char* applicationId);
static RpcConnection* Create(const char* applicationId, int pipe);
static void Destroy(RpcConnection*&);
inline bool IsOpen() const { return state == State::Connected; }