From dff667751bafa3564a8ab515672c28efe1f43922 Mon Sep 17 00:00:00 2001 From: Adam Honse Date: Tue, 21 Apr 2020 23:54:06 -0500 Subject: [PATCH] Start work on network client --- NetworkClient.cpp | 141 +++++++++++++++++++++++++++++++++++++++++- NetworkServer.cpp | 12 ++-- net_port/net_port.cpp | 67 +------------------- 3 files changed, 147 insertions(+), 73 deletions(-) diff --git a/NetworkClient.cpp b/NetworkClient.cpp index d1dbf018..7e61448d 100644 --- a/NetworkClient.cpp +++ b/NetworkClient.cpp @@ -1,8 +1,51 @@ #include "NetworkClient.h" +#ifdef WIN32 +#include +#else +#include + +static void Sleep(unsigned int milliseconds) +{ + usleep(1000 * milliseconds); +} +#endif + +//Thread functions have different types in Windows and Linux +#ifdef WIN32 +#define THREAD static void +#define THREADRETURN +#else +#define THREAD static void* +#define THREADRETURN return(NULL); +#endif + +THREAD connection_thread(void *param) +{ + NetworkClient* client = static_cast(param); + client->ConnectionThread(); + THREADRETURN +} + +THREAD listen_thread(void *param) +{ + NetworkClient* client = static_cast(param); + client->ListenThread(); + THREADRETURN +} + NetworkClient::NetworkClient(std::vector& control) : controllers(control) { + port.tcp_client("127.0.0.1", "1337"); + //Start the connection thread +#ifdef WIN32 + _beginthread(connection_thread, 0, this); +#else + pthread_t thread; + pthread_create(&thread, NULL, &connection_thread, this); + pthread_create(&thread, NULL, &listen_thread, this); +#endif } void NetworkClient::ConnectionThread() @@ -11,16 +54,110 @@ void NetworkClient::ConnectionThread() while(1) { //Connect to server and reconnect if the connection is lost + + //Try to connect to server + port.tcp_client_connect(); + + //Wait 1 second between tries; + Sleep(1000); } } void NetworkClient::ListenThread() { + printf("Network client listener started\n"); //This thread handles messages received from the server while(1) { - //Listen for request + NetPacketHeader header; + char * data = NULL; + int bytes_read = 0; - //Request received, select functionality based on request ID + //Read first byte of magic + do + { + bytes_read = port.tcp_listen(&header.pkt_magic[0], 1); + } while(bytes_read == 0); + + //Test first character of magic - 'O' + if(header.pkt_magic[0] != 'O') + { + continue; + } + + printf("Magic: 'O'\r\n"); + + //Read second byte of magic + do + { + bytes_read = port.tcp_listen(&header.pkt_magic[1], 1); + } while(bytes_read == 0); + + //Test second character of magic - 'R' + if(header.pkt_magic[1] != 'R') + { + continue; + } + + printf("Magic: 'R'\r\n"); + + //Read third byte of magic + do + { + bytes_read = port.tcp_listen(&header.pkt_magic[2], 1); + } while(bytes_read == 0); + + //Test third character of magic - 'G' + if(header.pkt_magic[2] != 'G') + { + continue; + } + + printf("Magic: 'G'\r\n"); + + //Read fourth byte of magic + do + { + bytes_read = port.tcp_listen(&header.pkt_magic[3], 1); + } while(bytes_read == 0); + + //Test fourth character of magic - 'B' + if(header.pkt_magic[3] != 'B') + { + continue; + } + + printf("Magic: 'B'\r\n"); + + //If we get to this point, the magic is correct. Read the rest of the header + bytes_read = 0; + do + { + bytes_read += port.tcp_listen((char *)&header.pkt_dev_idx + bytes_read, sizeof(header) - sizeof(header.pkt_magic) - bytes_read); + } while(bytes_read != sizeof(header) - sizeof(header.pkt_magic)); + + printf( "Received header, now receiving data of size %d\r\n", header.pkt_size); + + //Header received, now receive the data + if(header.pkt_size > 0) + { + unsigned int bytes_read = 0; + + data = new char[header.pkt_size]; + + do + { + bytes_read += port.tcp_listen(&data[bytes_read], 128); + } while (bytes_read < header.pkt_size); + } + + printf( "Received header and data\r\n" ); + printf( "Packet ID: %d \r\n", header.pkt_id); + + //Entire request received, select functionality based on request ID + switch(header.pkt_id) + { + + } } } \ No newline at end of file diff --git a/NetworkServer.cpp b/NetworkServer.cpp index 1368146e..2a143b97 100644 --- a/NetworkServer.cpp +++ b/NetworkServer.cpp @@ -23,18 +23,18 @@ #else #include -typedef struct listen_thread_param_type -{ - NetworkServer * this_ptr; - SOCKET * sock_ptr; -}; - static void Sleep(unsigned int milliseconds) { usleep(1000 * milliseconds); } #endif +typedef struct listen_thread_param_type +{ + NetworkServer * this_ptr; + SOCKET * sock_ptr; +}; + THREAD connection_thread(void *param) { NetworkServer* server = static_cast(param); diff --git a/net_port/net_port.cpp b/net_port/net_port.cpp index cfa349db..450e553e 100644 --- a/net_port/net_port.cpp +++ b/net_port/net_port.cpp @@ -271,72 +271,9 @@ void net_port::tcp_close() connected = false; } -int net_port::tcp_listen(char * recv_data, int /*length*/) +int net_port::tcp_listen(char * recv_data, int length) { - int ret = 0; - int len = 0; - int tot = 0; - timeval waitd; - - fd_set readfd; - - FD_ZERO(&readfd); - FD_SET(sock, &readfd); - - if (connected) - { - while(ret != sizeof(len)) - { - waitd.tv_sec = 10; - waitd.tv_usec = 0; - - if (select(sock + 1, &readfd, NULL, NULL, &waitd)) - { - ret = recv(sock, (char *)&len, sizeof(len), 0); - - if (ret == -1 || ret == 0) - { - closesocket(sock); - connected = false; - return(0); - } - } - else - { - closesocket(sock); - connected = false; - return(0); - } - } - - ret = 0; - while(tot != len) - { - waitd.tv_sec = 10; - waitd.tv_usec = 0; - - if (select(sock + 1, &readfd, NULL, NULL, &waitd)) - { - ret = recv(sock, recv_data + ret, len - ret, 0); - - if (ret == -1 || ret == 0) - { - closesocket(sock); - connected = false; - return(0); - } - - tot += ret; - } - else - { - closesocket(sock); - connected = false; - return(0); - } - } - } - return(ret); + return(read(sock, recv_data, length)); } int net_port::tcp_client_write(char * buffer, int length)