Serializing with alignment

Ben Franksen <ben.franksen <at> online.de>
2012-11-06 21:33:56 GMT

Hi Everyone
I want to implement a binary protocol that, unfortunately, has some
alignment restrictions. In order to fulfill these, I need access to the
"current offset" in the bytestring. The binary package does provide a
function
bytesRead :: Get Int64
but only for the Get monad; there is no equivalent for the Put monad.
So my first question: is there a serialization library that offers something
like
bytesWritten :: PutM Int64
Failing that, would you think adding it to binary is a reasonable feature
request? I have taken a cursory look at the implementation and it looks like
this is not a matter of simply adding a missing function, but would probably
need an addition to internal data structures.
I could also try and wrap the PutM from binary with a StateT transformer and
count the bytes myself. I will probably have to use at least a ReaderT
wrapper anyway, since I have to pass the byte order as a parameter (byte
order gets negotiated between client and server, it is not fixed).
I was really hoping that there is some library that has built-in support for
stateful serialization (alignment, byte-order, etc).
Any pointers, hints, etc are much appreciated.

Re: Serializing with alignment

Vincent Hanquez <tab <at> snarc.org>
2012-11-06 22:49:51 GMT

On 11/06/2012 09:33 PM, Ben Franksen wrote:
> Hi Everyone
>
> I want to implement a binary protocol that, unfortunately, has some
> alignment restrictions.
> [snip]
>
> Cheers
Hi Ben,
I don't think such thing currently exists.
Not sure that's any help to you (and definitely not answering your
binary question), but I rolled my own 'put' for a dbus implementation:
https://github.com/vincenthz/hs-udbus/blob/master/Network/DBus/Wire.hs
(it's quite simple, and could do with a builder and some tweaks)
--
--
Vincent <at> vincenthz