author | unc0rr |
Mon, 02 Mar 2009 21:13:07 +0000 | |
changeset 1854 | 6e05013899b2 |
parent 1841 | fba7210b438b |
child 1926 | cb46fbdcaa41 |
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 |
|
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
19 |
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
|
20 |
|
1804 | 21 |
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
|
22 |
reactCmd serverInfo clID cmd clients rooms = |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
23 |
liftM firstAway $ foldM processAction (clID, serverInfo, clients, rooms) $ handleCmd clID clients rooms cmd |
1804 | 24 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
25 |
mainLoop :: ServerInfo -> Clients -> Rooms -> IO () |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
26 |
mainLoop serverInfo clients rooms = do |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
27 |
r <- readChan $ coreChan serverInfo |
1804 | 28 |
|
29 |
(newServerInfo, mClients, mRooms) <- |
|
30 |
case r of |
|
31 |
Accept ci -> do |
|
32 |
let updatedClients = IntMap.insert (clientUID ci) ci clients |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
33 |
infoM "Clients" ("New client: id " ++ (show $ clientUID ci)) |
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
34 |
liftM firstAway $ processAction |
1804 | 35 |
(clientUID ci, serverInfo, updatedClients, rooms) |
36 |
(AnswerThisClient ["CONNECTED", "Hedgewars server http://www.hedgewars.org/"]) |
|
37 |
||
38 |
ClientMessage (clID, cmd) -> do |
|
39 |
debugM "Clients" $ (show clID) ++ ": " ++ (show cmd) |
|
40 |
if clID `IntMap.member` clients then |
|
41 |
reactCmd serverInfo clID cmd clients rooms |
|
42 |
else |
|
43 |
do |
|
44 |
debugM "Clients" "Message from dead client" |
|
45 |
return (serverInfo, clients, rooms) |
|
46 |
||
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
47 |
ClientAccountInfo clID info -> |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
48 |
if clID `IntMap.member` clients then |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
49 |
liftM firstAway $ processAction |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
50 |
(clID, serverInfo, clients, rooms) |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
51 |
(ProcessAccountInfo info) |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
52 |
else |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
53 |
do |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
54 |
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
|
55 |
return (serverInfo, clients, rooms) |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
56 |
|
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
57 |
|
1804 | 58 |
{- let hadRooms = (not $ null rooms) && (null mrooms) |
59 |
in unless ((not $ isDedicated serverInfo) && ((null clientsIn) || hadRooms)) $ |
|
60 |
mainLoop serverInfo acceptChan messagesChan clientsIn mrooms -} |
|
61 |
||
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
62 |
mainLoop newServerInfo mClients mRooms |
1804 | 63 |
|
64 |
startServer :: ServerInfo -> Chan CoreMessage -> Socket -> IO () |
|
65 |
startServer serverInfo coreChan serverSocket = do |
|
66 |
putStrLn $ "Listening on port " ++ show (listenPort serverInfo) |
|
67 |
||
68 |
forkIO $ |
|
69 |
acceptLoop |
|
70 |
serverSocket |
|
71 |
coreChan |
|
72 |
0 |
|
73 |
||
74 |
return () |
|
75 |
||
76 |
{- forkIO $ messagesLoop messagesChan |
|
77 |
forkIO $ timerLoop messagesChan-} |
|
78 |
||
1833 | 79 |
startDBConnection $ serverInfo |
1804 | 80 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
81 |
mainLoop serverInfo IntMap.empty (IntMap.singleton 0 newRoom) |
1804 | 82 |
|
83 |
||
84 |