Skip to content

Commit 2838168

Browse files
committed
ouroboros-network-framework: drivers - code style
Reorder cases so they are more logical, i.e. first handling trailing bytes from previous message, then bytes received from network followed by `DecodeDone` and `DecodeFail`.
1 parent 4ce4cb0 commit 2838168

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,6 @@ runDecoderWithLimit limit size Channel{recv} =
298298
-> DecodeStep bytes failure m (Identity a)
299299
-> m (Either (Maybe failure) (a, Maybe bytes))
300300

301-
go !sz !_ (DecodeDone (Identity x) trailing)
302-
| let sz' = sz - maybe 0 size trailing
303-
, sz' > limit = return (Left Nothing)
304-
| otherwise = return (Right (x, trailing))
305-
306-
go !_ !_ (DecodeFail failure) = return (Left (Just failure))
307-
308301
go !sz trailing (DecodePartial k)
309302
| sz > limit = return (Left Nothing)
310303
| otherwise = case trailing of
@@ -314,6 +307,13 @@ runDecoderWithLimit limit size Channel{recv} =
314307
Just bs -> do let sz' = sz + size bs
315308
go sz' Nothing =<< k (Just bs)
316309

310+
go !sz !_ (DecodeDone (Identity x) trailing)
311+
| let sz' = sz - maybe 0 size trailing
312+
, sz' > limit = return (Left Nothing)
313+
| otherwise = return (Right (x, trailing))
314+
315+
go !_ !_ (DecodeFail failure) = return (Left (Just failure))
316+
317317

318318
runAnnotatedDecoderWithLimit
319319
:: forall m bytes failure a.
@@ -350,16 +350,15 @@ runAnnotatedDecoderWithLimit limit size Channel{recv} =
350350
-> DecodeStep bytes failure m (bytes -> a)
351351
-> m (Either (Maybe failure) (a, Maybe bytes))
352352

353-
go !bytes !sz !_ (DecodeDone f trailing)
354-
| let sz' = sz - maybe 0 size trailing
355-
, sz' > limit = return (Left Nothing)
356-
| otherwise = return (Right (f (mconcat $ reverse bytes), trailing))
357-
358-
go !_ !_ !_ (DecodeFail failure) = return (Left (Just failure))
359353

360354
go !_ !sz !_ DecodePartial {} | sz > limit =
361355
return (Left Nothing)
362356

357+
go !bytes !sz (Just trailing) (DecodePartial k) = do
358+
let sz' = sz + size trailing
359+
step <- k (Just trailing)
360+
go (trailing : bytes) sz' Nothing step
361+
363362
go !bytes !sz Nothing (DecodePartial k) = do
364363
mbs <- recv
365364
let !sz' = sz + maybe 0 size mbs
@@ -369,10 +368,12 @@ runAnnotatedDecoderWithLimit limit size Channel{recv} =
369368
Just bs -> bs : bytes)
370369
sz' Nothing step
371370

372-
go !bytes !sz (Just trailing) (DecodePartial k) = do
373-
let sz' = sz + size trailing
374-
step <- k (Just trailing)
375-
go (trailing : bytes) sz' Nothing step
371+
go !bytes !sz !_ (DecodeDone f trailing)
372+
| let sz' = sz - maybe 0 size trailing
373+
, sz' > limit = return (Left Nothing)
374+
| otherwise = return (Right (f (mconcat $ reverse bytes), trailing))
375+
376+
go !_ !_ !_ (DecodeFail failure) = return (Left (Just failure))
376377

377378

378379
-- | Run a peer with limits.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,10 @@ runAnnotatedDecoderWithChannel Channel{recv} bs0 = go (maybeToList bs0) bs0
332332
-> Maybe bytes
333333
-> DecodeStep bytes failure m (bytes -> a)
334334
-> 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
338335
go !bytes (Just trailing) (DecodePartial k) = k (Just trailing) >>= go (trailing : bytes) Nothing
336+
go !bytes Nothing (DecodePartial k) = recv >>= \bs -> k bs >>= go (maybe bytes (: bytes) bs) Nothing
337+
go !bytes _ (DecodeDone f trailing) = return $ Right (f $ mconcat (reverse bytes), trailing)
338+
go _bytes _ (DecodeFail failure) = return (Left failure)
339339

340340

341341
data Role = Client | Server

0 commit comments

Comments
 (0)