Skip to content

Commit c342d0e

Browse files
committed
recvWithLength takes optional limit
Remove recvWithLengthLimited
1 parent 90beb96 commit c342d0e

File tree

3 files changed

+17
-22
lines changed

3 files changed

+17
-22
lines changed

src/Network/Transport/TCP.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import Network.Transport.TCP.Internal
5555
, encodeConnectionRequestResponse
5656
, decodeConnectionRequestResponse
5757
, forkServer
58-
, recvWithLengthLimited
58+
, recvWithLength
5959
, recvWord32
6060
, encodeWord32
6161
, tryCloseSocket
@@ -878,7 +878,7 @@ handleConnectionRequest transport sock = handle handleException $ do
878878
ourEndPointId <- recvWord32 sock
879879
let maxAddressLength = tcpMaxAddressLength $ transportParams transport
880880
theirAddress <- EndPointAddress . BS.concat <$>
881-
recvWithLengthLimited maxAddressLength sock
881+
recvWithLength maxAddressLength sock
882882
let ourAddress = encodeEndPointAddress (transportHost transport)
883883
(transportPort transport)
884884
ourEndPointId
@@ -1189,7 +1189,7 @@ handleIncomingMessages params (ourEndPoint, theirEndPoint) = do
11891189
-- overhead
11901190
readMessage :: N.Socket -> LightweightConnectionId -> IO ()
11911191
readMessage sock lcid =
1192-
recvWithLengthLimited recvLimit sock >>=
1192+
recvWithLength recvLimit sock >>=
11931193
qdiscEnqueue' ourQueue theirAddr . Received (connId lcid)
11941194

11951195
-- Stop probing a connection as a result of receiving a probe ack.

src/Network/Transport/TCP/Internal.hs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ module Network.Transport.TCP.Internal
88
, decodeConnectionRequestResponse
99
, forkServer
1010
, recvWithLength
11-
, recvWithLengthLimited
1211
, recvExact
1312
, recvWord32
1413
, encodeWord32
@@ -181,19 +180,15 @@ forkServer host port backlog reuseAddr terminationHandler requestHandler = do
181180
(tryCloseSocket . fst)
182181
(requestHandler . fst)
183182

184-
-- | Read a length and then a payload of that length
185-
recvWithLength :: N.Socket -> IO [ByteString]
186-
recvWithLength sock = recvWord32 sock >>= recvExact sock
187-
188-
-- | Read a length and then a payload of that length, subject to a limit on
189-
-- the length.
190-
recvWithLengthLimited :: Maybe Word32 -> N.Socket -> IO [ByteString]
191-
recvWithLengthLimited mlimit sock = case mlimit of
192-
Nothing -> recvWithLength sock
183+
-- | Read a length and then a payload of that length, subject to an optional
184+
-- limit on the length.
185+
recvWithLength :: Maybe Word32 -> N.Socket -> IO [ByteString]
186+
recvWithLength mlimit sock = case mlimit of
187+
Nothing -> recvWord32 sock >>= recvExact sock
193188
Just limit -> do
194189
length <- recvWord32 sock
195190
when (length > limit) $
196-
throwIO (userError "recvWithLengthLimit: limit exceeded")
191+
throwIO (userError "recvWithLength: limit exceeded")
197192
recvExact sock length
198193

199194
-- | Receive a 32-bit unsigned integer

tests/TestTCP.hs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ testEarlyDisconnect = do
165165
(clientPort, _) <- forkServer "127.0.0.1" "0" 5 True throwIO $ \sock -> do
166166
-- Initial setup
167167
0 <- recvWord32 sock
168-
_ <- recvWithLength sock
168+
_ <- recvWithLength Nothing sock
169169
sendMany sock [encodeWord32 (encodeConnectionRequestResponse ConnectionRequestAccepted)]
170170

171171
-- Server opens a logical connection
@@ -174,7 +174,7 @@ testEarlyDisconnect = do
174174

175175
-- Server sends a message
176176
1024 <- recvWord32 sock
177-
["ping"] <- recvWithLength sock
177+
["ping"] <- recvWithLength Nothing sock
178178

179179
-- Reply
180180
sendMany sock [
@@ -277,7 +277,7 @@ testEarlyCloseSocket = do
277277
(clientPort, _) <- forkServer "127.0.0.1" "0" 5 True throwIO $ \sock -> do
278278
-- Initial setup
279279
0 <- recvWord32 sock
280-
_ <- recvWithLength sock
280+
_ <- recvWithLength Nothing sock
281281
sendMany sock [encodeWord32 (encodeConnectionRequestResponse ConnectionRequestAccepted)]
282282

283283
-- Server opens a logical connection
@@ -286,7 +286,7 @@ testEarlyCloseSocket = do
286286

287287
-- Server sends a message
288288
1024 <- recvWord32 sock
289-
["ping"] <- recvWithLength sock
289+
["ping"] <- recvWithLength Nothing sock
290290

291291
-- Reply
292292
sendMany sock [
@@ -620,7 +620,7 @@ testReconnect = do
620620
(serverPort, _) <- forkServer "127.0.0.1" "0" 5 True throwIO $ \sock -> do
621621
-- Accept the connection
622622
Right 0 <- tryIO $ recvWord32 sock
623-
Right _ <- tryIO $ recvWithLength sock
623+
Right _ <- tryIO $ recvWithLength Nothing sock
624624

625625
-- The first time we close the socket before accepting the logical connection
626626
count <- modifyMVar counter $ \i -> return (i + 1, i)
@@ -639,7 +639,7 @@ testReconnect = do
639639
-- Client sends a message
640640
Right connId' <- tryIO $ (recvWord32 sock :: IO LightweightConnectionId)
641641
True <- return $ connId == connId'
642-
Right ["ping"] <- tryIO $ recvWithLength sock
642+
Right ["ping"] <- tryIO $ recvWithLength Nothing sock
643643
putMVar serverDone ()
644644

645645
Right () <- tryIO $ N.sClose sock
@@ -712,15 +712,15 @@ testUnidirectionalError = do
712712
-- would shutdown the socket in the other direction)
713713
void . (try :: IO () -> IO (Either SomeException ())) $ do
714714
0 <- recvWord32 sock
715-
_ <- recvWithLength sock
715+
_ <- recvWithLength Nothing sock
716716
() <- sendMany sock [encodeWord32 (encodeConnectionRequestResponse ConnectionRequestAccepted)]
717717

718718
Just CreatedNewConnection <- decodeControlHeader <$> recvWord32 sock
719719
connId <- recvWord32 sock :: IO LightweightConnectionId
720720

721721
connId' <- recvWord32 sock :: IO LightweightConnectionId
722722
True <- return $ connId == connId'
723-
["ping"] <- recvWithLength sock
723+
["ping"] <- recvWithLength Nothing sock
724724
putMVar serverGotPing ()
725725

726726
-- Client

0 commit comments

Comments
 (0)