Skip to content

Commit

Permalink
Communication Update
Browse files Browse the repository at this point in the history
Rework of the Client/Servver com protocol, after noticing slower tx speed than expected.
Remove the blocking socket status and client doesn't wait on server before sending its draw data anymore.
Now can almost match client update loop speed
  • Loading branch information
sammyfreg committed Nov 16, 2024
1 parent 8da2cf4 commit 21de6e7
Show file tree
Hide file tree
Showing 18 changed files with 501 additions and 340 deletions.
13 changes: 7 additions & 6 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/08/03
//! @version : v1.11.1
//! @date : 2024/11/16
//! @version : v1.11.2
//! @Details : For integration info : https://github.com/sammyfreg/netImgui/wiki
//=================================================================================================
#define NETIMGUI_VERSION "1.11.1" // Fix for rare disconnect issue (Github issue #59)
#define NETIMGUI_VERSION_NUM 11101
#define NETIMGUI_VERSION "1.11.2" // Improved connection speed
#define NETIMGUI_VERSION_NUM 11102



Expand Down Expand Up @@ -48,18 +48,19 @@
#include "NetImgui_Config.h"
#endif


//-------------------------------------------------------------------------------------------------
// If 'NETIMGUI_ENABLED' hasn't been defined yet (in project settings or NetImgui_Config.h')
// we define this library as 'Disabled'
//-------------------------------------------------------------------------------------------------
#if !defined(NETIMGUI_ENABLED)
#ifndef NETIMGUI_ENABLED
#define NETIMGUI_ENABLED 0
#endif

//-------------------------------------------------------------------------------------------------
// NetImgui needs to detect Dear ImGui to be active, otherwise we disable it
// When including this header, make sure imgui.h is included first
// (either always included in NetImgui_config.h or have it included after Imgui.h in your cpp)
//-------------------------------------------------------------------------------------------------
#if !defined(IMGUI_VERSION)
#undef NETIMGUI_ENABLED
#define NETIMGUI_ENABLED 0
Expand Down
46 changes: 36 additions & 10 deletions Code/Client/Private/NetImgui_Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,37 @@ void Disconnect(void)
{
if (!gpClientInfo) return;

// Attempt fake connection on local socket waiting for a Server connection,
// 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;
if( client.mSocketListenPort != 0 )
if( client.mpSocketListen.load() != nullptr )
{
Network::SocketInfo* FakeSocket = Network::Connect("127.0.0.1", client.mSocketListenPort);
client.mSocketListenPort = 0;
Network::Disconnect(FakeSocket);
Network::SocketInfo* pFakeSocket(nullptr);
if( client.mSocketListenPort != 0 )
{
pFakeSocket = Network::Connect("127.0.0.1", client.mSocketListenPort);
client.mSocketListenPort = 0;
}

if(pFakeSocket)
{
Network::Disconnect(pFakeSocket);
pFakeSocket = nullptr;
}
// If fake connection creation fails, disconnect the listen socket directly
// even though it might potentially cause a race condition
else
{
Network::SocketInfo* pSocket = client.mpSocketListen.exchange(nullptr);
Network::Disconnect(pSocket);
}
}

if( client.mpSocketPending.load() != nullptr )
{
Network::Disconnect(client.mpSocketPending);
client.mpSocketPending = nullptr;
}
}

Expand Down Expand Up @@ -173,9 +195,16 @@ bool NewFrame(bool bSupportFrameSkip)
client.ContextOverride();
}

auto elapsedCheck = std::chrono::steady_clock::now() - client.mLastOutgoingDrawCheckTime;
auto elapsedDraw = std::chrono::steady_clock::now() - client.mLastOutgoingDrawTime;
auto elapsedCheckMs = static_cast<float>(std::chrono::duration_cast<std::chrono::microseconds>(elapsedCheck).count()) / 1000.f;
auto elapsedDrawMs = static_cast<float>(std::chrono::duration_cast<std::chrono::microseconds>(elapsedDraw).count()) / 1000.f;
client.mLastOutgoingDrawCheckTime = std::chrono::steady_clock::now();

// Update input and see if remote netImgui expect a new frame
client.mSavedDisplaySize = ImGui::GetIO().DisplaySize;
client.mbValidDrawFrame = ProcessInputData(client);
client.mbValidDrawFrame = client.mDesiredFps > 0.f && (elapsedDrawMs + elapsedCheckMs*1.25f) > (1000.f/client.mDesiredFps); // Take into account delay until next method call, for more precise fps
ProcessInputData(client);

// We are about to start drawing for remote context, check for font data update
const ImFontAtlas* pFonts = ImGui::GetIO().Fonts;
Expand All @@ -193,10 +222,7 @@ bool NewFrame(bool bSupportFrameSkip)
assert(client.mbFontUploaded);

// Update current active content with our time
const auto TimeNow = std::chrono::high_resolution_clock::now();
std::chrono::duration<float> elapsedSec = TimeNow - client.mTimeTracking;
ImGui::GetIO().DeltaTime = std::max<float>(1.f / 1000.f, elapsedSec.count());
client.mTimeTracking = TimeNow;
ImGui::GetIO().DeltaTime = std::max<float>(1.f / 1000.f, elapsedCheckMs/1000.f);

// NetImgui isn't waiting for a new frame, try to skip drawing when caller supports it
if( !client.mbValidDrawFrame && bSupportFrameSkip )
Expand Down
Loading

0 comments on commit 21de6e7

Please sign in to comment.