moduleNetwork.Socket.HandlewhereimportqualifiedGHC.IO.Device(IODeviceType(Stream))importGHC.IO.Handle.FD(fdToHandle')importSystem.IO(IOMode(..),Handle,BufferMode(..),hSetBuffering)importNetwork.Socket.Types -- | Turns a Socket into an 'Handle'. By default, the new handle is-- unbuffered. Use 'System.IO.hSetBuffering' to change the buffering.---- Note that since a 'Handle' is automatically closed by a finalizer-- when it is no longer referenced, you should avoid doing any more-- operations on the 'Socket' after calling 'socketToHandle'. To-- close the 'Socket' after 'socketToHandle', call 'System.IO.hClose'-- on the 'Handle'.---- Caveat 'Handle' is not recommended for network programming in -- Haskell, e.g. merely performing 'hClose' on a TCP socket won't-- cooperate with peer's 'gracefulClose', i.e. proper shutdown-- sequence with appropriate handshakes specified by the protocol.socketToHandle ::Socket ->IOMode->IOHandlesocketToHandle :: Socket -> IOMode -> IO Handle
socketToHandle Socket
s IOMode
mode =Socket -> (CInt -> IO Handle) -> (CInt -> IO Handle) -> IO Handle
forall a. Socket -> (CInt -> IO a) -> (CInt -> IO a) -> IO a
invalidateSocket Socket
s CInt -> IO Handle
forall p a. p -> IO a
err ((CInt -> IO Handle) -> IO Handle)
-> (CInt -> IO Handle) -> IO Handle
forall a b. (a -> b) -> a -> b
$\CInt
oldfd ->doHandle
h <-CInt
-> Maybe IODeviceType
-> Bool
-> FilePath
-> IOMode
-> Bool
-> IO Handle
fdToHandle'CInt
oldfd (IODeviceType -> Maybe IODeviceType
forall a. a -> Maybe a
JustIODeviceType
GHC.IO.Device.Stream)Bool
True(Socket -> FilePath
forall a. Show a => a -> FilePath
showSocket
s )IOMode
mode Bool
True{-bin-}Handle -> BufferMode -> IO ()
hSetBufferingHandle
h BufferMode
NoBufferingHandle -> IO Handle
forall (m :: * -> *) a. Monad m => a -> m a
returnHandle
h whereerr :: p -> IO a
err p
_=IOError -> IO a
forall a. IOError -> IO a
ioError(IOError -> IO a) -> IOError -> IO a
forall a b. (a -> b) -> a -> b
$FilePath -> IOError
userError(FilePath -> IOError) -> FilePath -> IOError
forall a b. (a -> b) -> a -> b
$FilePath
"socketToHandle: socket is no longer valid"

AltStyle によって変換されたページ (->オリジナル) /