author | unc0rr |
Sat, 14 Feb 2009 16:54:28 +0000 | |
changeset 1795 | 2457fcc0dcd9 |
parent 1755 | cfb442f6a149 |
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 |
||
1755 | 5 |
import qualified Network |
6 |
import Network.Socket |
|
1370 | 7 |
import IO |
8 |
import System.IO |
|
9 |
import Control.Concurrent |
|
10 |
import Control.Concurrent.STM |
|
1510 | 11 |
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
|
12 |
import Control.Monad |
1391 | 13 |
import Maybe (fromMaybe, isJust, fromJust) |
1370 | 14 |
import Data.List |
15 |
import Miscutils |
|
16 |
import HWProto |
|
17 |
import Opts |
|
1478 | 18 |
import Data.Time |
1397 | 19 |
|
1398 | 20 |
#if !defined(mingw32_HOST_OS) |
1396 | 21 |
import System.Posix |
1397 | 22 |
#endif |
23 |
||
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 = |
1558
3370b7ffeb5c
- Send previous moves info to newly connected client when it joins a room with already started game
unc0rr
parents:
1514
diff
changeset
|
43 |
Control.Exception.handle (\(_ :: Exception) -> putStrLn "exception on connect" >> acceptLoop servSock acceptChan) $ |
1483 | 44 |
do |
1755 | 45 |
(cHandle, host, _) <- Network.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 |
|
1598 | 55 |
atomically $ writeTChan acceptChan |
56 |
(ClientInfo |
|
57 |
cChan |
|
58 |
sendChan |
|
59 |
cHandle |
|
60 |
host |
|
61 |
currentTime |
|
62 |
"" |
|
63 |
0 |
|
64 |
"" |
|
65 |
False |
|
66 |
False |
|
67 |
False |
|
68 |
False) |
|
69 |
||
1469 | 70 |
atomically $ writeTChan cChan ["ASKME"] |
1370 | 71 |
acceptLoop servSock acceptChan |
72 |
||
73 |
||
74 |
listenLoop :: Handle -> [String] -> TChan [String] -> IO () |
|
75 |
listenLoop handle buf chan = do |
|
76 |
str <- hGetLine handle |
|
77 |
if str == "" then do |
|
78 |
atomically $ writeTChan chan buf |
|
79 |
listenLoop handle [] chan |
|
80 |
else |
|
81 |
listenLoop handle (buf ++ [str]) chan |
|
82 |
||
83 |
||
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
84 |
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
|
85 |
clientRecvLoop handle chan = |
1370 | 86 |
listenLoop handle [] chan |
1478 | 87 |
`catch` (\e -> (clientOff $ show e) >> return ()) |
88 |
where clientOff msg = atomically $ writeTChan chan ["QUIT", msg] -- if the client disconnects, we perform as if it sent QUIT message |
|
1370 | 89 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
90 |
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
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
(\(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
|
95 |
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
|
96 |
hPutStrLn handle "" |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
97 |
hFlush handle |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
98 |
return $ isQuit answer |
1370 | 99 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
100 |
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
|
101 |
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
|
102 |
else |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
103 |
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
|
104 |
|
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
105 |
where |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
106 |
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
|
107 |
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
|
108 |
|
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
109 |
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
|
110 |
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do |
1370 | 111 |
let recipients = handlesFunc client clients rooms |
1476 | 112 |
--unless (null recipients) $ putStrLn ("< " ++ (show answer)) |
1478 | 113 |
when (head answer == "NICK") $ putStrLn (show answer) |
1370 | 114 |
|
115 |
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
|
116 |
\ch -> |
1370 | 117 |
do |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
118 |
atomically $ writeTChan (sendChan ch) answer |
1466 | 119 |
if head answer == "BYE" then return [ch] else return [] |
1370 | 120 |
|
1476 | 121 |
let outHandles = concat clHandles' |
1478 | 122 |
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
|
123 |
|
1686 | 124 |
let mclients = clients \\ outHandles |
1370 | 125 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
126 |
sendAnswers answers client mclients rooms |
1370 | 127 |
|
128 |
||
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
129 |
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
|
130 |
reactCmd serverInfo cmd client clients rooms = do |
1473 | 131 |
--putStrLn ("> " ++ show cmd) |
1391 | 132 |
|
1492 | 133 |
let (clientsFunc, roomsFunc, answerFuncs) = handleCmd client clients rooms $ cmd |
1391 | 134 |
let mrooms = roomsFunc rooms |
135 |
let mclients = (clientsFunc clients) |
|
136 |
let mclient = fromMaybe client $ find (== client) mclients |
|
1492 | 137 |
let answers = map (\x -> x serverInfo) answerFuncs |
1391 | 138 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
139 |
clientsIn <- sendAnswers answers mclient mclients mrooms |
1483 | 140 |
mapM_ (\cl -> atomically $ writeTChan (chan cl) ["QUIT", "Kicked"]) $ filter forceQuit $ clientsIn |
1598 | 141 |
|
142 |
let clientsFinal = map (\cl -> if partRoom cl then cl{room = [], partRoom = False} else cl) clientsIn |
|
143 |
return (clientsFinal, mrooms) |
|
1391 | 144 |
|
145 |
||
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
146 |
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
|
147 |
mainLoop serverInfo acceptChan messagesChan clients rooms = do |
1473 | 148 |
r <- atomically $ |
149 |
(Accept `fmap` readTChan acceptChan) `orElse` |
|
150 |
(ClientMessage `fmap` tselect clients) `orElse` |
|
151 |
(CoreMessage `fmap` readTChan messagesChan) |
|
1484 | 152 |
|
1370 | 153 |
case r of |
1478 | 154 |
Accept ci -> do |
155 |
let sameHostClients = filter (\cl -> host ci == host cl) clients |
|
1514 | 156 |
let haveJustConnected = not $ null $ filter (\cl -> connectTime ci `diffUTCTime` connectTime cl <= 25) sameHostClients |
1478 | 157 |
|
158 |
when haveJustConnected $ do |
|
1483 | 159 |
atomically $ do |
160 |
writeTChan (chan ci) ["QUIT", "Reconnected too fast"] |
|
1493 | 161 |
|
162 |
currentTime <- getCurrentTime |
|
163 |
let newServerInfo = serverInfo{ |
|
164 |
loginsNumber = loginsNumber serverInfo + 1, |
|
165 |
lastHourUsers = currentTime : lastHourUsers serverInfo |
|
166 |
} |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
167 |
mainLoop newServerInfo acceptChan messagesChan (clients ++ [ci]) rooms |
1484 | 168 |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
169 |
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
|
170 |
(clientsIn, mrooms) <- reactCmd serverInfo cmd client clients rooms |
1370 | 171 |
|
1385 | 172 |
let hadRooms = (not $ null rooms) && (null mrooms) |
1492 | 173 |
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
|
174 |
mainLoop serverInfo acceptChan messagesChan clientsIn mrooms |
1484 | 175 |
|
1493 | 176 |
CoreMessage msg -> case msg of |
177 |
["PING"] -> |
|
178 |
if not $ null $ clients then |
|
179 |
do |
|
180 |
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
|
181 |
(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
|
182 |
mainLoop serverInfo acceptChan messagesChan clientsIn mrooms |
1493 | 183 |
else |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
184 |
mainLoop serverInfo acceptChan messagesChan clients rooms |
1493 | 185 |
["MINUTELY"] -> do |
186 |
currentTime <- getCurrentTime |
|
187 |
let newServerInfo = serverInfo{ |
|
1494 | 188 |
lastHourUsers = filter (\t -> currentTime `diffUTCTime` t < 3600) $ lastHourUsers serverInfo |
1493 | 189 |
} |
1755 | 190 |
atomically $ swapTMVar |
191 |
(stats serverInfo) |
|
192 |
(StatisticsInfo |
|
193 |
(length clients) |
|
194 |
(length rooms) |
|
195 |
) |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
196 |
mainLoop newServerInfo acceptChan messagesChan clients rooms |
1370 | 197 |
|
1492 | 198 |
startServer :: ServerInfo -> Socket -> IO() |
199 |
startServer serverInfo serverSocket = do |
|
1370 | 200 |
acceptChan <- atomically newTChan |
201 |
forkIO $ acceptLoop serverSocket acceptChan |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
202 |
|
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
203 |
messagesChan <- atomically newTChan |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
204 |
forkIO $ messagesLoop messagesChan |
1493 | 205 |
forkIO $ timerLoop messagesChan |
1492 | 206 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
207 |
mainLoop serverInfo acceptChan messagesChan [] [] |
1370 | 208 |
|
1755 | 209 |
socketEcho :: Socket -> TMVar StatisticsInfo -> IO () |
210 |
socketEcho sock stats = do |
|
211 |
(msg, recv_count, client) <- recvFrom sock 128 |
|
212 |
currStats <- atomically $ readTMVar stats |
|
213 |
send_count <- sendTo sock (statsMsg1 currStats) client |
|
214 |
socketEcho sock stats |
|
215 |
where |
|
216 |
statsMsg1 currStats = (show $ playersNumber currStats) ++ "," ++ (show $ roomsNumber currStats) |
|
217 |
||
218 |
startUDPserver :: TMVar StatisticsInfo -> IO ThreadId |
|
219 |
startUDPserver stats = do |
|
220 |
sock <- socket AF_INET Datagram 0 |
|
221 |
bindSocket sock (SockAddrInet 46632 iNADDR_ANY) |
|
222 |
forkIO $ socketEcho sock stats |
|
1370 | 223 |
|
224 |
main = withSocketsDo $ do |
|
1398 | 225 |
#if !defined(mingw32_HOST_OS) |
1396 | 226 |
installHandler sigPIPE Ignore Nothing; |
1397 | 227 |
#endif |
1755 | 228 |
|
229 |
stats <- atomically $ newTMVar (StatisticsInfo 0 0) |
|
230 |
serverInfo <- getOpts $ newServerInfo stats |
|
1492 | 231 |
|
232 |
putStrLn $ "Listening on port " ++ show (listenPort serverInfo) |
|
1755 | 233 |
serverSocket <- Network.listenOn $ Network.PortNumber (listenPort serverInfo) |
234 |
||
235 |
startUDPserver stats |
|
1492 | 236 |
startServer serverInfo serverSocket `finally` sClose serverSocket |