add protocol asserts

This commit is contained in:
Scott R. Parish 2010-01-16 13:45:08 -06:00
parent 6a9a533fcc
commit ee1e4d3528

View file

@ -8,6 +8,7 @@ module Database.MongoDB
query
)
where
import Control.Exception (assert)
import Control.Monad
import Data.Binary
import Data.Binary.Get
@ -187,8 +188,8 @@ getReply c reqID = do
respTo <- getI32
op <- getI32
return $ Hdr msgLen reqID respTo $ toOpcode op
-- TODO assert OP_REPLY = hOp hdr
-- TODO assert hReqID hdr
assert (OP_REPLY == hOp hdr) $ return ()
assert (hRespTo hdr == reqID) $ return ()
replyBytes <- L.hGet h 20
let reply = flip runGet replyBytes $ do
respFlags <- getI32
@ -196,6 +197,7 @@ getReply c reqID = do
startFrom <- getI32
numReturned <- getI32
return $ (Reply respFlags cursorID startFrom numReturned)
assert (rRespFlags reply == 0) $ return ()
docBytes <- L.hGet h $ fromIntegral $ hMsgLen hdr - 16 - 20
return $ flip runGet docBytes $ do
forM [1 .. rNumReturned reply] $ \_ -> get