int read_file(const char *filename) { int file_status = -1; // Open the file in read-only binary mode FILE *fp = fopen(filename, "rb"); if (fp) { datagram temp_datagram; int status = STATUS_CONTINUE; uint32_t skips = 0; // Read each datagram in the file, until STOP or EOF is reached while (status == STATUS_CONTINUE) { size_t bytes_read = fread(&temp_datagram, 1, sizeof(datagram), fp); if (bytes_read == sizeof(datagram)) { // Process the datagram if its header was successfully read status = handle_datagram(&temp_datagram, fp, &skips); } else if (feof(fp)) { printf("Reached the end of the file.\n"); status = STATUS_STOP; } else { printf("There was an error reading the file.\n"); status = STATUS_FAIL; } } file_status = 0; fclose(fp); if (status == STATUS_FAIL) { printf("An error occurred while processing \"%s\".\n", filename); } } return file_status; }
void InternalConnection::handle_datagram(const Datagram &dg) { uint8_t channels = 0; DatagramIterator dgi(dg); try { channels = dgi.read_uint8(); DatagramIterator msg_dgi(dg, 1 + channels * sizeof(channel_t)); handle_datagram(dg, msg_dgi); } catch (DatagramIteratorEOF &e) { //Really, if this has been sent to us by the MD... we really shouldn't have an issue. //But as always, better safe then sorry. std::cout << "Detected truncated datagram in handle_datagram" << std::endl; } }