summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ConduitServer.hs21
-rw-r--r--Presence/ServerC.hs25
2 files changed, 33 insertions, 13 deletions
diff --git a/ConduitServer.hs b/ConduitServer.hs
index 4b28194e..0838ce26 100644
--- a/ConduitServer.hs
+++ b/ConduitServer.hs
@@ -6,7 +6,10 @@ import Data.Conduit.Network
6import Data.Conduit 6import Data.Conduit
7import qualified Data.Conduit.List as CL 7import qualified Data.Conduit.List as CL
8import qualified Data.ByteString.Char8 as S 8import qualified Data.ByteString.Char8 as S
9import qualified Data.ByteString.Lazy.Char8 as L 9
10import Network.Socket (Family(..))
11import Data.HList
12import ServerC
10 13
11{- 14{-
12data AppData m = AppData 15data AppData m = AppData
@@ -24,8 +27,22 @@ handleConnection appdata = do
24 sourceLbs "<stream>\n" $$ appSink appdata -- send bytestring 27 sourceLbs "<stream>\n" $$ appSink appdata -- send bytestring
25 appSource appdata $$ CL.mapM_ S.putStrLn -- display inbound bytestring 28 appSource appdata $$ CL.mapM_ S.putStrLn -- display inbound bytestring
26 29
27main = do 30mainOld = do
28 -- Listen to port 5222 and invoke handleConnection on every 31 -- Listen to port 5222 and invoke handleConnection on every
29 -- inbound connection. 32 -- inbound connection.
30 runTCPServer (serverSettings 5222 HostAny) handleConnection 33 runTCPServer (serverSettings 5222 HostAny) handleConnection
31 return () 34 return ()
35
36
37
38
39handleC st src snk = do
40 sourceLbs "<stream>\n" $$ snk
41 src $$ CL.mapM_ S.putStrLn
42
43mainC = do
44 doServer (AF_INET .*. 5222 .*. HNil) handleC
45 _ <- getLine
46 return ()
47
48main = mainC
diff --git a/Presence/ServerC.hs b/Presence/ServerC.hs
index 2e22825d..881ce5d2 100644
--- a/Presence/ServerC.hs
+++ b/Presence/ServerC.hs
@@ -2,7 +2,7 @@
2{-# LANGUAGE TypeOperators #-} 2{-# LANGUAGE TypeOperators #-}
3{-# LANGUAGE OverloadedStrings #-} 3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE RankNTypes #-} 4{-# LANGUAGE RankNTypes #-}
5module ServerC 5module ServerC
6 ( doServer 6 ( doServer
7 , ConnId(..) 7 , ConnId(..)
8 ) where 8 ) where
@@ -56,8 +56,8 @@ doServer ::
56 MonadIO m => 56 MonadIO m =>
57 HCons Socket.Family (HCons PortNumber l) 57 HCons Socket.Family (HCons PortNumber l)
58 -> (HCons RestrictedSocket (HCons ConnId l) 58 -> (HCons RestrictedSocket (HCons ConnId l)
59 -> Producer m S.ByteString 59 -> Source m S.ByteString
60 -> Consumer S.ByteString m () 60 -> Sink S.ByteString m ()
61 -> IO ()) 61 -> IO ())
62 -> IO Socket 62 -> IO Socket
63doServer (HCons family port) g = runServer port (runConn g) 63doServer (HCons family port) g = runServer port (runConn g)
@@ -99,7 +99,7 @@ doServer (HCons family port) g = runServer port (runConn g)
99 Retry -> threadDelay 500000 >> mainLoop sock idnum go 99 Retry -> threadDelay 500000 >> mainLoop sock idnum go
100 QuitOnException -> return () 100 QuitOnException -> return ()
101 101
102packets :: MonadIO m => Handle -> Producer m S.ByteString 102packets :: MonadIO m => Handle -> Source m S.ByteString
103packets h = do 103packets h = do
104 packet <- lift $ liftIO $ getPacket h 104 packet <- lift $ liftIO $ getPacket h
105 yield packet 105 yield packet
@@ -108,7 +108,7 @@ packets h = do
108 where 108 where
109 getPacket h = do { hWaitForInput h (-1) ; hGetNonBlocking h 1024 } 109 getPacket h = do { hWaitForInput h (-1) ; hGetNonBlocking h 1024 }
110 110
111outgoing :: MonadIO m => Handle -> Consumer S.ByteString m () 111outgoing :: MonadIO m => Handle -> Sink S.ByteString m ()
112outgoing h = do 112outgoing h = do
113 mpacket <- await 113 mpacket <- await
114 maybe (return ()) 114 maybe (return ())
@@ -116,14 +116,17 @@ outgoing h = do
116 mpacket 116 mpacket
117 117
118 118
119
120
119runConn :: 121runConn ::
120 MonadIO m => 122 MonadIO m =>
121 ( HCons RestrictedSocket st 123 (HCons RestrictedSocket st
122 -> Producer m S.ByteString 124 -> Source m S.ByteString
123 -> Consumer S.ByteString m () 125 -> Sink S.ByteString m ()
124 -> IO () ) 126 -> IO ())
125 -> st -> (Socket, t) -> IO () 127 -> st
126 128 -> (Socket, t)
129 -> IO ()
127runConn g st (sock,_) = do 130runConn g st (sock,_) = do
128 h <- socketToHandle sock ReadWriteMode 131 h <- socketToHandle sock ReadWriteMode
129 hSetBuffering h NoBuffering 132 hSetBuffering h NoBuffering