I'm trying to find the most efficient way to stream large amounts of data out of a UDP socket. Using a double (or triple) buffering approach in which one thread fills a large buffer with data and then another thread takes ownership of the buffer and streams it out via a socket seems more efficient than sending the sender thread small packet sized chunks.
However, the protocol I'm using requires that protocol-specific headers be added to each chunk of data before sending. The header must be at the beginning of each packet.
The best way I've thought to accomplish this so far would require memcopying every byte of data out of the large buffer before sending, similar to the code below:
void UdpSender::udp_send()
{
/**
* A large buffer, simulating a ping-pong buffer filled in a separate thread
*/
const size_t BUFFER_SIZE = 1024*1024*2;
uint8_t send_buffer[BUFFER_SIZE];
std::memset(send_buffer, 'a', BUFFER_SIZE);
const uint16_t MAX_PACKET_PAYLOAD_SIZE = 1400;
struct Packet
{
uint32_t header_value1;
uint32_t header_value2;
uint8_t payload[MAX_PACKET_PAYLOAD_SIZE];
};
//Offset into the large buffer
uint8_t send_buffer_offset = 0;
Packet packet;
packet.header_value1 = 1;
packet.header_value2 = 2;
//Requires copying everything out of the buffer before sending
std::memcpy(&packet.payload, &send_buffer+send_buffer_offset, MAX_PACKET_PAYLOAD_SIZE);
sendto(active_socket_fd, &packet, sizeof(packet), 0, p_dest_addr, dest_addr_size);
}
The data I need to send is already in a contiguous buffer that would be efficient to send (requiring no additional memcopys) if I didn't have to add a header to each chunk of data being sent out. Is there any better way I could approach this problem that would avoid memcopying the entire buffer?
Thanks!