forked from TeamFahQ/OpenRGB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkServer.h
79 lines (59 loc) · 2.7 KB
/
NetworkServer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*-----------------------------------------*\
| 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);
};