-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
344 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,73 @@ | ||
{-# LANGUAGE DuplicateRecordFields #-} | ||
|
||
module Kafka.Protocol.Message | ||
( RequestHeader (..) | ||
, ResponseHeader (..) | ||
, putResponseHeader | ||
, runPutResponseHeaderLazy | ||
, Unsupported (..) | ||
|
||
, module Kafka.Protocol.Message.Struct | ||
) where | ||
|
||
import qualified Data.ByteString.Lazy as BL | ||
import Data.Int | ||
import Data.Text (Text) | ||
import GHC.Generics | ||
|
||
import Kafka.Protocol.Encoding | ||
import Kafka.Protocol.Message.Struct | ||
|
||
-- TODO: Support Optional Tagged Fields | ||
data Unsupported = Unsupported | ||
deriving (Show, Eq, Generic) | ||
|
||
instance Serializable Unsupported | ||
|
||
data RequestHeader = RequestHeader | ||
{ requestApiKey :: !ApiKey | ||
, requestApiVersion :: !Int16 | ||
, requestCorrelationId :: !Int32 | ||
, requestClientId :: !(Maybe Text) | ||
} deriving (Show, Eq, Generic) | ||
{ requestApiKey :: {-# UNPACK #-} !ApiKey | ||
, requestApiVersion :: {-# UNPACK #-} !Int16 | ||
, requestCorrelationId :: {-# UNPACK #-} !Int32 | ||
, requestClientId :: !(Either Unsupported NullableString) | ||
, requesteTaggedFields :: !(Either Unsupported TaggedFields) | ||
} deriving (Show, Eq) | ||
|
||
instance Serializable RequestHeader where | ||
get = do | ||
requestApiKey <- get | ||
requestApiVersion <- get | ||
requestCorrelationId <- get | ||
let (reqHeaderVer, _) = getHeaderVersion requestApiKey requestApiVersion | ||
case reqHeaderVer of | ||
2 -> do requestClientId <- getEither True | ||
requesteTaggedFields <- getEither True | ||
pure RequestHeader{..} | ||
1 -> do requestClientId <- getEither True | ||
let requesteTaggedFields = Left Unsupported | ||
in pure RequestHeader{..} | ||
0 -> let requestClientId = Left Unsupported | ||
requesteTaggedFields = Left Unsupported | ||
in pure RequestHeader{..} | ||
v -> error $ "Unknown request header version" <> show v | ||
{-# INLINE get #-} | ||
|
||
put RequestHeader{..} = | ||
put requestApiKey | ||
<> put requestApiVersion | ||
<> put requestCorrelationId | ||
<> putEither requestClientId | ||
<> putEither requesteTaggedFields | ||
{-# INLINE put #-} | ||
|
||
instance Serializable RequestHeader | ||
data ResponseHeader = ResponseHeader | ||
{ responseCorrelationId :: {-# UNPACK #-} !Int32 | ||
, responseTaggedFields :: !(Either Unsupported TaggedFields) | ||
} deriving (Show, Eq) | ||
|
||
newtype ResponseHeader = ResponseHeader | ||
{ responseCorrelationId :: Int32 | ||
} deriving (Show, Eq, Generic) | ||
putResponseHeader :: ResponseHeader -> Builder | ||
putResponseHeader ResponseHeader{..} = | ||
put responseCorrelationId | ||
<> putEither responseTaggedFields | ||
{-# INLINE putResponseHeader #-} | ||
|
||
instance Serializable ResponseHeader | ||
runPutResponseHeaderLazy :: ResponseHeader -> BL.ByteString | ||
runPutResponseHeaderLazy = toLazyByteString . putResponseHeader | ||
{-# INLINE runPutResponseHeaderLazy #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.