Skip to content

Commit

Permalink
Prevent server 2nd connection to client while already connecting
Browse files Browse the repository at this point in the history
Also fix a shutdow error of DualUI sample
  • Loading branch information
sammyfreg committed Nov 24, 2024
1 parent 1aee485 commit bd23d27
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
6 changes: 4 additions & 2 deletions Code/Sample/Common/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,10 @@ int main(int, char**)
}
//=========================================================================================

g_pSwapChain->Present(1, 0); // Present with vsync
//g_pSwapChain->Present(0, 0); // Present without vsync
// Present
HRESULT hr = g_pSwapChain->Present(1, 0); // Present with vsync
//HRESULT hr = g_pSwapChain->Present(0, 0); // Present without vsync
g_SwapChainOccluded = (hr == DXGI_STATUS_OCCLUDED);
}

// Cleanup
Expand Down
7 changes: 6 additions & 1 deletion Code/Sample/SampleDualUI/SampleDualUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ bool SampleDualUI::Startup()
//=================================================================================================
void SampleDualUI::Shutdown()
{
ImGui::SetCurrentContext(mpContextExtra);
ImGui::GetIO().BackendRendererUserData = nullptr;
ImGui::GetIO().BackendPlatformUserData = nullptr;
ImGui::GetIO().BackendLanguageUserData = nullptr;
ImGui::DestroyContext(mpContextExtra);

ImGui::SetCurrentContext(mpContextMain);
mpContextMain = nullptr;
mpContextMain = nullptr;
mpContextExtra = nullptr;
Base::Shutdown();
}
Expand Down
3 changes: 3 additions & 0 deletions Code/ServerApp/Source/NetImguiServer_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Client
enum class eStatus : uint8_t
{
Disconnected, // No connection detected on client
Connecting, // This server is connecting to client
Connected, // This server is connect to client
Available, // Client already taken, but this server can take over
ErrorBusy, // Client already taken
Expand All @@ -62,6 +63,8 @@ class Client
inline bool IsReadOnly()const { return mReadOnly; };
inline bool IsTransient()const { return mConfigType == eConfigType::Transient; };
inline bool IsConnected()const { return mStatus == eStatus::Connected; }
inline bool IsAvailable()const { return mStatus != eStatus::Connected && mStatus != eStatus::Connecting;}

// Add/Edit/Remove config
static void SetConfig(const Client& config); //!< Add or replace a client configuration info
static void DelConfig(uint32_t configID); //!< Remove a client configuration
Expand Down
22 changes: 16 additions & 6 deletions Code/ServerApp/Source/NetImguiServer_Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ bool Communications_InitializeClient(NetImgui::Internal::Network::SocketInfo* pC
{
bool bAvailable = (cmdVersionRcv.mFlags & static_cast<uint8_t>(NetImgui::Internal::CmdVersion::eFlags::IsUnavailable)) == 0;
NetImguiServer::Config::Client::SetProperty_Status(pClient->mClientConfigID, bAvailable ? NetImguiServer::Config::Client::eStatus::Available
: NetImguiServer::Config::Client::eStatus::ErrorBusy);
: NetImguiServer::Config::Client::eStatus::ErrorBusy);
return false;
}

Expand All @@ -272,6 +272,7 @@ bool Communications_InitializeClient(NetImgui::Internal::Network::SocketInfo* pC
return true;
}
}
NetImguiServer::Config::Client::SetProperty_Status(pClient->mClientConfigID, NetImguiServer::Config::Client::eStatus::Disconnected);
return false;
}

Expand All @@ -285,9 +286,12 @@ void NetworkConnectionNew(NetImgui::Internal::Network::SocketInfo* pClientSocket
if (pNewClient == nullptr) {
zErrorMsg = "Too many connection on server already";
}

if (zErrorMsg == nullptr && !gbShutdown && Communications_InitializeClient(pClientSocket, pNewClient, ConnectForce) == false) {
zErrorMsg = "Initialization failed. Wrong communication version?";
else
{
NetImguiServer::Config::Client::SetProperty_Status(pNewClient->mClientConfigID, NetImguiServer::Config::Client::eStatus::Connecting);
if (zErrorMsg == nullptr && !gbShutdown && Communications_InitializeClient(pClientSocket, pNewClient, ConnectForce) == false) {
zErrorMsg = "Initialization failed. Wrong communication version?";
}
}

if (zErrorMsg == nullptr && !gbShutdown){
Expand All @@ -296,6 +300,7 @@ void NetworkConnectionNew(NetImgui::Internal::Network::SocketInfo* pClientSocket
}
else{
NetImgui::Internal::Network::Disconnect(pClientSocket);
NetImguiServer::Config::Client::SetProperty_Status(pNewClient->mClientConfigID, NetImguiServer::Config::Client::eStatus::Disconnected);
if (!gbShutdown) {
if (pNewClient){
pNewClient->mbIsFree = true;
Expand Down Expand Up @@ -376,7 +381,7 @@ void NetworkConnectRequest_Send()
if( NetImguiServer::Config::Client::GetConfigByIndex(configIdx, clientConfig) )
{
ConnectForce = clientConfig.mConnectForce;
if( (clientConfig.mConnectAuto || clientConfig.mConnectRequest || clientConfig.mConnectForce) && !clientConfig.IsConnected() && clientConfig.mHostPort != NetImguiServer::Config::Server::sPort)
if( (clientConfig.mConnectAuto || clientConfig.mConnectRequest || clientConfig.mConnectForce) && clientConfig.IsAvailable() && clientConfig.mHostPort != NetImguiServer::Config::Server::sPort)
{
NetImguiServer::Config::Client::SetProperty_ConnectRequest(clientConfig.mRuntimeID, false, false); // Reset the Connection request, we are processing it
NetImguiServer::Config::Client::SetProperty_Status(clientConfig.mRuntimeID, NetImguiServer::Config::Client::eStatus::Disconnected);
Expand All @@ -387,7 +392,7 @@ void NetworkConnectRequest_Send()

// Connection successful, find an available client slot
if( pClientSocket )
{
{
uint32_t freeIndex = RemoteClient::Client::GetFreeIndex();
if( freeIndex != RemoteClient::Client::kInvalidClient )
{
Expand All @@ -402,6 +407,11 @@ void NetworkConnectRequest_Send()
NetImguiServer::App::HAL_GetSocketInfo(pClientSocket, newClient.mConnectHost, sizeof(newClient.mConnectHost), newClient.mConnectPort);
NetworkConnectionNew(pClientSocket, &newClient, ConnectForce);
}
else
{
NetImgui::Internal::Network::Disconnect(pClientSocket);
NetImguiServer::Config::Client::SetProperty_Status(clientConfigID, NetImguiServer::Config::Client::eStatus::Disconnected);
}
}
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
}
Expand Down

0 comments on commit bd23d27

Please sign in to comment.