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;
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
return(port_ip);
|
||||
|
|
@ -44,6 +59,12 @@ bool NetworkClient::GetOnline()
|
|||
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)
|
||||
{
|
||||
if(server_connected == false)
|
||||
|
|
@ -81,11 +102,10 @@ void NetworkClient::StartClient()
|
|||
//Start the connection thread
|
||||
ConnectionThread = new std::thread(&NetworkClient::ConnectionThreadFunction, this);
|
||||
|
||||
//Wait for server to initialize and connect
|
||||
while((server_initialized == false) || (server_connected == false))
|
||||
{
|
||||
Sleep(100);
|
||||
}
|
||||
/*-------------------------------------------------*\
|
||||
| Client info has changed, call the callbacks |
|
||||
\*-------------------------------------------------*/
|
||||
ClientInfoChanged();
|
||||
}
|
||||
|
||||
void NetworkClient::StopClient()
|
||||
|
|
@ -119,6 +139,11 @@ void NetworkClient::ConnectionThreadFunction()
|
|||
|
||||
//Server is not initialized
|
||||
server_initialized = false;
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| Client info has changed, call the callbacks |
|
||||
\*-------------------------------------------------*/
|
||||
ClientInfoChanged();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -172,6 +197,11 @@ void NetworkClient::ConnectionThreadFunction()
|
|||
}
|
||||
|
||||
server_initialized = true;
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| Client info has changed, call the callbacks |
|
||||
\*-------------------------------------------------*/
|
||||
ClientInfoChanged();
|
||||
}
|
||||
|
||||
Sleep(1000);
|
||||
|
|
@ -357,6 +387,11 @@ listen_done:
|
|||
}
|
||||
|
||||
server_controllers.clear();
|
||||
|
||||
/*-------------------------------------------------*\
|
||||
| Client info has changed, call the callbacks |
|
||||
\*-------------------------------------------------*/
|
||||
ClientInfoChanged();
|
||||
}
|
||||
|
||||
void NetworkClient::ProcessReply_ControllerCount(unsigned int data_size, char * data)
|
||||
|
|
|
|||
|
|
@ -10,19 +10,26 @@
|
|||
#include "NetworkProtocol.h"
|
||||
#include "net_port.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
#pragma once
|
||||
|
||||
typedef void (*NetClientCallback)(void *);
|
||||
|
||||
class NetworkClient
|
||||
{
|
||||
public:
|
||||
NetworkClient(std::vector<RGBController *>& control);
|
||||
|
||||
void ClientInfoChanged();
|
||||
|
||||
const char * GetIP();
|
||||
unsigned short GetPort();
|
||||
bool GetOnline();
|
||||
|
||||
void RegisterClientInfoChangeCallback(NetClientCallback new_callback, void * new_callback_arg);
|
||||
|
||||
void SetIP(const char *new_ip);
|
||||
void SetName(const char *new_name);
|
||||
void SetPort(unsigned short new_port);
|
||||
|
|
@ -70,5 +77,9 @@ private:
|
|||
std::thread * ConnectionThread;
|
||||
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);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue