From 79626906a50200d3f18804f6f35cbc33774101bf Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Wed, 16 Jul 2025 00:01:03 -0500 Subject: [PATCH] Implement server name string, server sends its name to the client if protocol version >= 5 --- NetworkClient.cpp | 24 +++++++++ NetworkClient.h | 3 ++ NetworkProtocol.h | 1 + NetworkServer.cpp | 52 +++++++++++++++++-- NetworkServer.h | 3 ++ ResourceManager.cpp | 8 +++ .../OpenRGBClientInfoPage.cpp | 24 +++++++-- 7 files changed, 109 insertions(+), 6 deletions(-) diff --git a/NetworkClient.cpp b/NetworkClient.cpp index 5bbc2102..3091f06e 100644 --- a/NetworkClient.cpp +++ b/NetworkClient.cpp @@ -112,6 +112,11 @@ bool NetworkClient::GetOnline() return(server_connected && client_string_sent && protocol_initialized && server_initialized); } +std::string NetworkClient::GetServerName() +{ + return(server_name); +} + void NetworkClient::RegisterClientInfoChangeCallback(NetClientCallback new_callback, void * new_callback_arg) { ClientInfoChangeCallbacks.push_back(new_callback); @@ -540,6 +545,15 @@ void NetworkClient::ListenThreadFunction() ProcessReply_ProtocolVersion(header.pkt_size, data); break; + case NET_PACKET_ID_SET_SERVER_NAME: + if(data == NULL) + { + break; + } + + ProcessRequest_ServerString(header.pkt_size, data); + break; + case NET_PACKET_ID_DEVICE_LIST_UPDATED: ProcessRequest_DeviceListChanged(); break; @@ -730,6 +744,16 @@ void NetworkClient::ProcessRequest_DeviceListChanged() change_in_progress = false; } +void NetworkClient::ProcessRequest_ServerString(unsigned int data_size, char * data) +{ + server_name.assign(data, data_size); + + /*---------------------------------------------------------*\ + | Client info has changed, call the callbacks | + \*---------------------------------------------------------*/ + ClientInfoChanged(); +} + void NetworkClient::SendData_ClientString() { NetPacketHeader reply_hdr; diff --git a/NetworkClient.h b/NetworkClient.h index 2fb7213b..8486940b 100644 --- a/NetworkClient.h +++ b/NetworkClient.h @@ -33,6 +33,7 @@ public: unsigned short GetPort(); unsigned int GetProtocolVersion(); bool GetOnline(); + std::string GetServerName(); void ClearCallbacks(); void RegisterClientInfoChangeCallback(NetClientCallback new_callback, void * new_callback_arg); @@ -54,6 +55,7 @@ public: void ProcessReply_ProtocolVersion(unsigned int data_size, char * data); void ProcessRequest_DeviceListChanged(); + void ProcessRequest_ServerString(unsigned int data_size, char * data); void SendData_ClientString(); @@ -95,6 +97,7 @@ protected: private: SOCKET client_sock; std::string client_name; + std::string server_name; net_port port; std::string port_ip; unsigned short port_num; diff --git a/NetworkProtocol.h b/NetworkProtocol.h index 6719475c..0cf7e632 100644 --- a/NetworkProtocol.h +++ b/NetworkProtocol.h @@ -60,6 +60,7 @@ enum NET_PACKET_ID_REQUEST_PROTOCOL_VERSION = 40, /* Request OpenRGB SDK protocol version from server */ NET_PACKET_ID_SET_CLIENT_NAME = 50, /* Send client name string to server */ + NET_PACKET_ID_SET_SERVER_NAME = 51, /* Send server name string to client */ NET_PACKET_ID_DEVICE_LIST_UPDATED = 100, /* Indicate to clients that device list has updated */ diff --git a/NetworkServer.cpp b/NetworkServer.cpp index 0499a317..7a538873 100644 --- a/NetworkServer.cpp +++ b/NetworkServer.cpp @@ -13,7 +13,7 @@ #include "NetworkServer.h" #include "LogManager.h" -#ifndef WIN32 +#ifndef _WIN32 #include #include #include @@ -28,8 +28,9 @@ const char yes = 1; -#ifdef WIN32 +#ifdef _WIN32 #include +#define MSG_NOSIGNAL 0 #else #include #endif @@ -229,6 +230,22 @@ void NetworkServer::SetLegacyWorkaroundEnable(bool enable) legacy_workaround_enabled = enable; } +void NetworkServer::SetName(std::string new_name) +{ + /*---------------------------------------------------------*\ + | Store the server name | + \*---------------------------------------------------------*/ + server_name = new_name; + + /*---------------------------------------------------------*\ + | Send server name to all clients | + \*---------------------------------------------------------*/ + for(std::size_t client_idx = 0; client_idx < ServerClients.size(); client_idx++) + { + SendReply_ServerString(ServerClients[client_idx]->client_sock); + } +} + void NetworkServer::SetPort(unsigned short new_port) { if(server_online == false) @@ -655,8 +672,9 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info) break; case NET_PACKET_ID_REQUEST_PROTOCOL_VERSION: - SendReply_ProtocolVersion(client_sock); ProcessRequest_ClientProtocolVersion(client_sock, header.pkt_size, data); + SendReply_ProtocolVersion(client_sock); + SendReply_ServerString(client_sock); break; case NET_PACKET_ID_SET_CLIENT_NAME: @@ -1042,6 +1060,7 @@ void NetworkServer::ProcessRequest_ClientString(SOCKET client_sock, unsigned int break; } } + ServerClientsMutex.unlock(); /*---------------------------------------------------------*\ @@ -1106,6 +1125,33 @@ void NetworkServer::SendReply_ProtocolVersion(SOCKET client_sock) send_in_progress.unlock(); } +void NetworkServer::SendReply_ServerString(SOCKET client_sock) +{ + /*---------------------------------------------------------*\ + | Send server string to client only if protocol is 5 or | + | greater | + \*---------------------------------------------------------*/ + ServerClientsMutex.lock(); + for(unsigned int this_idx = 0; this_idx < ServerClients.size(); this_idx++) + { + if(ServerClients[this_idx]->client_sock == client_sock) + { + if(ServerClients[this_idx]->client_protocol_version >= 5) + { + NetPacketHeader reply_hdr; + + InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_SET_SERVER_NAME, (unsigned int)strlen(server_name.c_str()) + 1); + + send_in_progress.lock(); + send(client_sock, (char *)&reply_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL); + send(client_sock, (char *)server_name.c_str(), reply_hdr.pkt_size, MSG_NOSIGNAL); + send_in_progress.unlock(); + } + } + } + ServerClientsMutex.unlock(); +} + void NetworkServer::SendRequest_DeviceListChanged(SOCKET client_sock) { NetPacketHeader pkt_hdr; diff --git a/NetworkServer.h b/NetworkServer.h index 84701346..08a3d27e 100644 --- a/NetworkServer.h +++ b/NetworkServer.h @@ -73,6 +73,7 @@ public: void SetHost(std::string host); void SetLegacyWorkaroundEnable(bool enable); + void SetName(std::string new_name); void SetPort(unsigned short new_port); void StartServer(); @@ -88,6 +89,7 @@ public: void SendReply_ControllerCount(SOCKET client_sock); void SendReply_ControllerData(SOCKET client_sock, unsigned int dev_idx, unsigned int protocol_version); void SendReply_ProtocolVersion(SOCKET client_sock); + void SendReply_ServerString(SOCKET client_sock); void SendRequest_DeviceListChanged(SOCKET client_sock); void SendReply_ProfileList(SOCKET client_sock); @@ -102,6 +104,7 @@ public: protected: std::string host; unsigned short port_num; + std::string server_name; std::atomic server_online; std::atomic server_listening; diff --git a/ResourceManager.cpp b/ResourceManager.cpp index f5102669..d30ceccc 100644 --- a/ResourceManager.cpp +++ b/ResourceManager.cpp @@ -170,6 +170,14 @@ ResourceManager::ResourceManager() server = new NetworkServer(rgb_controllers_hw); } + /*-----------------------------------------------------*\ + | Set server name | + \*-----------------------------------------------------*/ + std::string titleString = "OpenRGB "; + titleString.append(VERSION_STRING); + + server->SetName(titleString); + /*-----------------------------------------------------*\ | Enable legacy SDK workaround in server if configured | \*-----------------------------------------------------*/ diff --git a/qt/OpenRGBClientInfoPage/OpenRGBClientInfoPage.cpp b/qt/OpenRGBClientInfoPage/OpenRGBClientInfoPage.cpp index d91924fd..a1fbec0c 100644 --- a/qt/OpenRGBClientInfoPage/OpenRGBClientInfoPage.cpp +++ b/qt/OpenRGBClientInfoPage/OpenRGBClientInfoPage.cpp @@ -131,11 +131,29 @@ void OpenRGBClientInfoPage::UpdateInfo() } /*-----------------------------------------------------*\ - | Create the top level tree widget items and display the| - | client IP addresses and protocol versions in them | + | Create the top level tree widget items | \*-----------------------------------------------------*/ QTreeWidgetItem* new_top_item = new QTreeWidgetItem(ui->ClientTree); - new_top_item->setText(0, QString::fromStdString(ResourceManager::get()->GetClients()[client_idx]->GetIP())); + + /*-----------------------------------------------------*\ + | First column, display the server IP and optionally | + | the server name if it exists | + \*-----------------------------------------------------*/ + std::string server_name = ResourceManager::get()->GetClients()[client_idx]->GetServerName(); + std::string ip = ResourceManager::get()->GetClients()[client_idx]->GetIP(); + + if(server_name == "") + { + new_top_item->setText(0, QString::fromStdString(ip)); + } + else + { + new_top_item->setText(0, QString::fromStdString(ip + ": " + server_name)); + } + + /*-----------------------------------------------------*\ + | Second column, display the protocol version | + \*-----------------------------------------------------*/ new_top_item->setText(1, QString::number(ResourceManager::get()->GetClients()[client_idx]->GetProtocolVersion())); /*-----------------------------------------------------*\