@@ -8,18 +8,15 @@ module Network.Wai.Handler.Warp.Recv (
88#if __GLASGOW_HASKELL__ < 709
99import Control.Applicative ((<$>))
1010#endif
11−import Control.Monad (void)
12−import qualified Data.ByteString as BS (empty)
13−import Data.ByteString.Internal (ByteString(..), mallocByteString)
11+import Data.ByteString (ByteString)
1412import Data.Word (Word8)
1513import Foreign.C.Error (eAGAIN, getErrno, throwErrno)
1614import Foreign.C.Types
17−import Foreign.ForeignPtr (withForeignPtr)
1815import Foreign.Ptr (Ptr, castPtr)
1916import GHC.Conc (threadWaitRead)
2017import Network.Socket (Socket, fdSocket)
21−import System.Posix.Types (Fd(..))
2218import Network.Wai.Handler.Warp.Buffer
19+import System.Posix.Types (Fd(..))
2320
2421#ifdef mingw32_HOST_OS
2522import GHC.IO.FD (FD(..), readRawBufferPtr)
@@ -28,37 +25,25 @@ import Network.Wai.Handler.Warp.Windows
2825
2926----------------------------------------------------------------
3027
31−receive :: Socket -> Buffer -> Int -> IO ByteString
32−receive sock buf size = do
33− bytes <- fromIntegral <$> receiveloop sock' buf' size'
34− if bytes == 0 then
35− return BS.empty
36− else do
37− fptr <- mallocByteString bytes
38− void $ withForeignPtr fptr $ \ptr ->
39− c_memcpy ptr buf (fromIntegral bytes)
40− return $! PS fptr 0 bytes
41− where
42− sock' = fdSocket sock
43− buf' = castPtr buf
44− size' = fromIntegral size
28+receive :: Socket -> BufferPool -> IO ByteString
29+receive sock pool = withBufferPool pool $ \ (ptr, size) -> do
30+ let sock' = fdSocket sock
31+ size' = fromIntegral size
32+ received <- fromIntegral <$> receiveloop sock' ptr size'
33+ return received
4534
46−#ifdef mingw32_HOST_OS
4735receiveloop :: CInt -> Ptr Word8 -> CSize -> IO CInt
48−#else
49−receiveloop :: CInt -> Ptr CChar -> CSize -> IO CInt
50−#endif
51−receiveloop sock buf size = do
36+receiveloop sock ptr size = do
5237#ifdef mingw32_HOST_OS
53− bytes <- windowsThreadBlockHack $ fmap fromIntegral $ readRawBufferPtr "recv" (FD sock 1) buf 0 size
38+ bytes <- windowsThreadBlockHack $ fmap fromIntegral $ readRawBufferPtr "recv" (FD sock 1) ptr 0 size
5439#else
55− bytes <- c_recv sock buf size 0
40+ bytes <- c_recv sock (castPtr ptr) size 0
5641#endif
5742 if bytes == -1 then do
5843 errno <- getErrno
5944 if errno == eAGAIN then do
6045 threadWaitRead (Fd sock)
61− receiveloop sock buf size
46+ receiveloop sock ptr size
6247 else
6348 throwErrno "receiveloop"
6449 else
@@ -67,6 +52,3 @@ receiveloop sock buf size = do
6752-- fixme: the type of the return value
6853foreign import ccall unsafe "recv"
6954 c_recv :: CInt -> Ptr CChar -> CSize -> CInt -> IO CInt
70−
71−foreign import ccall unsafe "string.h memcpy"
72− c_memcpy :: Ptr Word8 -> Ptr Word8 -> CSize -> IO (Ptr Word8)