author | unc0rr |
Mon, 17 Nov 2008 18:27:47 +0000 | |
changeset 1500 | 5721af6d73f0 |
parent 1499 | 870305c40b81 |
child 1502 | db1f1dd12321 |
permissions | -rw-r--r-- |
1497 | 1 |
{-# LANGUAGE CPP #-} |
1370 | 2 |
module Main where |
3 |
||
4 |
import Network |
|
5 |
import IO |
|
6 |
import System.IO |
|
7 |
import Control.Concurrent |
|
8 |
import Control.Concurrent.STM |
|
9 |
import Control.Exception (setUncaughtExceptionHandler, handle, finally) |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
10 |
import Control.Monad |
1391 | 11 |
import Maybe (fromMaybe, isJust, fromJust) |
1370 | 12 |
import Data.List |
13 |
import Miscutils |
|
14 |
import HWProto |
|
15 |
import Opts |
|
1478 | 16 |
import Data.Time |
1397 | 17 |
|
1398 | 18 |
#if !defined(mingw32_HOST_OS) |
1396 | 19 |
import System.Posix |
1397 | 20 |
#endif |
21 |
||
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
22 |
data Messages = |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
23 |
Accept ClientInfo |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
24 |
| ClientMessage ([String], ClientInfo) |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
25 |
| CoreMessage [String] |
1493 | 26 |
| TimerTick |
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
27 |
|
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
28 |
messagesLoop :: TChan [String] -> IO() |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
29 |
messagesLoop messagesChan = forever $ do |
1493 | 30 |
threadDelay (25 * 10^6) -- 25 seconds |
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
31 |
atomically $ writeTChan messagesChan ["PING"] |
1370 | 32 |
|
1493 | 33 |
timerLoop :: TChan [String] -> IO() |
34 |
timerLoop messagesChan = forever $ do |
|
35 |
threadDelay (60 * 10^6) -- 60 seconds |
|
36 |
atomically $ writeTChan messagesChan ["MINUTELY"] |
|
37 |
||
1370 | 38 |
acceptLoop :: Socket -> TChan ClientInfo -> IO () |
1483 | 39 |
acceptLoop servSock acceptChan = |
40 |
Control.Exception.handle (const $ putStrLn "exception on connect" >> acceptLoop servSock acceptChan) $ |
|
41 |
do |
|
1478 | 42 |
(cHandle, host, _) <- accept servSock |
1483 | 43 |
|
1478 | 44 |
currentTime <- getCurrentTime |
1481 | 45 |
putStrLn $ (show currentTime) ++ " new client: " ++ host |
1483 | 46 |
|
1370 | 47 |
cChan <- atomically newTChan |
48 |
forkIO $ clientLoop cHandle cChan |
|
1483 | 49 |
|
50 |
atomically $ writeTChan acceptChan (ClientInfo cChan cHandle host currentTime "" 0 "" False False False) |
|
1469 | 51 |
atomically $ writeTChan cChan ["ASKME"] |
1370 | 52 |
acceptLoop servSock acceptChan |
53 |
||
54 |
||
55 |
listenLoop :: Handle -> [String] -> TChan [String] -> IO () |
|
56 |
listenLoop handle buf chan = do |
|
57 |
str <- hGetLine handle |
|
58 |
if str == "" then do |
|
59 |
atomically $ writeTChan chan buf |
|
60 |
listenLoop handle [] chan |
|
61 |
else |
|
62 |
listenLoop handle (buf ++ [str]) chan |
|
63 |
||
64 |
||
65 |
clientLoop :: Handle -> TChan [String] -> IO () |
|
66 |
clientLoop handle chan = |
|
67 |
listenLoop handle [] chan |
|
1478 | 68 |
`catch` (\e -> (clientOff $ show e) >> return ()) |
69 |
where clientOff msg = atomically $ writeTChan chan ["QUIT", msg] -- if the client disconnects, we perform as if it sent QUIT message |
|
1370 | 70 |
|
71 |
||
72 |
sendAnswers [] _ clients _ = return clients |
|
73 |
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do |
|
74 |
let recipients = handlesFunc client clients rooms |
|
1476 | 75 |
--unless (null recipients) $ putStrLn ("< " ++ (show answer)) |
1478 | 76 |
when (head answer == "NICK") $ putStrLn (show answer) |
1370 | 77 |
|
78 |
clHandles' <- forM recipients $ |
|
1468 | 79 |
\ch -> Control.Exception.handle |
1478 | 80 |
(\e -> if head answer == "BYE" then |
1468 | 81 |
return [ch] |
82 |
else |
|
1478 | 83 |
atomically $ writeTChan (chan $ fromJust $ clientByHandle ch clients) ["QUIT", show e] >> return [] -- cannot just remove |
1468 | 84 |
) $ |
1370 | 85 |
do |
86 |
forM_ answer (\str -> hPutStrLn ch str) |
|
87 |
hPutStrLn ch "" |
|
1500 | 88 |
hFlush ch |
1466 | 89 |
if head answer == "BYE" then return [ch] else return [] |
1370 | 90 |
|
1476 | 91 |
let outHandles = concat clHandles' |
1478 | 92 |
unless (null outHandles) $ putStrLn ((show $ length outHandles) ++ " / " ++ (show $ length clients) ++ " : " ++ (show answer)) |
1499
870305c40b81
Don't close socket handles, just leave that job for garbage collector, as doing it manually seems to be the cause of server hangs
unc0rr
parents:
1497
diff
changeset
|
93 |
|
870305c40b81
Don't close socket handles, just leave that job for garbage collector, as doing it manually seems to be the cause of server hangs
unc0rr
parents:
1497
diff
changeset
|
94 |
-- strange, but this seems to be a bad idea to manually close these handles as it causes hangs |
870305c40b81
Don't close socket handles, just leave that job for garbage collector, as doing it manually seems to be the cause of server hangs
unc0rr
parents:
1497
diff
changeset
|
95 |
--mapM_ (\ch -> Control.Exception.handle (const $ putStrLn "error on hClose") (hClose ch)) outHandles |
1476 | 96 |
let mclients = remove clients outHandles |
1370 | 97 |
|
98 |
sendAnswers answers client mclients rooms |
|
99 |
where |
|
100 |
remove list rmClHandles = deleteFirstsBy2t (\ a b -> (Miscutils.handle a) == b) list rmClHandles |
|
101 |
||
102 |
||
1492 | 103 |
reactCmd :: ServerInfo -> [String] -> ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ([ClientInfo], [RoomInfo]) |
104 |
reactCmd serverInfo cmd client clients rooms = do |
|
1473 | 105 |
--putStrLn ("> " ++ show cmd) |
1391 | 106 |
|
1492 | 107 |
let (clientsFunc, roomsFunc, answerFuncs) = handleCmd client clients rooms $ cmd |
1391 | 108 |
let mrooms = roomsFunc rooms |
109 |
let mclients = (clientsFunc clients) |
|
110 |
let mclient = fromMaybe client $ find (== client) mclients |
|
1492 | 111 |
let answers = map (\x -> x serverInfo) answerFuncs |
1391 | 112 |
|
113 |
clientsIn <- sendAnswers answers mclient mclients mrooms |
|
1483 | 114 |
mapM_ (\cl -> atomically $ writeTChan (chan cl) ["QUIT", "Kicked"]) $ filter forceQuit $ clientsIn |
1474 | 115 |
|
1483 | 116 |
return (clientsIn, mrooms) |
1391 | 117 |
|
118 |
||
1492 | 119 |
mainLoop :: ServerInfo -> TChan ClientInfo -> TChan [String] -> [ClientInfo] -> [RoomInfo] -> IO () |
120 |
mainLoop serverInfo acceptChan messagesChan clients rooms = do |
|
1473 | 121 |
r <- atomically $ |
122 |
(Accept `fmap` readTChan acceptChan) `orElse` |
|
123 |
(ClientMessage `fmap` tselect clients) `orElse` |
|
124 |
(CoreMessage `fmap` readTChan messagesChan) |
|
1484 | 125 |
|
1370 | 126 |
case r of |
1478 | 127 |
Accept ci -> do |
128 |
let sameHostClients = filter (\cl -> host ci == host cl) clients |
|
1483 | 129 |
let haveJustConnected = not $ null $ filter (\cl -> connectTime ci `diffUTCTime` connectTime cl <= 25) sameHostClients |
1478 | 130 |
|
131 |
when haveJustConnected $ do |
|
1483 | 132 |
atomically $ do |
133 |
--writeTChan (chan ci) ["ERROR", "Reconnected too fast"] |
|
134 |
writeTChan (chan ci) ["QUIT", "Reconnected too fast"] |
|
1493 | 135 |
|
136 |
currentTime <- getCurrentTime |
|
137 |
let newServerInfo = serverInfo{ |
|
138 |
loginsNumber = loginsNumber serverInfo + 1, |
|
139 |
lastHourUsers = currentTime : lastHourUsers serverInfo |
|
140 |
} |
|
141 |
mainLoop newServerInfo acceptChan messagesChan (clients ++ [ci]) rooms |
|
1484 | 142 |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
143 |
ClientMessage (cmd, client) -> do |
1492 | 144 |
(clientsIn, mrooms) <- reactCmd serverInfo cmd client clients rooms |
1370 | 145 |
|
1385 | 146 |
let hadRooms = (not $ null rooms) && (null mrooms) |
1492 | 147 |
in unless ((not $ isDedicated serverInfo) && ((null clientsIn) || hadRooms)) $ |
148 |
mainLoop serverInfo acceptChan messagesChan clientsIn mrooms |
|
1484 | 149 |
|
1493 | 150 |
CoreMessage msg -> case msg of |
151 |
["PING"] -> |
|
152 |
if not $ null $ clients then |
|
153 |
do |
|
154 |
let client = head clients -- don't care |
|
155 |
(clientsIn, mrooms) <- reactCmd serverInfo msg client clients rooms |
|
156 |
mainLoop serverInfo acceptChan messagesChan clientsIn mrooms |
|
157 |
else |
|
158 |
mainLoop serverInfo acceptChan messagesChan clients rooms |
|
159 |
["MINUTELY"] -> do |
|
160 |
currentTime <- getCurrentTime |
|
161 |
let newServerInfo = serverInfo{ |
|
1494 | 162 |
lastHourUsers = filter (\t -> currentTime `diffUTCTime` t < 3600) $ lastHourUsers serverInfo |
1493 | 163 |
} |
164 |
mainLoop newServerInfo acceptChan messagesChan clients rooms |
|
1370 | 165 |
|
1492 | 166 |
startServer :: ServerInfo -> Socket -> IO() |
167 |
startServer serverInfo serverSocket = do |
|
1370 | 168 |
acceptChan <- atomically newTChan |
169 |
forkIO $ acceptLoop serverSocket acceptChan |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
170 |
|
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
171 |
messagesChan <- atomically newTChan |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
172 |
forkIO $ messagesLoop messagesChan |
1493 | 173 |
forkIO $ timerLoop messagesChan |
1492 | 174 |
|
175 |
mainLoop serverInfo acceptChan messagesChan [] [] |
|
1370 | 176 |
|
177 |
||
178 |
main = withSocketsDo $ do |
|
1398 | 179 |
#if !defined(mingw32_HOST_OS) |
1396 | 180 |
installHandler sigPIPE Ignore Nothing; |
1397 | 181 |
#endif |
1493 | 182 |
serverInfo <- getOpts $ newServerInfo |
1492 | 183 |
|
184 |
putStrLn $ "Listening on port " ++ show (listenPort serverInfo) |
|
1493 | 185 |
serverSocket <- listenOn $ PortNumber (listenPort serverInfo) |
1492 | 186 |
|
187 |
startServer serverInfo serverSocket `finally` sClose serverSocket |