blob: 7e72878613548887f92a8f6ddf2ad92239b8ceee [file] [log] [blame]
iproctorff8eb922007-07-25 19:06:13 +00001module TSocket(TSocket(..)) where
2import Thrift
3import Data.IORef
4import Network
5import IO
6import Control.Exception
7data TSocket = TSocket{host::[Char],port::PortNumber,chan :: Maybe Handle}
8
9instance 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