module Data.ConnectionPool.Internal.Streaming
(
acquireTcpClientConnection
, runTcpApp
, runTcpAppImpl
, fromClientSettings
#ifndef WINDOWS
, runUnixApp
, runUnixAppImpl
, fromClientSettingsUnix
#endif
)
where
import Data.Int (Int)
import Data.Maybe (Maybe(Just))
import System.IO (IO)
import Control.Monad.Trans.Control (MonadBaseControl)
import Network.Socket (Socket, SockAddr, sClose)
import Network.Socket.ByteString (sendAll)
import Data.Default.Class (Default(def))
import Data.Streaming.Network
( getSocketFamilyTCP
, safeRecv
#if MIN_VERSION_streaming_commons(0,1,13)
, getReadBufferSize
#endif
)
import Data.Streaming.Network.Internal
( AppData(AppData)
, ClientSettings(clientPort, clientHost, clientAddrFamily)
#ifndef WINDOWS
, AppDataUnix(AppDataUnix)
#if MIN_VERSION_streaming_commons(0,1,13)
, ClientSettingsUnix(clientReadBufferSizeUnix)
#endif
#endif
)
import qualified Data.Streaming.Network.Internal as AppData
( AppData
( appLocalAddr'
, appRead'
, appSockAddr'
, appWrite'
#if MIN_VERSION_streaming_commons(0,1,6)
, appCloseConnection'
#endif
#if MIN_VERSION_streaming_commons(0,1,12)
, appRawSocket'
#endif
))
import Data.ConnectionPool.Internal.HandlerParams
( HandlerParams(_readBufferSize)
)
#ifndef WINDOWS
import qualified Data.Streaming.Network.Internal as AppDataUnix
(AppDataUnix(appReadUnix, appWriteUnix))
#endif
runTcpApp
:: MonadBaseControl IO m
=> Maybe SockAddr
-> (AppData -> m r)
-> HandlerParams
-> Socket
-> SockAddr
-> m r
runTcpApp localAddr app params sock addr =
runTcpAppImpl localAddr sock addr bufSize app
where
bufSize = _readBufferSize params
runTcpAppImpl
:: MonadBaseControl IO m
=> Maybe SockAddr
-> Socket
-> SockAddr
-> Int
-> (AppData -> m r)
-> m r
runTcpAppImpl localAddr sock addr bufSize app = app AppData
{ AppData.appRead' = safeRecv sock bufSize
, AppData.appWrite' = sendAll sock
, AppData.appSockAddr' = addr
, AppData.appLocalAddr' = localAddr
#if MIN_VERSION_streaming_commons(0,1,6)
, AppData.appCloseConnection' = sClose sock
#endif
#if MIN_VERSION_streaming_commons(0,1,12)
, AppData.appRawSocket' = Just sock
#endif
}
acquireTcpClientConnection :: ClientSettings -> IO (Socket, SockAddr)
acquireTcpClientConnection settings = getSocketFamilyTCP host port addrFamily
where
port = clientPort settings
host = clientHost settings
addrFamily = clientAddrFamily settings
fromClientSettings :: ClientSettings -> HandlerParams
fromClientSettings _tcpParams = def
#if MIN_VERSION_streaming_commons(0,1,13)
{ _readBufferSize = getReadBufferSize _tcpParams
}
#endif
#ifndef WINDOWS
runUnixApp
:: MonadBaseControl IO m
=> (AppDataUnix -> m r)
-> HandlerParams
-> Socket
-> ()
-> m r
runUnixApp app params sock () = runUnixAppImpl sock bufSize app
where
bufSize = _readBufferSize params
runUnixAppImpl
:: MonadBaseControl IO m
=> Socket
-> Int
-> (AppDataUnix -> m r)
-> m r
runUnixAppImpl sock bufSize app = app AppDataUnix
{ AppDataUnix.appReadUnix = safeRecv sock bufSize
, AppDataUnix.appWriteUnix = sendAll sock
}
fromClientSettingsUnix :: ClientSettingsUnix -> HandlerParams
fromClientSettingsUnix _unixParams = def
#if MIN_VERSION_streaming_commons(0,1,13)
{ _readBufferSize = clientReadBufferSizeUnix _unixParams
}
#endif
#endif