4
Fork 0
CronosAgent/RingBuffer.c

47 lines
1.1 KiB
C

#include "RingBuffer.h"
#include <string.h>
#include <unistd.h>
void PushData(struct RingBuffer* buffer, uint32_t number, uint8_t* data, size_t length)
{
number %= BUFFER_LENGTH;
buffer->delay += (buffer->flags == 0); // Postpone processing if buffer empty
buffer->flags |= (1 << number); // Set processing flag 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->flags == 0)
{
// Nothing to process
return;
}
if (buffer->delay > 0)
{
// Processing was postponed
buffer->delay --;
return;
}
size_t mask = (1 << buffer->pointer);
if (buffer->flags & mask)
{
// Transmit scheduled data
struct BufferRecord* record = buffer->records + buffer->pointer;
sendto(handle, record->data, record->length, 0, (struct sockaddr*)address, sizeof(struct sockaddr_in));
// Clear processing flag
buffer->flags ^= mask;
}
buffer->pointer ++;
buffer->pointer %= BUFFER_LENGTH;
}