Check for local server before detecting devices from hardware and tweak timeouts to make network connections faster
This commit is contained in:
parent
4c6429ae76
commit
96af869d79
4 changed files with 51 additions and 16 deletions
|
|
@ -32,6 +32,9 @@ NetworkClient::NetworkClient(std::vector<RGBController *>& control) : controller
|
|||
port_num = OPENRGB_SDK_PORT;
|
||||
server_connected = false;
|
||||
server_controller_count = 0;
|
||||
|
||||
ListenThread = NULL;
|
||||
ConnectionThread = NULL;
|
||||
}
|
||||
|
||||
void NetworkClient::ClientInfoChanged()
|
||||
|
|
@ -59,6 +62,11 @@ unsigned short NetworkClient::GetPort()
|
|||
return port_num;
|
||||
}
|
||||
|
||||
bool NetworkClient::GetConnected()
|
||||
{
|
||||
return(server_connected);
|
||||
}
|
||||
|
||||
bool NetworkClient::GetOnline()
|
||||
{
|
||||
return(server_connected && server_initialized);
|
||||
|
|
@ -122,6 +130,7 @@ void NetworkClient::StopClient()
|
|||
|
||||
shutdown(client_sock, SD_RECEIVE);
|
||||
closesocket(client_sock);
|
||||
if(ListenThread)
|
||||
ListenThread->join();
|
||||
ConnectionThread->join();
|
||||
|
||||
|
|
@ -186,7 +195,7 @@ void NetworkClient::ConnectionThreadFunction()
|
|||
//Wait for server controller count
|
||||
while(server_controller_count == 0)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
std::this_thread::sleep_for(5ms);
|
||||
}
|
||||
|
||||
printf("Client: Received controller count from server: %d\r\n", server_controller_count);
|
||||
|
|
@ -196,12 +205,13 @@ void NetworkClient::ConnectionThreadFunction()
|
|||
{
|
||||
printf("Client: Requesting controller %d\r\n", requested_controllers);
|
||||
|
||||
controller_data_received = false;
|
||||
SendRequest_ControllerData(requested_controllers);
|
||||
|
||||
//Wait until controller is received
|
||||
while(server_controllers.size() == requested_controllers)
|
||||
while(controller_data_received == false)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
std::this_thread::sleep_for(5ms);
|
||||
}
|
||||
|
||||
requested_controllers++;
|
||||
|
|
@ -247,7 +257,6 @@ int NetworkClient::recv_select(SOCKET s, char *buf, int len, int flags)
|
|||
}
|
||||
else if(rv == 0)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
|
|
@ -432,6 +441,8 @@ void NetworkClient::ProcessReply_ControllerData(unsigned int data_size, char * d
|
|||
printf("Received controller: %s\r\n", new_controller->name.c_str());
|
||||
|
||||
server_controllers.push_back(new_controller);
|
||||
|
||||
controller_data_received = true;
|
||||
}
|
||||
|
||||
void NetworkClient::SendData_ClientString()
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ public:
|
|||
|
||||
void ClientInfoChanged();
|
||||
|
||||
bool GetConnected();
|
||||
const char * GetIP();
|
||||
unsigned short GetPort();
|
||||
bool GetOnline();
|
||||
|
|
@ -71,6 +72,7 @@ private:
|
|||
char port_ip[20];
|
||||
unsigned short port_num;
|
||||
bool client_active;
|
||||
bool controller_data_received;
|
||||
bool server_connected;
|
||||
bool server_initialized;
|
||||
unsigned int server_controller_count;
|
||||
|
|
|
|||
|
|
@ -284,7 +284,6 @@ int NetworkServer::accept_select(int sockfd, struct sockaddr *addr, socklen_t *a
|
|||
}
|
||||
else if(rv == 0)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
|
|
@ -316,7 +315,6 @@ int NetworkServer::recv_select(SOCKET s, char *buf, int len, int flags)
|
|||
}
|
||||
else if(rv == 0)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
|
|
|
|||
38
main.cpp
38
main.cpp
|
|
@ -6,6 +6,7 @@
|
|||
* *
|
||||
\******************************************************************************************/
|
||||
|
||||
#include "NetworkClient.h"
|
||||
#include "NetworkServer.h"
|
||||
#include "OpenRGB.h"
|
||||
#include "ProfileManager.h"
|
||||
|
|
@ -17,6 +18,7 @@
|
|||
|
||||
#include "OpenRGBDialog2.h"
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
extern std::vector<i2c_smbus_interface*> busses;
|
||||
extern std::vector<RGBController*> rgb_controllers;
|
||||
|
|
@ -47,7 +49,28 @@ int main(int argc, char* argv[])
|
|||
ProfileManager profile_manager(rgb_controllers);
|
||||
NetworkServer server(rgb_controllers);
|
||||
|
||||
NetworkClient * client = new NetworkClient(rgb_controllers);
|
||||
client->StartClient();
|
||||
|
||||
for(int timeout = 0; timeout < 10; timeout++)
|
||||
{
|
||||
if(client->GetConnected())
|
||||
{
|
||||
break;
|
||||
}
|
||||
std::this_thread::sleep_for(5ms);
|
||||
}
|
||||
|
||||
if(!client->GetConnected())
|
||||
{
|
||||
client->StopClient();
|
||||
|
||||
delete client;
|
||||
|
||||
client = NULL;
|
||||
|
||||
DetectRGBControllers();
|
||||
}
|
||||
|
||||
profile_manager.LoadSizeFromProfile("sizes.ors");
|
||||
|
||||
|
|
@ -77,17 +100,18 @@ int main(int argc, char* argv[])
|
|||
dlg.AddI2CToolsPage();
|
||||
}
|
||||
|
||||
//TODO:
|
||||
// Determine whether or not to add server tab
|
||||
// If application is open in client mode, do not show server tab
|
||||
if(client == NULL)
|
||||
{
|
||||
dlg.AddServerTab(&server);
|
||||
}
|
||||
|
||||
//TODO:
|
||||
// Determine whether or not to add client tab
|
||||
// Client tab should probably always show
|
||||
// Implement client tab
|
||||
dlg.AddClientTab();
|
||||
|
||||
if(client != NULL)
|
||||
{
|
||||
dlg.AddClient(client);
|
||||
}
|
||||
|
||||
if(ret_flags & RET_FLAG_START_MINIMIZED)
|
||||
{
|
||||
dlg.hide();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue