on master help sign in
Add withSocket, add networking examples to cabal file
commit22ccf96a1731a774d0f435ec2dfca7bd151ff0dc authorHarendra Kumar <harendra.kumar@gmail.com> dateFri 31 May 2019, 15:01 (7 years ago) parent0ae75fb
3 files changed+55−9
examples/EchoServer.hs+3−7 src/Streamly/Network/Socket.hs+13−2 streamly.cabal+39
examples/EchoServer.hs+3−7
@@ -2,14 +2,10 @@
22
33import Streamly
44import Streamly.Network.Socket
5+import Streamly.Network.Server
56import qualified Streamly.Prelude as S
6−import qualified Network.Socket as NS
77
88main :: IO ()
99main = S.drain
10− $ parallely $ S.mapM (withSocket (\sk -> writeArrays sk $ readArrays sk))
11− $ serially $ fmap fst (recvConnectionsOn 8090)
12−
13− where
14−
15− withSocket f sk = f sk >> NS.close sk
10+ $ parallely $ S.mapM (flip withSocket (\sk -> writeArrays sk $ readArrays sk))
11+ $ serially $ connectionsOnAllAddrs 8090
src/Streamly/Network/Socket.hs+13−2
@@ -32,8 +32,9 @@
3232
3333module Streamly.Network.Socket
3434 (
35+ withSocket
3536 -- * Read from connection
36− fromSocket
37+ , fromSocket
3738 , read
3839 -- , readUtf8
3940 -- , readLines
@@ -64,7 +65,7 @@ module Streamly.Network.Socket
6465where
6566
6667import Control.Concurrent (threadWaitWrite, rtsSupportsBoundThreads)
67−import Control.Monad.Catch (MonadCatch)
68+import Control.Monad.Catch (MonadCatch, onException)
6869import Control.Monad.IO.Class (MonadIO(..))
6970import Control.Monad (when)
7071import Data.Word (Word8)
@@ -93,6 +94,16 @@ import qualified Streamly.Mem.Array as A
9394import qualified Streamly.Mem.Array.Types as A hiding (flattenArrays)
9495import qualified Streamly.Prelude as S
9596
97+-- | @'withSocket' socket act@ runs the computation @act@ passing the socket
98+-- handle to it. The handle will be closed on exit from 'withSocket', whether
99+-- by normal termination or by raising an exception. If closing the handle
100+-- raises an exception, then this exception will be raised by 'withSocket'
101+-- rather than any exception raised by 'act'.
102+withSocket :: (MonadCatch m, MonadIO m) => Socket -> (Socket -> m ()) -> m ()
103+withSocket sk f = do
104+ f sk `onException` liftIO (Net.close sk)
105+ liftIO (Net.close sk)
106+
96107-------------------------------------------------------------------------------
97108-- Array IO (Input)
98109-------------------------------------------------------------------------------
streamly.cabal+39
@@ -959,3 +959,42 @@ executable FileIO
959959 , base >= 4.8 && < 5
960960 else
961961 buildable: False
962+
963+executable EchoServer
964+ default-language: Haskell2010
965+ main-is: EchoServer.hs
966+ hs-source-dirs: examples
967+ ghc-options: -Wall
968+ if flag(examples) || flag(examples-sdl)
969+ buildable: True
970+ build-Depends:
971+ streamly
972+ , base >= 4.8 && < 5
973+ else
974+ buildable: False
975+
976+executable FileSinkServer
977+ default-language: Haskell2010
978+ main-is: FileSinkServer.hs
979+ hs-source-dirs: examples
980+ ghc-options: -Wall
981+ if flag(examples) || flag(examples-sdl)
982+ buildable: True
983+ build-Depends:
984+ streamly
985+ , base >= 4.8 && < 5
986+ else
987+ buildable: False
988+
989+executable FromFileClient
990+ default-language: Haskell2010
991+ main-is: FromFileClient.hs
992+ hs-source-dirs: examples
993+ ghc-options: -Wall
994+ if flag(examples) || flag(examples-sdl)
995+ buildable: True
996+ build-Depends:
997+ streamly
998+ , base >= 4.8 && < 5
999+ else
1000+ buildable: False