on master help sign in
Add withSocket, add networking examples to cabal file
22ccf96Harendra Kumar
examples/EchoServer.hs+3−7
@@ -2,14 +2,10 @@
2
2
3import Streamly
3import Streamly
4import Streamly.Network.Socket
4import Streamly.Network.Socket
5+import Streamly.Network.Server
5import qualified Streamly.Prelude as S
6import qualified Streamly.Prelude as S
6−import qualified Network.Socket as NS
7
7
8main :: IO ()
8main :: IO ()
9main = S.drain
9main = S.drain
10− $ parallely $ S.mapM (withSocket (\sk -> writeArrays sk $ readArrays sk))
10+ $ parallely $ S.mapM (flip withSocket (\sk -> writeArrays sk $ readArrays sk))
11− $ serially $ fmap fst (recvConnectionsOn 8090)
11+ $ serially $ connectionsOnAllAddrs 8090
12−
13− where
14−
15− withSocket f sk = f sk >> NS.close sk
src/Streamly/Network/Socket.hs+13−2
@@ -32,8 +32,9 @@
32
32
33module Streamly.Network.Socket
33module Streamly.Network.Socket
34 (
34 (
35+ withSocket
35 -- * Read from connection
36 -- * Read from connection
36− fromSocket
37+ , fromSocket
37 , read
38 , read
38 -- , readUtf8
39 -- , readUtf8
39 -- , readLines
40 -- , readLines
@@ -64,7 +65,7 @@ module Streamly.Network.Socket
64where
65where
65
66
66import Control.Concurrent (threadWaitWrite, rtsSupportsBoundThreads)
67import Control.Concurrent (threadWaitWrite, rtsSupportsBoundThreads)
67−import Control.Monad.Catch (MonadCatch)
68+import Control.Monad.Catch (MonadCatch, onException)
68import Control.Monad.IO.Class (MonadIO(..))
69import Control.Monad.IO.Class (MonadIO(..))
69import Control.Monad (when)
70import Control.Monad (when)
70import Data.Word (Word8)
71import Data.Word (Word8)
@@ -93,6 +94,16 @@ import qualified Streamly.Mem.Array as A
93import qualified Streamly.Mem.Array.Types as A hiding (flattenArrays)
94import qualified Streamly.Mem.Array.Types as A hiding (flattenArrays)
94import qualified Streamly.Prelude as S
95import qualified Streamly.Prelude as S
95
96
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+
96-------------------------------------------------------------------------------
107-------------------------------------------------------------------------------
97-- Array IO (Input)
108-- Array IO (Input)
98-------------------------------------------------------------------------------
109-------------------------------------------------------------------------------
streamly.cabal+39
@@ -959,3 +959,42 @@ executable FileIO
959 , base >= 4.8 && < 5
959 , base >= 4.8 && < 5
960 else
960 else
961 buildable: False
961 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