author | unc0rr |
Thu, 20 Nov 2008 15:18:38 +0000 | |
changeset 1503 | a40b871d506b |
parent 1502 | db1f1dd12321 |
child 1508 | ef093f31ced1 |
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 |
||
1502
db1f1dd12321
Add new thread which would close handles for us (fixes forced disconnection problem)
unc0rr
parents:
1500
diff
changeset
|
38 |
socketCloseLoop :: TChan Handle -> IO() |
db1f1dd12321
Add new thread which would close handles for us (fixes forced disconnection problem)
unc0rr
parents:
1500
diff
changeset
|
39 |
socketCloseLoop closingChan = forever $ do |
db1f1dd12321
Add new thread which would close handles for us (fixes forced disconnection problem)
unc0rr
parents:
1500
diff
changeset
|
40 |
h <- atomically $ readTChan closingChan |
db1f1dd12321
Add new thread which would close handles for us (fixes forced disconnection problem)
unc0rr
parents:
1500
diff
changeset
|
41 |
Control.Exception.handle (const $ putStrLn "error on hClose") $ hClose h |
db1f1dd12321
Add new thread which would close handles for us (fixes forced disconnection problem)
unc0rr
parents:
1500
diff
changeset
|
42 |
|
1370 | 43 |
acceptLoop :: Socket -> TChan ClientInfo -> IO () |
1483 | 44 |
acceptLoop servSock acceptChan = |
45 |
Control.Exception.handle (const $ putStrLn "exception on connect" >> acceptLoop servSock acceptChan) $ |
|
46 |
do |
|
1478 | 47 |
(cHandle, host, _) <- accept servSock |
1483 | 48 |
|
1478 | 49 |
currentTime <- getCurrentTime |
1481 | 50 |
putStrLn $ (show currentTime) ++ " new client: " ++ host |
1483 | 51 |
|
1370 | 52 |
cChan <- atomically newTChan |
53 |
forkIO $ clientLoop cHandle cChan |
|
1483 | 54 |
|
55 |
atomically $ writeTChan acceptChan (ClientInfo cChan cHandle host currentTime "" 0 "" False False False) |
|
1469 | 56 |
atomically $ writeTChan cChan ["ASKME"] |
1370 | 57 |
acceptLoop servSock acceptChan |
58 |
||
59 |
||
60 |
listenLoop :: Handle -> [String] -> TChan [String] -> IO () |
|
61 |
listenLoop handle buf chan = do |
|
62 |
str <- hGetLine handle |
|
63 |
if str == "" then do |
|
64 |
atomically $ writeTChan chan buf |
|
65 |
listenLoop handle [] chan |
|
66 |
else |
|
67 |
listenLoop handle (buf ++ [str]) chan |
|
68 |
||
69 |
||
70 |
clientLoop :: Handle -> TChan [String] -> IO () |
|
71 |
clientLoop handle chan = |
|
72 |
listenLoop handle [] chan |
|
1478 | 73 |
`catch` (\e -> (clientOff $ show e) >> return ()) |
74 |
where clientOff msg = atomically $ writeTChan chan ["QUIT", msg] -- if the client disconnects, we perform as if it sent QUIT message |
|
1370 | 75 |
|
76 |
||
1502
db1f1dd12321
Add new thread which would close handles for us (fixes forced disconnection problem)
unc0rr
parents:
1500
diff
changeset
|
77 |
sendAnswers _ [] _ clients _ = return clients |
db1f1dd12321
Add new thread which would close handles for us (fixes forced disconnection problem)
unc0rr
parents:
1500
diff
changeset
|
78 |
sendAnswers closingChan ((handlesFunc, answer):answers) client clients rooms = do |
1370 | 79 |
let recipients = handlesFunc client clients rooms |
1476 | 80 |
--unless (null recipients) $ putStrLn ("< " ++ (show answer)) |
1478 | 81 |
when (head answer == "NICK") $ putStrLn (show answer) |
1370 | 82 |
|
83 |
clHandles' <- forM recipients $ |
|
1468 | 84 |
\ch -> Control.Exception.handle |
1478 | 85 |
(\e -> if head answer == "BYE" then |
1468 | 86 |
return [ch] |
87 |
else |
|
1478 | 88 |
atomically $ writeTChan (chan $ fromJust $ clientByHandle ch clients) ["QUIT", show e] >> return [] -- cannot just remove |
1468 | 89 |
) $ |
1370 | 90 |
do |
91 |
forM_ answer (\str -> hPutStrLn ch str) |
|
92 |
hPutStrLn ch "" |
|
1500 | 93 |
hFlush ch |
1466 | 94 |
if head answer == "BYE" then return [ch] else return [] |
1370 | 95 |
|
1476 | 96 |
let outHandles = concat clHandles' |
1478 | 97 |
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
|
98 |
|
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 |
-- 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
|
100 |
mapM_ (\ch -> atomically $ writeTChan closingChan ch) outHandles |
1476 | 101 |
let mclients = remove clients outHandles |
1370 | 102 |
|
1502
db1f1dd12321
Add new thread which would close handles for us (fixes forced disconnection problem)
unc0rr
parents:
1500
diff
changeset
|
103 |
sendAnswers closingChan answers client mclients rooms |
1370 | 104 |
where |
105 |
remove list rmClHandles = deleteFirstsBy2t (\ a b -> (Miscutils.handle a) == b) list rmClHandles |
|
106 |
||
107 |
||
1502
db1f1dd12321
Add new thread which would close handles for us (fixes forced disconnection problem)
unc0rr
parents:
1500
diff
changeset
|
108 |
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
|
109 |
reactCmd serverInfo closingChan cmd client clients rooms = do |
1473 | 110 |
--putStrLn ("> " ++ show cmd) |
1391 | 111 |
|
1492 | 112 |
let (clientsFunc, roomsFunc, answerFuncs) = handleCmd client clients rooms $ cmd |
1391 | 113 |
let mrooms = roomsFunc rooms |
114 |
let mclients = (clientsFunc clients) |
|
115 |
let mclient = fromMaybe client $ find (== client) mclients |
|
1492 | 116 |
let answers = map (\x -> x serverInfo) answerFuncs |
1391 | 117 |
|
1502
db1f1dd12321
Add new thread which would close handles for us (fixes forced disconnection problem)
unc0rr
parents:
1500
diff
changeset
|
118 |
clientsIn <- sendAnswers closingChan answers mclient mclients mrooms |
1483 | 119 |
mapM_ (\cl -> atomically $ writeTChan (chan cl) ["QUIT", "Kicked"]) $ filter forceQuit $ clientsIn |
1474 | 120 |
|
1483 | 121 |
return (clientsIn, mrooms) |
1391 | 122 |
|
123 |
||
1502
db1f1dd12321
Add new thread which would close handles for us (fixes forced disconnection problem)
unc0rr
parents:
1500
diff
changeset
|
124 |
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
|
125 |
mainLoop serverInfo acceptChan messagesChan closingChan clients rooms = do |
1473 | 126 |
r <- atomically $ |
127 |
(Accept `fmap` readTChan acceptChan) `orElse` |
|
128 |
(ClientMessage `fmap` tselect clients) `orElse` |
|
129 |
(CoreMessage `fmap` readTChan messagesChan) |
|
1484 | 130 |
|
1370 | 131 |
case r of |
1478 | 132 |
Accept ci -> do |
133 |
let sameHostClients = filter (\cl -> host ci == host cl) clients |
|
1502
db1f1dd12321
Add new thread which would close handles for us (fixes forced disconnection problem)
unc0rr
parents:
1500
diff
changeset
|
134 |
let haveJustConnected = False--not $ null $ filter (\cl -> connectTime ci `diffUTCTime` connectTime cl <= 25) sameHostClients |
1478 | 135 |
|
136 |
when haveJustConnected $ do |
|
1483 | 137 |
atomically $ do |
138 |
--writeTChan (chan ci) ["ERROR", "Reconnected too fast"] |
|
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 |