module System.IO.Streams.Network
(
socketToStreams
, socketToStreamsWithBufferSize
) where
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as S
import Foreign.Storable (sizeOf)
import Network.Socket (Socket)
import qualified Network.Socket.ByteString as N
import System.IO.Streams.Internal (InputStream, OutputStream)
import qualified System.IO.Streams.Internal as Streams
bUFSIZ :: Int
bUFSIZ = 8192 overhead
where
overhead = 4 * (sizeOf $! (0 :: Int))
socketToStreams :: Socket
-> IO (InputStream ByteString, OutputStream ByteString)
socketToStreams = socketToStreamsWithBufferSize bUFSIZ
socketToStreamsWithBufferSize
:: Int
-> Socket
-> IO (InputStream ByteString, OutputStream ByteString)
socketToStreamsWithBufferSize bufsiz socket = do
is <- Streams.makeInputStream input
os <- Streams.makeOutputStream output
return $! (is, os)
where
input = do
s <- N.recv socket bufsiz
return $! if S.null s then Nothing else Just s
output Nothing = return $! ()
output (Just s) = if S.null s then return $! () else N.sendAll socket s