Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Packed format is created as a node buffer on node, but still uses DataView. First loop over template and check arrays to calculate the max data size before creating buffer. Browser uses DataView and a main Uint8Array
- //Packed message format
- 0 = main event (e.g. Stream maps to 0)
- 1 = sub event (e.g. Get maps to 0 or 1)
- 2/3 = 2 byte UInt16 nonce follows if descriptor requires one or field is skipped if none needed (max nonce is thus 65535)
- //can start recursive here
- 4 = var type and size
- Types: 0 = null or undefined (size has to be 0 for this too!), 1 = bool, 2 = int, 3 = float, 4 = string, 5 = object, 6 = array with regular members, 7 = raw buffer data
- size = 0 (bool = false, 1 byte = object always, array/byte/packed float as byte/string or buffer with 1 byte length), 16 (bool = true, 2 byte = e.g. half float packed or 2 byte length for array/string/etc.), 32 (4 byte unsigned or float or 4 byte length), 64 (8 byte unsigned or double). 128 (1 byte signed/negative, 1 byte negative packed float), 144 (2 byte signed/negative, 2 byte negative packed float), 160 (4 byte signed/negative), 192 (8 byte signed/negative). Negative doesn't necessarily mean that the result will fit into its type still, e.g. a negative value can be stored unsigned as 1 byte, but signed will only fit into an int16 (the descriptor is important to consult here!)
- On encode just add the size and type
- On decode AND with 0xF0 (-16) for size AND with 0xF (15) for type
- Type and type size (or length of type) is known now no matter what
- 5/6/8 = size //skipped if type bool, int, float and null. Otherwise for object always 1 byte. For array, string types and raw buffer use 1/2/4 bytes length
- value follows in whole (unless empty array/object/string/buffer or null/bool which value is encoded into the size field)
- //if object
- //per param
- 6 = param index (always 1 byte due objects being limited to 1 byte, skipped if inside an array)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement