author | unc0rr |
Sat, 10 Jan 2009 15:48:02 +0000 | |
changeset 1636 | e528696f2177 |
parent 1598 | c853e02ed663 |
child 1686 | f42dbc52225c |
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 |
|
1598 | 54 |
atomically $ writeTChan acceptChan |
55 |
(ClientInfo |
|
56 |
cChan |
|
57 |
sendChan |
|
58 |
cHandle |
|
59 |
host |
|
60 |
currentTime |
|
61 |
"" |
|
62 |
0 |
|
63 |
"" |
|
64 |
False |
|
65 |
False |
|
66 |
False |
|
67 |
False) |
|
68 |
||
1469 | 69 |
atomically $ writeTChan cChan ["ASKME"] |
1370 | 70 |
acceptLoop servSock acceptChan |
71 |
||
72 |
||
73 |
listenLoop :: Handle -> [String] -> TChan [String] -> IO () |
|
74 |
listenLoop handle buf chan = do |
|
75 |
str <- hGetLine handle |
|
76 |
if str == "" then do |
|
77 |
atomically $ writeTChan chan buf |
|
78 |
listenLoop handle [] chan |
|
79 |
else |
|
80 |
listenLoop handle (buf ++ [str]) chan |
|
81 |
||
82 |
||
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
83 |
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
|
84 |
clientRecvLoop handle chan = |
1370 | 85 |
listenLoop handle [] chan |
1478 | 86 |
`catch` (\e -> (clientOff $ show e) >> return ()) |
87 |
where clientOff msg = atomically $ writeTChan chan ["QUIT", msg] -- if the client disconnects, we perform as if it sent QUIT message |
|
1370 | 88 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
89 |
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
|
90 |
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
|
91 |
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
|
92 |
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
|
93 |
(\(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
|
94 |
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
|
95 |
hPutStrLn handle "" |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
96 |
hFlush handle |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
97 |
return $ isQuit answer |
1370 | 98 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
99 |
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
|
100 |
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
|
101 |
else |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
102 |
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
|
103 |
|
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
104 |
where |
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
105 |
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
|
106 |
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
|
107 |
|
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
108 |
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
|
109 |
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do |
1370 | 110 |
let recipients = handlesFunc client clients rooms |
1476 | 111 |
--unless (null recipients) $ putStrLn ("< " ++ (show answer)) |
1478 | 112 |
when (head answer == "NICK") $ putStrLn (show answer) |
1370 | 113 |
|
114 |
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
|
115 |
\ch -> |
1370 | 116 |
do |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
117 |
atomically $ writeTChan (sendChan ch) answer |
1466 | 118 |
if head answer == "BYE" then return [ch] else return [] |
1370 | 119 |
|
1476 | 120 |
let outHandles = concat clHandles' |
1478 | 121 |
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
|
122 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
123 |
let mclients = deleteFirstsBy (==) clients outHandles |
1370 | 124 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
125 |
sendAnswers answers client mclients rooms |
1370 | 126 |
|
127 |
||
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
128 |
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
|
129 |
reactCmd serverInfo cmd client clients rooms = do |
1473 | 130 |
--putStrLn ("> " ++ show cmd) |
1391 | 131 |
|
1492 | 132 |
let (clientsFunc, roomsFunc, answerFuncs) = handleCmd client clients rooms $ cmd |
1391 | 133 |
let mrooms = roomsFunc rooms |
134 |
let mclients = (clientsFunc clients) |
|
135 |
let mclient = fromMaybe client $ find (== client) mclients |
|
1492 | 136 |
let answers = map (\x -> x serverInfo) answerFuncs |
1391 | 137 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
138 |
clientsIn <- sendAnswers answers mclient mclients mrooms |
1483 | 139 |
mapM_ (\cl -> atomically $ writeTChan (chan cl) ["QUIT", "Kicked"]) $ filter forceQuit $ clientsIn |
1598 | 140 |
|
141 |
let clientsFinal = map (\cl -> if partRoom cl then cl{room = [], partRoom = False} else cl) clientsIn |
|
142 |
return (clientsFinal, mrooms) |
|
1391 | 143 |
|
144 |
||
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
145 |
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
|
146 |
mainLoop serverInfo acceptChan messagesChan clients rooms = do |
1473 | 147 |
r <- atomically $ |
148 |
(Accept `fmap` readTChan acceptChan) `orElse` |
|
149 |
(ClientMessage `fmap` tselect clients) `orElse` |
|
150 |
(CoreMessage `fmap` readTChan messagesChan) |
|
1484 | 151 |
|
1370 | 152 |
case r of |
1478 | 153 |
Accept ci -> do |
154 |
let sameHostClients = filter (\cl -> host ci == host cl) clients |
|
1514 | 155 |
let haveJustConnected = not $ null $ filter (\cl -> connectTime ci `diffUTCTime` connectTime cl <= 25) sameHostClients |
1478 | 156 |
|
157 |
when haveJustConnected $ do |
|
1483 | 158 |
atomically $ do |
159 |
writeTChan (chan ci) ["QUIT", "Reconnected too fast"] |
|
1493 | 160 |
|
161 |
currentTime <- getCurrentTime |
|
162 |
let newServerInfo = serverInfo{ |
|
163 |
loginsNumber = loginsNumber serverInfo + 1, |
|
164 |
lastHourUsers = currentTime : lastHourUsers serverInfo |
|
165 |
} |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
166 |
mainLoop newServerInfo acceptChan messagesChan (clients ++ [ci]) rooms |
1484 | 167 |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
168 |
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
|
169 |
(clientsIn, mrooms) <- reactCmd serverInfo cmd client clients rooms |
1370 | 170 |
|
1385 | 171 |
let hadRooms = (not $ null rooms) && (null mrooms) |
1492 | 172 |
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
|
173 |
mainLoop serverInfo acceptChan messagesChan clientsIn mrooms |
1484 | 174 |
|
1493 | 175 |
CoreMessage msg -> case msg of |
176 |
["PING"] -> |
|
177 |
if not $ null $ clients then |
|
178 |
do |
|
179 |
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
|
180 |
(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
|
181 |
mainLoop serverInfo acceptChan messagesChan clientsIn mrooms |
1493 | 182 |
else |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
183 |
mainLoop serverInfo acceptChan messagesChan clients rooms |
1493 | 184 |
["MINUTELY"] -> do |
185 |
currentTime <- getCurrentTime |
|
186 |
let newServerInfo = serverInfo{ |
|
1494 | 187 |
lastHourUsers = filter (\t -> currentTime `diffUTCTime` t < 3600) $ lastHourUsers serverInfo |
1493 | 188 |
} |
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
189 |
mainLoop newServerInfo acceptChan messagesChan clients rooms |
1370 | 190 |
|
1492 | 191 |
startServer :: ServerInfo -> Socket -> IO() |
192 |
startServer serverInfo serverSocket = do |
|
1370 | 193 |
acceptChan <- atomically newTChan |
194 |
forkIO $ acceptLoop serverSocket acceptChan |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
195 |
|
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
196 |
messagesChan <- atomically newTChan |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
197 |
forkIO $ messagesLoop messagesChan |
1493 | 198 |
forkIO $ timerLoop messagesChan |
1492 | 199 |
|
1513
a35c90263e27
Refactor server a bit, now all socket operations are in own threads, two per client
unc0rr
parents:
1511
diff
changeset
|
200 |
mainLoop serverInfo acceptChan messagesChan [] [] |
1370 | 201 |
|
202 |
||
203 |
main = withSocketsDo $ do |
|
1398 | 204 |
#if !defined(mingw32_HOST_OS) |
1396 | 205 |
installHandler sigPIPE Ignore Nothing; |
1397 | 206 |
#endif |
1493 | 207 |
serverInfo <- getOpts $ newServerInfo |
1492 | 208 |
|
209 |
putStrLn $ "Listening on port " ++ show (listenPort serverInfo) |
|
1493 | 210 |
serverSocket <- listenOn $ PortNumber (listenPort serverInfo) |
1492 | 211 |
|
212 |
startServer serverInfo serverSocket `finally` sClose serverSocket |