This isn't the first time I run into this issue, but I haven't found a really nice solution for it so I thought I'd ask how others approach it.
Many protocols include the size of the message (or a part of it) inside the message itself. For instance, right now I'm looking at ModbusTCP where bytes 5 and 6 should hold the length of the remaining part of the message. Is there some nice and elegant way to deal with this?
The straight-forward builder would look something like this (using Serialize
from cereal):
put (ModTcpRequestFrame tid pid uid req)= do
putWord16be tid
putWord16be pid
putWord16be len
putWord8 uid
put req
where
len = .... -- what to put here?
Can it be done?