diff --git a/NetworkProtocol.h b/NetworkProtocol.h index b851ae9f..802d75f3 100644 --- a/NetworkProtocol.h +++ b/NetworkProtocol.h @@ -1,6 +1,6 @@ typedef struct NetPacketHeader { - unsigned char pkt_magic[4]; /* Magic value "ORGB" identifies beginning of packet */ + char pkt_magic[4]; /* Magic value "ORGB" identifies beginning of packet */ unsigned int pkt_dev_idx; /* Device index */ unsigned int pkt_id; /* Packet ID */ unsigned int pkt_size; /* Packet size */ diff --git a/NetworkServer.cpp b/NetworkServer.cpp index d8e5c1e8..2bd0ef9c 100644 --- a/NetworkServer.cpp +++ b/NetworkServer.cpp @@ -20,8 +20,96 @@ void NetworkServer::ListenThread() //This thread handles messages received from clients while(1) { - //Listen for request + NetPacketHeader header; + 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; + } + + //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; + } + + //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] != 'O') + { + continue; + } + + //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] != 'O') + { + continue; + } + + //If we get to this point, the magic is correct. Read the rest of the header + do + { + bytes_read = port.tcp_listen((char *)&header.pkt_dev_idx, sizeof(header) - sizeof(header.pkt_magic)); + } while(bytes_read == sizeof(header) - sizeof(header.pkt_magic)); + + //Header received, now receive the data + if(header.pkt_size > 0) + { + + } + + //Entire request received, select functionality based on request ID + switch(header.pkt_id) + { + NET_PACKET_ID_REQUEST_CONTROLLER_COUNT: + + break; + + NET_PACKET_ID_REQUEST_CONTROLLER_DATA: + break; + + NET_PACKET_ID_RGBCONTROLLER_RESIZEZONE: + break; + + NET_PACKET_ID_RGBCONTROLLER_UPDATELEDS: + break; + + NET_PACKET_ID_RGBCONTROLLER_UPDATEZONELEDS: + break; + + NET_PACKET_ID_RGBCONTROLLER_UPDATESINGLELED: + break; + + NET_PACKET_ID_RGBCONTROLLER_SETCUSTOMMODE: + break; + + NET_PACKET_ID_RGBCONTROLLER_UPDATEMODE: + break; + } } } \ No newline at end of file