@@ -1,4 +1,4 @@
1−{-# LANGUAGE DerivingVia, DeriveGeneric, RankNTypes, ScopedTypeVariables, MultiParamTypeClasses, OverloadedStrings, GeneralizedNewtypeDeriving, CPP, ExistentialQuantification, StandaloneDeriving, GADTs, RecordWildCards #-}
1+{-# LANGUAGE DerivingVia, DeriveGeneric, RankNTypes, ScopedTypeVariables, MultiParamTypeClasses, OverloadedStrings, GeneralizedNewtypeDeriving, CPP, ExistentialQuantification, StandaloneDeriving, GADTs #-}
2{-# OPTIONS_GHC -fno-warn-orphans #-}
2{-# OPTIONS_GHC -fno-warn-orphans #-}
3{- HLINT ignore "Use lambda-case" -}
3{- HLINT ignore "Use lambda-case" -}
4module Network.RPC.Curryer.Server where
4module Network.RPC.Curryer.Server where
@@ -35,13 +35,13 @@ import Data.Hashable
35import System.Timeout
35import System.Timeout
36import qualified Network.ByteOrder as BO
36import qualified Network.ByteOrder as BO
39-- for toArrayS conversion
38-- for toArrayS conversion
40−import qualified Data.ByteString.Internal as BSI
39+{-import qualified Data.ByteString.Internal as BSI
41−import qualified Streamly.Internal.Data.Array.Storable.Foreign.Types as Arr
40+import qualified Streamly.Internal.Data.Array.Storable.Foreign.Types as SA
41+import Foreign.ForeignPtr (plusForeignPtr)
42import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
42import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
43−import GHC.Ptr (minusPtr)
43+import GHC.Ptr (plusPtr)
45-- define CURRYER_SHOW_BYTES 1
45-- define CURRYER_SHOW_BYTES 1
46#if CURRYER_SHOW_BYTES == 1
46#if CURRYER_SHOW_BYTES == 1
47import Debug.Trace
47import Debug.Trace
@@ -111,14 +111,13 @@ data ConnectionState a = ConnectionState {
111 connectionSocket :: Locking Socket
111 connectionSocket :: Locking Socket
114−-- | Used by server-side request handlers to send additional messages to the client. This is useful for sending asynchronous responses to the client outside of the normal request-response flow. The locking socket can be found in the ConnectionState when a request handler is called.
114+-- | Used by server-side request handlers to send additional messages to the client. This is useful for sending asynchronous responses to the client outside of the normal request-response flow.
115−sendMessage :: Serialise a => Locking Socket -> a -> IO ()
115+sendMessage :: Serialise a => ConnectionState s -> a -> IO ()
116−sendMessage lockSock msg = do
116+sendMessage sState msg = do
117 requestID <- UUID <$> UUIDBase.nextRandom
117 requestID <- UUID <$> UUIDBase.nextRandom
118 let env =
118 let env =
119− Envelope (fingerprint msg) (RequestMessage timeout') requestID (serialise msg)
119+ Envelope (fingerprint msg) (RequestMessage 0) requestID (serialise msg)
121− sendEnvelope env lockSock
120+ sendEnvelope env (connectionSocket sState)
123--avoid orphan instance
122--avoid orphan instance
124newtype UUID = UUID { _unUUID :: UUIDBase.UUID }
123newtype UUID = UUID { _unUUID :: UUIDBase.UUID }
@@ -159,7 +158,8 @@ localHostAddr = (127,0,0,1)
159-- Each message is length-prefixed by a 32-bit unsigned length.
158-- Each message is length-prefixed by a 32-bit unsigned length.
160envelopeP :: Parser IO Word8 Envelope
159envelopeP :: Parser IO Word8 Envelope
161envelopeP = do
160envelopeP = do
162− let msgTypeP = (P.satisfy (== 0) *>
162+ msgTypeP = (P.satisfy (== 0) *>
163 (RequestMessage . fromIntegral <$> word32P)) `P.alt`
163 (RequestMessage . fromIntegral <$> word32P)) `P.alt`
164 (P.satisfy (== 1) $> ResponseMessage) `P.alt`
164 (P.satisfy (== 1) $> ResponseMessage) `P.alt`
165 (P.satisfy (== 2) $> TimeoutResponseMessage) `P.alt`
165 (P.satisfy (== 2) $> TimeoutResponseMessage) `P.alt`
@@ -170,7 +170,7 @@ envelopeP = do
170 if c == 0 then
170 if c == 0 then
171 pure BS.empty
171 pure BS.empty
173− fromArray <$> P.takeEQ c (Arr.writeN c)
173+ BS.pack <$> P.takeEQ c s
174 Envelope <$> fingerprintP <*> msgTypeP <*> uuidP <*> lenPrefixedByteStringP
174 Envelope <$> fingerprintP <*> msgTypeP <*> uuidP <*> lenPrefixedByteStringP
176encodeEnvelope :: Envelope -> BS.ByteString
176encodeEnvelope :: Envelope -> BS.ByteString
@@ -333,10 +333,3 @@ sendEnvelope envelope sockLock = do
333fingerprint :: Typeable a => a -> Fingerprint
333fingerprint :: Typeable a => a -> Fingerprint
334fingerprint = typeRepFingerprint . typeOf
334fingerprint = typeRepFingerprint . typeOf
336−fromArray :: Arr.Array Word8 -> BSI.ByteString
337−fromArray Arr.Array {..}
338− | aLen == 0 = mempty
339− | otherwise = BSI.PS aStart 0 aLen
341− aStartPtr = unsafeForeignPtrToPtr aStart
342− aLen = aEnd `minusPtr` aStartPtr