{-# LANGUAGE BangPatterns #-}{-# LANGUAGE OverloadedStrings #-}moduleNetwork.Socket.ByteString.Lazy.Posix(-- * Send data to a socketsend ,sendAll )whereimportqualifiedData.ByteString.LazyasLimportData.ByteString.Unsafe(unsafeUseAsCStringLen)importForeign.Marshal.Array(allocaArray)importNetwork.Socket.ByteString.Internal (c_writev )importNetwork.Socket.ByteString.IO (waitWhen0 )importNetwork.Socket.ByteString.IOVec (IOVec (IOVec ))importNetwork.Socket.Imports importNetwork.Socket.Internal importNetwork.Socket.Types -- ------------------------------------------------------------------------------- Sendingsend::Socket -- ^ Connected socket->L.ByteString-- ^ Data to send->IOInt64-- ^ Number of bytes sentsend s lbs =doletcs =takemaxNumChunks (L.toChunkslbs )len =lengthcs fd <-fdSocket s siz <-allocaArraylen $\ptr ->withPokes cs ptr $\niovs ->throwSocketErrorWaitWrite s "writev"$c_writev fd ptr niovs return$fromIntegralsiz 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->L.ByteString-- ^ Data to send->IO()sendAll _""=return()sendAlls bs =dosent <-send s bs waitWhen0 (fromIntegralsent )s when(sent >=0)$sendAll s $L.dropsent bs