SDK6 PROTOCOL CHANGE: Add client and server hostname set packets
This commit is contained in:
+55
-1
@@ -12,6 +12,7 @@
|
||||
#include <cstring>
|
||||
#include "LogManager.h"
|
||||
#include "NetworkClient.h"
|
||||
#include "NetworkProtocol.h"
|
||||
#include "ProfileManager.h"
|
||||
#include "ResourceManager.h"
|
||||
#include "RGBController_Network.h"
|
||||
@@ -81,6 +82,7 @@ NetworkClient::NetworkClient()
|
||||
| NET_CLIENT_FLAG_SUPPORTS_PROFILEMANAGER
|
||||
| NET_CLIENT_FLAG_SUPPORTS_SETTINGSMANAGER;
|
||||
client_flags_sent = false;
|
||||
client_hostname = GetHostname();
|
||||
client_is_local_client = false;
|
||||
client_string_sent = false;
|
||||
client_sock = -1;
|
||||
@@ -117,6 +119,7 @@ static bool IsCallbackPacket(unsigned int pkt_id)
|
||||
{
|
||||
case NET_PACKET_ID_REQUEST_CONTROLLER_COUNT:
|
||||
case NET_PACKET_ID_REQUEST_CONTROLLER_DATA:
|
||||
case NET_PACKET_ID_SET_SERVER_HOSTNAME:
|
||||
case NET_PACKET_ID_SET_SERVER_NAME:
|
||||
case NET_PACKET_ID_DEVICE_LIST_UPDATED:
|
||||
case NET_PACKET_ID_DETECTION_STARTED:
|
||||
@@ -214,6 +217,10 @@ void NetworkClient::ReceiveQueueThreadFunction()
|
||||
ProcessReply_ControllerData(entry.header.pkt_size, entry.data, entry.header.pkt_dev_id);
|
||||
break;
|
||||
|
||||
case NET_PACKET_ID_SET_SERVER_HOSTNAME:
|
||||
ProcessRequest_ServerHostname(entry.header.pkt_size, entry.data);
|
||||
break;
|
||||
|
||||
case NET_PACKET_ID_SET_SERVER_NAME:
|
||||
ProcessRequest_ServerString(entry.header.pkt_size, entry.data);
|
||||
break;
|
||||
@@ -320,6 +327,11 @@ bool NetworkClient::GetOnline()
|
||||
return(server_connected && client_string_sent && protocol_initialized && server_flags_initialized && server_initialized);
|
||||
}
|
||||
|
||||
std::string NetworkClient::GetServerHostname()
|
||||
{
|
||||
return(server_hostname);
|
||||
}
|
||||
|
||||
std::string NetworkClient::GetServerName()
|
||||
{
|
||||
return(server_name);
|
||||
@@ -1828,9 +1840,10 @@ void NetworkClient::ConnectionThreadFunction()
|
||||
{
|
||||
/*-----------------------------------------*\
|
||||
| Once server is connected, send client |
|
||||
| string |
|
||||
| string and hostname |
|
||||
\*-----------------------------------------*/
|
||||
SendData_ClientString();
|
||||
SendData_ClientHostname();
|
||||
|
||||
client_string_sent = true;
|
||||
}
|
||||
@@ -2049,6 +2062,7 @@ void NetworkClient::ListenThreadFunction()
|
||||
\*---------------------------------------------*/
|
||||
case NET_PACKET_ID_REQUEST_CONTROLLER_COUNT:
|
||||
case NET_PACKET_ID_REQUEST_CONTROLLER_DATA:
|
||||
case NET_PACKET_ID_SET_SERVER_HOSTNAME:
|
||||
case NET_PACKET_ID_SET_SERVER_NAME:
|
||||
case NET_PACKET_ID_DEVICE_LIST_UPDATED:
|
||||
case NET_PACKET_ID_DETECTION_STARTED:
|
||||
@@ -2559,6 +2573,25 @@ void NetworkClient::ProcessRequest_ServerFlags(unsigned int data_size, unsigned
|
||||
server_flags_initialized = true;
|
||||
}
|
||||
|
||||
void NetworkClient::ProcessRequest_ServerHostname(unsigned int data_size, unsigned char* data_ptr)
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Validate inputs |
|
||||
\*-----------------------------------------------------*/
|
||||
if((data_size == 0) || (data_ptr == NULL))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
server_hostname.assign((char*)data_ptr, data_size);
|
||||
server_hostname = StringUtils::remove_null_terminating_chars(server_hostname);
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Client info has changed, call the callbacks |
|
||||
\*-----------------------------------------------------*/
|
||||
SignalNetworkClientUpdate(NETWORKCLIENT_UPDATE_REASON_SERVER_HOSTNAME_RECEIVED);
|
||||
}
|
||||
|
||||
void NetworkClient::ProcessRequest_ServerString(unsigned int data_size, unsigned char* data_ptr)
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
@@ -2590,6 +2623,27 @@ void NetworkClient::SendData_ClientFlags()
|
||||
send_in_progress.unlock();
|
||||
}
|
||||
|
||||
void NetworkClient::SendData_ClientHostname()
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Client hostname was added in protocol 6, return if |
|
||||
| it is not supported |
|
||||
\*-----------------------------------------------------*/
|
||||
if(protocol_version < 6)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
NetPacketHeader reply_hdr;
|
||||
|
||||
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_SET_CLIENT_HOSTNAME, (unsigned int)strlen(client_hostname.c_str()) + 1);
|
||||
|
||||
send_in_progress.lock();
|
||||
send(client_sock, (char *)&reply_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
|
||||
send(client_sock, (char *)client_hostname.c_str(), reply_hdr.pkt_size, MSG_NOSIGNAL);
|
||||
send_in_progress.unlock();
|
||||
}
|
||||
|
||||
void NetworkClient::SendData_ClientString()
|
||||
{
|
||||
NetPacketHeader reply_hdr;
|
||||
|
||||
@@ -45,6 +45,7 @@ enum
|
||||
NETWORKCLIENT_UPDATE_REASON_DETECTION_COMPLETE, /* Detection completed */
|
||||
NETWORKCLIENT_UPDATE_REASON_PROFILEMANAGER_PROFILE_LIST_UPDATED, /* Profile list updated */
|
||||
NETWORKCLIENT_UPDATE_REASON_PROFILEMANAGER_ACTIVE_PROFILE_CHANGED, /* Active profile changed */
|
||||
NETWORKCLIENT_UPDATE_REASON_SERVER_HOSTNAME_RECEIVED, /* Server hostname received */
|
||||
};
|
||||
|
||||
typedef struct
|
||||
@@ -81,6 +82,7 @@ public:
|
||||
unsigned short GetPort();
|
||||
unsigned int GetProtocolVersion();
|
||||
bool GetOnline();
|
||||
std::string GetServerHostname();
|
||||
std::string GetServerName();
|
||||
bool GetSupportsRGBControllerAPI();
|
||||
bool GetSupportsLogManagerAPI();
|
||||
@@ -219,6 +221,7 @@ private:
|
||||
| Client information |
|
||||
\*-----------------------------------------------------*/
|
||||
unsigned int client_flags;
|
||||
std::string client_hostname;
|
||||
std::string client_name;
|
||||
SOCKET client_sock;
|
||||
net_port port;
|
||||
@@ -230,6 +233,7 @@ private:
|
||||
\*-----------------------------------------------------*/
|
||||
unsigned int server_flags;
|
||||
bool server_flags_initialized;
|
||||
std::string server_hostname;
|
||||
std::string server_name;
|
||||
bool server_connected;
|
||||
bool server_initialized;
|
||||
@@ -303,6 +307,7 @@ private:
|
||||
void ProcessRequest_DeviceListChanged();
|
||||
void ProcessRequest_RGBController_SignalUpdate(unsigned int data_size, unsigned char* data_ptr, unsigned int dev_id);
|
||||
void ProcessRequest_ServerFlags(unsigned int data_size, unsigned char* data_ptr);
|
||||
void ProcessRequest_ServerHostname(unsigned int data_size, unsigned char* data_ptr);
|
||||
void ProcessRequest_ServerString(unsigned int data_size, unsigned char* data_ptr);
|
||||
|
||||
void ProcessRequest_LogManager_LoggedEntry(unsigned int data_size, unsigned char* data_ptr);
|
||||
@@ -313,6 +318,7 @@ private:
|
||||
void ProcessRequest_ProfileManager_ProfileLoaded(unsigned int data_size, unsigned char* data_ptr);
|
||||
|
||||
void SendData_ClientFlags();
|
||||
void SendData_ClientHostname();
|
||||
void SendData_ClientString();
|
||||
void SendRequest_ControllerIDs();
|
||||
void SendRequest_ProtocolVersion();
|
||||
|
||||
@@ -10,6 +10,16 @@
|
||||
\*---------------------------------------------------------*/
|
||||
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#include <winsock2.h>
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <climits>
|
||||
#endif
|
||||
|
||||
#include "NetworkProtocol.h"
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
@@ -17,6 +27,46 @@
|
||||
\*-----------------------------------------------------*/
|
||||
const char openrgb_sdk_magic[OPENRGB_SDK_MAGIC_SIZE] = { 'O', 'R', 'G', 'B' };
|
||||
|
||||
std::string GetHostname()
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| Determine maximum hostname limit or fallback to |
|
||||
| defensive size |
|
||||
\*-----------------------------------------------------*/
|
||||
#if defined(HOST_NAME_MAX)
|
||||
const size_t buffer_size = HOST_NAME_MAX + 1;
|
||||
#else
|
||||
const size_t buffer_size = 256;
|
||||
#endif
|
||||
|
||||
char buffer[buffer_size];
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
/*-------------------------------------------------*\
|
||||
| Initialize Winsock data required on Windows |
|
||||
| before socket calls |
|
||||
\*-------------------------------------------------*/
|
||||
WSADATA wsa_data;
|
||||
if(WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0)
|
||||
{
|
||||
return "unknown";
|
||||
}
|
||||
#endif
|
||||
|
||||
std::string hostname = "unknown";
|
||||
|
||||
if(gethostname(buffer, sizeof(buffer)) == 0)
|
||||
{
|
||||
hostname = buffer;
|
||||
}
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
WSACleanup();
|
||||
#endif
|
||||
|
||||
return(hostname);
|
||||
}
|
||||
|
||||
void InitNetPacketHeader
|
||||
(
|
||||
NetPacketHeader * pkt_hdr,
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
/*---------------------------------------------------------------------*\
|
||||
| OpenRGB SDK protocol version |
|
||||
| |
|
||||
@@ -108,6 +110,8 @@ enum
|
||||
NET_PACKET_ID_SET_SERVER_NAME = 51, /* Send server name string to client */
|
||||
NET_PACKET_ID_SET_CLIENT_FLAGS = 52, /* Send client flags to server */
|
||||
NET_PACKET_ID_SET_SERVER_FLAGS = 53, /* Send server flags to client */
|
||||
NET_PACKET_ID_SET_CLIENT_HOSTNAME = 54, /* Send client hostname string to server */
|
||||
NET_PACKET_ID_SET_SERVER_HOSTNAME = 55, /* Send server hostname string to client */
|
||||
|
||||
/*----------------------------------------------------------------------------------------------------------*\
|
||||
| Detection functions |
|
||||
@@ -199,6 +203,8 @@ enum
|
||||
NET_PACKET_ID_RGBCONTROLLER_SIGNALUPDATE = 1150, /* RGBController::SignalUpdate() */
|
||||
};
|
||||
|
||||
std::string GetHostname();
|
||||
|
||||
void InitNetPacketHeader
|
||||
(
|
||||
NetPacketHeader * pkt_hdr,
|
||||
|
||||
+75
-5
@@ -154,6 +154,7 @@ NetworkServer::NetworkServer()
|
||||
{
|
||||
host = OPENRGB_SDK_HOST;
|
||||
port_num = OPENRGB_SDK_PORT;
|
||||
server_hostname = GetHostname();
|
||||
server_online = false;
|
||||
server_listening = false;
|
||||
legacy_workaround_enabled = false;
|
||||
@@ -220,24 +221,44 @@ unsigned int NetworkServer::GetNumClients()
|
||||
return((unsigned int)ServerClients.size());
|
||||
}
|
||||
|
||||
const char * NetworkServer::GetClientString(unsigned int client_num)
|
||||
std::string NetworkServer::GetClientHostname(unsigned int client_num)
|
||||
{
|
||||
const char * result;
|
||||
std::string hostname;
|
||||
|
||||
ServerClientsMutex.lock();
|
||||
|
||||
if(client_num < ServerClients.size())
|
||||
{
|
||||
result = ServerClients[client_num]->client_string.c_str();
|
||||
hostname = ServerClients[client_num]->client_hostname;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = "";
|
||||
hostname = "";
|
||||
}
|
||||
|
||||
ServerClientsMutex.unlock();
|
||||
|
||||
return(result);
|
||||
return(hostname);
|
||||
}
|
||||
|
||||
std::string NetworkServer::GetClientString(unsigned int client_num)
|
||||
{
|
||||
std::string name;
|
||||
|
||||
ServerClientsMutex.lock();
|
||||
|
||||
if(client_num < ServerClients.size())
|
||||
{
|
||||
name = ServerClients[client_num]->client_string.c_str();
|
||||
}
|
||||
else
|
||||
{
|
||||
name = "";
|
||||
}
|
||||
|
||||
ServerClientsMutex.unlock();
|
||||
|
||||
return(name);
|
||||
}
|
||||
|
||||
const char * NetworkServer::GetClientIP(unsigned int client_num)
|
||||
@@ -1318,6 +1339,11 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo* client_info)
|
||||
SendReply_ServerFlags(client_info);
|
||||
break;
|
||||
|
||||
case NET_PACKET_ID_SET_CLIENT_HOSTNAME:
|
||||
status = ProcessRequest_ClientHostname(client_info, header.pkt_size, data);
|
||||
SendReply_ServerHostname(client_info);
|
||||
break;
|
||||
|
||||
case NET_PACKET_ID_SET_CLIENT_NAME:
|
||||
status = ProcessRequest_ClientString(client_info, header.pkt_size, data);
|
||||
break;
|
||||
@@ -1611,6 +1637,29 @@ NetPacketStatus NetworkServer::ProcessRequest_ClientFlags(NetworkClientInfo* cli
|
||||
return(NET_PACKET_STATUS_ERROR_INVALID_DATA);
|
||||
}
|
||||
|
||||
NetPacketStatus NetworkServer::ProcessRequest_ClientHostname(NetworkClientInfo* client_info, unsigned int data_size, unsigned char* data_ptr)
|
||||
{
|
||||
/*-----------------------------------------------------*\
|
||||
| If data pointer is null, return |
|
||||
\*-----------------------------------------------------*/
|
||||
if(data_ptr == NULL)
|
||||
{
|
||||
return(NET_PACKET_STATUS_ERROR_INVALID_DATA);
|
||||
}
|
||||
|
||||
ServerClientsMutex.lock();
|
||||
client_info->client_hostname.assign((char*)data_ptr, data_size);
|
||||
client_info->client_hostname = StringUtils::remove_null_terminating_chars(client_info->client_hostname);
|
||||
ServerClientsMutex.unlock();
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| Client info has changed, call the callbacks |
|
||||
\*-------------------------------------------------*/
|
||||
SignalClientInfoChanged();
|
||||
|
||||
return(NET_PACKET_STATUS_OK);
|
||||
}
|
||||
|
||||
NetPacketStatus NetworkServer::ProcessRequest_ClientProtocolVersion(NetworkClientInfo* client_info, unsigned int data_size, unsigned char* data_ptr)
|
||||
{
|
||||
unsigned int protocol_version = 0;
|
||||
@@ -3687,6 +3736,27 @@ void NetworkServer::SendReply_ServerFlags(NetworkClientInfo* client_info)
|
||||
ServerClientsMutex.unlock();
|
||||
}
|
||||
|
||||
void NetworkServer::SendReply_ServerHostname(NetworkClientInfo* client_info)
|
||||
{
|
||||
/*---------------------------------------------------------*\
|
||||
| Send server hostname to client only if protocol is 6 or |
|
||||
| greater |
|
||||
\*---------------------------------------------------------*/
|
||||
ServerClientsMutex.lock();
|
||||
if(client_info->client_protocol_version >= 6)
|
||||
{
|
||||
NetPacketHeader reply_hdr;
|
||||
|
||||
InitNetPacketHeader(&reply_hdr, 0, NET_PACKET_ID_SET_SERVER_HOSTNAME, (unsigned int)strlen(server_hostname.c_str()) + 1);
|
||||
|
||||
send_in_progress.lock();
|
||||
send(client_info->client_sock, (char *)&reply_hdr, sizeof(NetPacketHeader), MSG_NOSIGNAL);
|
||||
send(client_info->client_sock, (char *)server_hostname.c_str(), reply_hdr.pkt_size, MSG_NOSIGNAL);
|
||||
send_in_progress.unlock();
|
||||
}
|
||||
ServerClientsMutex.unlock();
|
||||
}
|
||||
|
||||
void NetworkServer::SendReply_ServerString(NetworkClientInfo* client_info)
|
||||
{
|
||||
/*---------------------------------------------------------*\
|
||||
|
||||
+6
-1
@@ -59,6 +59,7 @@ public:
|
||||
SOCKET client_sock;
|
||||
unsigned int client_flags;
|
||||
std::thread* client_listen_thread;
|
||||
std::string client_hostname;
|
||||
std::string client_string;
|
||||
unsigned int client_protocol_version;
|
||||
std::string client_ip;
|
||||
@@ -119,7 +120,8 @@ public:
|
||||
bool GetOnline();
|
||||
bool GetListening();
|
||||
unsigned int GetNumClients();
|
||||
const char * GetClientString(unsigned int client_num);
|
||||
std::string GetClientHostname(unsigned int client_num);
|
||||
std::string GetClientString(unsigned int client_num);
|
||||
const char * GetClientIP(unsigned int client_num);
|
||||
unsigned int GetClientProtocolVersion(unsigned int client_num);
|
||||
|
||||
@@ -178,6 +180,7 @@ private:
|
||||
unsigned short port_num;
|
||||
std::mutex send_in_progress;
|
||||
unsigned int server_flags;
|
||||
std::string server_hostname;
|
||||
std::string server_name;
|
||||
std::atomic<bool> server_online;
|
||||
std::atomic<bool> server_listening;
|
||||
@@ -260,6 +263,7 @@ private:
|
||||
| Server Protocol functions |
|
||||
\*-----------------------------------------------------*/
|
||||
NetPacketStatus ProcessRequest_ClientFlags(NetworkClientInfo* client_info, unsigned int data_size, unsigned char* data_ptr);
|
||||
NetPacketStatus ProcessRequest_ClientHostname(NetworkClientInfo* client_info, unsigned int data_size, unsigned char* data_ptr);
|
||||
NetPacketStatus ProcessRequest_ClientProtocolVersion(NetworkClientInfo* client_info, unsigned int data_size, unsigned char* data_ptr);
|
||||
NetPacketStatus ProcessRequest_ClientString(NetworkClientInfo* client_info, unsigned int data_size, unsigned char* data_ptr);
|
||||
NetPacketStatus ProcessRequest_RescanDevices();
|
||||
@@ -311,6 +315,7 @@ private:
|
||||
void SendReply_ControllerData(NetworkClientInfo* client_info, unsigned int dev_id, unsigned int protocol_version);
|
||||
void SendReply_ProtocolVersion(NetworkClientInfo* client_info);
|
||||
void SendReply_ServerFlags(NetworkClientInfo* client_info);
|
||||
void SendReply_ServerHostname(NetworkClientInfo* client_info);
|
||||
void SendReply_ServerString(NetworkClientInfo* client_info);
|
||||
|
||||
void SendReply_PluginList(NetworkClientInfo* client_info);
|
||||
|
||||
@@ -147,17 +147,22 @@ void OpenRGBClientInfoPage::UpdateInfo()
|
||||
| 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();
|
||||
std::string server_hostname = ResourceManager::get()->GetClients()[client_idx]->GetServerHostname();
|
||||
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
|
||||
else if(server_hostname == "")
|
||||
{
|
||||
new_top_item->setText(0, QString::fromStdString(ip + ": " + server_name));
|
||||
}
|
||||
else
|
||||
{
|
||||
new_top_item->setText(0, QString::fromStdString(ip + ": " + server_name + " (" + server_hostname + ")"));
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------*\
|
||||
| Second column, display the protocol version |
|
||||
|
||||
@@ -95,7 +95,14 @@ void OpenRGBServerInfoPage::UpdateInfo()
|
||||
|
||||
new_item->setText(0, network_server->GetClientIP(client_idx));
|
||||
new_item->setText(1, QString::number(network_server->GetClientProtocolVersion(client_idx)));
|
||||
new_item->setText(2, network_server->GetClientString(client_idx));
|
||||
if(network_server->GetClientHostname(client_idx) != "")
|
||||
{
|
||||
new_item->setText(2, QString::fromStdString(network_server->GetClientString(client_idx) + " (" + network_server->GetClientHostname(client_idx) + ")"));
|
||||
}
|
||||
else
|
||||
{
|
||||
new_item->setText(2, QString::fromStdString(network_server->GetClientString(client_idx)));
|
||||
}
|
||||
|
||||
ui->ServerClientTree->addTopLevelItem(new_item);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user