/// Attempts to parse a packet from the given Socket::Buffer. /// Returns true if successful, removing the parsed part from the buffer. /// Returns false if invalid or not enough data is in the buffer. /// \arg buffer The Socket::Buffer to attempt to parse. bool DTSC::Stream::parsePacket(Socket::Buffer & buffer) { uint32_t len; static bool syncing = false; if (buffer.available(8)) { std::string header_bytes = buffer.copy(8); if (memcmp(header_bytes.c_str(), DTSC::Magic_Header, 4) == 0) { len = ntohl(((uint32_t *)header_bytes.c_str())[1]); if (!buffer.available(len + 8)) { return false; } unsigned int i = 0; std::string wholepacket = buffer.remove(len + 8); JSON::Value meta; JSON::fromDTMI((unsigned char *)wholepacket.c_str() + 8, len, i, meta); addMeta(meta); //recursively calls itself until failure or data packet instead of header return parsePacket(buffer); } int version = 0; if (memcmp(header_bytes.c_str(), DTSC::Magic_Packet, 4) == 0) { version = 1; } if (memcmp(header_bytes.c_str(), DTSC::Magic_Packet2, 4) == 0) { version = 2; } if (version) { len = ntohl(((uint32_t *)header_bytes.c_str())[1]); if (!buffer.available(len + 8)) { return false; } JSON::Value newPack; unsigned int i = 0; std::string wholepacket = buffer.remove(len + 8); if (version == 1) { JSON::fromDTMI((unsigned char *)wholepacket.c_str() + 8, len, i, newPack); } if (version == 2) { JSON::fromDTMI2((unsigned char *)wholepacket.c_str() + 8, len, i, newPack); } addPacket(newPack); syncing = false; return true; } #if DEBUG >= DLVL_WARN if (!syncing) { DEBUG_MSG(DLVL_WARN, "Invalid DTMI data detected - syncing"); syncing = true; } #endif buffer.get().clear(); } return false; }