| 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.Handle
|
|
|
21 |
( module Thrift.Transport
|
|
|
22 |
, HandleSource(..)
|
|
|
23 |
) where
|
|
|
24 |
|
|
|
25 |
import Control.Exception ( throw )
|
|
|
26 |
import Control.Monad ( replicateM )
|
|
|
27 |
|
|
|
28 |
import Network
|
|
|
29 |
|
|
|
30 |
import System.IO
|
|
|
31 |
import System.IO.Error ( isEOFError )
|
|
|
32 |
|
|
|
33 |
import Thrift.Transport
|
|
|
34 |
|
|
|
35 |
|
|
|
36 |
instance Transport Handle where
|
|
|
37 |
tIsOpen = hIsOpen
|
|
|
38 |
tClose h = hClose h
|
|
|
39 |
tRead h n = replicateM n (hGetChar h) `catch` handleEOF
|
|
|
40 |
tWrite h s = mapM_ (hPutChar h) s
|
|
|
41 |
tFlush = hFlush
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
-- | Type class for all types that can open a Handle. This class is used to
|
|
|
45 |
-- replace tOpen in the Transport type class.
|
|
|
46 |
class HandleSource s where
|
|
|
47 |
hOpen :: s -> IO Handle
|
|
|
48 |
|
|
|
49 |
instance HandleSource FilePath where
|
|
|
50 |
hOpen s = openFile s ReadWriteMode
|
|
|
51 |
|
|
|
52 |
instance HandleSource (HostName, PortID) where
|
|
|
53 |
hOpen = uncurry connectTo
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
handleEOF e = if isEOFError e
|
|
|
57 |
then return []
|
|
|
58 |
else throw $ TransportExn "TChannelTransport: Could not read" TE_UNKNOWN
|