Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs for shutting down sockets in a threaded scenario #548

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Network/Socket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,44 @@
-- unexpected things would happen. There is one exception for multiple
-- threads vs a single 'Socket': one thread reads data from a 'Socket'
-- only and the other thread writes data to the 'Socket' only.
--
-- The preferred way to terminate a thread that is blocked on a call to
-- 'Network.Socket.ByteString.recv' is to use 'shutdown'
--
-- > import Control.Concurrent (forkFinally, threadDelay)
-- > import qualified Control.Exception as E
-- > import Control.Monad (forever, guard, void)
-- > import Control.Monad.IO.Class (liftIO)
-- > import Control.Monad.Trans.Maybe (runMaybeT)
-- > import qualified Data.ByteString as BS
-- > import Network.Socket
-- > import Network.Socket.ByteString
-- >
-- > main =
-- > let maxQueuedConnections = 3
-- > gracefulCloseTimeout = 5000
-- > in do
-- > sock <- socket AF_UNIX Stream defaultProtocol
-- > bind sock (SockAddrUnix "./socket")
-- > listen sock maxQueuedConnections
-- >
-- > E.bracketOnError (accept sock) (\(connectedSock, _) -> close connectedSock) $
-- > \(connectedSock, _) -> void $ do
-- > forkFinally
-- > (printer connectedSock)
-- > (const $ gracefulClose connectedSock gracefulCloseTimeout >> print "closed")
-- >
-- > threadDelay (5_000_000)
-- > putStrLn "Time's up"
-- > shutdown connectedSock ShutdownBoth
-- >
-- > printer :: Socket -> IO ()
-- > printer connectedSock =
-- > -- https://www.haskellforall.com/2012/07/breaking-from-loop.html
-- > void $ runMaybeT $ forever $ do
-- > bytes <- liftIO $ recv connectedSock 4096
-- > guard $ not (BS.null bytes)
-- > liftIO $ print $ "Got bytes: " <> show bytes
-----------------------------------------------------------------------------

-- In order to process this file, you need to have CALLCONV defined.
Expand Down
6 changes: 6 additions & 0 deletions Network/Socket/Shutdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ sdownCmdToInt ShutdownBoth = 2
-- 'ShutdownReceive', further receives are disallowed. If it is
-- 'ShutdownSend', further sends are disallowed. If it is
-- 'ShutdownBoth', further sends and receives are disallowed.
--
-- This will wake up all threads that are blocked on a
-- 'Network.Socket.ByteString.recv' call on this socket, regardless
-- of which 'ShutdownCmd' is given.
-- Calling shutdown on a socket is the preferred way to abort a
-- connection from another thread.
shutdown :: Socket -> ShutdownCmd -> IO ()
shutdown s stype = void $ withFdSocket s $ \fd ->
throwSocketErrorIfMinus1Retry_ "Network.Socket.shutdown" $
Expand Down