author | unc0rr |
Fri, 31 Oct 2008 20:48:09 +0000 | |
changeset 1456 | 2539dd079be1 |
parent 1403 | b8c921ed0f13 |
child 1461 | 87e5a6c3882c |
permissions | -rw-r--r-- |
1370 | 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 (setUncaughtExceptionHandler, handle, finally) |
|
1384 | 9 |
import Control.Monad (forM, forM_, filterM, liftM, when, unless) |
1391 | 10 |
import Maybe (fromMaybe, isJust, fromJust) |
1370 | 11 |
import Data.List |
12 |
import Miscutils |
|
13 |
import HWProto |
|
14 |
import Opts |
|
1397 | 15 |
|
1398 | 16 |
#if !defined(mingw32_HOST_OS) |
1396 | 17 |
import System.Posix |
1397 | 18 |
#endif |
19 |
||
1370 | 20 |
|
21 |
acceptLoop :: Socket -> TChan ClientInfo -> IO () |
|
22 |
acceptLoop servSock acceptChan = do |
|
23 |
(cHandle, host, port) <- accept servSock |
|
1384 | 24 |
hPutStrLn cHandle "CONNECTED\n" |
25 |
hFlush cHandle |
|
1370 | 26 |
cChan <- atomically newTChan |
27 |
forkIO $ clientLoop cHandle cChan |
|
1403 | 28 |
atomically $ writeTChan acceptChan (ClientInfo cChan cHandle "" 0 "" False False False) |
1370 | 29 |
acceptLoop servSock acceptChan |
30 |
||
31 |
||
32 |
listenLoop :: Handle -> [String] -> TChan [String] -> IO () |
|
33 |
listenLoop handle buf chan = do |
|
34 |
str <- hGetLine handle |
|
35 |
if str == "" then do |
|
36 |
atomically $ writeTChan chan buf |
|
37 |
listenLoop handle [] chan |
|
38 |
else |
|
39 |
listenLoop handle (buf ++ [str]) chan |
|
40 |
||
41 |
||
42 |
clientLoop :: Handle -> TChan [String] -> IO () |
|
43 |
clientLoop handle chan = |
|
44 |
listenLoop handle [] chan |
|
45 |
`catch` (const $ clientOff >> return ()) |
|
46 |
where clientOff = atomically $ writeTChan chan ["QUIT"] -- if the client disconnects, we perform as if it sent QUIT message |
|
47 |
||
48 |
||
49 |
sendAnswers [] _ clients _ = return clients |
|
50 |
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do |
|
51 |
let recipients = handlesFunc client clients rooms |
|
1381 | 52 |
unless (null recipients) $ putStrLn ("< " ++ (show answer)) |
1370 | 53 |
|
54 |
clHandles' <- forM recipients $ |
|
1394
962001cfcf48
If exception is on client quit, then just remove that client
unc0rr
parents:
1393
diff
changeset
|
55 |
\ch -> Control.Exception.handle (\e -> putStrLn ("handle exception: " ++ show e) >> if head answer == "BYE" then return [ch] else return []) $ -- cannot just remove |
1370 | 56 |
do |
57 |
forM_ answer (\str -> hPutStrLn ch str) |
|
58 |
hPutStrLn ch "" |
|
59 |
hFlush ch |
|
1394
962001cfcf48
If exception is on client quit, then just remove that client
unc0rr
parents:
1393
diff
changeset
|
60 |
if head answer == "BYE" then hClose ch >> return [ch] else return [] |
1370 | 61 |
|
62 |
let mclients = remove clients $ concat clHandles' |
|
63 |
||
64 |
sendAnswers answers client mclients rooms |
|
65 |
where |
|
66 |
remove list rmClHandles = deleteFirstsBy2t (\ a b -> (Miscutils.handle a) == b) list rmClHandles |
|
67 |
||
68 |
||
1391 | 69 |
reactCmd :: [String] -> ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ([ClientInfo], [RoomInfo]) |
70 |
reactCmd cmd client clients rooms = do |
|
71 |
putStrLn ("> " ++ show cmd) |
|
72 |
||
73 |
let (clientsFunc, roomsFunc, answers) = handleCmd client clients rooms $ cmd |
|
74 |
let mrooms = roomsFunc rooms |
|
75 |
let mclients = (clientsFunc clients) |
|
76 |
let mclient = fromMaybe client $ find (== client) mclients |
|
77 |
||
78 |
clientsIn <- sendAnswers answers mclient mclients mrooms |
|
79 |
let quitClient = find forceQuit $ clientsIn |
|
80 |
if isJust quitClient then reactCmd ["QUIT"] (fromJust quitClient) clientsIn mrooms else return (clientsIn, mrooms) |
|
81 |
||
82 |
||
1370 | 83 |
mainLoop :: Socket -> TChan ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO () |
84 |
mainLoop servSock acceptChan clients rooms = do |
|
85 |
r <- atomically $ (Left `fmap` readTChan acceptChan) `orElse` (Right `fmap` tselect clients) |
|
86 |
case r of |
|
87 |
Left ci -> do |
|
88 |
mainLoop servSock acceptChan (clients ++ [ci]) rooms |
|
89 |
Right (cmd, client) -> do |
|
1391 | 90 |
(clientsIn, mrooms) <- reactCmd cmd client clients rooms |
1370 | 91 |
|
1385 | 92 |
let hadRooms = (not $ null rooms) && (null mrooms) |
93 |
in unless ((not $ isDedicated globalOptions) && ((null clientsIn) || hadRooms)) $ |
|
94 |
mainLoop servSock acceptChan clientsIn mrooms |
|
1370 | 95 |
|
96 |
||
97 |
startServer serverSocket = do |
|
98 |
acceptChan <- atomically newTChan |
|
99 |
forkIO $ acceptLoop serverSocket acceptChan |
|
100 |
mainLoop serverSocket acceptChan [] [] |
|
101 |
||
102 |
||
103 |
main = withSocketsDo $ do |
|
1398 | 104 |
#if !defined(mingw32_HOST_OS) |
1396 | 105 |
installHandler sigPIPE Ignore Nothing; |
1397 | 106 |
#endif |
1383 | 107 |
putStrLn $ "Listening on port " ++ show (listenPort globalOptions) |
108 |
serverSocket <- listenOn $ PortNumber (listenPort globalOptions) |
|
1370 | 109 |
startServer serverSocket `finally` sClose serverSocket |