Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module provides efficient receiving functions from the network.
recv
uses createAndTrim
which behaves as follows:
- Allocates a buffer whose size is decided from the first argument.
- Receives data with the buffer.
- Allocates another buffer whose size fits the received data.
- Copies the data from the first buffer to the second buffer.
On 64bit machines, the global lock is taken for the allocation of
a byte string whose length is larger than or equal to 3272 bytes.
So, for instance, if 4,096 is specified to recv
and the size of
received data is 3,300, the global lock is taken twice with the copy
overhead.
The efficient receiving functions provided here use a buffer pool. A large buffer is allocated at the beginning and it is divided into a used one and a leftover when receiving. The latter is kept in the buffer pooll and will be used next time. When the buffer gets small and usefless, a new large buffer is allocated.
Synopsis
- type Recv = IO ByteString
- receive :: Socket -> BufferPool -> Recv
- data BufferPool
- newBufferPool :: Int -> Int -> IO BufferPool
- withBufferPool :: BufferPool -> (Buffer -> BufSize -> IO Int) -> IO ByteString
- type RecvBuf = Buffer -> BufSize -> IO Bool
- receiveBuf :: Socket -> RecvBuf
- type RecvN = Int -> IO ByteString
- makeReceiveN :: ByteString -> Recv -> RecvBuf -> IO RecvN
- makePlainReceiveN :: Socket -> Int -> Int -> ByteString -> IO RecvN
- type Buffer = Ptr Word8
- type BufSize = Int
- mallocBS :: Int -> IO ByteString
- copy :: Buffer -> ByteString -> IO Buffer
Recv
type Recv = IO ByteString Source #
Type for the receiving function with a buffer pool.
receive :: Socket -> BufferPool -> Recv Source #
The receiving function with a buffer pool. The buffer pool is automatically managed.
data BufferPool Source #
Type for read buffer pool.
newBufferPool :: Int -> Int -> IO BufferPool Source #
Creating a buffer pool. The first argument is the lower limit. When the size of the buffer in the poll is lower than this limit, the buffer is thrown awany (and is eventually freed). Then a new buffer is allocated. The second argument is the size for the new allocation.
withBufferPool :: BufferPool -> (Buffer -> BufSize -> IO Int) -> IO ByteString Source #
Using a buffer pool. The second argument is a function which returns how many bytes are filled in the buffer. The buffer in the buffer pool is automatically managed.
RecvBuf
type RecvBuf = Buffer -> BufSize -> IO Bool Source #
Type for the receiving function with a buffer. The result boolean indicates whether or not the buffer is fully filled.
receiveBuf :: Socket -> RecvBuf Source #
The receiving function with a buffer. This tries to fill the buffer. This returns when the buffer is filled or reaches EOF.
RecvN
makeReceiveN :: ByteString -> Recv -> RecvBuf -> IO RecvN Source #
This function returns a receiving function based on two receiving functions. The returned function receives exactly N bytes. The first argument is an initial received data. After consuming the initial data, the two functions is used. When N is less than equal to 4096, the buffer pool is used. Otherwise, a new buffer is allocated. In this case, the global lock is taken.
makePlainReceiveN :: Socket -> Int -> Int -> ByteString -> IO RecvN Source #
This function returns a receiving function with two receiving
functions is created internally.
The second argument is the lower limit of the buffer pool.
The third argument is the size of the allocated buffer in the pool.
The fourth argument is an initial received data.
The returned function behaves as described in makeReceiveN
.