I have the following Structs in Dart:
final class Header extends Struct {
@Uint16()
external int version;
@Uint16()
external int type;
@Uint32()
external int size;
}
final class Payload extends Struct {
@Uint32()
external int x;
@Uint32()
external int y;
}
It's supposed to represent a Header + Payload for a network packet. I am aware that I can use ByteData to write the fields of each Struct manually to, for example, a Uint8List. However, I'd like to use the defined structure to create/parse packets. Currently, the idea is allocating memory for a complete packet, and then casting it to the Struct type.
- How would I set the fields of the
Structso that they would be stored in NBO/Big Endian in memory? - How would I "map" multiple
Structs to an area of allocated memory with offsets, similar to what would be done in C like this:
typedef struct __attribute__((packed)) {
uint32_t foo;
} s1;
typedef struct __attribute__((packed)) {
uint32_t bar;
} s2;
void *buf = malloc(sizeof s1 + sizeof s2);
s1 *a = buf;
s2 *b = buf + sizeof s1;
/* ... */
I'd like to get around writing my packet building/parsing in C and having to interface between dart in C as a consequence.