author | unc0rr |
Sun, 05 Oct 2008 23:27:53 +0000 | |
changeset 1305 | 453882eb4467 |
parent 1304 | 05cebf68ebd8 |
child 1306 | e848447f29be |
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 |
|
889 | 38 |
mainLoop :: Socket -> TChan ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO () |
39 |
mainLoop servSock acceptChan clients rooms = do |
|
877 | 40 |
r <- atomically $ (Left `fmap` readTChan acceptChan) `orElse` (Right `fmap` tselect clients) |
41 |
case r of |
|
889 | 42 |
Left ci -> do |
43 |
mainLoop servSock acceptChan (ci:clients) rooms |
|
1082 | 44 |
Right (cmd, client) -> do |
1302 | 45 |
putStrLn ("> " ++ show cmd) |
1305 | 46 |
let (clientsFunc, roomsFunc, answers) = handleCmd client clients rooms $ cmd |
890 | 47 |
|
1082 | 48 |
let mclients = clientsFunc clients |
49 |
let mrooms = roomsFunc rooms |
|
1305 | 50 |
|
51 |
clHandles' <- forM answers $ |
|
52 |
\(handlesFunc, answer) -> do |
|
53 |
putStrLn ("< " ++ show answer) |
|
54 |
let recipients = handlesFunc client mclients mrooms |
|
55 |
forM recipients $ |
|
56 |
\ch -> do |
|
1082 | 57 |
forM_ answer (\str -> hPutStrLn ch str) |
58 |
hPutStrLn ch "" |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
898
diff
changeset
|
59 |
hFlush ch |
1305 | 60 |
if (not $ null answer) && (head answer == "BYE") then hClose ch >> return [ch] else return [] |
61 |
`catch` const (hClose ch >> return [ch]) |
|
890 | 62 |
|
1305 | 63 |
mainLoop servSock acceptChan (remove mclients (concat $ concat clHandles')) mrooms |
890 | 64 |
where |
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
898
diff
changeset
|
65 |
remove list rmClHandles = deleteFirstsBy2t (\ a b -> (handle a) == b) list rmClHandles |
877 | 66 |
|
67 |
startServer serverSocket = do |
|
68 |
acceptChan <- atomically newTChan |
|
69 |
forkIO $ acceptLoop serverSocket acceptChan |
|
889 | 70 |
mainLoop serverSocket acceptChan [] [] |
877 | 71 |
|
878 | 72 |
main = withSocketsDo $ do |
877 | 73 |
serverSocket <- listenOn $ Service "hedgewars" |
74 |
startServer serverSocket `finally` sClose serverSocket |