forked from bravoserver/baskerville
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PacketDump.hs
30 lines (24 loc) · 826 Bytes
/
PacketDump.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
import Baskerville.Beta.Packets
import qualified Data.ByteString as BS
import Data.Serialize
import Numeric
import System.IO
splitBytes str = let
strs = words str
in map (fst . head . readHex) strs
strToBs s = BS.pack $ map fromIntegral s
getPackets :: BS.ByteString -> Either String ([Packet], BS.ByteString)
getPackets bytes = case runGetPartial get bytes of
Fail s -> Left s
Partial _ -> Right ([], bytes)
Done packet bytes' -> case getPackets bytes' of
Right (ps, bytes'') -> Right (packet : ps, bytes'')
l -> l
showPackets (Left s) = "Parse error: " ++ s
showPackets (Right (ps, rem)) = let
srem = "Remainder: " ++ show rem
sps = map show ps
allLines = sps ++ [srem]
in unlines allLines
derp = showPackets . getPackets . strToBs . splitBytes
main = interact derp