2016-06-08 19:45:47 +02:00
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
|
|
|
|
module Network.GRPC.LowLevel.CompletionQueue.Unregistered where
|
|
|
|
|
2016-06-22 19:41:14 +02:00
|
|
|
import Control.Exception (bracket)
|
2016-06-08 19:45:47 +02:00
|
|
|
import Foreign.Marshal.Alloc (free, malloc)
|
|
|
|
import Foreign.Storable (peek)
|
|
|
|
import Network.GRPC.LowLevel.Call
|
|
|
|
import qualified Network.GRPC.LowLevel.Call.Unregistered as U
|
|
|
|
import Network.GRPC.LowLevel.CompletionQueue.Internal
|
|
|
|
import Network.GRPC.LowLevel.GRPC
|
|
|
|
import qualified Network.GRPC.Unsafe as C
|
|
|
|
import qualified Network.GRPC.Unsafe.Constants as C
|
|
|
|
import qualified Network.GRPC.Unsafe.Metadata as C
|
|
|
|
import qualified Network.GRPC.Unsafe.Time as C
|
|
|
|
|
|
|
|
channelCreateCall :: C.Channel
|
|
|
|
-> C.Call
|
|
|
|
-> C.PropagationMask
|
|
|
|
-> CompletionQueue
|
|
|
|
-> MethodName
|
|
|
|
-> Endpoint
|
|
|
|
-> C.CTimeSpecPtr
|
|
|
|
-> IO (Either GRPCIOError ClientCall)
|
|
|
|
channelCreateCall chan parent mask cq@CompletionQueue{..} meth endpt deadline =
|
|
|
|
withPermission Push cq $ do
|
|
|
|
call <- C.grpcChannelCreateCall chan parent mask unsafeCQ
|
|
|
|
(unMethodName meth) (unEndpoint endpt) deadline C.reserved
|
|
|
|
return $ Right $ ClientCall call
|
|
|
|
|
|
|
|
|
2016-06-08 21:38:01 +02:00
|
|
|
serverRequestCall :: C.Server
|
|
|
|
-> CompletionQueue
|
|
|
|
-> IO (Either GRPCIOError U.ServerCall)
|
2016-06-22 19:41:14 +02:00
|
|
|
serverRequestCall server cq@CompletionQueue{..} =
|
|
|
|
withPermission Push cq $
|
|
|
|
bracket malloc free $ \callPtr ->
|
|
|
|
C.withMetadataArrayPtr $ \metadataArrayPtr ->
|
2016-07-11 19:53:19 +02:00
|
|
|
C.withCallDetails $ \callDetails ->
|
|
|
|
withPermission Pluck cq $ do
|
|
|
|
grpcDebug $ "serverRequestCall: callPtr is " ++ show callPtr
|
|
|
|
metadataArray <- peek metadataArrayPtr
|
|
|
|
tag <- newTag cq
|
|
|
|
callError <- C.grpcServerRequestCall server callPtr callDetails
|
|
|
|
metadataArray unsafeCQ unsafeCQ tag
|
|
|
|
grpcDebug $ "serverRequestCall: callError was " ++ show callError
|
|
|
|
if callError /= C.CallOk
|
|
|
|
then do grpcDebug "serverRequestCall: got call error; cleaning up."
|
|
|
|
return $ Left $ GRPCIOCallError callError
|
|
|
|
else do pluckResult <- pluck cq tag Nothing
|
|
|
|
grpcDebug $ "serverRequestCall: pluckResult was "
|
|
|
|
++ show pluckResult
|
|
|
|
case pluckResult of
|
|
|
|
Left x -> do
|
|
|
|
grpcDebug "serverRequestCall: pluck error."
|
|
|
|
return $ Left x
|
|
|
|
Right () -> do
|
|
|
|
rawCall <- peek callPtr
|
|
|
|
metadata <- C.getAllMetadataArray metadataArray
|
|
|
|
deadline <- getDeadline callDetails
|
|
|
|
method <- getMethod callDetails
|
|
|
|
host <- getHost callDetails
|
|
|
|
let call = U.ServerCall rawCall
|
|
|
|
metadata
|
|
|
|
Nothing
|
|
|
|
deadline
|
|
|
|
method
|
|
|
|
host
|
|
|
|
return $ Right call
|
2016-06-08 19:45:47 +02:00
|
|
|
|
2016-06-22 19:41:14 +02:00
|
|
|
where getDeadline callDetails = do
|
|
|
|
C.timeSpec <$> C.callDetailsGetDeadline callDetails
|
|
|
|
getMethod callDetails =
|
|
|
|
MethodName <$> C.callDetailsGetMethod callDetails
|
|
|
|
getHost callDetails =
|
|
|
|
Host <$> C.callDetailsGetHost callDetails
|