{-# LANGUAGE CPP #-}#include "HsNetDef.h"
moduleNetwork.Socket.Buffer(sendBufTo ,sendBuf ,recvBufFrom ,recvBuf )whereimportqualifiedControl.ExceptionasEimportForeign.Marshal.Alloc(alloca)importGHC.IO.Exception(IOErrorType(InvalidArgument))importSystem.IO.Error(mkIOError,ioeSetErrorString)#if defined(mingw32_HOST_OS)
importGHC.IO.FD(FD(..),readRawBufferPtr,writeRawBufferPtr)#endif
importNetwork.Socket.Imports importNetwork.Socket.Internal importNetwork.Socket.Name importNetwork.Socket.Types -- | 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.sendBufTo::SocketAddress sa =>Socket -- (possibly) bound/connected Socket->Ptra ->Int-- Data to send->sa ->IOInt-- Number of Bytes sentsendBufTo s ptr nbytes sa =withSocketAddress sa $\p_sa siz ->fromIntegral<$>dofd <-fdSocket s letsz =fromIntegralsiz n =fromIntegralnbytes flags =0throwSocketErrorWaitWrite s "Network.Socket.sendBufTo"$c_sendto fd ptr n flags p_sa sz #if defined(mingw32_HOST_OS)
socket2FD::Socket->IOFDsocket2FDs=dofd<-fdSockets-- HACK, 1 means Truereturn$FD{fdFD=fd,fdIsSocket_=1}#endif
-- | 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.---- Sending data to closed socket may lead to undefined behaviour.sendBuf::Socket -- Bound/Connected Socket->PtrWord8-- Pointer to the data to send->Int-- Length of the buffer->IOInt-- Number of Bytes sentsendBuf s str len =fromIntegral<$>do#if defined(mingw32_HOST_OS)
-- writeRawBufferPtr is supposed to handle checking for errors, but it's broken-- on x86_64 because of GHC bug #12010 so we duplicate the check here. The call-- to throwSocketErrorIfMinus1Retry can be removed when no GHC version with the-- bug is supported.fd<-socket2FDsletclen=fromIntegrallenthrowSocketErrorIfMinus1Retry"Network.Socket.sendBuf"$writeRawBufferPtr"Network.Socket.sendBuf"fd(castPtrstr)0clen#else
fd <-fdSocket s letflags =0clen =fromIntegrallen throwSocketErrorWaitWrite s "Network.Socket.sendBuf"$c_send fd str clen flags #endif
-- | Receive data from the socket, writing it into buffer instead of-- creating a new string. The socket need not be in a connected-- state. Returns @(nbytes, address)@ where @nbytes@ is the number of-- bytes received and @address@ is a 'SockAddr' representing the-- address of the sending socket.---- If the first return value is zero, it means EOF.---- For 'Stream' sockets, the second return value would be invalid.---- NOTE: blocking on Windows unless you compile with -threaded (see-- GHC ticket #1129)recvBufFrom::SocketAddress sa =>Socket ->Ptra ->Int->IO(Int,sa )recvBufFrom s ptr nbytes |nbytes <=0=ioError(mkInvalidRecvArgError "Network.Socket.recvBufFrom")|otherwise=withNewSocketAddress $\ptr_sa sz ->alloca$\ptr_len ->dofd <-fdSocket s pokeptr_len (fromIntegralsz )letcnbytes =fromIntegralnbytes flags =0len <-throwSocketErrorWaitRead s "Network.Socket.recvBufFrom"$c_recvfrom fd ptr cnbytes flags ptr_sa ptr_len sockaddr <-peekSocketAddress ptr_sa `E.catch`\(E.SomeException_)->getPeerName s return(fromIntegrallen ,sockaddr )-- | 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.---- The return value is the length of received data. Zero means-- EOF. Historical note: Version 2.8.x.y or earlier,-- an EOF error was thrown. This was changed in version 3.0.---- Receiving data from closed socket may lead to undefined behaviour.recvBuf::Socket ->PtrWord8->Int->IOIntrecvBuf s ptr nbytes |nbytes <=0=ioError(mkInvalidRecvArgError "Network.Socket.recvBuf")|otherwise=do#if defined(mingw32_HOST_OS)
-- see comment in sendBuf above.fd<-socket2FDsletcnbytes=fromIntegralnbyteslen<-throwSocketErrorIfMinus1Retry"Network.Socket.recvBuf"$readRawBufferPtr"Network.Socket.recvBuf"fdptr0cnbytes#else
fd <-fdSocket s len <-throwSocketErrorWaitRead s "Network.Socket.recvBuf"$c_recv fd (castPtrptr )(fromIntegralnbytes )0{-flags-}#endif
return$fromIntegrallen mkInvalidRecvArgError::String->IOErrormkInvalidRecvArgError loc =ioeSetErrorString(mkIOErrorInvalidArgumentloc NothingNothing)"non-positive length"#if !defined(mingw32_HOST_OS)
foreignimportccallunsafe"send"c_send::CInt->Ptra ->CSize->CInt->IOCIntforeignimportccallunsafe"recv"c_recv::CInt->PtrCChar->CSize->CInt->IOCInt#endif
foreignimportCALLCONVSAFE_ON_WIN"sendto"c_sendto::CInt->Ptra ->CSize->CInt->Ptrsa ->CInt->IOCIntforeignimportCALLCONVSAFE_ON_WIN"recvfrom"c_recvfrom::CInt->Ptra ->CSize->CInt->Ptrsa ->PtrCInt->IOCInt

AltStyle によって変換されたページ (->オリジナル) /