| 30 |
ashish |
1 |
--
|
|
|
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 |
|
|
|
20 |
module Thrift.Transport
|
|
|
21 |
( Transport(..)
|
|
|
22 |
, TransportExn(..)
|
|
|
23 |
, TransportExnType(..)
|
|
|
24 |
) where
|
|
|
25 |
|
|
|
26 |
import Control.Monad ( when )
|
|
|
27 |
import Control.Exception ( Exception, throw )
|
|
|
28 |
|
|
|
29 |
import Data.Typeable ( Typeable )
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
class 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 |
|
|
|
49 |
data TransportExn = TransportExn String TransportExnType
|
|
|
50 |
deriving ( Show, Typeable )
|
|
|
51 |
instance Exception TransportExn
|
|
|
52 |
|
|
|
53 |
data 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 |
|