Ben Franksen | 6 Nov 22:33 2012
Picon

Serializing with alignment

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.
(Continue reading)

Vincent Hanquez | 6 Nov 23:49 2012

Re: Serializing with alignment

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
Ben Franksen | 7 Nov 01:58 2012
Picon

Re: Serializing with alignment

Vincent Hanquez wrote:
> On 11/06/2012 09:33 PM, Ben Franksen wrote:
>> I want to implement a binary protocol that, unfortunately, has some
>> alignment restrictions.
>> [snip]
> 
> I don't think such thing currently exists.

Feared so.

> 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

Interesting. Seems you also needed aligned read/write. You are using a state 
monad, too. Maybe I should just go ahead and wrap a StateT over the PutM 
from binary, at least this lets me re-use the efficient concatenation that's 
built into binary's PutM (or rather, the Builder below it).

Thanks
--

-- 
Ben Franksen
()  ascii ribbon campaign - against html e-mail 
/\  www.asciiribbon.org   - against proprietary attachments

Gmane