#pragma once #include #include #include #include // Buffer that grows in both directions from a midpoint. Payload is appended at // the back; each protocol layer then prepends its header in reverse order // (UDP, then IPv4, then Ethernet) without moving payload bytes. Lets the // network stack build outbound frames bottom-up with no copies or scratch. template class prepend_buffer { uint8_t m_buf[N]; size_t m_start = N / 2; size_t m_end = N / 2; public: template T* prepend() { m_start -= sizeof(T); return reinterpret_cast(m_buf + m_start); } uint8_t* append(size_t len) { uint8_t* p = m_buf + m_end; m_end += len; return p; } void append_copy(std::span data) { memcpy(append(data.size()), data.data(), data.size()); } uint8_t* payload_ptr() { return m_buf + m_end; } uint8_t* data() { return m_buf + m_start; } const uint8_t* data() const { return m_buf + m_start; } size_t size() const { return m_end - m_start; } std::span span() const { return {data(), size()}; } };