1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CPP #-}
#define _FILE_OFFSET_BITS 64
module Network.Sendfile.Linux (
sendfile
, sendfile'
, sendfileFd
, sendfileFd'
, sendfileWithHeader
, sendfileFdWithHeader
) where
import Control.Exception
import Control.Monad
import Data.ByteString as B
import Data.ByteString.Internal
import Foreign.C.Error (eAGAIN, getErrno, throwErrno)
import Foreign.C.Types
import Foreign.Marshal (alloca)
import Foreign.Ptr (Ptr, plusPtr, castPtr)
import Foreign.ForeignPtr
import Foreign.Storable (poke, sizeOf)
import GHC.Conc (threadWaitWrite)
import Network.Sendfile.Types
import Network.Socket
import System.Posix.Files
import System.Posix.IO ( OpenMode(..)
, OpenFileFlags(..)
, defaultFileFlags
, closeFd
)
import System.Posix.Types
#include <sys/sendfile.h>
#include <sys/socket.h>
isLargeSize :: Bool
isLargeSize = sizeOf (0 :: CSize) == 8
safeSize :: CSize
safeSize
| isLargeSize = 2^(60 :: Int)
| otherwise = 2^(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 sock path range hook = bracket setup teardown $ \fd ->
sendfileFd sock fd range hook
where
setup = openFd path ReadOnly defaultFileFlags{nonBlock=True}
teardown = closeFd
sendfile' :: Fd -> ByteString -> FileRange -> IO () -> IO ()
sendfile' dst path range hook = bracket setup teardown $ \src ->
sendfileFd' dst src range hook
where
setup = openFdBS path ReadOnly defaultFileFlags{nonBlock=True}
teardown = 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 sock fd range hook = do
#if MIN_VERSION_network(3,1,0)
withFdSocket sock $ \s -> do
let dst = Fd s
#elif MIN_VERSION_network(3,0,0)
dst <- Fd <$> fdSocket sock
#else
let dst = Fd $ fdSocket sock
#endif
sendfileFd' dst fd range hook
sendfileFd' :: Fd -> Fd -> FileRange -> IO () -> IO ()
sendfileFd' dst src range hook =
alloca $ \offp -> case range of
EntireFile -> do
poke offp 0
-- System call is very slow. Use PartOfFile instead.
len <- fileSize <$> getFdStatus src
let len' = fromIntegral len
sendfileloop dst src offp len' hook
PartOfFile off len -> do
poke offp (fromIntegral off)
let len' = fromIntegral len
sendfileloop dst src offp len' hook
sendfileloop :: Fd -> Fd -> Ptr COff -> CSize -> IO () -> IO ()
sendfileloop dst src offp len hook = do
-- Multicore IO manager use edge-trigger mode.
-- So, calling threadWaitWrite only when errnor is eAGAIN.
let toSend
| len > safeSize = safeSize
| otherwise = len
bytes <- c_sendfile dst src offp toSend
case bytes of
-1 -> do
errno <- getErrno
if errno == eAGAIN then do
threadWaitWrite dst
sendfileloop dst src offp len hook
else
throwErrno "Network.SendFile.Linux.sendfileloop"
0 -> return () -- the file is truncated
_ -> do
hook
let left = len - fromIntegral bytes
when (left /= 0) $ sendfileloop dst src offp left hook
-- Dst Src in order. take care
foreign import ccall unsafe "sendfile"
c_sendfile :: Fd -> Fd -> Ptr COff -> CSize -> IO CSsize
----------------------------------------------------------------
-- |
-- Simple binding for send() and sendfile() of Linux.
-- Used system calls:
--
-- - EntireFile -- send(), open(), stat(), sendfile(), and close()
--
-- - PartOfFile -- send(), open(), 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 sock path range hook hdr = do
-- Copying is much faster than syscall.
sendMsgMore sock $ B.concat hdr
sendfile sock path range hook
-- |
-- 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 sock fd range hook hdr = do
-- Copying is much faster than syscall.
sendMsgMore sock $ B.concat hdr
sendfileFd sock fd range hook
sendMsgMore :: Socket -> ByteString -> IO ()
sendMsgMore sock bs = withForeignPtr fptr $ \ptr -> do
#if MIN_VERSION_network(3,1,0)
withFdSocket sock $ \fd -> do
let s = Fd fd
#elif MIN_VERSION_network(3,0,0)
s <- Fd <$> fdSocket sock
#else
let s = Fd $ fdSocket sock
#endif
let buf = castPtr (ptr `plusPtr` off)
siz = fromIntegral len
sendloop s buf siz
where
PS fptr off len = bs
sendloop :: Fd -> Ptr CChar -> CSize -> IO ()
sendloop s buf len = do
bytes <- c_send s buf len (#const MSG_MORE)
if bytes == -1 then do
errno <- getErrno
if errno == eAGAIN then do
threadWaitWrite s
sendloop s buf len
else
throwErrno "Network.SendFile.Linux.sendloop"
else do
let sent = fromIntegral bytes
when (sent /= len) $ do
let left = len - sent
ptr = buf `plusPtr` fromIntegral bytes
sendloop s ptr left
foreign import ccall unsafe "send"
c_send :: Fd -> Ptr CChar -> CSize -> CInt -> IO CSsize
|