Implement server name string, server sends its name to the client if protocol version >= 5

This commit is contained in:
Adam Honse
2025-07-16 00:01:03 -05:00
parent fed03f88c8
commit 79626906a5
7 changed files with 109 additions and 6 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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 */

View File

@@ -13,7 +13,7 @@
#include "NetworkServer.h"
#include "LogManager.h"
#ifndef WIN32
#ifndef _WIN32
#include <sys/ioctl.h>
#include <netinet/tcp.h>
#include <sys/types.h>
@@ -28,8 +28,9 @@
const char yes = 1;
#ifdef WIN32
#ifdef _WIN32
#include <Windows.h>
#define MSG_NOSIGNAL 0
#else
#include <unistd.h>
#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;

View File

@@ -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<bool> server_online;
std::atomic<bool> server_listening;

View File

@@ -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 |
\*-----------------------------------------------------*/

View File

@@ -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()));
/*-----------------------------------------------------*\