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