{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}{-# LANGUAGE BangPatterns #-}moduleNetwork.Socket.ByteString.Lazy.Posix(-- * Send data to a socketsend ,sendAll )whereimportControl.Monad(liftM)importControl.Monad(unless)importqualifiedData.ByteString.LazyasLimportData.ByteString.Lazy.Internal(ByteString(..))importData.ByteString.Unsafe(unsafeUseAsCStringLen)importData.Int(Int64)importForeign.Marshal.Array(allocaArray)importForeign.Ptr(plusPtr)importForeign.Storable(Storable(..))importNetwork.Socket (Socket (..))importNetwork.Socket.ByteString.IOVec (IOVec (IOVec ))importNetwork.Socket.ByteString.Internal (c_writev )importNetwork.Socket.Internal -- ------------------------------------------------------------------------------- Sendingsend::Socket -- ^ Connected socket->ByteString-- ^ Data to send->IOInt64-- ^ Number of bytes sentsend sock @(MkSocket fd ____)s =doletcs =takemaxNumChunks (L.toChunkss )len =lengthcs liftMfromIntegral.allocaArraylen $\ptr ->withPokes cs ptr $\niovs ->throwSocketErrorWaitWrite sock "writev"$c_writev (fromIntegralfd )ptr niovs wherewithPokes ss p f =loop ss p 00whereloop (c :cs )q k !niovs |k <maxNumBytes =unsafeUseAsCStringLenc $\(ptr ,len )->dopokeq $IOVec ptr (fromIntegrallen )loop cs (q `plusPtr`sizeOf(undefined::IOVec ))(k +fromIntegrallen )(niovs +1)|otherwise=f niovs loop___niovs =f niovs maxNumBytes =4194304::Int-- maximum number of bytes to transmit in one system callmaxNumChunks =1024::Int-- maximum number of chunks to transmit in one system callsendAll::Socket -- ^ Connected socket->ByteString-- ^ Data to send->IO()sendAll sock bs =dosent <-send sock bs letbs' =L.dropsent bs unless(L.nullbs' )$sendAll sock bs'