author | unc0rr |
Tue, 24 Feb 2009 19:39:49 +0000 | |
changeset 1839 | 5dd4cb7fd7e5 |
parent 1833 | e901ec5644b4 |
child 1841 | fba7210b438b |
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) |
22 |
reactCmd serverInfo clID cmd clients rooms = do |
|
23 |
(_ , serverInfo, clients, rooms) <- |
|
24 |
foldM processAction (clID, serverInfo, clients, rooms) $ handleCmd clID clients rooms cmd |
|
25 |
return (serverInfo, clients, rooms) |
|
26 |
||
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
27 |
mainLoop :: ServerInfo -> Clients -> Rooms -> IO () |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
28 |
mainLoop serverInfo clients rooms = do |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
29 |
r <- readChan $ coreChan serverInfo |
1804 | 30 |
|
31 |
(newServerInfo, mClients, mRooms) <- |
|
32 |
case r of |
|
33 |
Accept ci -> do |
|
34 |
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
|
35 |
infoM "Clients" ("New client: id " ++ (show $ clientUID ci)) |
1804 | 36 |
processAction |
37 |
(clientUID ci, serverInfo, updatedClients, rooms) |
|
38 |
(AnswerThisClient ["CONNECTED", "Hedgewars server http://www.hedgewars.org/"]) |
|
39 |
return (serverInfo, updatedClients, rooms) |
|
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 |
||
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
50 |
ClientAccountInfo clID info -> |
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) |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
59 |
|
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
60 |
|
1804 | 61 |
{- let hadRooms = (not $ null rooms) && (null mrooms) |
62 |
in unless ((not $ isDedicated serverInfo) && ((null clientsIn) || hadRooms)) $ |
|
63 |
mainLoop serverInfo acceptChan messagesChan clientsIn mrooms -} |
|
64 |
||
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
65 |
mainLoop newServerInfo mClients mRooms |
1804 | 66 |
|
67 |
startServer :: ServerInfo -> Chan CoreMessage -> Socket -> IO () |
|
68 |
startServer serverInfo coreChan serverSocket = do |
|
69 |
putStrLn $ "Listening on port " ++ show (listenPort serverInfo) |
|
70 |
||
71 |
forkIO $ |
|
72 |
acceptLoop |
|
73 |
serverSocket |
|
74 |
coreChan |
|
75 |
0 |
|
76 |
||
77 |
return () |
|
78 |
||
79 |
{- forkIO $ messagesLoop messagesChan |
|
80 |
forkIO $ timerLoop messagesChan-} |
|
81 |
||
1833 | 82 |
startDBConnection $ serverInfo |
1804 | 83 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
84 |
mainLoop serverInfo IntMap.empty (IntMap.singleton 0 newRoom) |
1804 | 85 |
|
86 |
||
87 |