Skip to content

Commit

Permalink
Connection improvements
Browse files Browse the repository at this point in the history
Reduced wait time on server connection attempts on clients (faster connection when multiple connection configured but most of them not available)
Fix client stopping to wait for connection after a disconnect from server
Improved draw fps a little more, pretty close to refresh rate of client now
  • Loading branch information
sammyfreg committed Nov 17, 2024
1 parent 99d3e79 commit 5d0ab0a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
13 changes: 7 additions & 6 deletions Code/Client/Private/NetImgui_Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ void Disconnect(void)
// so the blocking operation can terminate and release the communication thread
Client::ClientInfo& client = *gpClientInfo;
client.mbDisconnectRequest = true;
client.mbDisconnectListen = true;
if( client.mpSocketListen.load() != nullptr )
{
Network::SocketInfo* pFakeSocket(nullptr);
Expand Down Expand Up @@ -202,10 +203,10 @@ bool NewFrame(bool bSupportFrameSkip)
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 = client.mDesiredFps > 0.f && (elapsedDrawMs + elapsedCheckMs*1.25f) > (1000.f/client.mDesiredFps); // Take into account delay until next method call, for more precise fps
client.mSavedDisplaySize = ImGui::GetIO().DisplaySize;
client.mbValidDrawFrame = client.mDesiredFps > 0.f && (elapsedDrawMs + elapsedCheckMs/2.f) > (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;
if( pFonts->TexPixelsAlpha8 &&
Expand Down Expand Up @@ -293,9 +294,9 @@ void EndFrame(void)
}
}

client.mbIsRemoteDrawing = false;
client.mbIsDrawing = false;
client.mbValidDrawFrame = false;
client.mbIsRemoteDrawing = false;
client.mbIsDrawing = false;
client.mbValidDrawFrame = false;
}

//=================================================================================================
Expand Down
9 changes: 4 additions & 5 deletions Code/Client/Private/NetImgui_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,7 @@ void Communications_Outgoing_Frame(ClientInfo& client)
//---------------------------------------------------------------------
// Send Command to server
pPendingDraw->ToOffsets();
if( Network::DataSend(client.mpSocketComs, pPendingDraw, pPendingDraw->mHeader.mSize) )
{
client.mLastOutgoingDrawTime = std::chrono::steady_clock::now();
}
Network::DataSend(client.mpSocketComs, pPendingDraw, pPendingDraw->mHeader.mSize);

//---------------------------------------------------------------------
// Free created data once sent (when not used in next frame)
Expand Down Expand Up @@ -385,9 +382,10 @@ void CommunicationsHost(void* pClientVoid)
ClientInfo* pClient = reinterpret_cast<ClientInfo*>(pClientVoid);
pClient->mbListenThreadActive = true;
pClient->mbDisconnectRequest = false;
pClient->mbDisconnectListen = false;
pClient->mpSocketListen = pClient->mpSocketPending.exchange(nullptr);

while( pClient->mpSocketListen.load() != nullptr && !pClient->mbDisconnectRequest )
while( pClient->mpSocketListen.load() != nullptr && !pClient->mbDisconnectListen )
{
pClient->mpSocketPending = Network::ListenConnect(pClient->mpSocketListen);
if( pClient->mpSocketPending.load() != nullptr && Communications_Initialize(*pClient) )
Expand Down Expand Up @@ -607,6 +605,7 @@ void ClientInfo::ProcessDrawData(const ImDrawData* pDearImguiData, ImGuiMouseCur

CmdDrawFrame* pDrawFrameNew = ConvertToCmdDrawFrame(pDearImguiData, mouseCursor);
pDrawFrameNew->mCompressed = mClientCompressionMode == eCompressionMode::kForceEnable || (mClientCompressionMode == eCompressionMode::kUseServerSetting && mServerCompressionEnabled);
mLastOutgoingDrawTime = std::chrono::steady_clock::now();
mPendingFrameOut.Assign(pDrawFrameNew);
}

Expand Down
3 changes: 2 additions & 1 deletion Code/Client/Private/NetImgui_Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ struct ClientInfo
SavedImguiContext mSavedContextValues;
std::atomic_uint32_t mTexturesPendingSent;
std::atomic_uint32_t mTexturesPendingCreated;


bool mbDisconnectListen = false; // Waiting to Stop listening to connection request
bool mbDisconnectRequest = false; // Waiting to Disconnect
bool mbDisconnectProcessed = false; // Disconnect command sent to server, ready to disconnect
bool mbClientThreadActive = false;
Expand Down
31 changes: 25 additions & 6 deletions Code/Client/Private/NetImgui_NetworkWin32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,41 @@ void Shutdown()
//=================================================================================================
SocketInfo* Connect(const char* ServerHost, uint32_t ServerPort)
{
const timeval kConnectTimeout = {1, 0}; // Waiting 1 seconds before failing connection attempt
u_long kNonBlocking = true;

SOCKET ClientSocket = socket(AF_INET , SOCK_STREAM , 0);
if(ClientSocket == INVALID_SOCKET)
return nullptr;

char zPortName[32]={};
addrinfo* pResults = nullptr;
SocketInfo* pSocketInfo = nullptr;
char zPortName[32] = {};
addrinfo* pResults = nullptr;
SocketInfo* pSocketInfo = nullptr;
NetImgui::Internal::StringFormat(zPortName, "%i", ServerPort);
getaddrinfo(ServerHost, zPortName, nullptr, &pResults);
addrinfo* pResultCur = pResults;
addrinfo* pResultCur = pResults;
fd_set SocketSet;

ioctlsocket(ClientSocket, static_cast<long>(FIONBIO), &kNonBlocking);
while( pResultCur && !pSocketInfo )
{
if( connect(ClientSocket, pResultCur->ai_addr, static_cast<int>(pResultCur->ai_addrlen)) == 0 )
int Result = connect(ClientSocket, pResultCur->ai_addr, static_cast<int>(pResultCur->ai_addrlen));
bool Connected = Result != SOCKET_ERROR;

// Not connected yet, wait some time before bailing out
if( Result == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK )
{
FD_ZERO(&SocketSet);
FD_SET(ClientSocket, &SocketSet);
Result = select(0, nullptr, &SocketSet, nullptr, &kConnectTimeout);
Connected = Result != SOCKET_ERROR;
}

if( Connected )
{
pSocketInfo = netImguiNew<SocketInfo>(ClientSocket);
}
}

pResultCur = pResultCur->ai_next;
}
freeaddrinfo(pResults);
Expand Down
4 changes: 2 additions & 2 deletions Code/Sample/Common/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,14 @@ int main(int, char**)

// Draw the Local Imgui UI and remote imgui UI
sLastTime = std::chrono::steady_clock::now();
const float clear_color_with_alpha[4] = { clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w };
const float clear_color_with_alpha[4] = { clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w };
g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha);
ImDrawData* pDrawData = sample.Draw();
if( pDrawData ){
ImGui_ImplDX11_RenderDrawData(pDrawData);
}
// Update and Render additional Platform Windows
// Update and render additional Platform Windows
static int sLastFrame = -1;
int newFrame = ImGui::GetFrameCount();
if (sLastFrame != newFrame && io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
Expand Down
2 changes: 1 addition & 1 deletion Code/ServerApp/Source/NetImguiServer_Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ void NetworkConnectRequest_Send()
NetworkConnectionNew(pClientSocket, &newClient, ConnectForce);
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::this_thread::sleep_for(std::chrono::milliseconds(500)); // There's already a wait time in Connect attempt, so no need to sleep for too long here
}
gActiveThreadConnectOut = false;
}
Expand Down

0 comments on commit 5d0ab0a

Please sign in to comment.