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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
#include "HsNet.h"
-- |
-- Module : Network.Socket.ByteString.IO
-- Copyright : (c) Johan Tibell 2007-2010
-- License : BSD-style
--
-- Maintainer : johan.tibell@gmail.com
-- Stability : stable
-- Portability : portable
--
module Network.Socket.ByteString.IO
(
-- * Send data to a socket
send
, sendAll
, sendTo
, sendAllTo
-- ** Vectored I/O
-- $vectored
, sendMany
, sendManyTo
-- * Receive data from a socket
, recv
, recvFrom
, waitWhen0
-- * Advanced send and recv
, sendMsg
, recvMsg
, MsgFlag(..)
, Cmsg(..)
) where
import Control.Concurrent (threadWaitWrite, rtsSupportsBoundThreads)
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.ByteString.Internal (createAndTrim)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Foreign.Marshal.Alloc (allocaBytes)
import Network.Socket.Buffer
import Network.Socket.ByteString.Internal
import Network.Socket.Imports
import Network.Socket.Types
import Data.ByteString.Internal (create, ByteString(..))
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Marshal.Utils (with)
import Network.Socket.Internal
import Network.Socket.Flag
#if !defined(mingw32_HOST_OS)
import Network.Socket.Posix.Cmsg
import Network.Socket.Posix.IOVec
import Network.Socket.Posix.MsgHdr (MsgHdr(..))
#else
import Foreign.Marshal.Alloc (alloca)
import Network.Socket.Win32.Cmsg
import Network.Socket.Win32.WSABuf
import Network.Socket.Win32.MsgHdr (MsgHdr(..))
#endif
-- ----------------------------------------------------------------------------
-- Sending
-- | Send data to the socket. The socket must be connected to a
-- remote socket. Returns the number of bytes sent. Applications are
-- responsible for ensuring that all data has been sent.
send :: Socket -- ^ Connected socket
-> ByteString -- ^ Data to send
-> IO Int -- ^ Number of bytes sent
send s xs = unsafeUseAsCStringLen xs $ \(str, len) ->
sendBuf s (castPtr str) len
waitWhen0 :: Int -> Socket -> IO ()
waitWhen0 0 s = when rtsSupportsBoundThreads $
withFdSocket s $ \fd -> threadWaitWrite $ fromIntegral fd
waitWhen0 _ _ = return ()
-- | Send data to the socket. The socket must be connected to a
-- remote socket. Unlike 'send', this function continues to send data
-- until either all data has been sent or an error occurs. On error,
-- an exception is raised, and there is no way to determine how much
-- data, if any, was successfully sent.
sendAll :: Socket -- ^ Connected socket
-> ByteString -- ^ Data to send
-> IO ()
sendAll _ "" = return ()
sendAll s bs0 = loop bs0
where
loop bs = do
-- "send" throws an exception.
sent <- send s bs
waitWhen0 sent s
when (sent /= B.length bs) $ loop $ B.drop sent bs
-- | Send data to the socket. The recipient can be specified
-- explicitly, so the socket need not be in a connected state.
-- Returns the number of bytes sent. Applications are responsible for
-- ensuring that all data has been sent.
sendTo :: SocketAddress sa =>
Socket -- ^ Socket
-> ByteString -- ^ Data to send
-> sa -- ^ Recipient address
-> IO Int -- ^ Number of bytes sent
sendTo s xs sa =
unsafeUseAsCStringLen xs $ \(str, len) -> sendBufTo s str len sa
-- | Send data to the socket. The recipient can be specified
-- explicitly, so the socket need not be in a connected state. Unlike
-- 'sendTo', this function continues to send data until either all
-- data has been sent or an error occurs. On error, an exception is
-- raised, and there is no way to determine how much data, if any, was
-- successfully sent.
sendAllTo :: SocketAddress sa =>
Socket -- ^ Socket
-> ByteString -- ^ Data to send
-> sa -- ^ Recipient address
-> IO ()
sendAllTo _ "" _ = return ()
sendAllTo s bs0 sa = loop bs0
where
loop bs = do
-- "send" throws an exception.
sent <- sendTo s bs sa
waitWhen0 sent s
when (sent /= B.length bs) $ loop $ B.drop sent bs
-- | Send data to the socket. The socket must be in a connected
-- state. The data is sent as if the parts have been concatenated.
-- This function continues to send data until either all data has been
-- sent or an error occurs. On error, an exception is raised, and
-- there is no way to determine how much data, if any, was
-- successfully sent.
sendMany :: Socket -- ^ Connected socket
-> [ByteString] -- ^ Data to send
-> IO ()
sendMany _ [] = return ()
sendMany s cs = do
sent <- sendManyInner
waitWhen0 sent s
when (sent >= 0) $ sendMany s $ remainingChunks sent cs
where
sendManyInner =
#if !defined(mingw32_HOST_OS)
fmap fromIntegral . withIOVecfromBS cs $ \(iovsPtr, iovsLen) ->
withFdSocket s $ \fd -> do
let len = fromIntegral $ min iovsLen (#const IOV_MAX)
throwSocketErrorWaitWrite s "Network.Socket.ByteString.sendMany" $
c_writev fd iovsPtr len
#else
fmap fromIntegral . withWSABuffromBS cs $ \(wsabsPtr, wsabsLen) ->
withFdSocket s $ \fd -> do
let len = fromIntegral wsabsLen
alloca $ \send_ptr -> do
_ <- throwSocketErrorWaitWrite s "Network.Socket.ByteString.sendMany" $
c_wsasend fd wsabsPtr len send_ptr 0 nullPtr nullPtr
peek send_ptr
#endif
-- | Send data to the socket. The recipient can be specified
-- explicitly, so the socket need not be in a connected state. The
-- data is sent as if the parts have been concatenated. This function
-- continues to send data until either all data has been sent or an
-- error occurs. On error, an exception is raised, and there is no
-- way to determine how much data, if any, was successfully sent.
sendManyTo :: Socket -- ^ Socket
-> [ByteString] -- ^ Data to send
-> SockAddr -- ^ Recipient address
-> IO ()
sendManyTo _ [] _ = return ()
sendManyTo s cs addr = do
sent <- fromIntegral <$> sendManyToInner
waitWhen0 sent s
when (sent >= 0) $ sendManyTo s (remainingChunks sent cs) addr
where
sendManyToInner =
withSockAddr addr $ \addrPtr addrSize ->
#if !defined(mingw32_HOST_OS)
withIOVecfromBS cs $ \(iovsPtr, iovsLen) -> do
let msgHdr = MsgHdr {
msgName = addrPtr
, msgNameLen = fromIntegral addrSize
, msgIov = iovsPtr
, msgIovLen = fromIntegral iovsLen
, msgCtrl = nullPtr
, msgCtrlLen = 0
, msgFlags = 0
}
withFdSocket s $ \fd ->
with msgHdr $ \msgHdrPtr ->
throwSocketErrorWaitWrite s "Network.Socket.ByteString.sendManyTo" $
c_sendmsg fd msgHdrPtr 0
#else
withWSABuffromBS cs $ \(wsabsPtr, wsabsLen) -> do
let msgHdr = MsgHdr {
msgName = addrPtr
, msgNameLen = fromIntegral addrSize
, msgBuffer = wsabsPtr
, msgBufferLen = fromIntegral wsabsLen
, msgCtrl = nullPtr
, msgCtrlLen = 0
, msgFlags = 0
}
withFdSocket s $ \fd ->
with msgHdr $ \msgHdrPtr ->
alloca $ \send_ptr -> do
_ <- throwSocketErrorWaitWrite s "Network.Socket.ByteString.sendManyTo" $
c_sendmsg fd msgHdrPtr 0 send_ptr nullPtr nullPtr
peek send_ptr
#endif
-- ----------------------------------------------------------------------------
-- Receiving
-- | Receive data from the socket. The socket must be in a connected
-- state. This function may return fewer bytes than specified. If
-- the message is longer than the specified length, it may be
-- discarded depending on the type of socket. This function may block
-- until a message arrives.
--
-- Considering hardware and network realities, the maximum number of bytes to
-- receive should be a small power of 2, e.g., 4096.
--
-- For TCP sockets, a zero length return value means the peer has
-- closed its half side of the connection.
--
-- Currently, the 'recv' family is blocked on Windows because a proper
-- IO manager is not implemented. To use with 'System.Timeout.timeout'
-- on Windows, use 'Network.Socket.setSocketOption' with
-- 'Network.Socket.RecvTimeOut' as well.
recv :: Socket -- ^ Connected socket
-> Int -- ^ Maximum number of bytes to receive
-> IO ByteString -- ^ Data received
recv s nbytes
| nbytes < 0 = ioError (mkInvalidRecvArgError "Network.Socket.ByteString.recv")
| otherwise = createAndTrim nbytes $ \ptr -> recvBuf s ptr nbytes
-- | Receive data from the socket. The socket need not be in a
-- connected state. Returns @(bytes, address)@ where @bytes@ is a
-- 'ByteString' representing the data received and @address@ is a
-- 'SockAddr' representing the address of the sending socket.
--
-- If the first return value is zero, it means EOF.
recvFrom :: SocketAddress sa =>
Socket -- ^ Socket
-> Int -- ^ Maximum number of bytes to receive
-> IO (ByteString, sa) -- ^ Data received and sender address
recvFrom sock nbytes =
allocaBytes nbytes $ \ptr -> do
(len, sockaddr) <- recvBufFrom sock ptr nbytes
str <- B.packCStringLen (ptr, len)
return (str, sockaddr)
-- ----------------------------------------------------------------------------
-- Not exported
-- | Suppose we try to transmit a list of chunks @cs@ via a gathering write
-- operation and find that @n@ bytes were sent. Then @remainingChunks n cs@ is
-- list of chunks remaining to be sent.
remainingChunks :: Int -> [ByteString] -> [ByteString]
remainingChunks _ [] = []
remainingChunks i (x:xs)
| i < len = B.drop i x : xs
| otherwise = let i' = i - len in i' `seq` remainingChunks i' xs
where
len = B.length x
#if !defined(mingw32_HOST_OS)
-- | @withIOVecfromBS cs f@ executes the computation @f@, passing as argument a pair
-- consisting of a pointer to a temporarily allocated array of pointers to
-- IOVec made from @cs@ and the number of pointers (@length cs@).
-- /Unix only/.
withIOVecfromBS :: [ByteString] -> ((Ptr IOVec, Int) -> IO a) -> IO a
withIOVecfromBS cs f = withBufSizs cs $ \bufsizs -> withIOVec bufsizs f
#else
-- | @withWSABuffromBS cs f@ executes the computation @f@, passing as argument a pair
-- consisting of a pointer to a temporarily allocated array of pointers to
-- WSABuf made from @cs@ and the number of pointers (@length cs@).
-- /Windows only/.
withWSABuffromBS :: [ByteString] -> ((Ptr WSABuf, Int) -> IO a) -> IO a
withWSABuffromBS cs f = withBufSizs cs $ \bufsizs -> withWSABuf bufsizs f
#endif
withBufSizs :: [ByteString] -> ([(Ptr Word8, Int)] -> IO a) -> IO a
withBufSizs bss0 f = loop bss0 id
where
loop [] !build = f $ build []
loop (PS fptr off len:bss) !build = withForeignPtr fptr $ \ptr -> do
let !ptr' = ptr `plusPtr` off
loop bss (build . ((ptr',len) :))
-- | Send data to the socket using sendmsg(2).
sendMsg :: Socket -- ^ Socket
-> SockAddr -- ^ Destination address
-> [ByteString] -- ^ Data to be sent
-> [Cmsg] -- ^ Control messages
-> MsgFlag -- ^ Message flags
-> IO Int -- ^ The length actually sent
sendMsg _ _ [] _ _ = return 0
sendMsg s addr bss cmsgs flags = withBufSizs bss $ \bufsizs ->
sendBufMsg s addr bufsizs cmsgs flags
-- | Receive data from the socket using recvmsg(2).
recvMsg :: Socket -- ^ Socket
-> Int -- ^ The maximum length of data to be received
-- If the total length is not large enough,
-- 'MSG_TRUNC' is returned
-> Int -- ^ The buffer size for control messages.
-- If the length is not large enough,
-- 'MSG_CTRUNC' is returned
-> MsgFlag -- ^ Message flags
-> IO (SockAddr, ByteString, [Cmsg], MsgFlag) -- ^ Source address, received data, control messages and message flags
recvMsg s siz clen flags = do
bs@(PS fptr _ _) <- create siz $ \ptr -> zeroMemory ptr (fromIntegral siz)
withForeignPtr fptr $ \ptr -> do
(addr,len,cmsgs,flags') <- recvBufMsg s [(ptr,siz)] clen flags
let bs' | len < siz = PS fptr 0 len
| otherwise = bs
return (addr, bs', cmsgs, flags')
|