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