network-run (empty) → 0.0.0
raw patch · 5 files changed
+111/−0 lines, 5 filesdep +basedep +networksetup-changed
Dependencies added: base, network
Files
- CHANGELOG.md +5/−0
- LICENSE +29/−0
- Network/Run/TCP.hs +49/−0
- Setup.hs +2/−0
- network-run.cabal +26/−0
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for network-run++## 0.0.0++* First version.
+ LICENSE view
@@ -0,0 +1,29 @@+Copyright (c) 2019, IIJ Innovation Institute Inc.+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions+are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.+ * Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in+ the documentation and/or other materials provided with the+ distribution.+ * Neither the name of the copyright holders nor the names of its+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN+ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE+POSSIBILITY OF SUCH DAMAGE.
+ Network/Run/TCP.hs view
@@ -0,0 +1,49 @@+{-# LANGUAGE OverloadedStrings #-}++-- | Simple functions to run TCP clients and servers.+module Network.Run.TCP (+ runTCPClient+ , runTCPServer+ ) where++import Control.Concurrent (forkFinally)+import qualified Control.Exception as E+import Control.Monad (forever, void)+import Network.Socket++-- | Running a TCP client with a connected socket.+runTCPClient :: HostName -> ServiceName -> (Socket -> IO a) -> IO a+runTCPClient host port client = withSocketsDo $ do+ addr <- resolve+ E.bracket (open addr) (\sock -> gracefulClose sock 5000) client+ where+ resolve = do+ let hints = defaultHints { addrSocketType = Stream }+ head <$> getAddrInfo (Just hints) (Just host) (Just port)+ open addr = do+ sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)+ connect sock $ addrAddress addr+ return sock++-- | Running a TCP server with an accepted socket and its peer name.+runTCPServer :: Maybe HostName -> ServiceName -> (Socket -> SockAddr -> IO a) -> IO a+runTCPServer mhost port server = withSocketsDo $ do+ addr <- resolve+ E.bracket (open addr) close loop+ where+ resolve = do+ let hints = defaultHints {+ addrFlags = [AI_PASSIVE]+ , addrSocketType = Stream+ }+ head <$> getAddrInfo (Just hints) mhost (Just port)+ open addr = do+ sock <- socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)+ setSocketOption sock ReuseAddr 1+ withFdSocket sock $ setCloseOnExecIfNeeded+ bind sock $ addrAddress addr+ listen sock 1024+ return sock+ loop sock = forever $ do+ (conn, peer) <- accept sock+ void $ forkFinally (server conn peer) (const $ gracefulClose conn 5000)
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ network-run.cabal view
@@ -0,0 +1,26 @@+name: network-run+version: 0.0.0+synopsis: Simple network runner library+description: Simple functions to run network clients and servers.+-- bug-reports:+license: BSD3+license-file: LICENSE+author: Kazu Yamamoto+maintainer: kazu@iij.ad.jp+category: Network+build-type: Simple+extra-source-files: CHANGELOG.md+cabal-version: >=1.10++library+ exposed-modules: Network.Run.TCP+ -- other-modules:+ -- other-extensions:+ build-depends: base >= 4 && < 5+ , network >= 3.1.1+ -- hs-source-dirs:+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/kazu-yamamoto/network-run