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