#include "RingBuffer.h" #include #include void PokeData(struct RingBuffer* buffer, uint32_t number, uint8_t* data, size_t length) { number %= BUFFER_LENGTH; buffer->delay += buffer->marks == 0; // Postpone processing if buffer empty buffer->marks |= 1 << number; // Set processing mark for the record struct BufferRecord* record = buffer->records + number; record->length = length; memcpy(record->data, data, length); } void ProcessBuffer(struct RingBuffer* buffer, int handle, struct sockaddr_in* address) { if (buffer->marks == 0) { // Nothing to process return; } if (buffer->delay > 0) { // Processing was postponed buffer->delay --; return; } size_t mark = 1 << buffer->index; if (buffer->marks & mark) { // Transmit scheduled data struct BufferRecord* record = buffer->records + buffer->index; sendto(handle, record->data, record->length, 0, (struct sockaddr*)address, sizeof(struct sockaddr_in)); // Clear processing mark buffer->marks ^= mark; } buffer->index ++; buffer->index %= BUFFER_LENGTH; }