blob: 306edc2080de19c4e830c46ef3d1dc6b1f22accb [file] [log] [blame]
Bryan Duxburye59a80f2010-09-20 15:21:37 +00001{-# LANGUAGE DeriveDataTypeable #-}
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -07002{-# LANGUAGE MultiParamTypeClasses #-}
Bryan Duxbury0781f2b2009-04-07 23:29:42 +00003--
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
22module Thrift.Transport
23 ( Transport(..)
24 , TransportExn(..)
25 , TransportExnType(..)
26 ) where
27
28import Control.Monad ( when )
29import Control.Exception ( Exception, throw )
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070030import Data.Functor ( (<$>) )
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000031import Data.Typeable ( Typeable )
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070032import Data.Word
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000033
Christian Lavoieae7f7fa2010-11-02 21:42:53 +000034import qualified Data.ByteString.Lazy as LBS
David Reiss752529e2010-01-11 19:12:56 +000035import Data.Monoid
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000036
37class Transport a where
38 tIsOpen :: a -> IO Bool
39 tClose :: a -> IO ()
David Reiss752529e2010-01-11 19:12:56 +000040 tRead :: a -> Int -> IO LBS.ByteString
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070041 tPeek :: a -> IO (Maybe Word8)
David Reiss752529e2010-01-11 19:12:56 +000042 tWrite :: a -> LBS.ByteString -> IO ()
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000043 tFlush :: a -> IO ()
David Reiss752529e2010-01-11 19:12:56 +000044 tReadAll :: a -> Int -> IO LBS.ByteString
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000045
Bryan Duxburye59a80f2010-09-20 15:21:37 +000046 tReadAll _ 0 = return mempty
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000047 tReadAll a len = do
48 result <- tRead a len
David Reiss752529e2010-01-11 19:12:56 +000049 let rlen = fromIntegral $ LBS.length result
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000050 when (rlen == 0) (throw $ TransportExn "Cannot read. Remote side has closed." TE_UNKNOWN)
51 if len <= rlen
Noam Zilbersteinaf5d64a2014-07-31 15:44:13 -070052 then return result
53 else (result `mappend`) <$> tReadAll a (len - rlen)
Bryan Duxbury0781f2b2009-04-07 23:29:42 +000054
55data TransportExn = TransportExn String TransportExnType
56 deriving ( Show, Typeable )
57instance Exception TransportExn
58
59data 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 )