Jump to content

Recommended Posts

  • Active+ Member

 A fluent, move-only builder that serializes trivially-copyable packet structs and variable payloads into a buffer, then dispatches it through a send strategy of your choice (as long as it's a stateless, default-initializable functor/stateless lambda).

"Why would I need this?", the following mistakes get caught at compile time:

-> writing a struct with padding into the buffer as raw bytes

-> trying to serialize a pointer

 

You can pass a contiguous range, a forward range, or a single POD as the argument. Contiguous ranges are written in a single block; other forward ranges element-by-element, with byte-identical output.

 

Main benefits: compile-time validation and type safety. Of course, if your take is "99% of the codebase doesn't follow this, why should we" — that's up to you. It works for me, and I just wanted to share something simple.

C++23 and GCC 14 will do the job; adapting it to older standards is left to you.

The buffer type is customizable, satisfying the concept is enough. I wrote the concept for the default TEMP_BUFFER; adjust it to your own API as needed.

Example usage:

 

constexpr auto my_send{[](Character ch, std::span<const std::uint8_t> data,
                            const TEMP_BUFFER& buffer) noexcept
{
    ch.print();
    for (auto elm : data)
        std::cout << static_cast<int>(elm) << '\n';
    return 1;
}};

const auto built{
    make_builder<my_send>()
        .add_payload(payload_bytes)
        .add_payload(Payload{.i1 = 1, .i2 = true, .i4 = 2})
        .send(ch, payload_bytes)};

const auto pkt{make_builder()
    .add_payload(std::uint8_t{42})
    .add_payload(std::uint8_t{42})
    .add_member_val(&MyPacket::size, 3)
    .build_as<MyPacket>()};

if (pkt)
    std::cout << pkt->size;

PacketBuilder moves the buffer along the chain via std::forward_like. So if you plan to use TEMP_BUFFER with this builder, you'll need to fix the copy/move issue I described in this thread first:

 

If I missed anything, let me know and I'll fix it.

Example: https://godbolt.org/z/zKnW41cfG

 

Source:

https://gist.githubusercontent.com/mberk-yilmaz/40b938f38d136b63e3ddee6f3f300889/raw/6bfca921b890e776730b5c798c89bf82b8c3fa89/network_helper.hpp

Software Engineer | Low-Latency C++

Link to comment
https://metin2.dev/topic/34594-compile-time-safe-packet-builderserializer/
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Popular Days

Don't use any images from : imgur, turkmmop, freakgamers, inforge, hizliresim... Or your content will be deleted without notice...
Use : https://metin2.download/media/add/

Please use https://metin2.download/ when uploading files smaller than 100MB, otherwise the approval will take longer due to manual upload.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.