module Network.MessagePack.RPC.Client.Internal where
import Control.Concurrent (ThreadId)
import Control.Concurrent.MVar (MVar)
import qualified Data.ByteString as B
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HM
import Data.IORef (IORef)
import qualified Data.IORef as IORef
import qualified Data.MessagePack as MsgPack
import Data.MessagePack.RPC
data Client = Client {
clientSessionState :: !SessionState
, clientBackend :: !Backend
, clientLog :: Logger
, clientFormat :: Formatter
, clientHandlerTid :: IORef (Maybe ThreadId)
}
data SessionState = SessionState {
lastMessageId :: IORef MessageId
, dispatchTable :: IORef (HashMap MessageId (MVar Result))
}
data Backend = Backend {
backendSend :: B.ByteString -> IO ()
, backendRecv :: IO B.ByteString
, backendClose :: IO ()
}
type Logger = String -> IO ()
type Formatter = Message -> String
type Result = Either MsgPack.Object MsgPack.Object
initSessionState :: IO SessionState
initSessionState =
SessionState <$> IORef.newIORef 0 <*> IORef.newIORef HM.empty