from Hacker News

Printf Oriented Message Protocol

by Davidbrcz on 8/13/24, 12:41 PM with 42 comments

  • by unwind on 8/13/24, 2:49 PM

    This is innovative and kind of brilliant, I'm impressed!

    - Binary message formatting for efficiency and data integrity (float to/from string is messy)

    - Familiar printf()-style format strings that the compiler can check[*] gives type safety!

    - No preprocessing and/or code generation from message templates or anything like that.

    Really nice!

    Edits: markdown and grammar.

    [*]: This uses an extension I think to mark the argument as a format string

  • by chrchr on 8/13/24, 5:48 PM

    It reminds me of Perl's 'pack' and 'unpack'.

    https://perldoc.perl.org/perlpacktut

  • by fisian on 8/13/24, 2:53 PM

    To mention a usecase for this library: it is used as part of the software Parrot ships with their drones.
  • by immibis on 8/13/24, 4:36 PM

    IIRC Cube 2: Sauerbraten uses a similar concept for network, though the format strings aren't similar to printf (just a simple list of sized types) and the types/format aren't transmitted on the wire.
  • by nurettin on 8/13/24, 6:10 PM

    > How the knowledge of the format string is shared is out of the scope of this library. It can simply be a shared header with defines.

    They don't force you to share the format strings at connection time. In fact, it is just a thin layer around sockets and you just override the message handler. I like it! Might be improved further with something like libfmt which is also used by spdlog.

  • by jbverschoor on 8/13/24, 3:32 PM

    How is the string length determined? How does this handle packed bits/ and odd sized numbers, for example a 5 bit digit and 3 flags
  • by tonyg on 8/13/24, 3:49 PM

    Feels similar to the dbus serialisation format.
  • by inetknght on 8/13/24, 6:05 PM

    I've used libpomp to interact with Parrot's Anafi drones.

    This library takes the idea of modern type safety and throws it away. Instead, the library leans in on `printf()` instead, which is known to be unsafe. And it does it in a memory-unsafe language.

    ...on a drone. Where safety needs to be important (even a small drone can do significant damage).

    It's neat and all. But that's a killer anti-feature in my opinion. I wouldn't use it on my drones.

  • by khimaros on 8/13/24, 9:43 PM

    i wonder if there is anything equivalent to this for rust?
  • by touringmachine on 8/13/24, 8:26 PM

    This is very clever. Furthermore, lmao.
  • by lukevp on 8/13/24, 2:29 PM

    I don’t really get the advantage of this over either json (for human readability), protobuf (for type safety and schemas at the parsing layer), etc. it seems like it mostly has disadvantages and would be difficult to implement efficiently because there are not consistent characters to tokenize.
  • by lanstin on 8/13/24, 5:06 PM

    The format (%ii) semantics aren't platform independent that seems like an obvious error.

    And code gen and protocol descriptions bring a certain useful discipline to modifying existing protocols.

    But it has that funky C style so I bet people using C like it.