/
Packet.hs
53 lines (45 loc) · 1.41 KB
/
Packet.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-- |
-- Module : Data.BERT.Packet
-- Copyright : (c) marius a. eriksen 2009
--
-- License : BSD3
-- Maintainer : marius@monkey.org
-- Stability : experimental
-- Portability : GHC
--
-- BERP (BERT packets) support.
module Data.BERT.Packet
( Packet(..)
, fromPacket
, packets
) where
import Control.Monad (liftM)
import Data.ByteString.Lazy as L
import Data.Binary (Binary(..), Get(..), encode, decode)
import Data.Binary.Put (putWord32be, putLazyByteString)
import Data.Binary.Get (getWord32be, getLazyByteString, runGet, runGetState)
import Data.BERT.Term
import Data.BERT.Types (Term(..))
-- | A single BERP. Little more than a wrapper for a term.
data Packet
= Packet Term
deriving (Show, Ord, Eq)
fromPacket (Packet t) = t
instance Binary Packet where
put (Packet term) =
putWord32be (fromIntegral len) >> putLazyByteString encoded
where encoded = encode term
len = L.length encoded
get = getPacket
getPacket =
liftM fromIntegral getWord32be >>=
getLazyByteString >>=
return . Packet . decode
-- | From a lazy bytestring, return a (lazy) list of packets. This is
-- convenient for parsing a stream of adjacent packets. (Eg. by using
-- some form of @getContents@ to get a @ByteString@ out of a data
-- source).
packets :: L.ByteString -> [Packet]
packets b
| L.null b = []
| otherwise = p:packets b' where (p, b', _) = runGetState getPacket b 0