Permalink
Browse files

Add Socket.hs

  • Loading branch information...
1 parent 0918692 commit f7706d552d3ed7a863552b0bbeed38dec07d80a1 @napthats committed Jun 24, 2013
Showing with 38 additions and 0 deletions.
  1. +38 −0 PhiVty/Socket.hs
View
@@ -0,0 +1,38 @@
+module PhiVty.Socket (
+ connect,
+ close,
+ send,
+ PhiSocket()
+ ) where
+
+import Network
+import System.IO
+import Control.Exception
+import Control.Concurrent
+import Prelude hiding (catch)
+
+
+data PhiSocket = PhiSocket {internalHandle :: Handle, recvThreadId :: ThreadId}
+
+connect :: String -> Int -> (String -> IO ()) -> IO PhiSocket
+connect addr port recv_handler = withSocketsDo $ do
+ hSetBuffering stdout NoBuffering
+ h <- connectTo addr (PortNumber $ fromIntegral port)
+ hSetBuffering h LineBuffering
+ tId <- forkIO $ do
+ sequence_ $ repeat $ do
+ res <- hGetLine h
+ recv_handler res
+ `catch` (\(SomeException e) -> return () )
+ `finally` do
+ hClose h
+ return $ PhiSocket {internalHandle = h, recvThreadId = tId}
+
+close :: PhiSocket -> IO ()
+close soc =
+ killThread $ recvThreadId soc
+
+send :: String -> PhiSocket -> IO ()
+send mes soc =
+ hPutStrLn (internalHandle soc) mes
+ `catch` (\(SomeException e) -> return ())

0 comments on commit f7706d5

Please sign in to comment.