Skip to content

Commit

Permalink
Define msHeaderLength
Browse files Browse the repository at this point in the history
Define msHeaderLength and use it instead of 8 as hardcoded headerlength.
  • Loading branch information
karknu committed Feb 18, 2025
1 parent e6f0d43 commit 2e1b74b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion network-mux/src/Network/Mux/Bearer/NamedPipe.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namedPipeAsBearer sduSize tracer h =
readNamedPipe :: Mx.TimeoutFn IO -> IO (Mx.SDU, Time)
readNamedPipe _ = do
traceWith tracer Mx.TraceRecvHeaderStart
hbuf <- recvLen' True 8 []
hbuf <- recvLen' True Mx.msHeaderLength []
case Mx.decodeSDU hbuf of
Left e -> throwIO e
Right header@Mx.SDU { Mx.msHeader } -> do
Expand Down
2 changes: 1 addition & 1 deletion network-mux/src/Network/Mux/Bearer/Pipe.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pipeAsBearer sduSize tracer channel =
readPipe :: Mx.TimeoutFn IO -> IO (Mx.SDU, Time)
readPipe _ = do
traceWith tracer Mx.TraceRecvHeaderStart
hbuf <- recvLen' 8 []
hbuf <- recvLen' (fromIntegral Mx.msHeaderLength) []
case Mx.decodeSDU hbuf of
Left e -> throwIO e
Right header@Mx.SDU { Mx.msHeader } -> do
Expand Down
6 changes: 2 additions & 4 deletions network-mux/src/Network/Mux/Bearer/Socket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,12 @@ socketAsBearer sduSize batchSize readBuffer_m sduTimeout tracer sd =
Mx.name = "socket-bearer"
}
where
hdrLenght = 8

readSocket :: Mx.TimeoutFn IO -> IO (Mx.SDU, Time)
readSocket timeout = do
traceWith tracer Mx.TraceRecvHeaderStart

-- Wait for the first part of the header without any timeout
h0 <- recvAtMost True hdrLenght
h0 <- recvAtMost True Mx.msHeaderLength

-- Optionally wait at most sduTimeout seconds for the complete SDU.
r_m <- timeout sduTimeout $ recvRem h0
Expand All @@ -85,7 +83,7 @@ socketAsBearer sduSize batchSize readBuffer_m sduTimeout tracer sd =

recvRem :: BL.ByteString -> IO (Mx.SDU, Time)
recvRem !h0 = do
hbuf <- recvLen' (hdrLenght - BL.length h0) [h0]
hbuf <- recvLen' (Mx.msHeaderLength - BL.length h0) [h0]
case Mx.decodeSDU hbuf of
Left e -> throwIO e
Right header@Mx.SDU { Mx.msHeader } -> do
Expand Down
7 changes: 4 additions & 3 deletions network-mux/src/Network/Mux/Egress.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Network.Mux.Egress

import Control.Monad
import Data.ByteString.Lazy qualified as BL
import Data.Int

import Control.Concurrent.Class.MonadSTM.Strict
import Control.Monad.Class.MonadAsync
Expand Down Expand Up @@ -154,8 +155,8 @@ muxer egressQueue Bearer { writeMany, sduSize, batchSize } =
maxSDUsPerBatch :: Int
maxSDUsPerBatch = 100

sduLength :: SDU -> Int
sduLength sdu = 8 + fromIntegral (msLength sdu)
sduLength :: SDU -> Int64
sduLength sdu = msHeaderLength + fromIntegral (msLength sdu)

-- Build a batch of SDUs to submit in one go to the bearer.
-- The egress queue is still processed one SDU at the time
Expand All @@ -164,7 +165,7 @@ muxer egressQueue Bearer { writeMany, sduSize, batchSize } =
-- (e.g the SO_SNDBUF for Socket) or number of SDUs.
--
buildBatch sdus _ | length sdus >= maxSDUsPerBatch = return $ reverse sdus
buildBatch sdus sdusLength | sdusLength >= batchSize = return $ reverse sdus
buildBatch sdus sdusLength | sdusLength >= fromIntegral batchSize = return $ reverse sdus
buildBatch sdus !sdusLength = do
demand_m <- atomically $ tryReadTBQueue egressQueue
case demand_m of
Expand Down
5 changes: 5 additions & 0 deletions network-mux/src/Network/Mux/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module Network.Mux.Types
, msNum
, msDir
, msLength
, msHeaderLength
, RemoteClockModel (..)
, remoteClockPrecision
, RuntimeError (..)
Expand All @@ -47,6 +48,7 @@ import Prelude hiding (read)
import Control.Exception (Exception, SomeException)
import Data.ByteString.Lazy qualified as BL
import Data.Functor (void)
import Data.Int
import Data.Ix (Ix (..))
import Data.Word
import Foreign.Ptr (Ptr)
Expand Down Expand Up @@ -223,6 +225,9 @@ msDir = mhDir . msHeader
msLength :: SDU -> Word16
msLength = mhLength . msHeader

-- | Size of a MuxHeader in Bytes
msHeaderLength :: Int64
msHeaderLength = 8

-- | Low level access to underlying socket or pipe. There are three smart
-- constructors:
Expand Down
8 changes: 4 additions & 4 deletions network-mux/test/Test/Mux.hs
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,12 @@ instance Arbitrary ArbitrarySDU where
invalidLenght = do
ts <- arbitrary
mid <- arbitrary
realLen <- choose (0, 8) -- Size of mux header is 8
len <- if realLen == 8 then return 0
else arbitrary
realLen <- choose (0, Mx.msHeaderLength)
len <- if realLen == Mx.msHeaderLength then return 0
else arbitrary
p <- arbitrary

return $ ArbitraryInvalidSDU (InvalidSDU (Mx.RemoteClockModel ts) mid len realLen p)
return $ ArbitraryInvalidSDU (InvalidSDU (Mx.RemoteClockModel ts) mid len (fromIntegral realLen) p)
(Mx.SDUDecodeError "")

instance Arbitrary Mx.BearerState where
Expand Down

0 comments on commit 2e1b74b

Please sign in to comment.