Skip to content

Commit 4ce4cb0

Browse files
committed
fixup! ouroboros-network-framework: drivers for annotated decoders
1 parent 328f239 commit 4ce4cb0

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

ouroboros-network-framework/src/Ouroboros/Network/Driver/Limits.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ runAnnotatedDecoderWithLimit limit size Channel{recv} =
344344
-- This leaves just one special case: if the decoder finishes with that
345345
-- final chunk, we must check if it consumed too much of the final chunk.
346346
--
347-
go :: bytes
347+
go :: [bytes] -- ^ accumulation of chunks received from the network
348348
-> Word -- ^ size of consumed input so far
349349
-> Maybe bytes -- ^ any trailing data
350350
-> DecodeStep bytes failure m (bytes -> a)
@@ -353,7 +353,7 @@ runAnnotatedDecoderWithLimit limit size Channel{recv} =
353353
go !bytes !sz !_ (DecodeDone f trailing)
354354
| let sz' = sz - maybe 0 size trailing
355355
, sz' > limit = return (Left Nothing)
356-
| otherwise = return (Right (f bytes, trailing))
356+
| otherwise = return (Right (f (mconcat $ reverse bytes), trailing))
357357

358358
go !_ !_ !_ (DecodeFail failure) = return (Left (Just failure))
359359

@@ -364,12 +364,15 @@ runAnnotatedDecoderWithLimit limit size Channel{recv} =
364364
mbs <- recv
365365
let !sz' = sz + maybe 0 size mbs
366366
step <- k mbs
367-
go (bytes <> fromMaybe mempty mbs) sz' Nothing step
367+
go (case mbs of
368+
Nothing -> bytes
369+
Just bs -> bs : bytes)
370+
sz' Nothing step
368371

369372
go !bytes !sz (Just trailing) (DecodePartial k) = do
370373
let sz' = sz + size trailing
371374
step <- k (Just trailing)
372-
go (bytes <> trailing) sz' Nothing step
375+
go (trailing : bytes) sz' Nothing step
373376

374377

375378
-- | Run a peer with limits.

ouroboros-network-framework/src/Ouroboros/Network/Driver/Simple.hs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import Control.Monad.Class.MonadFork
4949
import Control.Monad.Class.MonadThrow
5050
import Control.Tracer (Tracer (..), contramap, traceWith)
5151
import Data.Functor.Identity (Identity (..))
52-
import Data.Maybe (fromMaybe)
52+
import Data.Maybe (maybeToList)
5353

5454

5555
-- $intro
@@ -326,13 +326,16 @@ runAnnotatedDecoderWithChannel
326326
-> DecodeStep bytes failure m (bytes -> a)
327327
-> m (Either failure (a, Maybe bytes))
328328

329-
runAnnotatedDecoderWithChannel Channel{recv} bs0 = go (fromMaybe mempty bs0) bs0
329+
runAnnotatedDecoderWithChannel Channel{recv} bs0 = go (maybeToList bs0) bs0
330330
where
331-
go :: bytes -> Maybe bytes -> DecodeStep bytes failure m (bytes -> a) -> m (Either failure (a, Maybe bytes))
332-
go bytes _ (DecodeDone f trailing) = return $ Right (f bytes, trailing)
333-
go _bytes _ (DecodeFail failure) = return (Left failure)
334-
go bytes Nothing (DecodePartial k) = recv >>= \bs -> k bs >>= go (bytes <> fromMaybe mempty bs) Nothing
335-
go bytes (Just trailing) (DecodePartial k) = k (Just trailing) >>= go (bytes <> trailing) Nothing
331+
go :: [bytes]
332+
-> Maybe bytes
333+
-> DecodeStep bytes failure m (bytes -> a)
334+
-> m (Either failure (a, Maybe bytes))
335+
go !bytes _ (DecodeDone f trailing) = return $ Right (f $ mconcat (reverse bytes), trailing)
336+
go _bytes _ (DecodeFail failure) = return (Left failure)
337+
go !bytes Nothing (DecodePartial k) = recv >>= \bs -> k bs >>= go (maybe bytes (: bytes) bs) Nothing
338+
go !bytes (Just trailing) (DecodePartial k) = k (Just trailing) >>= go (trailing : bytes) Nothing
336339

337340

338341
data Role = Client | Server

0 commit comments

Comments
 (0)