Ticket #2996: closed-state.patch

File closed-state.patch, 1.0 KB (added by amthrax, 7 years ago)

Simple patch to add a Closed state and make sClose idempotent. Operations that perform state transitions already check the socket state and will refuse to operate on a Closed socket, but this does not add checks for other operations like send/recv.

  • Network/Socket.hsc

    diff -r 190661e92e68 Network/Socket.hsc
    a b  
    252252  | Listening           -- listen
    253253  | Connected           -- connect/accept
    254254  | ConvertedToHandle   -- is now a Handle, don't touch
     255  | Closed              -- sClose
    255256    deriving (Eq, Show)
    256257
    257258INSTANCE_TYPEABLE0(SocketStatus,socketStatusTc,"SocketStatus")
     
    19911992-- | Closes a socket
    19921993sClose   :: Socket -> IO ()
    19931994sClose (MkSocket s _ _ _ socketStatus) = do
    1994  withMVar socketStatus $ \ status ->
    1995    if status == ConvertedToHandle
    1996         then ioError (userError ("sClose: converted to a Handle, use hClose instead"))
    1997         else c_close s; return ()
     1995 modifyMVar_ socketStatus $ \ status ->
     1996   case status of
     1997     ConvertedToHandle ->
     1998         ioError (userError ("sClose: converted to a Handle, use hClose instead"))
     1999     Closed ->
     2000         return status
     2001     _ -> c_close s >> return Closed
    19982002
    19992003-- -----------------------------------------------------------------------------
    20002004