mirror of
https://github.com/CalcProgrammer1/OpenRGB.git
synced 2026-01-05 13:47:50 -05:00
* SDK Protocol
* Server sends its name to client
* ProfileManager
* Rename existing profile commands
* Add Upload Profile, Download Profile, and Get Active Profile commands
* SettingsManager
* Add Get, Set, and Save Settings commands
* NetworkServer
* Formatting cleanup
* Use per-controller threads for handling NetworkServer controller-specific packets to avoid delays from controller mutexes
* NetworkClient
* Formatting cleanup
* RGBController
* Clean up and modularize descriptor functions
193 lines
10 KiB
C++
193 lines
10 KiB
C++
/*---------------------------------------------------------*\
|
|
| NetworkClient.h |
|
|
| |
|
|
| OpenRGB SDK network client |
|
|
| |
|
|
| Adam Honse (CalcProgrammer1) 09 May 2020 |
|
|
| |
|
|
| This file is part of the OpenRGB project |
|
|
| SPDX-License-Identifier: GPL-2.0-or-later |
|
|
\*---------------------------------------------------------*/
|
|
|
|
#pragma once
|
|
|
|
#include <condition_variable>
|
|
#include <mutex>
|
|
#include <thread>
|
|
#include <condition_variable>
|
|
#include "RGBController.h"
|
|
#include "NetworkProtocol.h"
|
|
#include "net_port.h"
|
|
|
|
typedef void (*NetClientCallback)(void *);
|
|
|
|
class NetworkClient
|
|
{
|
|
public:
|
|
NetworkClient(std::vector<RGBController *>& control);
|
|
~NetworkClient();
|
|
|
|
/*-----------------------------------------------------*\
|
|
| Client Information functions |
|
|
\*-----------------------------------------------------*/
|
|
bool GetConnected();
|
|
std::string GetIP();
|
|
unsigned short GetPort();
|
|
unsigned int GetProtocolVersion();
|
|
bool GetOnline();
|
|
std::string GetServerName();
|
|
|
|
/*-----------------------------------------------------*\
|
|
| Client Control functions |
|
|
\*-----------------------------------------------------*/
|
|
void SetIP(std::string new_ip);
|
|
void SetName(std::string new_name);
|
|
void SetPort(unsigned short new_port);
|
|
|
|
void StartClient();
|
|
void StopClient();
|
|
|
|
void SendRequest_ControllerData(unsigned int dev_idx);
|
|
void SendRequest_RescanDevices();
|
|
|
|
/*-----------------------------------------------------*\
|
|
| Client Callback functions |
|
|
\*-----------------------------------------------------*/
|
|
void ClearCallbacks();
|
|
void RegisterClientInfoChangeCallback(NetClientCallback new_callback, void * new_callback_arg);
|
|
|
|
/*-----------------------------------------------------*\
|
|
| ProfileManager functions |
|
|
\*-----------------------------------------------------*/
|
|
char * ProfileManager_GetProfileList();
|
|
void ProfileManager_LoadProfile(std::string profile_name);
|
|
void ProfileManager_SaveProfile(std::string profile_name);
|
|
void ProfileManager_DeleteProfile(std::string profile_name);
|
|
void ProfileManager_UploadProfile(std::string profile_json_str);
|
|
std::string ProfileManager_DownloadProfile(std::string profile_name);
|
|
std::string ProfileManager_GetActiveProfile();
|
|
|
|
/*-----------------------------------------------------*\
|
|
| SettingsManager functions |
|
|
\*-----------------------------------------------------*/
|
|
std::string SettingsManager_GetSettings(std::string settings_key);
|
|
void SettingsManager_SaveSettings();
|
|
void SettingsManager_SetSettings(std::string settings_json_str);
|
|
|
|
/*-----------------------------------------------------*\
|
|
| RGBController functions |
|
|
\*-----------------------------------------------------*/
|
|
void SendRequest_RGBController_ClearSegments(unsigned int dev_idx, int zone);
|
|
void SendRequest_RGBController_AddSegment(unsigned int dev_idx, unsigned char * data, unsigned int size);
|
|
void SendRequest_RGBController_ResizeZone(unsigned int dev_idx, int zone, int new_size);
|
|
|
|
void SendRequest_RGBController_UpdateLEDs(unsigned int dev_idx, unsigned char * data, unsigned int size);
|
|
void SendRequest_RGBController_UpdateZoneLEDs(unsigned int dev_idx, unsigned char * data, unsigned int size);
|
|
void SendRequest_RGBController_UpdateSingleLED(unsigned int dev_idx, unsigned char * data, unsigned int size);
|
|
|
|
void SendRequest_RGBController_SetCustomMode(unsigned int dev_idx);
|
|
|
|
void SendRequest_RGBController_UpdateMode(unsigned int dev_idx, unsigned char * data, unsigned int size);
|
|
void SendRequest_RGBController_UpdateZoneMode(unsigned int dev_idx, unsigned char * data, unsigned int size);
|
|
void SendRequest_RGBController_SaveMode(unsigned int dev_idx, unsigned char * data, unsigned int size);
|
|
|
|
void WaitOnControllerData();
|
|
|
|
std::vector<RGBController *> server_controllers;
|
|
|
|
private:
|
|
/*-----------------------------------------------------*\
|
|
| Client state variables |
|
|
\*-----------------------------------------------------*/
|
|
std::atomic<bool> client_active;
|
|
bool client_string_sent;
|
|
bool controller_data_received;
|
|
bool controller_data_requested;
|
|
bool protocol_initialized;
|
|
bool change_in_progress;
|
|
unsigned int requested_controllers;
|
|
std::mutex send_in_progress;
|
|
|
|
NetPacketHeader response_header;
|
|
char * response_data_ptr;
|
|
std::mutex waiting_on_response_mutex;
|
|
std::condition_variable waiting_on_response_cv;
|
|
|
|
/*-----------------------------------------------------*\
|
|
| Client information |
|
|
\*-----------------------------------------------------*/
|
|
std::string client_name;
|
|
SOCKET client_sock;
|
|
net_port port;
|
|
std::string port_ip;
|
|
unsigned short port_num;
|
|
|
|
/*-----------------------------------------------------*\
|
|
| Server information |
|
|
\*-----------------------------------------------------*/
|
|
std::string server_name;
|
|
bool server_connected;
|
|
bool server_initialized;
|
|
bool server_reinitialize;
|
|
unsigned int server_controller_count;
|
|
bool server_controller_count_requested;
|
|
bool server_controller_count_received;
|
|
unsigned int server_protocol_version;
|
|
bool server_protocol_version_received;
|
|
|
|
/*-----------------------------------------------------*\
|
|
| Client threads |
|
|
\*-----------------------------------------------------*/
|
|
std::mutex connection_mutex;
|
|
std::condition_variable connection_cv;
|
|
std::thread * ConnectionThread;
|
|
std::thread * ListenThread;
|
|
|
|
/*-----------------------------------------------------*\
|
|
| Callbacks |
|
|
\*-----------------------------------------------------*/
|
|
std::mutex ClientInfoChangeMutex;
|
|
std::vector<NetClientCallback> ClientInfoChangeCallbacks;
|
|
std::vector<void *> ClientInfoChangeCallbackArgs;
|
|
|
|
/*-----------------------------------------------------*\
|
|
| Controller list |
|
|
\*-----------------------------------------------------*/
|
|
std::mutex ControllerListMutex;
|
|
std::vector<RGBController *>& controllers;
|
|
|
|
/*-----------------------------------------------------*\
|
|
| Client callback signal functions |
|
|
\*-----------------------------------------------------*/
|
|
void ClientInfoChanged();
|
|
|
|
/*-----------------------------------------------------*\
|
|
| Client thread functions |
|
|
\*-----------------------------------------------------*/
|
|
void ConnectionThreadFunction();
|
|
void ListenThreadFunction();
|
|
|
|
/*-----------------------------------------------------*\
|
|
| Private Client functions |
|
|
\*-----------------------------------------------------*/
|
|
void ProcessReply_ControllerCount(unsigned int data_size, char * data);
|
|
void ProcessReply_ControllerData(unsigned int data_size, char * data, unsigned int dev_idx);
|
|
void ProcessReply_ProtocolVersion(unsigned int data_size, char * data);
|
|
void ProcessRequest_DeviceListChanged();
|
|
void ProcessRequest_ServerString(unsigned int data_size, char * data);
|
|
|
|
void SendData_ClientString();
|
|
void SendRequest_ControllerCount();
|
|
void SendRequest_ProtocolVersion();
|
|
|
|
/*-----------------------------------------------------*\
|
|
| Private ProfileManager functions |
|
|
\*-----------------------------------------------------*/
|
|
std::vector<std::string> * ProcessReply_ProfileList(unsigned int data_size, char * data);
|
|
|
|
/*-----------------------------------------------------*\
|
|
| Private helper functions |
|
|
\*-----------------------------------------------------*/
|
|
int recv_select(SOCKET s, char *buf, int len, int flags);
|
|
};
|