Well, as always, I intend to use Struct (Python standard library).
The packet node takes all nodes connected to it, and packages them using struct.
I am not sure if you are aware how struct works, but it’s a dumb packaging library. You define a format string, and pass arguments to pack into bytes.
In order to optimise the setup, it is best to use struct class instances rather than the pack and unpack methods. Thus, you have to determine the format string on runtime. It is important that the server and client (or sender and receiver) have the same format string. Thus, I’m going to base this off my existing system (which is equally as compatibile in theory, not in practice at present):
You get all the connected nodes to the packet nodes. You sort them in the following fashion: Sort by type (e.g node.class.name) that way bitstruct nodes are near the front. Then, within that sorted list, you sort each group by id.
Thus, assuming that both the sender and the receiver has the same noodle, both will realise the same format string.
However, in order to distinguish packets, you give them a header. This header contains the id of the packet itself (I did not include this in the diagram.). In order to unpack the bytes into each packet (if you send >1 packet to the network node, it just concats them to one bytestream) you need to know the size of each packet. At present, i do so like this:
- Get the header id of the packet (always first byte). Now, look into the connected packet nodes and match the id. Now, look into the children of each packet node, and do the following:
- Following the order specified in the calculated format string for that packet noodle:
- Go down from first character to left.
- If the character is not variable (any type but string or array) then just calculate the size of that character (struct.calcsize(character)). Increment a size counter by that returned value.
- If it is variable, we do the following: slice the bytes at the index of header_size + current_count_size; the first byte will say how long the variable structure is. You know now that N bytes after that character is the variable structure. Add the value to the current count and also the size of the character (one byte) and continue down the string.
It’s very simple but effective.