author | unc0rr |
Wed, 30 Apr 2008 20:48:12 +0000 | |
changeset 893 | 149244d86bf1 |
parent 892 | dfe97199f17e |
child 894 | 2ca76a7f3121 |
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 |
|
889 | 19 |
atomically $ writeTChan acceptChan (ClientInfo cChan cHandle "" "" 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 |
`finally` hClose handle |
|
33 |
where clientOff = atomically $ writeTChan chan "QUIT" |
|
34 |
||
889 | 35 |
mainLoop :: Socket -> TChan ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO () |
36 |
mainLoop servSock acceptChan clients rooms = do |
|
877 | 37 |
r <- atomically $ (Left `fmap` readTChan acceptChan) `orElse` (Right `fmap` tselect clients) |
38 |
case r of |
|
889 | 39 |
Left ci -> do |
40 |
mainLoop servSock acceptChan (ci:clients) rooms |
|
890 | 41 |
Right (line, client) -> do |
893 | 42 |
let (mclient, mrooms, recipients, strs) = handleCmd client clients rooms $ words line |
890 | 43 |
|
891
701f86df9b4c
Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents:
890
diff
changeset
|
44 |
clients' <- forM recipients $ |
889 | 45 |
\ci -> do |
890 | 46 |
forM_ strs (\str -> hPutStrLn (handle ci) str) |
47 |
hFlush (handle ci) |
|
48 |
return [] |
|
49 |
`catch` const (hClose (handle ci) >> return [ci]) |
|
50 |
||
892
dfe97199f17e
Better QUIT handling imlementation - reduces function return values count
unc0rr
parents:
891
diff
changeset
|
51 |
client' <- if 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
|
52 |
|
893 | 53 |
mainLoop servSock acceptChan (remove (remove (mclient : filter (\cl -> handle cl /= handle client) clients) (concat clients')) client') mrooms |
890 | 54 |
where |
891
701f86df9b4c
Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents:
890
diff
changeset
|
55 |
remove list rmClients = deleteFirstsBy (\ a b -> handle a == handle b) list rmClients |
877 | 56 |
|
57 |
startServer serverSocket = do |
|
58 |
acceptChan <- atomically newTChan |
|
59 |
forkIO $ acceptLoop serverSocket acceptChan |
|
889 | 60 |
mainLoop serverSocket acceptChan [] [] |
877 | 61 |
|
878 | 62 |
main = withSocketsDo $ do |
877 | 63 |
serverSocket <- listenOn $ Service "hedgewars" |
64 |
startServer serverSocket `finally` sClose serverSocket |