From edf1b251c0d024cf84d2a766938f9bf2e014773b Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Fri, 8 May 2020 22:07:34 -0500 Subject: [PATCH] Send and receive client string --- NetworkClient.cpp | 30 ++++++++++++++++++++++++++++++ NetworkClient.h | 4 ++++ NetworkProtocol.h | 3 ++- NetworkServer.cpp | 25 +++++++++++++++++++++++++ NetworkServer.h | 2 ++ 5 files changed, 63 insertions(+), 1 deletion(-) diff --git a/NetworkClient.cpp b/NetworkClient.cpp index b1787179..9c9dd80e 100644 --- a/NetworkClient.cpp +++ b/NetworkClient.cpp @@ -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; diff --git a/NetworkClient.h b/NetworkClient.h index 78162568..b5dbca24 100644 --- a/NetworkClient.h +++ b/NetworkClient.h @@ -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 server_controllers; private: + std::string client_name; net_port port; char port_ip[20]; unsigned short port_num; diff --git a/NetworkProtocol.h b/NetworkProtocol.h index 5ff19dfe..daaab366 100644 --- a/NetworkProtocol.h +++ b/NetworkProtocol.h @@ -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 - ); \ No newline at end of file + ); diff --git a/NetworkServer.cpp b/NetworkServer.cpp index 40d0eec6..07d86242 100644 --- a/NetworkServer.cpp +++ b/NetworkServer.cpp @@ -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; diff --git a/NetworkServer.h b/NetworkServer.h index b56f4cc5..17a57760 100644 --- a/NetworkServer.h +++ b/NetworkServer.h @@ -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);