module Network.Socket.Handle where
import qualified GHC.IO.Device (IODeviceType(Stream))
import GHC.IO.Handle.FD (fdToHandle')
import System.IO (IOMode(..), Handle, BufferMode(..), hSetBuffering)
import Network.Socket.Types
socketToHandle :: Socket -> IOMode -> IO Handle
socketToHandle s mode = invalidateSocket s err $ \oldfd -> do
h <- fdToHandle' oldfd (Just GHC.IO.Device.Stream) True (show s) mode True
hSetBuffering h NoBuffering
return h
where
err _ = ioError $ userError $ "socketToHandle: socket is no longer valid"