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