author | unc0rr |
Sat, 20 Jun 2009 07:50:03 +0000 | |
changeset 2176 | fc082baf448c |
parent 2173 | 98cde8645e21 |
child 2184 | f59f80e034b1 |
permissions | -rw-r--r-- |
1804 | 1 |
module ServerCore where |
2 |
||
3 |
import Network |
|
4 |
import Control.Concurrent |
|
5 |
import Control.Concurrent.STM |
|
6 |
import Control.Concurrent.Chan |
|
7 |
import Control.Monad |
|
8 |
import qualified Data.IntMap as IntMap |
|
9 |
import System.Log.Logger |
|
10 |
-------------------------------------- |
|
11 |
import CoreTypes |
|
12 |
import NetRoutines |
|
13 |
import Utils |
|
14 |
import HWProtoCore |
|
15 |
import Actions |
|
1833 | 16 |
import OfficialServer.DBInteraction |
1804 | 17 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
18 |
|
2173 | 19 |
timerLoop :: Int -> Chan CoreMessage -> IO() |
20 |
timerLoop tick messagesChan = do |
|
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
21 |
threadDelay (30 * 10^6) -- 30 seconds |
2173 | 22 |
writeChan messagesChan $ TimerAction tick |
23 |
timerLoop (tick + 1) messagesChan |
|
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
24 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
25 |
firstAway (_, a, b, c) = (a, b, c) |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
26 |
|
1804 | 27 |
reactCmd :: ServerInfo -> Int -> [String] -> Clients -> Rooms -> IO (ServerInfo, Clients, Rooms) |
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
28 |
reactCmd serverInfo clID cmd clients rooms = |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
29 |
liftM firstAway $ foldM processAction (clID, serverInfo, clients, rooms) $ handleCmd clID clients rooms cmd |
1804 | 30 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
31 |
mainLoop :: ServerInfo -> Clients -> Rooms -> IO () |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
32 |
mainLoop serverInfo clients rooms = do |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
33 |
r <- readChan $ coreChan serverInfo |
1804 | 34 |
|
35 |
(newServerInfo, mClients, mRooms) <- |
|
36 |
case r of |
|
37 |
Accept ci -> do |
|
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
38 |
liftM firstAway $ processAction |
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1841
diff
changeset
|
39 |
(clientUID ci, serverInfo, clients, rooms) (AddClient ci) |
1804 | 40 |
|
41 |
ClientMessage (clID, cmd) -> do |
|
42 |
debugM "Clients" $ (show clID) ++ ": " ++ (show cmd) |
|
43 |
if clID `IntMap.member` clients then |
|
44 |
reactCmd serverInfo clID cmd clients rooms |
|
45 |
else |
|
46 |
do |
|
47 |
debugM "Clients" "Message from dead client" |
|
48 |
return (serverInfo, clients, rooms) |
|
49 |
||
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1927
diff
changeset
|
50 |
ClientAccountInfo (clID, info) -> |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
51 |
if clID `IntMap.member` clients then |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
52 |
liftM firstAway $ processAction |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
53 |
(clID, serverInfo, clients, rooms) |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
54 |
(ProcessAccountInfo info) |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
55 |
else |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
56 |
do |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
57 |
debugM "Clients" "Got info for dead client" |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
58 |
return (serverInfo, clients, rooms) |
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
59 |
|
2173 | 60 |
TimerAction tick -> |
2172 | 61 |
liftM firstAway $ |
2173 | 62 |
foldM processAction (0, serverInfo, clients, rooms) $ |
63 |
PingAll : if even tick then [StatsAction] else [] |
|
2172 | 64 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
65 |
|
1804 | 66 |
{- let hadRooms = (not $ null rooms) && (null mrooms) |
67 |
in unless ((not $ isDedicated serverInfo) && ((null clientsIn) || hadRooms)) $ |
|
68 |
mainLoop serverInfo acceptChan messagesChan clientsIn mrooms -} |
|
69 |
||
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
70 |
mainLoop newServerInfo mClients mRooms |
1804 | 71 |
|
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
72 |
startServer :: ServerInfo -> Socket -> IO () |
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
73 |
startServer serverInfo serverSocket = do |
1804 | 74 |
putStrLn $ "Listening on port " ++ show (listenPort serverInfo) |
75 |
||
76 |
forkIO $ |
|
77 |
acceptLoop |
|
78 |
serverSocket |
|
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
79 |
(coreChan serverInfo) |
1804 | 80 |
0 |
81 |
||
82 |
return () |
|
83 |
||
2173 | 84 |
forkIO $ timerLoop 0 $ coreChan serverInfo |
1804 | 85 |
|
1833 | 86 |
startDBConnection $ serverInfo |
1804 | 87 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
88 |
mainLoop serverInfo IntMap.empty (IntMap.singleton 0 newRoom) |