Skip to content

Commit b22124b

Browse files
committed
Merge PR #451.
2 parents a4c0ead + e5283b8 commit b22124b

File tree

1 file changed

+17
-16
lines changed
  • Network/Socket/ByteString

1 file changed

+17
-16
lines changed

Network/Socket/ByteString/IO.hsc

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE BangPatterns #-}
12
{-# LANGUAGE RecordWildCards #-}
23
{-# LANGUAGE OverloadedStrings #-}
34

@@ -269,22 +270,23 @@ remainingChunks i (x:xs)
269270
-- IOVec made from @cs@ and the number of pointers (@length cs@).
270271
-- /Unix only/.
271272
withIOVecfromBS :: [ByteString] -> ((Ptr IOVec, Int) -> IO a) -> IO a
272-
withIOVecfromBS cs f = do
273-
bufsizs <- mapM getBufsiz cs
274-
withIOVec bufsizs f
273+
withIOVecfromBS cs f = withBufSizs cs $ \bufsizs -> withIOVec bufsizs f
275274
#else
276275
-- | @withWSABuffromBS cs f@ executes the computation @f@, passing as argument a pair
277276
-- consisting of a pointer to a temporarily allocated array of pointers to
278277
-- WSABuf made from @cs@ and the number of pointers (@length cs@).
279278
-- /Windows only/.
280279
withWSABuffromBS :: [ByteString] -> ((Ptr WSABuf, Int) -> IO a) -> IO a
281-
withWSABuffromBS cs f = do
282-
bufsizs <- mapM getBufsiz cs
283-
withWSABuf bufsizs f
280+
withWSABuffromBS cs f = withBufSizs cs $ \bufsizs -> withWSABuf bufsizs f
284281
#endif
285282

286-
getBufsiz :: ByteString -> IO (Ptr Word8, Int)
287-
getBufsiz (PS fptr off len) = withForeignPtr fptr $ \ptr -> return (ptr `plusPtr` off, len)
283+
withBufSizs :: [ByteString] -> ([(Ptr Word8, Int)] -> IO a) -> IO a
284+
withBufSizs bss0 f = loop bss0 id
285+
where
286+
loop [] !build = f $ build []
287+
loop (PS fptr off len:bss) !build = withForeignPtr fptr $ \ptr -> do
288+
let !ptr' = ptr `plusPtr` off
289+
loop bss (build . ((ptr',len) :))
288290

289291
-- | Send data to the socket using sendmsg(2).
290292
sendMsg :: Socket -- ^ Socket
@@ -294,8 +296,7 @@ sendMsg :: Socket -- ^ Socket
294296
-> MsgFlag -- ^ Message flags
295297
-> IO Int -- ^ The length actually sent
296298
sendMsg _ _ [] _ _ = return 0
297-
sendMsg s addr bss cmsgs flags = do
298-
bufsizs <- mapM getBufsiz bss
299+
sendMsg s addr bss cmsgs flags = withBufSizs bss $ \bufsizs ->
299300
sendBufMsg s addr bufsizs cmsgs flags
300301

301302
-- | Receive data from the socket using recvmsg(2).
@@ -309,9 +310,9 @@ recvMsg :: Socket -- ^ Socket
309310
-> MsgFlag -- ^ Message flags
310311
-> IO (SockAddr, ByteString, [Cmsg], MsgFlag) -- ^ Source address, received data, control messages and message flags
311312
recvMsg s siz clen flags = do
312-
bs <- create siz $ \ptr -> zeroMemory ptr (fromIntegral siz)
313-
bufsiz <- getBufsiz bs
314-
(addr,len,cmsgs,flags') <- recvBufMsg s [bufsiz] clen flags
315-
let bs' | len < siz = let PS buf 0 _ = bs in PS buf 0 len
316-
| otherwise = bs
317-
return (addr, bs', cmsgs, flags')
313+
bs@(PS fptr _ _) <- create siz $ \ptr -> zeroMemory ptr (fromIntegral siz)
314+
withForeignPtr fptr $ \ptr -> do
315+
(addr,len,cmsgs,flags') <- recvBufMsg s [(ptr,siz)] clen flags
316+
let bs' | len < siz = PS fptr 0 len
317+
| otherwise = bs
318+
return (addr, bs', cmsgs, flags')

0 commit comments

Comments
 (0)