53 lines
1.1 KiB
C
53 lines
1.1 KiB
C
#include "RingBuffer.h"
|
|
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
void PokeData(struct RingBuffer* buffer, uint32_t number, uint8_t* data, size_t length)
|
|
{
|
|
struct BufferRecord* record;
|
|
|
|
number %= BUFFER_LENGTH;
|
|
|
|
if (buffer->marks == 0)
|
|
{
|
|
buffer->delay ++;
|
|
buffer->index = number;
|
|
}
|
|
|
|
buffer->marks |= 1 << number;
|
|
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;
|
|
}
|