author | unc0rr |
Sun, 12 Apr 2009 18:51:35 +0000 | |
changeset 1972 | e7a7c5eb5d86 |
parent 1953 | fd9c8e3c734d |
child 1977 | 2284d7fefe4f |
permissions | -rw-r--r-- |
1804 | 1 |
module Actions where |
2 |
||
3 |
import Control.Concurrent.STM |
|
4 |
import Control.Concurrent.Chan |
|
5 |
import Data.IntMap |
|
6 |
import qualified Data.IntSet as IntSet |
|
1813 | 7 |
import qualified Data.Sequence as Seq |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
8 |
import System.Log.Logger |
1804 | 9 |
import Monad |
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
10 |
import Data.Time |
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
11 |
import Maybe |
1804 | 12 |
----------------------------- |
13 |
import CoreTypes |
|
1813 | 14 |
import Utils |
1804 | 15 |
|
16 |
data Action = |
|
17 |
AnswerThisClient [String] |
|
18 |
| AnswerAll [String] |
|
19 |
| AnswerAllOthers [String] |
|
20 |
| AnswerThisRoom [String] |
|
21 |
| AnswerOthersInRoom [String] |
|
22 |
| AnswerLobby [String] |
|
1923 | 23 |
| SendServerMessage |
1804 | 24 |
| RoomAddThisClient Int -- roomID |
25 |
| RoomRemoveThisClient |
|
1813 | 26 |
| RemoveTeam String |
1804 | 27 |
| RemoveRoom |
1811 | 28 |
| UnreadyRoomClients |
1972 | 29 |
Derek Pomery <nemo@m8y.org> |
30 |
Daniel Martin <elhombresinremedio@gmail.com> |
|
31 |
"Nils Lück" and email: "nils.luck.design@gmail.com" |
|
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
32 |
| MoveToLobby |
1804 | 33 |
| ProtocolError String |
34 |
| Warning String |
|
35 |
| ByeClient String |
|
1862 | 36 |
| KickClient Int -- clID |
1879 | 37 |
| KickRoomClient Int -- clID |
1921 | 38 |
| BanClient String -- nick |
1879 | 39 |
| RemoveClientTeams Int -- clID |
1804 | 40 |
| ModifyClient (ClientInfo -> ClientInfo) |
41 |
| ModifyRoom (RoomInfo -> RoomInfo) |
|
1925 | 42 |
| ModifyServerInfo (ServerInfo -> ServerInfo) |
1804 | 43 |
| AddRoom String String |
1834 | 44 |
| CheckRegistered |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
45 |
| ProcessAccountInfo AccountInfo |
1804 | 46 |
| Dump |
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
47 |
| AddClient ClientInfo |
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
48 |
| PingAll |
1804 | 49 |
|
50 |
type CmdHandler = Int -> Clients -> Rooms -> [String] -> [Action] |
|
51 |
||
1862 | 52 |
replaceID a (b, c, d, e) = (a, c, d, e) |
1804 | 53 |
|
54 |
processAction :: (Int, ServerInfo, Clients, Rooms) -> Action -> IO (Int, ServerInfo, Clients, Rooms) |
|
55 |
||
56 |
||
57 |
processAction (clID, serverInfo, clients, rooms) (AnswerThisClient msg) = do |
|
58 |
writeChan (sendChan $ clients ! clID) msg |
|
59 |
return (clID, serverInfo, clients, rooms) |
|
60 |
||
61 |
||
62 |
processAction (clID, serverInfo, clients, rooms) (AnswerAll msg) = do |
|
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
63 |
mapM_ (\cl -> writeChan (sendChan cl) msg) (elems clients) |
1804 | 64 |
return (clID, serverInfo, clients, rooms) |
65 |
||
66 |
||
67 |
processAction (clID, serverInfo, clients, rooms) (AnswerAllOthers msg) = do |
|
1879 | 68 |
mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) $ |
69 |
Prelude.filter (\id' -> (id' /= clID) && (logonPassed $ clients ! id')) (keys clients) |
|
1804 | 70 |
return (clID, serverInfo, clients, rooms) |
71 |
||
72 |
||
73 |
processAction (clID, serverInfo, clients, rooms) (AnswerThisRoom msg) = do |
|
74 |
mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) roomClients |
|
75 |
return (clID, serverInfo, clients, rooms) |
|
76 |
where |
|
77 |
roomClients = IntSet.elems $ playersIDs room |
|
78 |
room = rooms ! rID |
|
79 |
rID = roomID client |
|
80 |
client = clients ! clID |
|
81 |
||
82 |
||
83 |
processAction (clID, serverInfo, clients, rooms) (AnswerOthersInRoom msg) = do |
|
84 |
mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) $ Prelude.filter (/= clID) roomClients |
|
85 |
return (clID, serverInfo, clients, rooms) |
|
86 |
where |
|
87 |
roomClients = IntSet.elems $ playersIDs room |
|
88 |
room = rooms ! rID |
|
89 |
rID = roomID client |
|
90 |
client = clients ! clID |
|
91 |
||
92 |
||
93 |
processAction (clID, serverInfo, clients, rooms) (AnswerLobby msg) = do |
|
94 |
mapM_ (\id -> writeChan (sendChan $ clients ! id) msg) roomClients |
|
95 |
return (clID, serverInfo, clients, rooms) |
|
96 |
where |
|
97 |
roomClients = IntSet.elems $ playersIDs room |
|
98 |
room = rooms ! 0 |
|
99 |
||
100 |
||
1923 | 101 |
processAction (clID, serverInfo, clients, rooms) SendServerMessage = do |
1953 | 102 |
writeChan (sendChan $ clients ! clID) $ ["SERVER_MESSAGE", message serverInfo] |
1923 | 103 |
return (clID, serverInfo, clients, rooms) |
1953 | 104 |
where |
105 |
client = clients ! clID |
|
106 |
message = if clientProto client < 25 then |
|
107 |
serverMessageForOldVersions |
|
108 |
else |
|
109 |
serverMessageForOldVersions |
|
1923 | 110 |
|
111 |
||
1804 | 112 |
processAction (clID, serverInfo, clients, rooms) (ProtocolError msg) = do |
113 |
writeChan (sendChan $ clients ! clID) ["ERROR", msg] |
|
114 |
return (clID, serverInfo, clients, rooms) |
|
115 |
||
116 |
||
117 |
processAction (clID, serverInfo, clients, rooms) (Warning msg) = do |
|
118 |
writeChan (sendChan $ clients ! clID) ["WARNING", msg] |
|
119 |
return (clID, serverInfo, clients, rooms) |
|
120 |
||
121 |
||
122 |
processAction (clID, serverInfo, clients, rooms) (ByeClient msg) = do |
|
1929 | 123 |
(_, _, newClients, newRooms) <- |
1930 | 124 |
if roomID client /= 0 then |
125 |
processAction (clID, serverInfo, clients, rooms) |
|
1929 | 126 |
(if isMaster client then RemoveRoom else RemoveClientTeams clID) |
1930 | 127 |
else |
128 |
return (clID, serverInfo, clients, rooms) |
|
1929 | 129 |
|
130 |
mapM_ (processAction (clID, serverInfo, newClients, newRooms)) $ answerOthersQuit ++ answerInformRoom |
|
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
131 |
writeChan (sendChan $ clients ! clID) ["BYE", msg] |
1804 | 132 |
return ( |
133 |
0, |
|
134 |
serverInfo, |
|
1929 | 135 |
delete clID newClients, |
1823 | 136 |
adjust (\r -> r{ |
137 |
playersIDs = IntSet.delete clID (playersIDs r), |
|
138 |
playersIn = (playersIn r) - 1, |
|
139 |
readyPlayers = if isReady client then readyPlayers r - 1 else readyPlayers r |
|
1930 | 140 |
}) (roomID $ newClients ! clID) newRooms |
1804 | 141 |
) |
142 |
where |
|
143 |
client = clients ! clID |
|
144 |
clientNick = nick client |
|
145 |
answerInformRoom = |
|
146 |
if roomID client /= 0 then |
|
147 |
if not $ Prelude.null msg then |
|
148 |
[AnswerThisRoom ["LEFT", clientNick, msg]] |
|
149 |
else |
|
150 |
[AnswerThisRoom ["LEFT", clientNick]] |
|
151 |
else |
|
152 |
[] |
|
153 |
answerOthersQuit = |
|
1846 | 154 |
if logonPassed client then |
1804 | 155 |
if not $ Prelude.null msg then |
156 |
[AnswerAll ["LOBBY:LEFT", clientNick, msg]] |
|
157 |
else |
|
158 |
[AnswerAll ["LOBBY:LEFT", clientNick]] |
|
159 |
else |
|
160 |
[] |
|
161 |
||
162 |
||
163 |
processAction (clID, serverInfo, clients, rooms) (ModifyClient func) = do |
|
164 |
return (clID, serverInfo, adjust func clID clients, rooms) |
|
165 |
||
166 |
||
167 |
processAction (clID, serverInfo, clients, rooms) (ModifyRoom func) = do |
|
168 |
return (clID, serverInfo, clients, adjust func rID rooms) |
|
169 |
where |
|
170 |
rID = roomID $ clients ! clID |
|
171 |
||
172 |
||
1925 | 173 |
processAction (clID, serverInfo, clients, rooms) (ModifyServerInfo func) = do |
174 |
return (clID, func serverInfo, clients, rooms) |
|
175 |
||
176 |
||
1804 | 177 |
processAction (clID, serverInfo, clients, rooms) (RoomAddThisClient rID) = do |
178 |
processAction ( |
|
179 |
clID, |
|
180 |
serverInfo, |
|
181 |
adjust (\cl -> cl{roomID = rID}) clID clients, |
|
182 |
adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r), playersIn = (playersIn r) + 1}) rID $ |
|
183 |
adjust (\r -> r{playersIDs = IntSet.delete clID (playersIDs r)}) 0 rooms |
|
184 |
) joinMsg |
|
185 |
where |
|
186 |
client = clients ! clID |
|
187 |
joinMsg = if rID == 0 then |
|
188 |
AnswerAllOthers ["LOBBY:JOINED", nick client] |
|
189 |
else |
|
190 |
AnswerThisRoom ["JOINED", nick client] |
|
191 |
||
192 |
||
193 |
processAction (clID, serverInfo, clients, rooms) (RoomRemoveThisClient) = do |
|
194 |
when (rID /= 0) $ (processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["LEFT", nick client, "part"]) >> return () |
|
195 |
return ( |
|
196 |
clID, |
|
197 |
serverInfo, |
|
1929 | 198 |
adjust (\cl -> cl{roomID = 0, isMaster = False, isReady = False}) clID clients, |
1823 | 199 |
adjust (\r -> r{ |
200 |
playersIDs = IntSet.delete clID (playersIDs r), |
|
201 |
playersIn = (playersIn r) - 1, |
|
1827 | 202 |
readyPlayers = if isReady client then (readyPlayers r) - 1 else readyPlayers r |
1823 | 203 |
}) rID $ |
1804 | 204 |
adjust (\r -> r{playersIDs = IntSet.insert clID (playersIDs r)}) 0 rooms |
205 |
) |
|
206 |
where |
|
207 |
rID = roomID client |
|
208 |
client = clients ! clID |
|
209 |
||
210 |
||
211 |
processAction (clID, serverInfo, clients, rooms) (AddRoom roomName roomPassword) = do |
|
212 |
let newServerInfo = serverInfo {nextRoomID = newID} |
|
213 |
let room = newRoom{ |
|
214 |
roomUID = newID, |
|
215 |
name = roomName, |
|
216 |
password = roomPassword, |
|
217 |
roomProto = (clientProto client) |
|
218 |
} |
|
219 |
||
220 |
processAction (clID, serverInfo, clients, rooms) $ AnswerLobby ["ROOM", "ADD", roomName] |
|
221 |
||
222 |
processAction ( |
|
223 |
clID, |
|
224 |
newServerInfo, |
|
225 |
adjust (\cl -> cl{isMaster = True}) clID clients, |
|
226 |
insert newID room rooms |
|
227 |
) $ RoomAddThisClient newID |
|
228 |
where |
|
229 |
newID = (nextRoomID serverInfo) - 1 |
|
230 |
client = clients ! clID |
|
231 |
||
232 |
||
233 |
processAction (clID, serverInfo, clients, rooms) (RemoveRoom) = do |
|
1811 | 234 |
processAction (clID, serverInfo, clients, rooms) $ AnswerLobby ["ROOM", "DEL", name room] |
235 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["ROOMABANDONED", name room] |
|
1804 | 236 |
return (clID, |
237 |
serverInfo, |
|
1827 | 238 |
Data.IntMap.map (\cl -> if roomID cl == rID then cl{roomID = 0, isMaster = False, isReady = False} else cl) clients, |
1811 | 239 |
delete rID $ adjust (\r -> r{playersIDs = IntSet.union (playersIDs room) (playersIDs r)}) 0 rooms |
1804 | 240 |
) |
241 |
where |
|
1811 | 242 |
room = rooms ! rID |
1804 | 243 |
rID = roomID client |
244 |
client = clients ! clID |
|
245 |
||
1813 | 246 |
|
1811 | 247 |
processAction (clID, serverInfo, clients, rooms) (UnreadyRoomClients) = do |
248 |
processAction (clID, serverInfo, clients, rooms) $ AnswerThisRoom ("NOT_READY" : roomPlayers) |
|
249 |
return (clID, |
|
250 |
serverInfo, |
|
251 |
Data.IntMap.map (\cl -> if roomID cl == rID then cl{isReady = False} else cl) clients, |
|
1827 | 252 |
adjust (\r -> r{readyPlayers = 0}) rID rooms) |
1811 | 253 |
where |
254 |
room = rooms ! rID |
|
255 |
rID = roomID client |
|
256 |
client = clients ! clID |
|
257 |
roomPlayers = Prelude.map (nick . (clients !)) roomPlayersIDs |
|
258 |
roomPlayersIDs = IntSet.elems $ playersIDs room |
|
259 |
||
260 |
||
1813 | 261 |
processAction (clID, serverInfo, clients, rooms) (RemoveTeam teamName) = do |
262 |
newRooms <- if not $ gameinprogress room then |
|
263 |
do |
|
264 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["REMOVE_TEAM", teamName] |
|
265 |
return $ |
|
266 |
adjust (\r -> r{teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r}) rID rooms |
|
267 |
else |
|
268 |
do |
|
1866 | 269 |
processAction (clID, serverInfo, clients, rooms) $ AnswerOthersInRoom ["EM", rmTeamMsg] |
1813 | 270 |
return $ |
271 |
adjust (\r -> r{ |
|
272 |
teams = Prelude.filter (\t -> teamName /= teamname t) $ teams r, |
|
273 |
leftTeams = teamName : leftTeams r, |
|
274 |
roundMsgs = roundMsgs r Seq.|> rmTeamMsg |
|
275 |
}) rID rooms |
|
276 |
return (clID, serverInfo, clients, newRooms) |
|
277 |
where |
|
278 |
room = rooms ! rID |
|
279 |
rID = roomID client |
|
280 |
client = clients ! clID |
|
281 |
rmTeamMsg = toEngineMsg $ 'F' : teamName |
|
282 |
||
283 |
||
1834 | 284 |
processAction (clID, serverInfo, clients, rooms) (CheckRegistered) = do |
1921 | 285 |
writeChan (dbQueries serverInfo) $ CheckAccount client |
1834 | 286 |
return (clID, serverInfo, clients, rooms) |
287 |
where |
|
288 |
client = clients ! clID |
|
289 |
||
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
290 |
|
1804 | 291 |
processAction (clID, serverInfo, clients, rooms) (Dump) = do |
292 |
writeChan (sendChan $ clients ! clID) ["DUMP", show serverInfo, showTree clients, showTree rooms] |
|
293 |
return (clID, serverInfo, clients, rooms) |
|
294 |
||
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
295 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
296 |
processAction (clID, serverInfo, clients, rooms) (ProcessAccountInfo info) = do |
1923 | 297 |
processAction (clID, serverInfo, clients, rooms) SendServerMessage |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
298 |
case info of |
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1846
diff
changeset
|
299 |
HasAccount passwd isAdmin -> do |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
300 |
infoM "Clients" $ show clID ++ " has account" |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
301 |
writeChan (sendChan $ clients ! clID) ["ASKPASSWORD"] |
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1846
diff
changeset
|
302 |
return (clID, serverInfo, adjust (\cl -> cl{webPassword = passwd, isAdministrator = isAdmin}) clID clients, rooms) |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
303 |
Guest -> do |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
304 |
infoM "Clients" $ show clID ++ " is guest" |
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
305 |
processAction (clID, serverInfo, adjust (\cl -> cl{logonPassed = True}) clID clients, rooms) MoveToLobby |
1921 | 306 |
Admin -> do |
307 |
infoM "Clients" $ show clID ++ " is admin" |
|
308 |
foldM processAction (clID, serverInfo, adjust (\cl -> cl{logonPassed = True, isAdministrator = True}) clID clients, rooms) [MoveToLobby, AnswerThisClient ["ADMIN_ACCESS"]] |
|
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
309 |
|
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
310 |
|
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
311 |
processAction (clID, serverInfo, clients, rooms) (MoveToLobby) = do |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
312 |
foldM processAction (clID, serverInfo, clients, rooms) $ |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
313 |
(RoomAddThisClient 0) |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
314 |
: answerLobbyNicks |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
315 |
-- ++ (answerServerMessage client clients) |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
316 |
where |
1846 | 317 |
lobbyNicks = Prelude.map nick $ Prelude.filter logonPassed $ elems clients |
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
318 |
answerLobbyNicks = if not $ Prelude.null lobbyNicks then |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
319 |
[AnswerThisClient (["LOBBY:JOINED"] ++ lobbyNicks)] |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
320 |
else |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
321 |
[] |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
322 |
|
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
323 |
|
1862 | 324 |
processAction (clID, serverInfo, clients, rooms) (KickClient kickID) = do |
1879 | 325 |
liftM2 replaceID (return clID) (processAction (kickID, serverInfo, clients, rooms) $ ByeClient "Kicked") |
326 |
||
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
327 |
|
1921 | 328 |
processAction (clID, serverInfo, clients, rooms) (BanClient banNick) = do |
329 |
return (clID, serverInfo, clients, rooms) |
|
330 |
||
1879 | 331 |
|
332 |
processAction (clID, serverInfo, clients, rooms) (KickRoomClient kickID) = do |
|
333 |
writeChan (sendChan $ clients ! kickID) ["KICKED"] |
|
334 |
liftM2 replaceID (return clID) (processAction (kickID, serverInfo, clients, rooms) $ RoomRemoveThisClient) |
|
335 |
||
336 |
||
337 |
processAction (clID, serverInfo, clients, rooms) (RemoveClientTeams teamsClID) = do |
|
338 |
liftM2 replaceID (return clID) $ |
|
339 |
foldM processAction (teamsClID, serverInfo, clients, rooms) $ removeTeamsActions |
|
340 |
where |
|
341 |
client = clients ! teamsClID |
|
342 |
room = rooms ! (roomID client) |
|
343 |
teamsToRemove = Prelude.filter (\t -> teamowner t == nick client) $ teams room |
|
344 |
removeTeamsActions = Prelude.map (RemoveTeam . teamname) teamsToRemove |
|
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
345 |
|
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
346 |
|
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
347 |
processAction (clID, serverInfo, clients, rooms) (AddClient client) = do |
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
348 |
let updatedClients = insert (clientUID client) client clients |
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
349 |
infoM "Clients" ((show $ clientUID client) ++ ": New client. Time: " ++ (show $ connectTime client)) |
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
350 |
writeChan (sendChan $ client) ["CONNECTED", "Hedgewars server http://www.hedgewars.org/"] |
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
351 |
|
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
352 |
let newLogins = takeWhile (\(_ , time) -> (connectTime client) `diffUTCTime` time <= 20) $ lastLogins serverInfo |
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1925
diff
changeset
|
353 |
|
1931 | 354 |
if isJust $ host client `Prelude.lookup` newLogins then |
355 |
processAction (clID, serverInfo{lastLogins = newLogins}, updatedClients, rooms) $ ByeClient "Reconnected too fast" |
|
356 |
else |
|
357 |
return (clID, serverInfo{lastLogins = (host client, connectTime client) : newLogins}, updatedClients, rooms) |
|
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
358 |
|
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
359 |
|
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
360 |
processAction (clID, serverInfo, clients, rooms) PingAll = do |
1928 | 361 |
(_, _, newClients, newRooms) <- foldM kickTimeouted (clID, serverInfo, clients, rooms) $ elems clients |
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
362 |
processAction (clID, |
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
363 |
serverInfo, |
1928 | 364 |
Data.IntMap.map (\cl -> cl{pingsQueue = pingsQueue cl + 1}) newClients, |
365 |
newRooms) $ AnswerAll ["PING"] |
|
366 |
where |
|
367 |
kickTimeouted (clID, serverInfo, clients, rooms) client = |
|
368 |
if pingsQueue client > 0 then |
|
369 |
processAction (clientUID client, serverInfo, clients, rooms) $ ByeClient "Ping timeout" |
|
370 |
else |
|
371 |
return (clID, serverInfo, clients, rooms) |