author | unc0rr |
Wed, 30 Apr 2008 20:14:09 +0000 | |
changeset 891 | 701f86df9b4c |
parent 890 | 1d8c4a5ec622 |
child 892 | dfe97199f17e |
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 |
891
701f86df9b4c
Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents:
890
diff
changeset
|
42 |
let (doQuit, recipients, strs) = handleCmd client sameRoom 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 |
||
891
701f86df9b4c
Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents:
890
diff
changeset
|
51 |
client' <- if doQuit then hClose (handle client) >> return [client] else return [] |
701f86df9b4c
Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents:
890
diff
changeset
|
52 |
|
701f86df9b4c
Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents:
890
diff
changeset
|
53 |
mainLoop servSock acceptChan (remove (remove clients (concat clients')) client') rooms |
890 | 54 |
where |
55 |
sameRoom = filter (\cl -> room cl == room client) clients |
|
891
701f86df9b4c
Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents:
890
diff
changeset
|
56 |
remove list rmClients = deleteFirstsBy (\ a b -> handle a == handle b) list rmClients |
877 | 57 |
|
58 |
startServer serverSocket = do |
|
59 |
acceptChan <- atomically newTChan |
|
60 |
forkIO $ acceptLoop serverSocket acceptChan |
|
889 | 61 |
mainLoop serverSocket acceptChan [] [] |
877 | 62 |
|
878 | 63 |
main = withSocketsDo $ do |
877 | 64 |
serverSocket <- listenOn $ Service "hedgewars" |
65 |
startServer serverSocket `finally` sClose serverSocket |