simple-sendfile-0.2.34: Network/Sendfile/BSD.hsc
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CPP #-}
module Network.Sendfile.BSD (
sendfile
, sendfileFd
, sendfileWithHeader
, sendfileFdWithHeader
, sendfileWith
, sendfileFdWith
, sendfileWithHeaderWith
, sendfileFdWithHeaderWith
) where
import Control.Concurrent
import Control.Exception
import Data.Void (Void, absurd)
import GHC.Conc (STM)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Foreign.C.Error (eAGAIN, eINTR, getErrno, throwErrno)
import Foreign.C.Types
import Foreign.Marshal (alloca)
import Foreign.Ptr (Ptr, nullPtr)
import Foreign.Storable (peek, poke)
import Network.Sendfile.IOVec
import Network.Sendfile.Types
import Network.Socket
import Network.Socket.ByteString
import System.Posix.IO ( OpenMode(..)
, OpenFileFlags(..)
, defaultFileFlags
, closeFd
)
import System.Posix.Types
#include <sys/types.h>
entire :: COff
entire = 0
-- |
-- Simple binding for sendfile() of BSD and MacOS.
--
-- - Used system calls: open(), sendfile(), and close().
--
-- 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 sock path range hook = bracket setup teardown $ \fd ->
sendfileFd sock fd range hook
where
setup = openFd path ReadOnly defaultFileFlags{nonBlock=True}
teardown = closeFd
-- |
-- Simple binding for sendfile() of BSD and MacOS.
--
-- - Used system calls: sendfile()
--
-- 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 sock fd range hook =
withFdSocket sock $ \s ->
sendfileFdWait (blocking (Fd s)) sock fd range hook >>= either absurd 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 sock path range hook giveUp = bracket setup teardown $ \fd ->
sendfileFdWith sock fd range hook giveUp
where
setup = openFd path ReadOnly defaultFileFlags{nonBlock=True}
teardown = closeFd
-- | 'sendfileFd' which gives up as 'sendfileWith' does.
sendfileFdWith :: Socket -> Fd -> FileRange -> IO () -> STM a -> IO (Either a ())
sendfileFdWith sock fd range hook giveUp =
sendfileFdWait (writable sock giveUp) sock fd range hook
-- Waiting for the socket to become writable: 'Left' when given up.
type Wait a = IO (Either a ())
blocking :: Fd -> Wait Void
blocking dst = Right <$> threadWaitWrite dst
writable :: Socket -> STM a -> Wait a
writable sock giveUp = withWritable sock giveUp (return ())
sendfileFdWait :: Wait a -> Socket -> Fd -> FileRange -> IO () -> IO (Either a ())
sendfileFdWait wait sock fd range hook = do
checkRange range
withFdSocket sock $ \s -> do
let dst = Fd s
alloca $ \sentp -> case range of
EntireFile -> sendloop wait dst fd 0 entire sentp hook
-- 0 means the entire file for BSD sendfile(), so skip it.
PartOfFile off len
| len <= 0 -> return $ Right ()
| otherwise -> sendloop wait dst fd (fromInteger off) (fromInteger len) sentp hook
sendloop :: Wait a -> Fd -> Fd -> COff -> COff -> Ptr COff -> IO () -> IO (Either a ())
sendloop wait dst src off len sentp hook = do
rc <- sendFile src dst off len sentp nullPtr
if rc == 0 then return $ Right () else do
errno <- getErrno
if errno `elem` [eAGAIN, eINTR] then do
sent <- peek sentp
hook
let newoff = off + sent
newlen = if len == entire then entire else len - sent
-- newlen must not reach 0, which means the entire file.
if len /= entire && newlen <= 0 then return $ Right () else do
-- Parallel IO manager use edge-trigger mode.
-- So, waiting only when errnor is eAGAIN.
r <- if errno == eAGAIN then wait else return $ Right ()
either (return . Left) (\_ -> sendloop wait dst src newoff newlen sentp hook) r
else
throwErrno "Network.Sendfile.BSD.sendloop"
----------------------------------------------------------------
-- |
-- Simple binding for sendfile() of BSD and MacOS.
--
-- - Used system calls: open(), sendfile(), and close().
--
-- The fifth header is also sent with sendfile(). If the file is
-- small enough, the header and the file is send in a single TCP packet
-- on FreeBSD. MacOS sends the header and the file separately but it is
-- fast.
--
-- 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 sock path range hook hdr =
bracket setup teardown $ \fd -> sendfileFdWithHeader sock fd range hook hdr
where
setup = openFd path ReadOnly defaultFileFlags{nonBlock=True}
teardown = closeFd
-- |
-- Simple binding for sendfile() of BSD and MacOS.
--
-- - Used system calls: sendfile()
--
-- The fifth header is also sent with sendfile(). If the file is
-- small enough, the header and the file is send in a single TCP packet
-- on FreeBSD. MacOS sends the header and the file separately but it is
-- fast.
--
-- 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 sock fd range hook hdr =
withFdSocket sock $ \s ->
sendfileFdWithHeaderWait (blocking (Fd s)) (fmap Right . sendMany sock) sock fd range hook hdr
>>= either absurd return
-- | 'sendfileWithHeader' which gives up as 'sendfileWith' does, also
-- while sending the header.
sendfileWithHeaderWith :: Socket -> FilePath -> FileRange -> IO () -> [ByteString] -> STM a -> IO (Either a ())
sendfileWithHeaderWith sock path range hook hdr giveUp =
bracket setup teardown $ \fd -> sendfileFdWithHeaderWith sock fd range hook hdr giveUp
where
setup = openFd path ReadOnly defaultFileFlags{nonBlock=True}
teardown = 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 sock fd range hook hdr giveUp =
sendfileFdWithHeaderWait (writable sock giveUp) (\bss -> sendManyWith sock bss giveUp) sock fd range hook hdr
sendfileFdWithHeaderWait
:: Wait a -> ([ByteString] -> IO (Either a ())) -> Socket -> Fd -> FileRange -> IO () -> [ByteString] -> IO (Either a ())
sendfileFdWithHeaderWait wait sendHdr sock fd range hook hdr = do
checkRange range
withFdSocket sock $ \s -> do
let dst = Fd s
alloca $ \sentp ->
if isEmptyPart then
-- 0 means the entire file for BSD sendfile(), so skip it.
sendHdr hdr
else if isFreeBSD && hlen >= 8192 then do
-- If the length of the header is larger than 8191,
-- threadWaitWrite does not come back on FreeBSD, sigh.
-- We use writev() for the header and sendfile() for the file.
r <- sendHdr hdr
either (return . Left) (\_ -> sendfileFdWait wait sock fd range hook) r
else do
-- On MacOS, the header and the body are sent separately.
-- But it's fast. the writev() and sendfile() combination
-- is also fast.
let (off,len) = case range of
EntireFile -> (0,entire)
PartOfFile off' len' -> (fromInteger off'
,fromInteger len' + hlen)
mrc <- sendloopHeader wait dst fd off len sentp hdr hlen hook
case mrc of
Left a -> return $ Left a
Right Nothing -> return $ Right ()
Right (Just (newoff,newlen)) -> sendloop wait dst fd newoff newlen sentp hook
where
hlen = fromIntegral . sum . map BS.length $ hdr
isEmptyPart = case range of
PartOfFile _ len -> len <= 0
EntireFile -> False
sendloopHeader :: Wait a -> Fd -> Fd -> COff -> COff -> Ptr COff -> [ByteString] -> COff -> IO () -> IO (Either a (Maybe (COff, COff)))
sendloopHeader wait dst src off len sentp hdr hlen hook = do
rc <- withSfHdtr hdr $ sendFile src dst off len sentp
if rc == 0 then
return $ Right Nothing
else do
errno <- getErrno
if errno `elem` [eAGAIN, eINTR] then do
sent <- peek sentp
hook
-- Parallel IO manager use edge-trigger mode.
-- So, calling threadWaitWrite only when errnor is eAGAIN.
let waitThen k = do
r <- if errno == eAGAIN then wait else return $ Right ()
either (return . Left) (const k) r
if sent >= hlen then do
let newoff = off + sent - hlen
if len == entire then
waitThen $ return $ Right $ Just (newoff, entire)
else if len - sent <= 0 then
-- 0 means the entire file, so stop here.
return $ Right Nothing
else
waitThen $ return $ Right $ Just (newoff, len - sent)
else do
let newlen = if len == entire then entire else len - sent
newhdr = remainingChunks (fromIntegral sent) hdr
newhlen = hlen - sent
waitThen $ sendloopHeader wait dst src off newlen sentp newhdr newhlen hook
else
throwErrno "Network.Sendfile.BSD.sendloopHeader"
----------------------------------------------------------------
#ifdef OS_MacOS
-- Shuffle the order of arguments for currying.
sendFile :: Fd -> Fd -> COff -> COff -> Ptr COff -> Ptr SfHdtr -> IO CInt
sendFile fd s off len sentp hdrp = do
poke sentp len
c_sendfile fd s off sentp hdrp 0
foreign import ccall unsafe "sys/uio.h sendfile"
c_sendfile :: Fd -> Fd -> COff -> Ptr COff -> Ptr SfHdtr -> CInt -> IO CInt
isFreeBSD :: Bool
isFreeBSD = False
#else
-- Let's don't use CSize for 'len' and use COff for convenience.
-- Shuffle the order of arguments for currying.
sendFile :: Fd -> Fd -> COff -> COff -> Ptr COff -> Ptr SfHdtr -> IO CInt
sendFile fd s off len sentp hdrp =
c_sendfile fd s off (fromIntegral len) hdrp sentp 0
foreign import ccall unsafe "sys/uio.h sendfile"
c_sendfile :: Fd -> Fd -> COff -> CSize -> Ptr SfHdtr -> Ptr COff -> CInt -> IO CInt
isFreeBSD :: Bool
isFreeBSD = True
#endif