diff options
author | joe <joe@jerkface.net> | 2013-07-12 15:43:47 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2013-07-12 15:43:47 -0400 |
commit | 6cf176ef39ef6e9616c74cbfc7c728c18d066526 (patch) | |
tree | afb536dddf26a4e286927ac504faf27b2e641c33 /Presence/Server.hs | |
parent | 77f1978d8f73d4b5292c90283ec22ddffab7b077 (diff) |
Removed HaXML based parsing & deleted obsolete modules.
Diffstat (limited to 'Presence/Server.hs')
-rw-r--r-- | Presence/Server.hs | 97 |
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 #-} | ||
4 | module Server where | ||
5 | |||
6 | import Network.Socket | ||
7 | import Data.ByteString.Lazy.Char8 as L | ||
8 | ( fromChunks | ||
9 | , putStrLn ) | ||
10 | import Data.ByteString.Char8 | ||
11 | ( hGetNonBlocking | ||
12 | ) | ||
13 | import System.IO | ||
14 | ( IOMode(..) | ||
15 | , hSetBuffering | ||
16 | , BufferMode(..) | ||
17 | , hWaitForInput | ||
18 | , hClose | ||
19 | , hIsEOF | ||
20 | ) | ||
21 | import Control.Monad | ||
22 | import Control.Concurrent (forkIO) | ||
23 | import Control.Exception (handle,SomeException(..)) | ||
24 | import Data.HList | ||
25 | import Data.HList.TypeEqGeneric1() | ||
26 | import Data.HList.TypeCastGeneric1() | ||
27 | import System.IO.Error | ||
28 | import ByteStringOperators | ||
29 | |||
30 | |||
31 | newtype ConnId = ConnId Int | ||
32 | deriving Eq | ||
33 | |||
34 | newtype ConnectionFinalizer = ConnectionFinalizer (IO ()) | ||
35 | |||
36 | getPacket h = do { hWaitForInput h (-1) ; fmap (fromChunks . (:[])) $ hGetNonBlocking h 1024 } | ||
37 | |||
38 | newtype ServerHandle = ServerHandle Socket | ||
39 | |||
40 | quitListening :: ServerHandle -> IO () | ||
41 | quitListening (ServerHandle socket) = sClose socket | ||
42 | |||
43 | doServer addrfamily port g startCon = do | ||
44 | doServer' addrfamily port g startCon | ||
45 | |||
46 | doServer' 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 | |||