network-run 0.4.3 → 0.4.4
raw patch · 5 files changed
+28/−22 lines, 5 files
Files
- Network/Run/Core.hs +4/−4
- Network/Run/TCP.hs +15/−10
- Network/Run/TCP/Timeout.hs +4/−3
- Network/Run/UDP.hs +4/−4
- network-run.cabal +1/−1
Network/Run/Core.hs view
@@ -16,12 +16,11 @@ labelMe, ) where -import qualified Data.List.NonEmpty as NE+import Data.List.NonEmpty (NonEmpty) import Control.Arrow import Control.Concurrent import qualified Control.Exception as E import Control.Monad (when)-import Foreign (Storable) import GHC.Conc.Sync import Network.Socket @@ -30,9 +29,10 @@ -> Maybe HostName -> ServiceName -> [AddrInfoFlag]+ -> (NonEmpty AddrInfo -> AddrInfo) -> IO AddrInfo-resolve socketType mhost port flags =- NE.head <$> getAddrInfo (Just hints) mhost (Just port)+resolve socketType mhost port flags select =+ select <$> getAddrInfo (Just hints) mhost (Just port) where hints = defaultHints
Network/Run/TCP.hs view
@@ -16,15 +16,18 @@ Settings, defaultSettings, settingsOpenClientSocket,+ settingsSelectAddrInfo, runTCPClientWithSettings, openClientSocket, openClientSocketWithOptions, openClientSocketWithOpts, ) where -import Control.Concurrent (forkFinally, threadDelay, forkIO)+import Control.Concurrent (forkFinally) import qualified Control.Exception as E import Control.Monad (forever, void)+import Data.List.NonEmpty (NonEmpty)+import qualified Data.List.NonEmpty as NE import Network.Socket import Network.Run.Core@@ -33,8 +36,8 @@ -- | Running a TCP server with an accepted socket and its peer name. runTCPServer :: Maybe HostName -> ServiceName -> (Socket -> IO a) -> IO a-runTCPServer mhost port server = withSocketsDo $ do- addr <- resolve Stream mhost port [AI_PASSIVE]+runTCPServer mhost port server = do+ addr <- resolve Stream mhost port [AI_PASSIVE] NE.head E.bracket (openTCPServerSocket addr) close $ \sock -> runTCPServerWithSocket sock server @@ -45,11 +48,10 @@ -> (Socket -> IO a) -- ^ Called for each incoming connection, in a new thread -> IO a-runTCPServerWithSocket sock server = withSocketsDo $- forever $- E.bracketOnError (accept sock) (close . fst) $- \(conn, _peer) ->- void $ forkFinally (labelMe "TCP server" >> server conn) (const $ gclose conn)+runTCPServerWithSocket sock server = forever $+ E.bracketOnError (accept sock) (close . fst) $+ \(conn, _peer) ->+ void $ forkFinally (labelMe "TCP server" >> server conn) (const $ gclose conn) ---------------------------------------------------------------- @@ -57,6 +59,8 @@ data Settings = Settings { settingsOpenClientSocket :: AddrInfo -> IO Socket -- ^ Opening a socket. Use 'openClientSocketWithOptions' to specify 'SocketOption'+ , settingsSelectAddrInfo :: NonEmpty AddrInfo -> AddrInfo+ -- ^ Selecting 'AddrInfo'. } -- | Default settings.@@ -64,6 +68,7 @@ defaultSettings = Settings { settingsOpenClientSocket = openClientSocket+ , settingsSelectAddrInfo = NE.head } -- | Running a TCP client with a connected socket.@@ -83,6 +88,6 @@ -> ServiceName -> (Socket -> IO a) -> IO a-runTCPClientWithSettings Settings{..} host port client = withSocketsDo $ do- addr <- resolve Stream (Just host) port [AI_ADDRCONFIG]+runTCPClientWithSettings Settings{..} host port client = do+ addr <- resolve Stream (Just host) port [AI_ADDRCONFIG] settingsSelectAddrInfo E.bracket (settingsOpenClientSocket addr) close client
Network/Run/TCP/Timeout.hs view
@@ -15,6 +15,7 @@ import Control.Concurrent (forkFinally) import qualified Control.Exception as E import Control.Monad (forever, void)+import qualified Data.List.NonEmpty as NE import Network.Socket import qualified System.TimeManager as T @@ -38,8 +39,8 @@ -> ServiceName -> TimeoutServer a -> IO a-runTCPServer tm mhost port server = withSocketsDo $ do- addr <- resolve Stream mhost port [AI_PASSIVE]+runTCPServer tm mhost port server = do+ addr <- resolve Stream mhost port [AI_PASSIVE] NE.head E.bracket (openTCPServerSocket addr) close $ \sock -> runTCPServerWithSocket tm sock server @@ -51,7 +52,7 @@ -> Socket -> TimeoutServer a -> IO a-runTCPServerWithSocket tm sock server = withSocketsDo $ do+runTCPServerWithSocket tm sock server = do T.withManager (tm * 1000000) $ \mgr -> forever $ E.bracketOnError (accept sock) (close . fst) $ \(conn, _peer) -> void $ forkFinally (server' mgr conn) (const $ gclose conn)
Network/Run/UDP.hs view
@@ -20,15 +20,15 @@ -- server's socket address. -- They should be used with 'sendTo'. runUDPClient :: HostName -> ServiceName -> (Socket -> SockAddr -> IO a) -> IO a-runUDPClient host port client = withSocketsDo $ do- addr <- resolve Datagram (Just host) port [AI_ADDRCONFIG]+runUDPClient host port client = do+ addr <- resolve Datagram (Just host) port [AI_ADDRCONFIG] NE.head let sockAddr = addrAddress addr E.bracket (openSocket addr) close $ \sock -> client sock sockAddr -- | Running a UDP server with an open socket in a single Haskell thread. runUDPServer :: Maybe HostName -> ServiceName -> (Socket -> IO a) -> IO a-runUDPServer mhost port server = withSocketsDo $ do- addr <- resolve Datagram mhost port [AI_PASSIVE]+runUDPServer mhost port server = do+ addr <- resolve Datagram mhost port [AI_PASSIVE] NE.head E.bracket (openServerSocket addr) close server -- | Running a UDP server with a connected socket in each Haskell thread.
network-run.cabal view
@@ -1,6 +1,6 @@ cabal-version: >=1.10 name: network-run-version: 0.4.3+version: 0.4.4 license: BSD3 license-file: LICENSE maintainer: kazu@iij.ad.jp