{-# OPTIONS_GHC -optc-D_FILE_OFFSET_BITS=64 #-}
{-# LINE 1 "Network/Sendfile/Linux.hsc" #-}
{-# LANGUAGE CApiFFI #-}
-- Let sendfile() take a 64-bit off_t also on 32-bit platforms.
{-# OPTIONS_GHC -optc-D_FILE_OFFSET_BITS=64 #-}



module Network.Sendfile.Linux (
    sendfile
  , sendfile'
  , sendfileFd
  , sendfileFd'
  , sendfileWithHeader
  , sendfileFdWithHeader
  , sendfileWith
  , sendfileFdWith
  , sendfileWithHeaderWith
  , sendfileFdWithHeaderWith
  ) where

import Control.Exception
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Foreign.C.Error (eAGAIN, eINTR, getErrno, throwErrno)
import Foreign.C.Types
import Foreign.Marshal (alloca)
import Foreign.Ptr (Ptr, plusPtr)
import Data.Void (Void, absurd)
import Foreign.Storable (poke, sizeOf)
import GHC.Conc (STM, threadWaitWrite)
import Network.Sendfile.Types
import Network.Socket
import System.Posix.Files
import System.Posix.IO ( OpenMode(..)
                       , OpenFileFlags(..)
                       , defaultFileFlags
                       , closeFd
                       )
import System.Posix.Types




isLargeSize :: Bool
isLargeSize :: Bool
isLargeSize = CSize -> Int
forall a. Storable a => a -> Int
sizeOf (CSize
0 :: CSize) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
8

safeSize :: CSize
safeSize :: CSize
safeSize
  | Bool
isLargeSize = CSize
2CSize -> Int -> CSize
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
60 :: Int)
  | Bool
otherwise   = CSize
2CSize -> Int -> CSize
forall a b. (Num a, Integral b) => a -> b -> a
^(Int
30 :: Int)

----------------------------------------------------------------

-- |
-- Simple binding for sendfile() of Linux.
-- Used system calls:
--
--  - EntireFile -- open(), stat(), sendfile(), and close()
--
--  - PartOfFile -- open(), sendfile(), and close()
--
-- If the size of the file is unknown when sending the entire file,
-- specifying PartOfFile is much faster.
--
-- The fourth action argument is called when a file is sent as chunks.
-- Chucking is inevitable if the socket is non-blocking (this is the
-- default) and the file is large. The action is called after a chunk
-- is sent and before waiting the socket to be ready for writing.

sendfile :: Socket -> FilePath -> FileRange -> IO () -> IO ()
sendfile :: Socket -> FilePath -> FileRange -> IO () -> IO ()
sendfile Socket
sock FilePath
path FileRange
range IO ()
hook = IO Fd -> (Fd -> IO ()) -> (Fd -> IO ()) -> IO ()
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket IO Fd
setup Fd -> IO ()
teardown ((Fd -> IO ()) -> IO ()) -> (Fd -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Fd
fd ->
    Socket -> Fd -> FileRange -> IO () -> IO ()
sendfileFd Socket
sock Fd
fd FileRange
range IO ()
hook
  where
    setup :: IO Fd
setup = FilePath -> OpenMode -> OpenFileFlags -> IO Fd
openFd FilePath
path OpenMode
ReadOnly OpenFileFlags
defaultFileFlags{nonBlock=True}
    teardown :: Fd -> IO ()
teardown = Fd -> IO ()
closeFd

sendfile' :: Fd -> ByteString -> FileRange -> IO () -> IO ()
sendfile' :: Fd -> ByteString -> FileRange -> IO () -> IO ()
sendfile' Fd
dst ByteString
path FileRange
range IO ()
hook = IO Fd -> (Fd -> IO ()) -> (Fd -> IO ()) -> IO ()
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket IO Fd
setup Fd -> IO ()
teardown ((Fd -> IO ()) -> IO ()) -> (Fd -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Fd
src ->
    Fd -> Fd -> FileRange -> IO () -> IO ()
sendfileFd' Fd
dst Fd
src FileRange
range IO ()
hook
  where
    setup :: IO Fd
setup = ByteString -> OpenMode -> OpenFileFlags -> IO Fd
openFdBS ByteString
path OpenMode
ReadOnly OpenFileFlags
defaultFileFlags{nonBlock=True}
    teardown :: Fd -> IO ()
teardown = Fd -> IO ()
closeFd

-- |
-- Simple binding for sendfile() of Linux.
-- Used system calls:
--
--  - EntireFile -- stat() and sendfile()
--
--  - PartOfFile -- sendfile()
--
-- If the size of the file is unknown when sending the entire file,
-- specifying PartOfFile is much faster.
--
-- The fourth action argument is called when a file is sent as chunks.
-- Chucking is inevitable if the socket is non-blocking (this is the
-- default) and the file is large. The action is called after a chunk
-- is sent and before waiting the socket to be ready for writing.
sendfileFd :: Socket -> Fd -> FileRange -> IO () -> IO ()
sendfileFd :: Socket -> Fd -> FileRange -> IO () -> IO ()
sendfileFd Socket
sock Fd
fd FileRange
range IO ()
hook = do
  Socket -> (CInt -> IO ()) -> IO ()
forall r. Socket -> (CInt -> IO r) -> IO r
withFdSocket Socket
sock ((CInt -> IO ()) -> IO ()) -> (CInt -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CInt
s -> do
    let dst :: Fd
dst = CInt -> Fd
Fd CInt
s
    Fd -> Fd -> FileRange -> IO () -> IO ()
sendfileFd' Fd
dst Fd
fd FileRange
range IO ()
hook

sendfileFd' :: Fd -> Fd -> FileRange -> IO () -> IO ()
sendfileFd' :: Fd -> Fd -> FileRange -> IO () -> IO ()
sendfileFd' Fd
dst Fd
src FileRange
range IO ()
hook =
    Wait Void -> Fd -> Fd -> FileRange -> IO () -> Wait Void
forall a. Wait a -> Fd -> Fd -> FileRange -> IO () -> Wait a
sendfileFdWait (Fd -> Wait Void
blocking Fd
dst) Fd
dst Fd
src FileRange
range IO ()
hook Wait Void -> (Either Void () -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Void -> IO ()) -> (() -> IO ()) -> Either Void () -> IO ()
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Void -> IO ()
forall a. Void -> a
absurd () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return

-- | 'sendfile' which gives up once the 'STM' action succeeds, while
--   waiting for the socket to become writable.  What the action gave is
--   returned as 'Left'; part of the file may have been sent by then.
sendfileWith :: Socket -> FilePath -> FileRange -> IO () -> STM a -> IO (Either a ())
sendfileWith :: forall a.
Socket
-> FilePath -> FileRange -> IO () -> STM a -> IO (Either a ())
sendfileWith Socket
sock FilePath
path FileRange
range IO ()
hook STM a
giveUp = IO Fd
-> (Fd -> IO ()) -> (Fd -> IO (Either a ())) -> IO (Either a ())
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket IO Fd
setup Fd -> IO ()
teardown ((Fd -> IO (Either a ())) -> IO (Either a ()))
-> (Fd -> IO (Either a ())) -> IO (Either a ())
forall a b. (a -> b) -> a -> b
$ \Fd
fd ->
    Socket -> Fd -> FileRange -> IO () -> STM a -> IO (Either a ())
forall a.
Socket -> Fd -> FileRange -> IO () -> STM a -> IO (Either a ())
sendfileFdWith Socket
sock Fd
fd FileRange
range IO ()
hook STM a
giveUp
  where
    setup :: IO Fd
setup = FilePath -> OpenMode -> OpenFileFlags -> IO Fd
openFd FilePath
path OpenMode
ReadOnly OpenFileFlags
defaultFileFlags{nonBlock=True}
    teardown :: Fd -> IO ()
teardown = Fd -> IO ()
closeFd

-- | 'sendfileFd' which gives up as 'sendfileWith' does.
sendfileFdWith :: Socket -> Fd -> FileRange -> IO () -> STM a -> IO (Either a ())
sendfileFdWith :: forall a.
Socket -> Fd -> FileRange -> IO () -> STM a -> IO (Either a ())
sendfileFdWith Socket
sock Fd
fd FileRange
range IO ()
hook STM a
giveUp =
    Socket -> (CInt -> IO (Either a ())) -> IO (Either a ())
forall r. Socket -> (CInt -> IO r) -> IO r
withFdSocket Socket
sock ((CInt -> IO (Either a ())) -> IO (Either a ()))
-> (CInt -> IO (Either a ())) -> IO (Either a ())
forall a b. (a -> b) -> a -> b
$ \CInt
s ->
        IO (Either a ())
-> Fd -> Fd -> FileRange -> IO () -> IO (Either a ())
forall a. Wait a -> Fd -> Fd -> FileRange -> IO () -> Wait a
sendfileFdWait (Socket -> STM a -> IO (Either a ())
forall a. Socket -> STM a -> Wait a
writable Socket
sock STM a
giveUp) (CInt -> Fd
Fd CInt
s) Fd
fd FileRange
range IO ()
hook

-- Waiting for the socket to become writable: 'Left' when given up.
type Wait a = IO (Either a ())

blocking :: Fd -> Wait Void
blocking :: Fd -> Wait Void
blocking Fd
dst = () -> Either Void ()
forall a b. b -> Either a b
Right (() -> Either Void ()) -> IO () -> Wait Void
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Fd -> IO ()
threadWaitWrite Fd
dst

writable :: Socket -> STM a -> Wait a
writable :: forall a. Socket -> STM a -> Wait a
writable Socket
sock STM a
giveUp = Socket -> STM a -> IO () -> IO (Either a ())
forall a b. Socket -> STM a -> IO b -> IO (Either a b)
withWritable Socket
sock STM a
giveUp (() -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ())

sendfileFdWait :: Wait a -> Fd -> Fd -> FileRange -> IO () -> IO (Either a ())
sendfileFdWait :: forall a. Wait a -> Fd -> Fd -> FileRange -> IO () -> Wait a
sendfileFdWait Wait a
wait Fd
dst Fd
src FileRange
range IO ()
hook = do
  FileRange -> IO ()
checkRange FileRange
range
  (Ptr COff -> Wait a) -> Wait a
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr COff -> Wait a) -> Wait a) -> (Ptr COff -> Wait a) -> Wait a
forall a b. (a -> b) -> a -> b
$ \Ptr COff
offp -> case FileRange
range of
        FileRange
EntireFile -> do
            Ptr COff -> COff -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr COff
offp COff
0
            -- System call is very slow. Use PartOfFile instead.
            len <- FileStatus -> COff
fileSize (FileStatus -> COff) -> IO FileStatus -> IO COff
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Fd -> IO FileStatus
getFdStatus Fd
src
            sendfileloop wait dst src offp len hook
        PartOfFile Integer
off Integer
len -> do
            Ptr COff -> COff -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr COff
offp (Integer -> COff
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
off)
            Wait a -> Fd -> Fd -> Ptr COff -> COff -> IO () -> Wait a
forall a. Wait a -> Fd -> Fd -> Ptr COff -> COff -> IO () -> Wait a
sendfileloop Wait a
wait Fd
dst Fd
src Ptr COff
offp (Integer -> COff
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
len) IO ()
hook

-- The remaining length is COff since CSize is 32-bit on 32-bit platforms.
sendfileloop :: Wait a -> Fd -> Fd -> Ptr COff -> COff -> IO () -> IO (Either a ())
sendfileloop :: forall a. Wait a -> Fd -> Fd -> Ptr COff -> COff -> IO () -> Wait a
sendfileloop Wait a
wait Fd
dst Fd
src Ptr COff
offp COff
len IO ()
hook = do
    -- Multicore IO manager use edge-trigger mode.
    -- So, calling threadWaitWrite only when errnor is eAGAIN.
    let toSend :: CSize
toSend
          | COff
len COff -> COff -> Bool
forall a. Ord a => a -> a -> Bool
> CSize -> COff
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
safeSize = CSize
safeSize
          | Bool
otherwise                   = COff -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral COff
len
    bytes <- Fd -> Fd -> Ptr COff -> CSize -> IO CSsize
c_sendfile Fd
dst Fd
src Ptr COff
offp CSize
toSend
    case bytes of
        -1 -> do
            errno <- IO Errno
getErrno
            if errno == eAGAIN then
                wait >>= either (return . Left) (\()
_ -> Wait a -> Fd -> Fd -> Ptr COff -> COff -> IO () -> Wait a
forall a. Wait a -> Fd -> Fd -> Ptr COff -> COff -> IO () -> Wait a
sendfileloop Wait a
wait Fd
dst Fd
src Ptr COff
offp COff
len IO ()
hook)
              else if errno == eINTR then
                sendfileloop wait dst src offp len hook
              else
                throwErrno "Network.Sendfile.Linux.sendfileloop"
        CSsize
0  -> Either a () -> Wait a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either a () -> Wait a) -> Either a () -> Wait a
forall a b. (a -> b) -> a -> b
$ () -> Either a ()
forall a b. b -> Either a b
Right () -- the file is truncated
        CSsize
_  -> do
            IO ()
hook
            let left :: COff
left = COff
len COff -> COff -> COff
forall a. Num a => a -> a -> a
- CSsize -> COff
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSsize
bytes
            if COff
left COff -> COff -> Bool
forall a. Eq a => a -> a -> Bool
/= COff
0 then Wait a -> Fd -> Fd -> Ptr COff -> COff -> IO () -> Wait a
forall a. Wait a -> Fd -> Fd -> Ptr COff -> COff -> IO () -> Wait a
sendfileloop Wait a
wait Fd
dst Fd
src Ptr COff
offp COff
left IO ()
hook else Either a () -> Wait a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either a () -> Wait a) -> Either a () -> Wait a
forall a b. (a -> b) -> a -> b
$ () -> Either a ()
forall a b. b -> Either a b
Right ()

-- Dst Src in order. take care
foreign import capi unsafe "sys/sendfile.h sendfile"
    c_sendfile :: Fd -> Fd -> Ptr COff -> CSize -> IO CSsize

----------------------------------------------------------------

-- |
-- Simple binding for send() and sendfile() of Linux.
-- Used system calls:
--
--  - EntireFile -- open(), send(), stat(), sendfile(), and close()
--
--  - PartOfFile -- open(), send(), sendfile(), and close()
--
-- The fifth header is sent with send() + the MSG_MORE flag. If the
-- file is small enough, the header and the file is send in a single
-- TCP packet.
--
-- If the size of the file is unknown when sending the entire file,
-- specifying PartOfFile is much faster.
--
-- The fourth action argument is called when a file is sent as chunks.
-- Chucking is inevitable if the socket is non-blocking (this is the
-- default) and the file is large. The action is called after a chunk
-- is sent and before waiting the socket to be ready for writing.

sendfileWithHeader :: Socket -> FilePath -> FileRange -> IO () -> [ByteString] -> IO ()
sendfileWithHeader :: Socket -> FilePath -> FileRange -> IO () -> [ByteString] -> IO ()
sendfileWithHeader Socket
sock FilePath
path FileRange
range IO ()
hook [ByteString]
hdr = IO Fd -> (Fd -> IO ()) -> (Fd -> IO ()) -> IO ()
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket IO Fd
setup Fd -> IO ()
teardown ((Fd -> IO ()) -> IO ()) -> (Fd -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Fd
fd ->
    Socket -> Fd -> FileRange -> IO () -> [ByteString] -> IO ()
sendfileFdWithHeader Socket
sock Fd
fd FileRange
range IO ()
hook [ByteString]
hdr
  where
    setup :: IO Fd
setup = FilePath -> OpenMode -> OpenFileFlags -> IO Fd
openFd FilePath
path OpenMode
ReadOnly OpenFileFlags
defaultFileFlags{nonBlock=True}
    teardown :: Fd -> IO ()
teardown = Fd -> IO ()
closeFd

-- |
-- Simple binding for send() and sendfile() of Linux.
-- Used system calls:
--
--  - EntireFile -- send(), stat() and sendfile()
--
--  - PartOfFile -- send() and sendfile()
--
-- The fifth header is sent with send() + the MSG_MORE flag. If the
-- file is small enough, the header and the file is send in a single
-- TCP packet.
--
-- If the size of the file is unknown when sending the entire file,
-- specifying PartOfFile is much faster.
--
-- The fourth action argument is called when a file is sent as chunks.
-- Chucking is inevitable if the socket is non-blocking (this is the
-- default) and the file is large. The action is called after a chunk
-- is sent and before waiting the socket to be ready for writing.

sendfileFdWithHeader :: Socket -> Fd -> FileRange -> IO () -> [ByteString] -> IO ()
sendfileFdWithHeader :: Socket -> Fd -> FileRange -> IO () -> [ByteString] -> IO ()
sendfileFdWithHeader Socket
sock Fd
fd FileRange
range IO ()
hook [ByteString]
hdr = do
    FileRange -> IO ()
checkRange FileRange
range
    -- Copying is much faster than syscall.
    (Fd -> Wait Void) -> Socket -> ByteString -> Wait Void
forall a. (Fd -> Wait a) -> Socket -> ByteString -> Wait a
sendMsgMore Fd -> Wait Void
blocking Socket
sock ([ByteString] -> ByteString
B.concat [ByteString]
hdr) Wait Void -> (Either Void () -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Void -> IO ()) -> (() -> IO ()) -> Either Void () -> IO ()
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Void -> IO ()
forall a. Void -> a
absurd () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return
    Socket -> Fd -> FileRange -> IO () -> IO ()
sendfileFd Socket
sock Fd
fd FileRange
range IO ()
hook

-- | 'sendfileWithHeader' which gives up as 'sendfileWith' does, also
--   while sending the header.
sendfileWithHeaderWith :: Socket -> FilePath -> FileRange -> IO () -> [ByteString] -> STM a -> IO (Either a ())
sendfileWithHeaderWith :: forall a.
Socket
-> FilePath
-> FileRange
-> IO ()
-> [ByteString]
-> STM a
-> IO (Either a ())
sendfileWithHeaderWith Socket
sock FilePath
path FileRange
range IO ()
hook [ByteString]
hdr STM a
giveUp = IO Fd
-> (Fd -> IO ()) -> (Fd -> IO (Either a ())) -> IO (Either a ())
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket IO Fd
setup Fd -> IO ()
teardown ((Fd -> IO (Either a ())) -> IO (Either a ()))
-> (Fd -> IO (Either a ())) -> IO (Either a ())
forall a b. (a -> b) -> a -> b
$ \Fd
fd ->
    Socket
-> Fd
-> FileRange
-> IO ()
-> [ByteString]
-> STM a
-> IO (Either a ())
forall a.
Socket
-> Fd
-> FileRange
-> IO ()
-> [ByteString]
-> STM a
-> IO (Either a ())
sendfileFdWithHeaderWith Socket
sock Fd
fd FileRange
range IO ()
hook [ByteString]
hdr STM a
giveUp
  where
    setup :: IO Fd
setup = FilePath -> OpenMode -> OpenFileFlags -> IO Fd
openFd FilePath
path OpenMode
ReadOnly OpenFileFlags
defaultFileFlags{nonBlock=True}
    teardown :: Fd -> IO ()
teardown = Fd -> IO ()
closeFd

-- | 'sendfileFdWithHeader' which gives up as 'sendfileWith' does, also
--   while sending the header.
sendfileFdWithHeaderWith :: Socket -> Fd -> FileRange -> IO () -> [ByteString] -> STM a -> IO (Either a ())
sendfileFdWithHeaderWith :: forall a.
Socket
-> Fd
-> FileRange
-> IO ()
-> [ByteString]
-> STM a
-> IO (Either a ())
sendfileFdWithHeaderWith Socket
sock Fd
fd FileRange
range IO ()
hook [ByteString]
hdr STM a
giveUp = do
    FileRange -> IO ()
checkRange FileRange
range
    r <- (Fd -> Wait a) -> Socket -> ByteString -> Wait a
forall a. (Fd -> Wait a) -> Socket -> ByteString -> Wait a
sendMsgMore (Wait a -> Fd -> Wait a
forall a b. a -> b -> a
const (Wait a -> Fd -> Wait a) -> Wait a -> Fd -> Wait a
forall a b. (a -> b) -> a -> b
$ Socket -> STM a -> Wait a
forall a. Socket -> STM a -> Wait a
writable Socket
sock STM a
giveUp) Socket
sock (ByteString -> Wait a) -> ByteString -> Wait a
forall a b. (a -> b) -> a -> b
$ [ByteString] -> ByteString
B.concat [ByteString]
hdr
    either (return . Left) (\()
_ -> Socket -> Fd -> FileRange -> IO () -> STM a -> Wait a
forall a.
Socket -> Fd -> FileRange -> IO () -> STM a -> IO (Either a ())
sendfileFdWith Socket
sock Fd
fd FileRange
range IO ()
hook STM a
giveUp) r

sendMsgMore :: (Fd -> Wait a) -> Socket -> ByteString -> IO (Either a ())
sendMsgMore :: forall a. (Fd -> Wait a) -> Socket -> ByteString -> Wait a
sendMsgMore Fd -> Wait a
wait Socket
sock ByteString
bs = ByteString -> (CStringLen -> Wait a) -> Wait a
forall a. ByteString -> (CStringLen -> IO a) -> IO a
unsafeUseAsCStringLen ByteString
bs ((CStringLen -> Wait a) -> Wait a)
-> (CStringLen -> Wait a) -> Wait a
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
buf, Int
len) ->
    Socket -> (CInt -> Wait a) -> Wait a
forall r. Socket -> (CInt -> IO r) -> IO r
withFdSocket Socket
sock ((CInt -> Wait a) -> Wait a) -> (CInt -> Wait a) -> Wait a
forall a b. (a -> b) -> a -> b
$ \CInt
fd -> Wait a -> Fd -> Ptr CChar -> CSize -> Wait a
forall a. Wait a -> Fd -> Ptr CChar -> CSize -> Wait a
sendloop (Fd -> Wait a
wait (CInt -> Fd
Fd CInt
fd)) (CInt -> Fd
Fd CInt
fd) Ptr CChar
buf (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)

sendloop :: Wait a -> Fd -> Ptr CChar -> CSize -> IO (Either a ())
sendloop :: forall a. Wait a -> Fd -> Ptr CChar -> CSize -> Wait a
sendloop Wait a
wait Fd
s Ptr CChar
buf CSize
len = do
    bytes <- Fd -> Ptr CChar -> CSize -> CInt -> IO CSsize
c_send Fd
s Ptr CChar
buf CSize
len (CInt
32768)
{-# LINE 255 "Network/Sendfile/Linux.hsc" #-}
    if bytes == -1 then do
        errno <- getErrno
        if errno == eAGAIN then
            wait >>= either (return . Left) (\_ -> sendloop wait s buf len)
          else if errno == eINTR then
            sendloop wait s buf len
          else
            throwErrno "Network.Sendfile.Linux.sendloop"
      else do
        let sent = fromIntegral bytes
        if sent /= len then do
            let left = len - sent
                ptr = buf `plusPtr` fromIntegral bytes
            sendloop wait s ptr left
          else return $ Right ()

foreign import ccall unsafe "send"
  c_send :: Fd -> Ptr CChar -> CSize -> CInt -> IO CSsize