Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 1 | {-# LANGUAGE ExistentialQuantification #-} |
| 2 | {-# LANGUAGE MagicHash #-} |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 3 | -- |
| 4 | -- Licensed to the Apache Software Foundation (ASF) under one |
| 5 | -- or more contributor license agreements. See the NOTICE file |
| 6 | -- distributed with this work for additional information |
| 7 | -- regarding copyright ownership. The ASF licenses this file |
| 8 | -- to you under the Apache License, Version 2.0 (the |
| 9 | -- "License"); you may not use this file except in compliance |
| 10 | -- with the License. You may obtain a copy of the License at |
| 11 | -- |
| 12 | -- http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | -- |
| 14 | -- Unless required by applicable law or agreed to in writing, |
| 15 | -- software distributed under the License is distributed on an |
| 16 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | -- KIND, either express or implied. See the License for the |
| 18 | -- specific language governing permissions and limitations |
| 19 | -- under the License. |
| 20 | -- |
| 21 | |
| 22 | module Thrift.Protocol.Binary |
| 23 | ( module Thrift.Protocol |
| 24 | , BinaryProtocol(..) |
| 25 | ) where |
| 26 | |
| 27 | import Control.Exception ( throw ) |
David Reiss | 752529e | 2010-01-11 19:12:56 +0000 | [diff] [blame] | 28 | import Control.Monad ( liftM ) |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 29 | |
Christian Lavoie | ae7f7fa | 2010-11-02 21:42:53 +0000 | [diff] [blame] | 30 | import qualified Data.Binary |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 31 | import Data.Bits |
| 32 | import Data.Int |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 33 | |
| 34 | import GHC.Exts |
| 35 | import GHC.Word |
| 36 | |
| 37 | import Thrift.Protocol |
| 38 | import Thrift.Transport |
| 39 | |
Bryan Duxbury | 75a33e8 | 2010-09-22 00:48:56 +0000 | [diff] [blame] | 40 | import qualified Data.ByteString.Lazy as LBS |
Christian Lavoie | ae7f7fa | 2010-11-02 21:42:53 +0000 | [diff] [blame] | 41 | import qualified Data.ByteString.Lazy.Char8 as LBSChar8 |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 42 | |
Bryan Duxbury | 75a33e8 | 2010-09-22 00:48:56 +0000 | [diff] [blame] | 43 | version_mask :: Int32 |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 44 | version_mask = 0xffff0000 |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 45 | |
Bryan Duxbury | 75a33e8 | 2010-09-22 00:48:56 +0000 | [diff] [blame] | 46 | version_1 :: Int32 |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 47 | version_1 = 0x80010000 |
| 48 | |
| 49 | data BinaryProtocol a = Transport a => BinaryProtocol a |
| 50 | |
| 51 | |
| 52 | instance Protocol BinaryProtocol where |
| 53 | getTransport (BinaryProtocol t) = t |
| 54 | |
| 55 | writeMessageBegin p (n, t, s) = do |
Bryan Duxbury | 75a33e8 | 2010-09-22 00:48:56 +0000 | [diff] [blame] | 56 | writeI32 p (version_1 .|. (fromIntegral $ fromEnum t)) |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 57 | writeString p n |
| 58 | writeI32 p s |
| 59 | writeMessageEnd _ = return () |
| 60 | |
| 61 | writeStructBegin _ _ = return () |
| 62 | writeStructEnd _ = return () |
| 63 | writeFieldBegin p (_, t, i) = writeType p t >> writeI16 p i |
| 64 | writeFieldEnd _ = return () |
| 65 | writeFieldStop p = writeType p T_STOP |
| 66 | writeMapBegin p (k, v, n) = writeType p k >> writeType p v >> writeI32 p n |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 67 | writeMapEnd _ = return () |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 68 | writeListBegin p (t, n) = writeType p t >> writeI32 p n |
| 69 | writeListEnd _ = return () |
| 70 | writeSetBegin p (t, n) = writeType p t >> writeI32 p n |
| 71 | writeSetEnd _ = return () |
| 72 | |
Christian Lavoie | ae7f7fa | 2010-11-02 21:42:53 +0000 | [diff] [blame] | 73 | writeBool p b = tWrite (getTransport p) $ LBS.singleton $ toEnum $ if b then 1 else 0 |
| 74 | writeByte p b = tWrite (getTransport p) $ Data.Binary.encode b |
| 75 | writeI16 p b = tWrite (getTransport p) $ Data.Binary.encode b |
| 76 | writeI32 p b = tWrite (getTransport p) $ Data.Binary.encode b |
| 77 | writeI64 p b = tWrite (getTransport p) $ Data.Binary.encode b |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 78 | writeDouble p d = writeI64 p (fromIntegral $ floatBits d) |
Bryan Duxbury | 75a33e8 | 2010-09-22 00:48:56 +0000 | [diff] [blame] | 79 | writeString p s = writeI32 p (fromIntegral $ length s) >> tWrite (getTransport p) (LBSChar8.pack s) |
| 80 | writeBinary p s = writeI32 p (fromIntegral $ LBS.length s) >> tWrite (getTransport p) s |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 81 | |
| 82 | readMessageBegin p = do |
| 83 | ver <- readI32 p |
| 84 | if (ver .&. version_mask /= version_1) |
| 85 | then throw $ ProtocolExn PE_BAD_VERSION "Missing version identifier" |
| 86 | else do |
| 87 | s <- readString p |
| 88 | sz <- readI32 p |
Bryan Duxbury | 75a33e8 | 2010-09-22 00:48:56 +0000 | [diff] [blame] | 89 | return (s, toEnum $ fromIntegral $ ver .&. 0xFF, sz) |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 90 | readMessageEnd _ = return () |
| 91 | readStructBegin _ = return "" |
| 92 | readStructEnd _ = return () |
| 93 | readFieldBegin p = do |
| 94 | t <- readType p |
| 95 | n <- if t /= T_STOP then readI16 p else return 0 |
| 96 | return ("", t, n) |
| 97 | readFieldEnd _ = return () |
| 98 | readMapBegin p = do |
| 99 | kt <- readType p |
| 100 | vt <- readType p |
| 101 | n <- readI32 p |
| 102 | return (kt, vt, n) |
| 103 | readMapEnd _ = return () |
| 104 | readListBegin p = do |
| 105 | t <- readType p |
| 106 | n <- readI32 p |
| 107 | return (t, n) |
| 108 | readListEnd _ = return () |
| 109 | readSetBegin p = do |
| 110 | t <- readType p |
| 111 | n <- readI32 p |
| 112 | return (t, n) |
| 113 | readSetEnd _ = return () |
| 114 | |
| 115 | readBool p = (== 1) `fmap` readByte p |
Christian Lavoie | ae7f7fa | 2010-11-02 21:42:53 +0000 | [diff] [blame] | 116 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 117 | readByte p = do |
| 118 | bs <- tReadAll (getTransport p) 1 |
Christian Lavoie | ae7f7fa | 2010-11-02 21:42:53 +0000 | [diff] [blame] | 119 | return $ Data.Binary.decode bs |
| 120 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 121 | readI16 p = do |
| 122 | bs <- tReadAll (getTransport p) 2 |
Christian Lavoie | ae7f7fa | 2010-11-02 21:42:53 +0000 | [diff] [blame] | 123 | return $ Data.Binary.decode bs |
| 124 | |
| 125 | readI32 p = do |
| 126 | bs <- tReadAll (getTransport p) 4 |
| 127 | return $ Data.Binary.decode bs |
| 128 | |
| 129 | readI64 p = do |
| 130 | bs <- tReadAll (getTransport p) 8 |
| 131 | return $ Data.Binary.decode bs |
| 132 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 133 | readDouble p = do |
| 134 | bs <- readI64 p |
| 135 | return $ floatOfBits $ fromIntegral bs |
Christian Lavoie | ae7f7fa | 2010-11-02 21:42:53 +0000 | [diff] [blame] | 136 | |
David Reiss | 752529e | 2010-01-11 19:12:56 +0000 | [diff] [blame] | 137 | readString p = do |
| 138 | i <- readI32 p |
Bryan Duxbury | 75a33e8 | 2010-09-22 00:48:56 +0000 | [diff] [blame] | 139 | LBSChar8.unpack `liftM` tReadAll (getTransport p) (fromIntegral i) |
Christian Lavoie | ae7f7fa | 2010-11-02 21:42:53 +0000 | [diff] [blame] | 140 | |
Bryan Duxbury | 75a33e8 | 2010-09-22 00:48:56 +0000 | [diff] [blame] | 141 | readBinary p = do |
| 142 | i <- readI32 p |
| 143 | tReadAll (getTransport p) (fromIntegral i) |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 144 | |
| 145 | |
| 146 | -- | Write a type as a byte |
| 147 | writeType :: (Protocol p, Transport t) => p t -> ThriftType -> IO () |
Bryan Duxbury | 75a33e8 | 2010-09-22 00:48:56 +0000 | [diff] [blame] | 148 | writeType p t = writeByte p (fromIntegral $ fromEnum t) |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 149 | |
| 150 | -- | Read a byte as though it were a ThriftType |
| 151 | readType :: (Protocol p, Transport t) => p t -> IO ThriftType |
Bryan Duxbury | 75a33e8 | 2010-09-22 00:48:56 +0000 | [diff] [blame] | 152 | readType p = do |
| 153 | b <- readByte p |
| 154 | return $ toEnum $ fromIntegral b |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 155 | |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 156 | floatBits :: Double -> Word64 |
| 157 | floatBits (D# d#) = W64# (unsafeCoerce# d#) |
| 158 | |
| 159 | floatOfBits :: Word64 -> Double |
| 160 | floatOfBits (W64# b#) = D# (unsafeCoerce# b#) |
| 161 | |