author | unc0rr |
Tue, 04 Nov 2008 22:49:13 +0000 | |
changeset 1475 | bab5650fc894 |
parent 1474 | 8817adb86da6 |
child 1476 | b3b28e99570f |
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) |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
9 |
import Control.Monad |
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 |
||
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
20 |
data Messages = |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
21 |
Accept ClientInfo |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
22 |
| ClientMessage ([String], ClientInfo) |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
23 |
| CoreMessage [String] |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
24 |
|
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
25 |
messagesLoop :: TChan [String] -> IO() |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
26 |
messagesLoop messagesChan = forever $ do |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
27 |
threadDelay (30 * 10^6) -- 30 seconds |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
28 |
atomically $ writeTChan messagesChan ["PING"] |
1370 | 29 |
|
30 |
acceptLoop :: Socket -> TChan ClientInfo -> IO () |
|
1469 | 31 |
acceptLoop servSock acceptChan = Control.Exception.handle (const $ putStrLn "exception on connect" >> acceptLoop servSock acceptChan) $ do |
1370 | 32 |
(cHandle, host, port) <- accept servSock |
33 |
cChan <- atomically newTChan |
|
34 |
forkIO $ clientLoop cHandle cChan |
|
1403 | 35 |
atomically $ writeTChan acceptChan (ClientInfo cChan cHandle "" 0 "" False False False) |
1469 | 36 |
atomically $ writeTChan cChan ["ASKME"] |
1370 | 37 |
acceptLoop servSock acceptChan |
38 |
||
39 |
||
40 |
listenLoop :: Handle -> [String] -> TChan [String] -> IO () |
|
41 |
listenLoop handle buf chan = do |
|
42 |
str <- hGetLine handle |
|
43 |
if str == "" then do |
|
44 |
atomically $ writeTChan chan buf |
|
45 |
listenLoop handle [] chan |
|
46 |
else |
|
47 |
listenLoop handle (buf ++ [str]) chan |
|
48 |
||
49 |
||
50 |
clientLoop :: Handle -> TChan [String] -> IO () |
|
51 |
clientLoop handle chan = |
|
52 |
listenLoop handle [] chan |
|
53 |
`catch` (const $ clientOff >> return ()) |
|
54 |
where clientOff = atomically $ writeTChan chan ["QUIT"] -- if the client disconnects, we perform as if it sent QUIT message |
|
55 |
||
56 |
||
57 |
sendAnswers [] _ clients _ = return clients |
|
58 |
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do |
|
59 |
let recipients = handlesFunc client clients rooms |
|
1475
bab5650fc894
- Fix ConfigAsked not sending full config (leads to team divide checkbox inconsistency)
unc0rr
parents:
1474
diff
changeset
|
60 |
unless (null recipients) $ putStrLn ("< " ++ (show answer)) |
1370 | 61 |
|
62 |
clHandles' <- forM recipients $ |
|
1468 | 63 |
\ch -> Control.Exception.handle |
64 |
(\e -> putStrLn ("handle exception: " ++ show e) >> |
|
65 |
if head answer == "BYE" then |
|
66 |
return [ch] |
|
67 |
else |
|
68 |
atomically $ writeTChan (chan $ fromJust $ clientByHandle ch clients) ["QUIT"] >> return [] -- cannot just remove |
|
69 |
) $ |
|
1370 | 70 |
do |
71 |
forM_ answer (\str -> hPutStrLn ch str) |
|
72 |
hPutStrLn ch "" |
|
73 |
hFlush ch |
|
1466 | 74 |
if head answer == "BYE" then return [ch] else return [] |
1370 | 75 |
|
76 |
let mclients = remove clients $ concat clHandles' |
|
77 |
||
78 |
sendAnswers answers client mclients rooms |
|
79 |
where |
|
80 |
remove list rmClHandles = deleteFirstsBy2t (\ a b -> (Miscutils.handle a) == b) list rmClHandles |
|
81 |
||
82 |
||
1391 | 83 |
reactCmd :: [String] -> ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ([ClientInfo], [RoomInfo]) |
84 |
reactCmd cmd client clients rooms = do |
|
1473 | 85 |
--putStrLn ("> " ++ show cmd) |
1391 | 86 |
|
87 |
let (clientsFunc, roomsFunc, answers) = handleCmd client clients rooms $ cmd |
|
88 |
let mrooms = roomsFunc rooms |
|
89 |
let mclients = (clientsFunc clients) |
|
90 |
let mclient = fromMaybe client $ find (== client) mclients |
|
91 |
||
92 |
clientsIn <- sendAnswers answers mclient mclients mrooms |
|
93 |
let quitClient = find forceQuit $ clientsIn |
|
1474 | 94 |
|
1473 | 95 |
if isJust quitClient then |
96 |
reactCmd ["QUIT"] (fromJust quitClient) clientsIn mrooms |
|
1474 | 97 |
else |
1473 | 98 |
return (clientsIn, mrooms) |
1391 | 99 |
|
100 |
||
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
101 |
mainLoop :: Socket -> TChan ClientInfo -> TChan [String] -> [ClientInfo] -> [RoomInfo] -> IO () |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
102 |
mainLoop servSock acceptChan messagesChan clients rooms = do |
1473 | 103 |
r <- atomically $ |
104 |
(Accept `fmap` readTChan acceptChan) `orElse` |
|
105 |
(ClientMessage `fmap` tselect clients) `orElse` |
|
106 |
(CoreMessage `fmap` readTChan messagesChan) |
|
1370 | 107 |
case r of |
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
108 |
Accept ci -> |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
109 |
mainLoop servSock acceptChan messagesChan (clients ++ [ci]) rooms |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
110 |
ClientMessage (cmd, client) -> do |
1391 | 111 |
(clientsIn, mrooms) <- reactCmd cmd client clients rooms |
1370 | 112 |
|
1385 | 113 |
let hadRooms = (not $ null rooms) && (null mrooms) |
114 |
in unless ((not $ isDedicated globalOptions) && ((null clientsIn) || hadRooms)) $ |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
115 |
mainLoop servSock acceptChan messagesChan clientsIn mrooms |
1473 | 116 |
CoreMessage msg -> |
117 |
if not $ null $ clients then |
|
118 |
do |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
119 |
let client = head clients -- don't care |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
120 |
(clientsIn, mrooms) <- reactCmd msg client clients rooms |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
121 |
mainLoop servSock acceptChan messagesChan clientsIn mrooms |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
122 |
else |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
123 |
mainLoop servSock acceptChan messagesChan clients rooms |
1370 | 124 |
|
125 |
||
126 |
startServer serverSocket = do |
|
127 |
acceptChan <- atomically newTChan |
|
128 |
forkIO $ acceptLoop serverSocket acceptChan |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
129 |
|
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
130 |
messagesChan <- atomically newTChan |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
131 |
forkIO $ messagesLoop messagesChan |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
132 |
|
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
133 |
mainLoop serverSocket acceptChan messagesChan [] [] |
1370 | 134 |
|
135 |
||
136 |
main = withSocketsDo $ do |
|
1398 | 137 |
#if !defined(mingw32_HOST_OS) |
1396 | 138 |
installHandler sigPIPE Ignore Nothing; |
1397 | 139 |
#endif |
1383 | 140 |
putStrLn $ "Listening on port " ++ show (listenPort globalOptions) |
141 |
serverSocket <- listenOn $ PortNumber (listenPort globalOptions) |
|
1370 | 142 |
startServer serverSocket `finally` sClose serverSocket |