Skip to content

Commit

Permalink
Improve connection status handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpartanJ committed Feb 11, 2025
1 parent a260d87 commit 6652d90
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
5 changes: 1 addition & 4 deletions src/tools/ecode/plugins/discordRPC/discordRPCplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,9 @@ void DiscordRPCplugin::onUnregisterEditor( UICodeEditor* editor ) {

void DiscordRPCplugin::onRegisterListeners( UICodeEditor* editor, std::vector<Uint32>& listeners ) {
listeners.push_back( editor->on( Event::OnFocus, [this, editor]( const Event* ) {
// `this` in the scope of the lambda is the parent `DiscordRPCplugin`

auto& doc = editor->getDocument();
if ( !doc.hasFilepath() ) {
if ( !doc.hasFilepath() )
return;
}

auto filename = doc.getFilename();

Expand Down
27 changes: 22 additions & 5 deletions src/tools/ecode/plugins/discordRPC/sdk/ipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ using namespace EE::System;

using json = nlohmann::json;

DiscordIPC::DiscordIPC() {
DiscordIPC::DiscordIPC()
#if EE_PLATFORM == EE_PLATFORM_WIN
:
mSocket( INVALID_HANDLE_VALUE )
#endif
{
mPID = Sys::getProcessID();
}

Expand All @@ -42,8 +47,10 @@ bool DiscordIPC::tryConnect() {
mSocket = CreateFile( mIpcPath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr,
OPEN_EXISTING, 0, nullptr );

doHandshake();
return true;
if ( mSocket != INVALID_HANDLE_VALUE ) {
doHandshake();
return true;
}
}
}

Expand Down Expand Up @@ -111,7 +118,7 @@ bool DiscordIPC::tryConnect() {
mSocket = socket( AF_UNIX, SOCK_STREAM, 0 );
if ( mSocket == -1 ) {
Log::error( "dcIPC: Discord IPC socket cold not be opened: %s", mIpcPath );
mIpcPath = "";
mIpcPath.clear();
continue;
}

Expand All @@ -125,6 +132,8 @@ bool DiscordIPC::tryConnect() {
if ( connect( mSocket, reinterpret_cast<struct sockaddr*>( &serverAddr ),
sizeof( serverAddr ) ) == -1 ) {
close( mSocket );
mSocket = -1;
mIpcPath.clear();
return false;
}

Expand Down Expand Up @@ -217,7 +226,7 @@ void DiscordIPC::setActivity( DiscordIPCActivity&& a ) {
}

void DiscordIPC::sendPacket( DiscordIPCOpcodes opcode, json j ) {
if ( !std::filesystem::exists( mIpcPath ) ) {
if ( !isConnected() ) {
reconnect();
return;
}
Expand Down Expand Up @@ -327,3 +336,11 @@ DiscordIPC::~DiscordIPC() {
CloseHandle( mSocket );
#endif
}

bool DiscordIPC::isConnected() const {
#if defined( EE_PLATFORM_POSIX )
return mSocket != -1;
#elif EE_PLATFORM == EE_PLATFORM_WIN
return mSocket != INVALID_HANDLE_VALUE;
#endif
}
3 changes: 2 additions & 1 deletion src/tools/ecode/plugins/discordRPC/sdk/ipc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class DiscordIPC {
// true - Success
bool tryConnect();
void reconnect();
bool isConnected() const;

void setActivity( DiscordIPCActivity&& a );
const DiscordIPCActivity& getActivity() { return mActivity; }
Expand All @@ -69,7 +70,7 @@ class DiscordIPC {
DiscordIPCActivity mActivity;

#if defined( EE_PLATFORM_POSIX )
int mSocket;
int mSocket = -1;
#elif EE_PLATFORM == EE_PLATFORM_WIN
void* mSocket;
#endif
Expand Down

0 comments on commit 6652d90

Please sign in to comment.