-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathServer.hs
More file actions
49 lines (40 loc) · 1.37 KB
/
Server.hs
File metadata and controls
49 lines (40 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Server (server) where
import Control.Concurrent
import Control.Concurrent.STM
import Control.Exception as E
import Control.Monad (forever)
import Debug.Trace
import Data.Map as M
import Network
import System.IO
import Dispatch
import DataStores
import Message
server :: PortID -> IO ()
server port = withSocketsDo $ do
userStore <- atomically $ newTVar M.empty
roomStore <- atomically $ newTVar M.empty
serverSock <- listenOn port
(waitForClientsWrapper serverSock userStore roomStore
`finally`
sClose serverSock)
waitForClientsWrapper :: Socket ->
UserStore ->
RoomStore ->
IO ()
waitForClientsWrapper serverSock userStore roomStore = do
waitForClients serverSock userStore roomStore
`E.catch`
listenThreadExceptionHandler (waitForClients serverSock userStore roomStore)
waitForClients :: Socket ->
UserStore ->
RoomStore ->
IO ()
waitForClients serverSock userStore roomStore = forever $ do
(handle, _, _) <- accept serverSock
hSetBuffering handle LineBuffering
hSetNewlineMode handle (NewlineMode CRLF CRLF)
forkIO $ trace "Socket accepted, forking dispatcher" $
loginThreadWrapper userStore roomStore handle
listenThreadExceptionHandler :: IO () -> IOException -> IO ()
listenThreadExceptionHandler continue e = continue