mirror of
https://github.com/CalcProgrammer1/OpenRGB.git
synced 2025-12-31 03:07:49 -05:00
This patch resolves several bugs: * NetworkServer would allocate various instances of `NetworkClientInfo`. This is patched by deallocating the NetworkClientInfo when it's listening thread ends in the `listen_done` section. * Memory would be allocated for threads that wouldn't be freed. This is resolved by detaching from the threads, so they no longer need to be joined to be freed, and then freeing the memory as they finish. * Thread-Safety issues involving `ServerClients` would result in stray `NetworkClientInfo`'s not being removed from the list in certain situations. This is resolved by used a mutex to lock access to this from different threads.
80 lines
2.7 KiB
C++
80 lines
2.7 KiB
C++
/*-----------------------------------------*\
|
|
| NetworkServer.h |
|
|
| |
|
|
| Server header for OpenRGB SDK |
|
|
| |
|
|
| Adam Honse (CalcProgrammer1) 5/9/2020 |
|
|
\*-----------------------------------------*/
|
|
|
|
#include "RGBController.h"
|
|
#include "NetworkProtocol.h"
|
|
#include "net_port.h"
|
|
|
|
#include <mutex>
|
|
#include <thread>
|
|
#include <chrono>
|
|
|
|
#pragma once
|
|
|
|
typedef void (*NetServerCallback)(void *);
|
|
|
|
struct NetworkClientInfo
|
|
{
|
|
SOCKET client_sock;
|
|
std::thread * client_listen_thread;
|
|
std::string client_string;
|
|
char client_ip[INET_ADDRSTRLEN];
|
|
};
|
|
|
|
class NetworkServer
|
|
{
|
|
public:
|
|
NetworkServer(std::vector<RGBController *>& control);
|
|
|
|
unsigned short GetPort();
|
|
bool GetOnline();
|
|
unsigned int GetNumClients();
|
|
const char * GetClientString(unsigned int client_num);
|
|
const char * GetClientIP(unsigned int client_num);
|
|
|
|
void ClientInfoChanged();
|
|
void RegisterClientInfoChangeCallback(NetServerCallback, void * new_callback_arg);
|
|
|
|
void SetPort(unsigned short new_port);
|
|
|
|
void StartServer();
|
|
void StopServer();
|
|
|
|
void ConnectionThreadFunction();
|
|
void ListenThreadFunction(NetworkClientInfo * client_sock);
|
|
|
|
void ProcessRequest_ClientString(SOCKET client_sock, unsigned int data_size, char * data);
|
|
|
|
void SendReply_ControllerCount(SOCKET client_sock);
|
|
void SendReply_ControllerData(SOCKET client_sock, unsigned int dev_idx);
|
|
|
|
protected:
|
|
unsigned short port_num;
|
|
bool server_online;
|
|
|
|
std::vector<RGBController *>& controllers;
|
|
|
|
std::mutex ServerClientsMutex;
|
|
std::vector<NetworkClientInfo *> ServerClients;
|
|
std::thread * ConnectionThread;
|
|
|
|
std::mutex ClientInfoChangeMutex;
|
|
std::vector<NetServerCallback> ClientInfoChangeCallbacks;
|
|
std::vector<void *> ClientInfoChangeCallbackArgs;
|
|
|
|
private:
|
|
#ifdef WIN32
|
|
WSADATA wsa;
|
|
#endif
|
|
|
|
SOCKET server_sock;
|
|
|
|
int accept_select(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
|
|
int recv_select(SOCKET s, char *buf, int len, int flags);
|
|
};
|