Send and receive client string

This commit is contained in:
Adam Honse 2020-05-08 22:07:34 -05:00
parent 9ff4314840
commit edf1b251c0
5 changed files with 63 additions and 1 deletions

View file

@ -39,6 +39,16 @@ void NetworkClient::SetIP(const char *new_ip)
}
}
void NetworkClient::SetName(const char *new_name)
{
client_name = new_name;
if(server_connected == true)
{
SendData_ClientString();
}
}
void NetworkClient::SetPort(unsigned short new_port)
{
if(server_connected == false)
@ -65,6 +75,9 @@ void NetworkClient::StartClient()
//Start the listener thread
ListenThread = new std::thread(&NetworkClient::ListenThreadFunction, this);
//Send Client String
SendData_ClientString();
//Wait for server to connect
while(!server_connected)
{
@ -253,6 +266,23 @@ void NetworkClient::ProcessReply_ControllerData(unsigned int data_size, char * d
server_controllers.push_back(new_controller);
}
void NetworkClient::SendData_ClientString()
{
NetPacketHeader reply_hdr;
reply_hdr.pkt_magic[0] = 'O';
reply_hdr.pkt_magic[1] = 'R';
reply_hdr.pkt_magic[2] = 'G';
reply_hdr.pkt_magic[3] = 'B';
reply_hdr.pkt_dev_idx = 0;
reply_hdr.pkt_id = NET_PACKET_ID_SET_CLIENT_NAME;
reply_hdr.pkt_size = strlen(client_name.c_str()) + 1;
port.tcp_client_write((char *)&reply_hdr, sizeof(NetPacketHeader));
port.tcp_client_write((char *)client_name.c_str(), reply_hdr.pkt_size);
}
void NetworkClient::SendRequest_ControllerCount()
{
NetPacketHeader reply_hdr;

View file

@ -16,6 +16,7 @@ public:
bool GetOnline();
void SetIP(const char *new_ip);
void SetName(const char *new_name);
void SetPort(unsigned short new_port);
void StartClient();
@ -27,6 +28,8 @@ public:
void ProcessReply_ControllerCount(unsigned int data_size, char * data);
void ProcessReply_ControllerData(unsigned int data_size, char * data, unsigned int dev_idx);
void SendData_ClientString();
void SendRequest_ControllerCount();
void SendRequest_ControllerData(unsigned int dev_idx);
@ -45,6 +48,7 @@ protected:
std::vector<RGBController *> server_controllers;
private:
std::string client_name;
net_port port;
char port_ip[20];
unsigned short port_num;

View file

@ -15,6 +15,7 @@ enum
\*----------------------------------------------------------------------------------------------------------*/
NET_PACKET_ID_REQUEST_CONTROLLER_COUNT = 0, /* Request RGBController device count from server */
NET_PACKET_ID_REQUEST_CONTROLLER_DATA = 1, /* Request RGBController data block */
NET_PACKET_ID_SET_CLIENT_NAME = 50, /* Send client name string to server */
/*----------------------------------------------------------------------------------------------------------*\
| RGBController class functions |
@ -34,4 +35,4 @@ NetPacketHeader * InitNetPacketHeader
unsigned int pkt_dev_idx,
unsigned int pkt_id,
unsigned int pkt_size
);
);

View file

@ -431,6 +431,11 @@ void NetworkServer::ListenThreadFunction(NetworkClientInfo * client_info)
SendReply_ControllerData(client_sock, header.pkt_dev_idx);
break;
case NET_PACKET_ID_SET_CLIENT_NAME:
//printf "NET_PACKET_ID_SET_CLIENT_NAME\r\n" );
ProcessRequest_ClientString(client_sock, header.pkt_size, data);
break;
case NET_PACKET_ID_RGBCONTROLLER_RESIZEZONE:
//printf( "NET_PACKET_ID_RGBCONTROLLER_RESIZEZONE\r\n" );
@ -530,6 +535,26 @@ listen_done:
}
}
void NetworkServer::ProcessRequest_ClientString(int client_sock, unsigned int data_size, char *data)
{
for(unsigned int this_idx = 0; this_idx < ServerClients.size(); this_idx++)
{
if(ServerClients[this_idx]->client_sock == client_sock)
{
ServerClients[this_idx]->client_string = data;
break;
}
}
/*-------------------------------------------------*\
| 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]);
}
}
void NetworkServer::SendReply_ControllerCount(SOCKET client_sock)
{
NetPacketHeader reply_hdr;

View file

@ -37,6 +37,8 @@ public:
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);