Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 1 | {-# LANGUAGE DeriveDataTypeable #-} |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 2 | {-# LANGUAGE MultiParamTypeClasses #-} |
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.Transport |
| 23 | ( Transport(..) |
| 24 | , TransportExn(..) |
| 25 | , TransportExnType(..) |
| 26 | ) where |
| 27 | |
| 28 | import Control.Monad ( when ) |
| 29 | import Control.Exception ( Exception, throw ) |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 30 | import Data.Functor ( (<$>) ) |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 31 | import Data.Typeable ( Typeable ) |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 32 | import Data.Word |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 33 | |
Christian Lavoie | ae7f7fa | 2010-11-02 21:42:53 +0000 | [diff] [blame] | 34 | import qualified Data.ByteString.Lazy as LBS |
David Reiss | 752529e | 2010-01-11 19:12:56 +0000 | [diff] [blame] | 35 | import Data.Monoid |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 36 | |
| 37 | class Transport a where |
| 38 | tIsOpen :: a -> IO Bool |
| 39 | tClose :: a -> IO () |
David Reiss | 752529e | 2010-01-11 19:12:56 +0000 | [diff] [blame] | 40 | tRead :: a -> Int -> IO LBS.ByteString |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 41 | tPeek :: a -> IO (Maybe Word8) |
David Reiss | 752529e | 2010-01-11 19:12:56 +0000 | [diff] [blame] | 42 | tWrite :: a -> LBS.ByteString -> IO () |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 43 | tFlush :: a -> IO () |
David Reiss | 752529e | 2010-01-11 19:12:56 +0000 | [diff] [blame] | 44 | tReadAll :: a -> Int -> IO LBS.ByteString |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 45 | |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 46 | tReadAll _ 0 = return mempty |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 47 | tReadAll a len = do |
| 48 | result <- tRead a len |
David Reiss | 752529e | 2010-01-11 19:12:56 +0000 | [diff] [blame] | 49 | let rlen = fromIntegral $ LBS.length result |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 50 | when (rlen == 0) (throw $ TransportExn "Cannot read. Remote side has closed." TE_UNKNOWN) |
| 51 | if len <= rlen |
Noam Zilberstein | af5d64a | 2014-07-31 15:44:13 -0700 | [diff] [blame] | 52 | then return result |
| 53 | else (result `mappend`) <$> tReadAll a (len - rlen) |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 54 | |
| 55 | data TransportExn = TransportExn String TransportExnType |
| 56 | deriving ( Show, Typeable ) |
| 57 | instance Exception TransportExn |
| 58 | |
| 59 | data TransportExnType |
| 60 | = TE_UNKNOWN |
| 61 | | TE_NOT_OPEN |
| 62 | | TE_ALREADY_OPEN |
| 63 | | TE_TIMED_OUT |
| 64 | | TE_END_OF_FILE |
| 65 | deriving ( Eq, Show, Typeable ) |