Skip to content

Commit

Permalink
Disconnect race condition fix
Browse files Browse the repository at this point in the history
Have not been able to reproduce issue #59, but with well detailed description, was able to change code to hopefully avoid the problem
  • Loading branch information
sammyfreg committed Aug 3, 2024
1 parent a052a97 commit aaa564d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Code/Client/NetImgui_Api.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
//! @Name : NetImgui
//=================================================================================================
//! @author : Sammy Fatnassi
//! @date : 2024/05/26
//! @version : v1.11.0
//! @date : 2024/08/03
//! @version : v1.11.1
//! @Details : For integration info : https://github.com/sammyfreg/netImgui/wiki
//=================================================================================================
#define NETIMGUI_VERSION "1.11" // Version 1.1 Release
#define NETIMGUI_VERSION_NUM 11100
#define NETIMGUI_VERSION "1.11.1" // Fix for rare disconnect issue (Github issue #59)
#define NETIMGUI_VERSION_NUM 11101



Expand Down
10 changes: 9 additions & 1 deletion Code/Client/Private/NetImgui_Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ bool ConnectFromApp(const char* clientName, uint32_t serverPort, ThreadFunctPtr
if (client.mpSocketPending.load() != nullptr)
{
client.ContextInitialize();
client.mSocketListenPort = serverPort;
client.mThreadFunction(Client::CommunicationsHost, &client);
}

Expand All @@ -97,9 +98,16 @@ void Disconnect(void)
{
if (!gpClientInfo) return;

// Attempt fake connection on local socket waiting for a Server connection,
// so the blocking operation can terminate and release the communication thread
Client::ClientInfo& client = *gpClientInfo;
client.mbDisconnectRequest = true;
client.KillSocketListen();
if( client.mSocketListenPort != 0 )
{
Network::SocketInfo* FakeSocket = Network::Connect("127.0.0.1", client.mSocketListenPort);
client.mSocketListenPort = 0;
Network::Disconnect(FakeSocket);
}
}

//=================================================================================================
Expand Down
4 changes: 3 additions & 1 deletion Code/Client/Private/NetImgui_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ void CommunicationsHost(void* pClientVoid)
}
std::this_thread::sleep_for(std::chrono::milliseconds(16)); // Prevents this thread from taking entire core, waiting on server connection
}
pClient->KillSocketListen();

Network::SocketInfo* pSocket = pClient->mpSocketListen.exchange(nullptr);
NetImgui::Internal::Network::Disconnect(pSocket);
pClient->mbListenThreadActive = false;
}

Expand Down
2 changes: 1 addition & 1 deletion Code/Client/Private/NetImgui_Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ struct ClientInfo
std::atomic<Network::SocketInfo*> mpSocketPending; // Hold socket info until communication is established
std::atomic<Network::SocketInfo*> mpSocketComs; // Socket used for communications with server
std::atomic<Network::SocketInfo*> mpSocketListen; // Socket used to wait for communication request from server
uint32_t mSocketListenPort = 0; // Socket Port number used to wait for communication request from server
VecTexture mTextures; // List if textures created by this client (used un main thread)
char mName[64] = {};
uint64_t mFrameIndex = 0; // Incremented everytime we send a DrawFrame Command
Expand Down Expand Up @@ -127,7 +128,6 @@ struct ClientInfo
inline bool IsConnectPending()const;
inline bool IsActive()const;
inline void KillSocketComs(); // Kill communication sockets (should only be called from communication thread)
inline void KillSocketListen(); // Kill connecting listening socket (should only be called from communication thread)

// Prevent warnings about implicitly created copy
protected:
Expand Down
9 changes: 0 additions & 9 deletions Code/Client/Private/NetImgui_Client.inl
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ void ClientInfo::KillSocketComs()
}
}

void ClientInfo::KillSocketListen()
{
Network::SocketInfo* pSocket = mpSocketListen.exchange(nullptr);
if (pSocket)
{
NetImgui::Internal::Network::Disconnect(pSocket);
}
}

bool ClientInfo::IsContextOverriden()const
{
return mSavedContextValues.mSavedContext;
Expand Down

0 comments on commit aaa564d

Please sign in to comment.