Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 1 | {-# LANGUAGE DeriveDataTypeable #-} |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 2 | -- |
| 3 | -- Licensed to the Apache Software Foundation (ASF) under one |
| 4 | -- or more contributor license agreements. See the NOTICE file |
| 5 | -- distributed with this work for additional information |
| 6 | -- regarding copyright ownership. The ASF licenses this file |
| 7 | -- to you under the Apache License, Version 2.0 (the |
| 8 | -- "License"); you may not use this file except in compliance |
| 9 | -- with the License. You may obtain a copy of the License at |
| 10 | -- |
| 11 | -- http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | -- |
| 13 | -- Unless required by applicable law or agreed to in writing, |
| 14 | -- software distributed under the License is distributed on an |
| 15 | -- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | -- KIND, either express or implied. See the License for the |
| 17 | -- specific language governing permissions and limitations |
| 18 | -- under the License. |
| 19 | -- |
| 20 | |
| 21 | module Thrift.Transport |
| 22 | ( Transport(..) |
| 23 | , TransportExn(..) |
| 24 | , TransportExnType(..) |
| 25 | ) where |
| 26 | |
| 27 | import Control.Monad ( when ) |
| 28 | import Control.Exception ( Exception, throw ) |
| 29 | |
| 30 | import Data.Typeable ( Typeable ) |
| 31 | |
Christian Lavoie | ae7f7fa | 2010-11-02 21:42:53 +0000 | [diff] [blame^] | 32 | import qualified Data.ByteString.Lazy as LBS |
David Reiss | 752529e | 2010-01-11 19:12:56 +0000 | [diff] [blame] | 33 | import Data.Monoid |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 34 | |
| 35 | class Transport a where |
| 36 | tIsOpen :: a -> IO Bool |
| 37 | tClose :: a -> IO () |
David Reiss | 752529e | 2010-01-11 19:12:56 +0000 | [diff] [blame] | 38 | tRead :: a -> Int -> IO LBS.ByteString |
| 39 | tWrite :: a -> LBS.ByteString -> IO () |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 40 | tFlush :: a -> IO () |
David Reiss | 752529e | 2010-01-11 19:12:56 +0000 | [diff] [blame] | 41 | tReadAll :: a -> Int -> IO LBS.ByteString |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 42 | |
Bryan Duxbury | e59a80f | 2010-09-20 15:21:37 +0000 | [diff] [blame] | 43 | tReadAll _ 0 = return mempty |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 44 | tReadAll a len = do |
| 45 | result <- tRead a len |
David Reiss | 752529e | 2010-01-11 19:12:56 +0000 | [diff] [blame] | 46 | let rlen = fromIntegral $ LBS.length result |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 47 | when (rlen == 0) (throw $ TransportExn "Cannot read. Remote side has closed." TE_UNKNOWN) |
| 48 | if len <= rlen |
| 49 | then return result |
David Reiss | 752529e | 2010-01-11 19:12:56 +0000 | [diff] [blame] | 50 | else (result `mappend`) `fmap` (tReadAll a (len - rlen)) |
Bryan Duxbury | 0781f2b | 2009-04-07 23:29:42 +0000 | [diff] [blame] | 51 | |
| 52 | data TransportExn = TransportExn String TransportExnType |
| 53 | deriving ( Show, Typeable ) |
| 54 | instance Exception TransportExn |
| 55 | |
| 56 | data TransportExnType |
| 57 | = TE_UNKNOWN |
| 58 | | TE_NOT_OPEN |
| 59 | | TE_ALREADY_OPEN |
| 60 | | TE_TIMED_OUT |
| 61 | | TE_END_OF_FILE |
| 62 | deriving ( Eq, Show, Typeable ) |
| 63 | |