author | unc0rr |
Sun, 05 Oct 2008 23:36:11 +0000 | |
changeset 1306 | e848447f29be |
parent 1305 | 453882eb4467 |
child 1307 | ce26e16d18ab |
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) |
1082 | 20 |
hPutStrLn cHandle "CONNECTED\n" |
877 | 21 |
acceptLoop servSock acceptChan |
22 |
||
1082 | 23 |
listenLoop :: Handle -> [String] -> TChan [String] -> IO () |
24 |
listenLoop handle buf chan = do |
|
877 | 25 |
str <- hGetLine handle |
1082 | 26 |
if str == "" then do |
27 |
atomically $ writeTChan chan buf |
|
28 |
listenLoop handle [] chan |
|
29 |
else |
|
30 |
listenLoop handle (buf ++ [str]) chan |
|
877 | 31 |
|
1082 | 32 |
clientLoop :: Handle -> TChan [String] -> IO () |
877 | 33 |
clientLoop handle chan = |
1082 | 34 |
listenLoop handle [] chan |
877 | 35 |
`catch` (const $ clientOff >> return ()) |
1304
05cebf68ebd8
Start refactoring standalone server (prepare to change protocol)
unc0rr
parents:
1302
diff
changeset
|
36 |
where clientOff = atomically $ writeTChan chan ["QUIT"] -- если клиент отключается, то делаем вид, что от него пришла команда QUIT |
877 | 37 |
|
1306 | 38 |
sendAnswers [] _ clients _ = return clients |
39 |
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do |
|
40 |
putStrLn ("< " ++ show answer) |
|
41 |
||
42 |
let recipients = handlesFunc client clients rooms |
|
43 |
||
44 |
clHandles' <- forM recipients $ |
|
45 |
\ch -> do |
|
46 |
forM_ answer (\str -> hPutStrLn ch str) |
|
47 |
hPutStrLn ch "" |
|
48 |
hFlush ch |
|
49 |
if (not $ null answer) && (head answer == "BYE") then hClose ch >> return [ch] else return [] |
|
50 |
`catch` const (hClose ch >> return [ch]) |
|
51 |
||
52 |
let mclients = remove clients $ concat clHandles' |
|
53 |
||
54 |
sendAnswers answers client mclients rooms |
|
55 |
where |
|
56 |
remove list rmClHandles = deleteFirstsBy2t (\ a b -> (handle a) == b) list rmClHandles |
|
57 |
||
58 |
||
889 | 59 |
mainLoop :: Socket -> TChan ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO () |
60 |
mainLoop servSock acceptChan clients rooms = do |
|
877 | 61 |
r <- atomically $ (Left `fmap` readTChan acceptChan) `orElse` (Right `fmap` tselect clients) |
62 |
case r of |
|
889 | 63 |
Left ci -> do |
64 |
mainLoop servSock acceptChan (ci:clients) rooms |
|
1082 | 65 |
Right (cmd, client) -> do |
1302 | 66 |
putStrLn ("> " ++ show cmd) |
1305 | 67 |
let (clientsFunc, roomsFunc, answers) = handleCmd client clients rooms $ cmd |
890 | 68 |
|
1082 | 69 |
let mclients = clientsFunc clients |
70 |
let mrooms = roomsFunc rooms |
|
1305 | 71 |
|
1306 | 72 |
mclients <- sendAnswers answers client clients rooms |
73 |
||
74 |
mainLoop servSock acceptChan mclients mrooms |
|
877 | 75 |
|
76 |
startServer serverSocket = do |
|
77 |
acceptChan <- atomically newTChan |
|
78 |
forkIO $ acceptLoop serverSocket acceptChan |
|
889 | 79 |
mainLoop serverSocket acceptChan [] [] |
877 | 80 |
|
878 | 81 |
main = withSocketsDo $ do |
877 | 82 |
serverSocket <- listenOn $ Service "hedgewars" |
83 |
startServer serverSocket `finally` sClose serverSocket |