@@ -8,18 +8,15 @@ module Network.Wai.Handler.Warp.Recv (
8#if __GLASGOW_HASKELL__ < 709
8#if __GLASGOW_HASKELL__ < 709
9import Control.Applicative ((<$>))
9import Control.Applicative ((<$>))
11−import Control.Monad (void)
12−import qualified Data.ByteString as BS (empty)
11+import Data.ByteString (ByteString)
13−import Data.ByteString.Internal (ByteString(..), mallocByteString)
14import Data.Word (Word8)
12import Data.Word (Word8)
15import Foreign.C.Error (eAGAIN, getErrno, throwErrno)
13import Foreign.C.Error (eAGAIN, getErrno, throwErrno)
16import Foreign.C.Types
14import Foreign.C.Types
17−import Foreign.ForeignPtr (withForeignPtr)
18import Foreign.Ptr (Ptr, castPtr)
15import Foreign.Ptr (Ptr, castPtr)
19import GHC.Conc (threadWaitRead)
16import GHC.Conc (threadWaitRead)
20import Network.Socket (Socket, fdSocket)
17import Network.Socket (Socket, fdSocket)
21−import System.Posix.Types (Fd(..))
22import Network.Wai.Handler.Warp.Buffer
18import Network.Wai.Handler.Warp.Buffer
19+import System.Posix.Types (Fd(..))
24#ifdef mingw32_HOST_OS
21#ifdef mingw32_HOST_OS
25import GHC.IO.FD (FD(..), readRawBufferPtr)
22import GHC.IO.FD (FD(..), readRawBufferPtr)
@@ -28,37 +25,25 @@ import Network.Wai.Handler.Warp.Windows
29----------------------------------------------------------------
26----------------------------------------------------------------
31−receive :: Socket -> Buffer -> Int -> IO ByteString
28+receive :: Socket -> BufferPool -> IO ByteString
32−receive sock buf size = do
29+receive sock pool = withBufferPool pool $ \ (ptr, size) -> do
33− bytes <- fromIntegral <$> receiveloop sock' buf' size'
37− fptr <- mallocByteString bytes
38− void $ withForeignPtr fptr $ \ptr ->
39− c_memcpy ptr buf (fromIntegral bytes)
40− return $! PS fptr 0 bytes
42− sock' = fdSocket sock
30+ let sock' = fdSocket sock
43− buf' = castPtr buf
31+ size' = fromIntegral size
44− size' = fromIntegral size
32+ received <- fromIntegral <$> receiveloop sock' ptr size'
46−#ifdef mingw32_HOST_OS
47receiveloop :: CInt -> Ptr Word8 -> CSize -> IO CInt
35receiveloop :: CInt -> Ptr Word8 -> CSize -> IO CInt
49−receiveloop :: CInt -> Ptr CChar -> CSize -> IO CInt
51−receiveloop sock buf size = do
36+receiveloop sock ptr size = do
52#ifdef mingw32_HOST_OS
37#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) (castPtr ptr) 0 size
55− bytes <- c_recv sock buf size 0
40+ bytes <- c_recv sock (castPtr ptr) size 0
57 if bytes == -1 then do
42 if bytes == -1 then do
58 errno <- getErrno
43 errno <- getErrno
59 if errno == eAGAIN then do
44 if errno == eAGAIN then do
60 threadWaitRead (Fd sock)
45 threadWaitRead (Fd sock)
61− receiveloop sock buf size
46+ receiveloop sock ptr size
63 throwErrno "receiveloop"
48 throwErrno "receiveloop"
@@ -67,6 +52,3 @@ receiveloop sock buf size = do
67-- fixme: the type of the return value
52-- fixme: the type of the return value
68foreign import ccall unsafe "recv"
53foreign import ccall unsafe "recv"
69 c_recv :: CInt -> Ptr CChar -> CSize -> CInt -> IO CInt
54 c_recv :: CInt -> Ptr CChar -> CSize -> CInt -> IO CInt
71−foreign import ccall unsafe "string.h memcpy"
72− c_memcpy :: Ptr Word8 -> Ptr Word8 -> CSize -> IO (Ptr Word8)