summaryrefslogtreecommitdiff
path: root/Presence/Server.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2013-07-12 15:43:47 -0400
committerjoe <joe@jerkface.net>2013-07-12 15:43:47 -0400
commit6cf176ef39ef6e9616c74cbfc7c728c18d066526 (patch)
treeafb536dddf26a4e286927ac504faf27b2e641c33 /Presence/Server.hs
parent77f1978d8f73d4b5292c90283ec22ddffab7b077 (diff)
Removed HaXML based parsing & deleted obsolete modules.
Diffstat (limited to 'Presence/Server.hs')
-rw-r--r--Presence/Server.hs97
1 files changed, 0 insertions, 97 deletions
diff --git a/Presence/Server.hs b/Presence/Server.hs
deleted file mode 100644
index 80a6e4ba..00000000
--- a/Presence/Server.hs
+++ /dev/null
@@ -1,97 +0,0 @@
1{-# LANGUAGE TypeFamilies #-}
2{-# LANGUAGE TypeOperators #-}
3{-# LANGUAGE OverloadedStrings #-}
4module Server where
5
6import Network.Socket
7import Data.ByteString.Lazy.Char8 as L
8 ( fromChunks
9 , putStrLn )
10import Data.ByteString.Char8
11 ( hGetNonBlocking
12 )
13import System.IO
14 ( IOMode(..)
15 , hSetBuffering
16 , BufferMode(..)
17 , hWaitForInput
18 , hClose
19 , hIsEOF
20 )
21import Control.Monad
22import Control.Concurrent (forkIO)
23import Control.Exception (handle,SomeException(..))
24import Data.HList
25import Data.HList.TypeEqGeneric1()
26import Data.HList.TypeCastGeneric1()
27import System.IO.Error
28import ByteStringOperators
29
30
31newtype ConnId = ConnId Int
32 deriving Eq
33
34newtype ConnectionFinalizer = ConnectionFinalizer (IO ())
35
36getPacket h = do { hWaitForInput h (-1) ; fmap (fromChunks . (:[])) $ hGetNonBlocking h 1024 }
37
38newtype ServerHandle = ServerHandle Socket
39
40quitListening :: ServerHandle -> IO ()
41quitListening (ServerHandle socket) = sClose socket
42
43doServer addrfamily port g startCon = do
44 doServer' addrfamily port g startCon
45
46doServer' family port g startCon = runServer2 port (runConn2 g)
47 where
48 runConn2 g st (sock,_) = do
49 h <- socketToHandle sock ReadWriteMode
50 hSetBuffering h NoBuffering
51 st'' <- startCon sock (h .*. st)
52 let doException (SomeException e) = Prelude.putStrLn ("\n\nexception: " ++ show e ++ "\n\n")
53 handle doException $ fix $ \loop -> do
54 let continue () = hIsEOF h >>= flip when loop . not
55 packet <- getPacket h
56 g st'' packet continue
57 let ConnectionFinalizer cleanup = hOccursFst st''
58 cleanup
59 hClose h
60
61 {-
62 runServer2 ::
63 Num num =>
64 PortNumber -> (num -> (Socket, SockAddr) -> IO b -> IO b) -> IO b
65 -}
66 runServer2 st@(HCons port _) go = do
67 sock <- socket family Stream 0
68 setSocketOption sock ReuseAddr 1
69 case family of
70 AF_INET -> bindSocket sock (SockAddrInet port iNADDR_ANY)
71 AF_INET6 -> bindSocket sock (SockAddrInet6 port 0 iN6ADDR_ANY 0)
72 listen sock 2
73 forkIO $ do
74 mainLoop sock (ConnId 0) go
75 -- L.putStrLn $ "quit accept loop"
76 return (ServerHandle sock)
77 where
78 mainLoop sock idnum@(ConnId n) go = do
79 let doException ioerror = do
80 let typ = ioeGetErrorType ioerror
81 if -- typ == InvalidArgument
82 -- but the symbol is not exported :/
83 bshow typ=="invalid argument"
84 then do
85 L.putStrLn $ "quit accept-loop."
86 else do
87 L.putStrLn ("accept-loop exception: " <++> bshow ioerror <++> "\n")
88 return Nothing
89 mcon <- handle doException $ fix $ \loop -> do
90 con <- accept sock
91 return $ Just con
92 case mcon of
93 Just con -> do
94 forkIO $ go (idnum .*. st) con
95 mainLoop sock (ConnId (n+1)) go
96 Nothing -> return ()
97