Add client status update callback to NetworkClient
This commit is contained in:
parent
e2c2b8c1df
commit
15d23d3009
2 changed files with 51 additions and 5 deletions
|
|
@ -29,6 +29,21 @@ NetworkClient::NetworkClient(std::vector<RGBController *>& control) : controller
|
||||||
server_controller_count = 0;
|
server_controller_count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NetworkClient::ClientInfoChanged()
|
||||||
|
{
|
||||||
|
ClientInfoChangeMutex.lock();
|
||||||
|
|
||||||
|
/*-------------------------------------------------*\
|
||||||
|
| Client info has changed, call the callbacks |
|
||||||
|
\*-------------------------------------------------*/
|
||||||
|
for(unsigned int callback_idx = 0; callback_idx < ClientInfoChangeCallbacks.size(); callback_idx++)
|
||||||
|
{
|
||||||
|
ClientInfoChangeCallbacks[callback_idx](ClientInfoChangeCallbackArgs[callback_idx]);
|
||||||
|
}
|
||||||
|
|
||||||
|
ClientInfoChangeMutex.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
const char * NetworkClient::GetIP()
|
const char * NetworkClient::GetIP()
|
||||||
{
|
{
|
||||||
return(port_ip);
|
return(port_ip);
|
||||||
|
|
@ -44,6 +59,12 @@ bool NetworkClient::GetOnline()
|
||||||
return server_connected;
|
return server_connected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NetworkClient::RegisterClientInfoChangeCallback(NetClientCallback new_callback, void * new_callback_arg)
|
||||||
|
{
|
||||||
|
ClientInfoChangeCallbacks.push_back(new_callback);
|
||||||
|
ClientInfoChangeCallbackArgs.push_back(new_callback_arg);
|
||||||
|
}
|
||||||
|
|
||||||
void NetworkClient::SetIP(const char *new_ip)
|
void NetworkClient::SetIP(const char *new_ip)
|
||||||
{
|
{
|
||||||
if(server_connected == false)
|
if(server_connected == false)
|
||||||
|
|
@ -81,11 +102,10 @@ void NetworkClient::StartClient()
|
||||||
//Start the connection thread
|
//Start the connection thread
|
||||||
ConnectionThread = new std::thread(&NetworkClient::ConnectionThreadFunction, this);
|
ConnectionThread = new std::thread(&NetworkClient::ConnectionThreadFunction, this);
|
||||||
|
|
||||||
//Wait for server to initialize and connect
|
/*-------------------------------------------------*\
|
||||||
while((server_initialized == false) || (server_connected == false))
|
| Client info has changed, call the callbacks |
|
||||||
{
|
\*-------------------------------------------------*/
|
||||||
Sleep(100);
|
ClientInfoChanged();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkClient::StopClient()
|
void NetworkClient::StopClient()
|
||||||
|
|
@ -119,6 +139,11 @@ void NetworkClient::ConnectionThreadFunction()
|
||||||
|
|
||||||
//Server is not initialized
|
//Server is not initialized
|
||||||
server_initialized = false;
|
server_initialized = false;
|
||||||
|
|
||||||
|
/*-------------------------------------------------*\
|
||||||
|
| Client info has changed, call the callbacks |
|
||||||
|
\*-------------------------------------------------*/
|
||||||
|
ClientInfoChanged();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -172,6 +197,11 @@ void NetworkClient::ConnectionThreadFunction()
|
||||||
}
|
}
|
||||||
|
|
||||||
server_initialized = true;
|
server_initialized = true;
|
||||||
|
|
||||||
|
/*-------------------------------------------------*\
|
||||||
|
| Client info has changed, call the callbacks |
|
||||||
|
\*-------------------------------------------------*/
|
||||||
|
ClientInfoChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
Sleep(1000);
|
Sleep(1000);
|
||||||
|
|
@ -357,6 +387,11 @@ listen_done:
|
||||||
}
|
}
|
||||||
|
|
||||||
server_controllers.clear();
|
server_controllers.clear();
|
||||||
|
|
||||||
|
/*-------------------------------------------------*\
|
||||||
|
| Client info has changed, call the callbacks |
|
||||||
|
\*-------------------------------------------------*/
|
||||||
|
ClientInfoChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkClient::ProcessReply_ControllerCount(unsigned int data_size, char * data)
|
void NetworkClient::ProcessReply_ControllerCount(unsigned int data_size, char * data)
|
||||||
|
|
|
||||||
|
|
@ -10,19 +10,26 @@
|
||||||
#include "NetworkProtocol.h"
|
#include "NetworkProtocol.h"
|
||||||
#include "net_port.h"
|
#include "net_port.h"
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
typedef void (*NetClientCallback)(void *);
|
||||||
|
|
||||||
class NetworkClient
|
class NetworkClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NetworkClient(std::vector<RGBController *>& control);
|
NetworkClient(std::vector<RGBController *>& control);
|
||||||
|
|
||||||
|
void ClientInfoChanged();
|
||||||
|
|
||||||
const char * GetIP();
|
const char * GetIP();
|
||||||
unsigned short GetPort();
|
unsigned short GetPort();
|
||||||
bool GetOnline();
|
bool GetOnline();
|
||||||
|
|
||||||
|
void RegisterClientInfoChangeCallback(NetClientCallback new_callback, void * new_callback_arg);
|
||||||
|
|
||||||
void SetIP(const char *new_ip);
|
void SetIP(const char *new_ip);
|
||||||
void SetName(const char *new_name);
|
void SetName(const char *new_name);
|
||||||
void SetPort(unsigned short new_port);
|
void SetPort(unsigned short new_port);
|
||||||
|
|
@ -70,5 +77,9 @@ private:
|
||||||
std::thread * ConnectionThread;
|
std::thread * ConnectionThread;
|
||||||
std::thread * ListenThread;
|
std::thread * ListenThread;
|
||||||
|
|
||||||
|
std::mutex ClientInfoChangeMutex;
|
||||||
|
std::vector<NetClientCallback> ClientInfoChangeCallbacks;
|
||||||
|
std::vector<void *> ClientInfoChangeCallbackArgs;
|
||||||
|
|
||||||
int recv_select(SOCKET s, char *buf, int len, int flags);
|
int recv_select(SOCKET s, char *buf, int len, int flags);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue