author | unc0rr |
Fri, 05 Dec 2008 21:34:09 +0000 | |
changeset 1537 | d34ef0fb003d |
parent 1514 | c4170faf7b0a |
child 1558 | 3370b7ffeb5c |
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 |
||
1514 | 23 |
-- #define IOException Exception |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
24 |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
25 |
data Messages = |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
26 |
Accept ClientInfo |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
27 |
| ClientMessage ([String], ClientInfo) |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
28 |
| CoreMessage [String] |
1493 | 29 |
| TimerTick |
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
30 |
|
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
31 |
messagesLoop :: TChan [String] -> IO() |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
32 |
messagesLoop messagesChan = forever $ do |
1493 | 33 |
threadDelay (25 * 10^6) -- 25 seconds |
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
34 |
atomically $ writeTChan messagesChan ["PING"] |
1370 | 35 |
|
1493 | 36 |
timerLoop :: TChan [String] -> IO() |
37 |
timerLoop messagesChan = forever $ do |
|
38 |
threadDelay (60 * 10^6) -- 60 seconds |
|
39 |
atomically $ writeTChan messagesChan ["MINUTELY"] |
|
40 |
||
1370 | 41 |
acceptLoop :: Socket -> TChan ClientInfo -> IO () |
1483 | 42 |
acceptLoop servSock acceptChan = |
1511 | 43 |
Control.Exception.handle (\(_ :: IOException) -> putStrLn "exception on connect" >> acceptLoop servSock acceptChan) $ |
1483 | 44 |
do |
1478 | 45 |
(cHandle, host, _) <- accept servSock |
1483 | 46 |
|
1478 | 47 |
currentTime <- getCurrentTime |
1481 | 48 |
putStrLn $ (show currentTime) ++ " new client: " ++ host |
1483 | 49 |
|
1370 | 50 |
cChan <- atomically newTChan |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
51 |
sendChan <- atomically newTChan |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
52 |
forkIO $ clientRecvLoop cHandle cChan |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
53 |
forkIO $ clientSendLoop cHandle cChan sendChan |
1483 | 54 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
55 |
atomically $ writeTChan acceptChan (ClientInfo cChan sendChan 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 |
||
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
70 |
clientRecvLoop :: Handle -> TChan [String] -> IO () |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
71 |
clientRecvLoop handle chan = |
1370 | 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 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
76 |
clientSendLoop :: Handle -> TChan[String] -> TChan[String] -> IO() |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
77 |
clientSendLoop handle clChan chan = do |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
78 |
answer <- atomically $ readTChan chan |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
79 |
doClose <- Control.Exception.handle |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
80 |
(\(e :: IOException) -> if isQuit answer then return True else sendQuit e >> return False) $ do |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
81 |
forM_ answer (\str -> hPutStrLn handle str) |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
82 |
hPutStrLn handle "" |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
83 |
hFlush handle |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
84 |
return $ isQuit answer |
1370 | 85 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
86 |
if doClose then |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
87 |
Control.Exception.handle (\(_ :: IOException) -> putStrLn "error on hClose") $ hClose handle |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
88 |
else |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
89 |
clientSendLoop handle clChan chan |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
90 |
|
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
91 |
where |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
92 |
sendQuit e = atomically $ writeTChan clChan ["QUIT", show e] |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
93 |
isQuit answer = head answer == "BYE" |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
94 |
|
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
95 |
sendAnswers [] _ clients _ = return clients |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
96 |
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do |
1370 | 97 |
let recipients = handlesFunc client clients rooms |
1476 | 98 |
--unless (null recipients) $ putStrLn ("< " ++ (show answer)) |
1478 | 99 |
when (head answer == "NICK") $ putStrLn (show answer) |
1370 | 100 |
|
101 |
clHandles' <- forM recipients $ |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
102 |
\ch -> |
1370 | 103 |
do |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
104 |
atomically $ writeTChan (sendChan ch) answer |
1466 | 105 |
if head answer == "BYE" then return [ch] else return [] |
1370 | 106 |
|
1476 | 107 |
let outHandles = concat clHandles' |
1478 | 108 |
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
|
109 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
110 |
let mclients = deleteFirstsBy (==) clients outHandles |
1370 | 111 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
112 |
sendAnswers answers client mclients rooms |
1370 | 113 |
|
114 |
||
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
115 |
reactCmd :: ServerInfo -> [String] -> ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ([ClientInfo], [RoomInfo]) |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
116 |
reactCmd serverInfo cmd client clients rooms = do |
1473 | 117 |
--putStrLn ("> " ++ show cmd) |
1391 | 118 |
|
1492 | 119 |
let (clientsFunc, roomsFunc, answerFuncs) = handleCmd client clients rooms $ cmd |
1391 | 120 |
let mrooms = roomsFunc rooms |
121 |
let mclients = (clientsFunc clients) |
|
122 |
let mclient = fromMaybe client $ find (== client) mclients |
|
1492 | 123 |
let answers = map (\x -> x serverInfo) answerFuncs |
1391 | 124 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
125 |
clientsIn <- sendAnswers answers mclient mclients mrooms |
1483 | 126 |
mapM_ (\cl -> atomically $ writeTChan (chan cl) ["QUIT", "Kicked"]) $ filter forceQuit $ clientsIn |
1474 | 127 |
|
1483 | 128 |
return (clientsIn, mrooms) |
1391 | 129 |
|
130 |
||
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
131 |
mainLoop :: ServerInfo -> TChan ClientInfo -> TChan [String] -> [ClientInfo] -> [RoomInfo] -> IO () |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
132 |
mainLoop serverInfo acceptChan messagesChan clients rooms = do |
1473 | 133 |
r <- atomically $ |
134 |
(Accept `fmap` readTChan acceptChan) `orElse` |
|
135 |
(ClientMessage `fmap` tselect clients) `orElse` |
|
136 |
(CoreMessage `fmap` readTChan messagesChan) |
|
1484 | 137 |
|
1370 | 138 |
case r of |
1478 | 139 |
Accept ci -> do |
140 |
let sameHostClients = filter (\cl -> host ci == host cl) clients |
|
1514 | 141 |
let haveJustConnected = not $ null $ filter (\cl -> connectTime ci `diffUTCTime` connectTime cl <= 25) sameHostClients |
1478 | 142 |
|
143 |
when haveJustConnected $ do |
|
1483 | 144 |
atomically $ do |
145 |
writeTChan (chan ci) ["QUIT", "Reconnected too fast"] |
|
1493 | 146 |
|
147 |
currentTime <- getCurrentTime |
|
148 |
let newServerInfo = serverInfo{ |
|
149 |
loginsNumber = loginsNumber serverInfo + 1, |
|
150 |
lastHourUsers = currentTime : lastHourUsers serverInfo |
|
151 |
} |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
152 |
mainLoop newServerInfo acceptChan messagesChan (clients ++ [ci]) rooms |
1484 | 153 |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
154 |
ClientMessage (cmd, client) -> do |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
155 |
(clientsIn, mrooms) <- reactCmd serverInfo cmd client clients rooms |
1370 | 156 |
|
1385 | 157 |
let hadRooms = (not $ null rooms) && (null mrooms) |
1492 | 158 |
in unless ((not $ isDedicated serverInfo) && ((null clientsIn) || hadRooms)) $ |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
159 |
mainLoop serverInfo acceptChan messagesChan clientsIn mrooms |
1484 | 160 |
|
1493 | 161 |
CoreMessage msg -> case msg of |
162 |
["PING"] -> |
|
163 |
if not $ null $ clients then |
|
164 |
do |
|
165 |
let client = head clients -- don't care |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
166 |
(clientsIn, mrooms) <- reactCmd serverInfo msg client clients rooms |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
167 |
mainLoop serverInfo acceptChan messagesChan clientsIn mrooms |
1493 | 168 |
else |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
169 |
mainLoop serverInfo acceptChan messagesChan clients rooms |
1493 | 170 |
["MINUTELY"] -> do |
171 |
currentTime <- getCurrentTime |
|
172 |
let newServerInfo = serverInfo{ |
|
1494 | 173 |
lastHourUsers = filter (\t -> currentTime `diffUTCTime` t < 3600) $ lastHourUsers serverInfo |
1493 | 174 |
} |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
175 |
mainLoop newServerInfo acceptChan messagesChan clients rooms |
1370 | 176 |
|
1492 | 177 |
startServer :: ServerInfo -> Socket -> IO() |
178 |
startServer serverInfo serverSocket = do |
|
1370 | 179 |
acceptChan <- atomically newTChan |
180 |
forkIO $ acceptLoop serverSocket acceptChan |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
181 |
|
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
182 |
messagesChan <- atomically newTChan |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
183 |
forkIO $ messagesLoop messagesChan |
1493 | 184 |
forkIO $ timerLoop messagesChan |
1492 | 185 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
186 |
mainLoop serverInfo acceptChan messagesChan [] [] |
1370 | 187 |
|
188 |
||
189 |
main = withSocketsDo $ do |
|
1398 | 190 |
#if !defined(mingw32_HOST_OS) |
1396 | 191 |
installHandler sigPIPE Ignore Nothing; |
1397 | 192 |
#endif |
1493 | 193 |
serverInfo <- getOpts $ newServerInfo |
1492 | 194 |
|
195 |
putStrLn $ "Listening on port " ++ show (listenPort serverInfo) |
|
1493 | 196 |
serverSocket <- listenOn $ PortNumber (listenPort serverInfo) |
1492 | 197 |
|
198 |
startServer serverInfo serverSocket `finally` sClose serverSocket |