author | unc0rr |
Tue, 07 Oct 2008 12:37:06 +0000 | |
changeset 1311 | 49beb1221c0b |
parent 1309 | 1a38a967bd48 |
child 1321 | d7dc4e86201e |
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) |
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
10 |
import Maybe (fromMaybe) |
890 | 11 |
import Data.List |
877 | 12 |
import Miscutils |
890 | 13 |
import HWProto |
877 | 14 |
|
889 | 15 |
acceptLoop :: Socket -> TChan ClientInfo -> IO () |
877 | 16 |
acceptLoop servSock acceptChan = do |
17 |
(cHandle, host, port) <- accept servSock |
|
18 |
cChan <- atomically newTChan |
|
19 |
forkIO $ clientLoop cHandle cChan |
|
894 | 20 |
atomically $ writeTChan acceptChan (ClientInfo cChan cHandle "" 0 "" False) |
1082 | 21 |
hPutStrLn cHandle "CONNECTED\n" |
877 | 22 |
acceptLoop servSock acceptChan |
23 |
||
1307 | 24 |
|
1082 | 25 |
listenLoop :: Handle -> [String] -> TChan [String] -> IO () |
26 |
listenLoop handle buf chan = do |
|
877 | 27 |
str <- hGetLine handle |
1082 | 28 |
if str == "" then do |
29 |
atomically $ writeTChan chan buf |
|
30 |
listenLoop handle [] chan |
|
31 |
else |
|
32 |
listenLoop handle (buf ++ [str]) chan |
|
877 | 33 |
|
1307 | 34 |
|
1082 | 35 |
clientLoop :: Handle -> TChan [String] -> IO () |
877 | 36 |
clientLoop handle chan = |
1082 | 37 |
listenLoop handle [] chan |
877 | 38 |
`catch` (const $ clientOff >> return ()) |
1309 | 39 |
where clientOff = atomically $ writeTChan chan ["QUIT"] -- if the client disconnects, we perform as if it sent QUIT message |
877 | 40 |
|
1307 | 41 |
|
1306 | 42 |
sendAnswers [] _ clients _ = return clients |
43 |
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do |
|
44 |
let recipients = handlesFunc client clients rooms |
|
1307 | 45 |
putStrLn ("< " ++ (show answer) ++ " (" ++ (show $ length recipients) ++ " recipients)") |
1306 | 46 |
|
47 |
clHandles' <- forM recipients $ |
|
1307 | 48 |
\ch -> Control.Exception.handle (\e -> putStrLn (show e) >> hClose ch >> return [ch]) $ |
1309 | 49 |
if (not $ null answer) && (head answer == "off") then hClose ch >> return [ch] else -- probably client with exception, don't send him anything |
1307 | 50 |
do |
1306 | 51 |
forM_ answer (\str -> hPutStrLn ch str) |
52 |
hPutStrLn ch "" |
|
53 |
hFlush ch |
|
54 |
if (not $ null answer) && (head answer == "BYE") then hClose ch >> return [ch] else return [] |
|
55 |
||
56 |
let mclients = remove clients $ concat clHandles' |
|
57 |
||
58 |
sendAnswers answers client mclients rooms |
|
59 |
where |
|
1307 | 60 |
remove list rmClHandles = deleteFirstsBy2t (\ a b -> (Miscutils.handle a) == b) list rmClHandles |
1306 | 61 |
|
62 |
||
889 | 63 |
mainLoop :: Socket -> TChan ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO () |
64 |
mainLoop servSock acceptChan clients rooms = do |
|
877 | 65 |
r <- atomically $ (Left `fmap` readTChan acceptChan) `orElse` (Right `fmap` tselect clients) |
66 |
case r of |
|
889 | 67 |
Left ci -> do |
68 |
mainLoop servSock acceptChan (ci:clients) rooms |
|
1082 | 69 |
Right (cmd, client) -> do |
1302 | 70 |
putStrLn ("> " ++ show cmd) |
1307 | 71 |
|
1305 | 72 |
let (clientsFunc, roomsFunc, answers) = handleCmd client clients rooms $ cmd |
1082 | 73 |
let mrooms = roomsFunc rooms |
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
74 |
let mclients = (clientsFunc clients) |
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
75 |
let mclient = fromMaybe client $ find (== client) mclients |
1305 | 76 |
|
1308
d5dcd6cfa5e2
Fix another server failure (when second client in room disconnects)
unc0rr
parents:
1307
diff
changeset
|
77 |
clientsIn <- sendAnswers answers mclient mclients mrooms |
1306 | 78 |
|
1307 | 79 |
mainLoop servSock acceptChan clientsIn mrooms |
80 |
||
877 | 81 |
|
82 |
startServer serverSocket = do |
|
83 |
acceptChan <- atomically newTChan |
|
84 |
forkIO $ acceptLoop serverSocket acceptChan |
|
889 | 85 |
mainLoop serverSocket acceptChan [] [] |
877 | 86 |
|
1307 | 87 |
|
878 | 88 |
main = withSocketsDo $ do |
877 | 89 |
serverSocket <- listenOn $ Service "hedgewars" |
90 |
startServer serverSocket `finally` sClose serverSocket |