author | unc0rr |
Thu, 01 May 2008 14:30:12 +0000 | |
changeset 894 | 2ca76a7f3121 |
parent 893 | 149244d86bf1 |
child 898 | 344ba7dba23d |
permissions | -rw-r--r-- |
877 | 1 |
module Main where |
2 |
||
3 |
import Network |
|
4 |
import IO |
|
5 |
import System.IO |
|
6 |
import Control.Concurrent |
|
7 |
import Control.Concurrent.STM |
|
8 |
import Control.Exception (finally) |
|
890 | 9 |
import Control.Monad (forM, forM_, filterM, liftM) |
10 |
import Data.List |
|
877 | 11 |
import Miscutils |
890 | 12 |
import HWProto |
877 | 13 |
|
889 | 14 |
acceptLoop :: Socket -> TChan ClientInfo -> IO () |
877 | 15 |
acceptLoop servSock acceptChan = do |
16 |
(cHandle, host, port) <- accept servSock |
|
17 |
cChan <- atomically newTChan |
|
18 |
forkIO $ clientLoop cHandle cChan |
|
894 | 19 |
atomically $ writeTChan acceptChan (ClientInfo cChan cHandle "" 0 "" False) |
877 | 20 |
acceptLoop servSock acceptChan |
21 |
||
22 |
listenLoop :: Handle -> TChan String -> IO () |
|
23 |
listenLoop handle chan = do |
|
24 |
str <- hGetLine handle |
|
25 |
atomically $ writeTChan chan str |
|
26 |
listenLoop handle chan |
|
27 |
||
28 |
clientLoop :: Handle -> TChan String -> IO () |
|
29 |
clientLoop handle chan = |
|
30 |
listenLoop handle chan |
|
31 |
`catch` (const $ clientOff >> return ()) |
|
32 |
where clientOff = atomically $ writeTChan chan "QUIT" |
|
33 |
||
889 | 34 |
mainLoop :: Socket -> TChan ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO () |
35 |
mainLoop servSock acceptChan clients rooms = do |
|
877 | 36 |
r <- atomically $ (Left `fmap` readTChan acceptChan) `orElse` (Right `fmap` tselect clients) |
37 |
case r of |
|
889 | 38 |
Left ci -> do |
39 |
mainLoop servSock acceptChan (ci:clients) rooms |
|
890 | 40 |
Right (line, client) -> do |
893 | 41 |
let (mclient, mrooms, recipients, strs) = handleCmd client clients rooms $ words line |
890 | 42 |
|
891
701f86df9b4c
Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents:
890
diff
changeset
|
43 |
clients' <- forM recipients $ |
889 | 44 |
\ci -> do |
890 | 45 |
forM_ strs (\str -> hPutStrLn (handle ci) str) |
46 |
hFlush (handle ci) |
|
47 |
return [] |
|
48 |
`catch` const (hClose (handle ci) >> return [ci]) |
|
49 |
||
894 | 50 |
client' <- if (not $ null strs) && (head strs == "QUIT") then hClose (handle client) >> return [client] else return [] |
891
701f86df9b4c
Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents:
890
diff
changeset
|
51 |
|
893 | 52 |
mainLoop servSock acceptChan (remove (remove (mclient : filter (\cl -> handle cl /= handle client) clients) (concat clients')) client') mrooms |
890 | 53 |
where |
891
701f86df9b4c
Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents:
890
diff
changeset
|
54 |
remove list rmClients = deleteFirstsBy (\ a b -> handle a == handle b) list rmClients |
877 | 55 |
|
56 |
startServer serverSocket = do |
|
57 |
acceptChan <- atomically newTChan |
|
58 |
forkIO $ acceptLoop serverSocket acceptChan |
|
889 | 59 |
mainLoop serverSocket acceptChan [] [] |
877 | 60 |
|
878 | 61 |
main = withSocketsDo $ do |
877 | 62 |
serverSocket <- listenOn $ Service "hedgewars" |
63 |
startServer serverSocket `finally` sClose serverSocket |