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