Safe Haskell | None |
---|---|
Language | Haskell2010 |
For efficiency reasons, Store
does not provide facilities for
incrementally consuming input. In order to avoid partial input, this
module introduces Message
s that wrap values of instances of Store
.
In addition to the serialisation of a value, the serialised message also contains the length of the serialisation. This way, instead of consuming input incrementally, more input can be demanded before serialisation is attempted in the first place.
Each message starts with a fixed magic number, in order to detect (randomly) invalid data.
- newtype Message a = Message {
- fromMessage :: a
- encodeMessage :: Store a => Message a -> ByteString
- data PeekMessage m a
- = Done (Message a)
- | NeedMoreInput (ByteString -> m (PeekMessage m a))
- peekMessage :: (MonadIO m, Store a) => ByteBuffer -> m (PeekMessage m a)
- decodeMessage :: (MonadIO m, Store a) => ByteBuffer -> m (Maybe ByteString) -> m (Maybe (Message a))
- conduitEncode :: (Monad m, Store a) => Conduit (Message a) m ByteString
- conduitDecode :: (MonadIO m, MonadResource m, Store a) => Maybe Int -> Conduit ByteString m (Message a)
Message
s to stream data using Store
for serialisation.
If a
is an instance of Store
, Message a
can be serialised
and deserialised in a streaming fashion.
Message | |
|
Encoding Message
s
encodeMessage :: Store a => Message a -> ByteString Source #
Encode a Message
to a ByteString
.
Decoding Message
s
data PeekMessage m a Source #
The result of peeking at the next message can either be a
successfully deserialised Message
, or a request for more input.
Done (Message a) | |
NeedMoreInput (ByteString -> m (PeekMessage m a)) |
peekMessage :: (MonadIO m, Store a) => ByteBuffer -> m (PeekMessage m a) Source #
Decode some Message
from a ByteBuffer
, by first reading its
header, and then the actual Message
.
decodeMessage :: (MonadIO m, Store a) => ByteBuffer -> m (Maybe ByteString) -> m (Maybe (Message a)) Source #
Decode a Message
from a ByteBuffer
and an action that can get
additional ByteString
s to refill the buffer when necessary.
The only conditions under which this function will give Nothing
,
is when the ByteBuffer
contains zero bytes, and refilling yields
Nothing
. If there is some data available, but not enough to
decode the whole Message
, a PeekException
will be thrown.
Conduits for encoding and decoding
conduitEncode :: (Monad m, Store a) => Conduit (Message a) m ByteString Source #
Conduit for encoding Message
s to ByteString
s.
:: (MonadIO m, MonadResource m, Store a) | |
=> Maybe Int | Initial length of the |
-> Conduit ByteString m (Message a) |
Conduit for decoding Message
s from ByteString
s.