iproctor | ff8eb92 | 2007-07-25 19:06:13 +0000 | [diff] [blame^] | 1 | module TSocket(TSocket(..)) where |
| 2 | import Thrift |
| 3 | import Data.IORef |
| 4 | import Network |
| 5 | import IO |
| 6 | import Control.Exception |
| 7 | data TSocket = TSocket{host::[Char],port::PortNumber,chan :: Maybe Handle} |
| 8 | |
| 9 | instance TTransport TSocket where |
| 10 | tisOpen a = case chan a of |
| 11 | Just _ -> True |
| 12 | Nothing -> False |
| 13 | topen a = do h <- connectTo (host a) (PortNumber (port a)) |
| 14 | return $ (a{chan = Just h}) |
| 15 | tclose a = case chan a of |
| 16 | Just h -> do hClose h |
| 17 | return $ a{chan=Nothing} |
| 18 | Nothing -> return a |
| 19 | tread a 0 = return [] |
| 20 | tread a n = case chan a of |
| 21 | Just h -> handle (\e -> throwDyn (TransportExn "TSocket: Could not read." TE_UNKNOWN)) |
| 22 | (do c <- hGetChar h |
| 23 | l <- tread a (n-1) |
| 24 | return $ c:l) |
| 25 | Nothing -> return [] |
| 26 | twrite a s = case chan a of |
| 27 | Just h -> hPutStr h s |
| 28 | Nothing -> return () |
| 29 | tflush a = case chan a of |
| 30 | Just h -> hFlush h |
| 31 | Nothing -> return () |
| 32 | |
| 33 | |