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 |
|
|
18 |
reactCmd :: ServerInfo -> Int -> [String] -> Clients -> Rooms -> IO (ServerInfo, Clients, Rooms)
|
|
19 |
reactCmd serverInfo clID cmd clients rooms = do
|
|
20 |
(_ , serverInfo, clients, rooms) <-
|
|
21 |
foldM processAction (clID, serverInfo, clients, rooms) $ handleCmd clID clients rooms cmd
|
|
22 |
return (serverInfo, clients, rooms)
|
|
23 |
|
|
24 |
mainLoop :: Chan CoreMessage -> ServerInfo -> Clients -> Rooms -> IO ()
|
|
25 |
mainLoop coreChan serverInfo clients rooms = do
|
|
26 |
r <- readChan coreChan
|
|
27 |
|
|
28 |
(newServerInfo, mClients, mRooms) <-
|
|
29 |
case r of
|
|
30 |
Accept ci -> do
|
|
31 |
let updatedClients = IntMap.insert (clientUID ci) ci clients
|
|
32 |
--infoM "Clients" ("New client: id " ++ (show $ clientUID ci))
|
|
33 |
processAction
|
|
34 |
(clientUID ci, serverInfo, updatedClients, rooms)
|
|
35 |
(AnswerThisClient ["CONNECTED", "Hedgewars server http://www.hedgewars.org/"])
|
|
36 |
return (serverInfo, updatedClients, rooms)
|
|
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 |
|
|
47 |
{- let hadRooms = (not $ null rooms) && (null mrooms)
|
|
48 |
in unless ((not $ isDedicated serverInfo) && ((null clientsIn) || hadRooms)) $
|
|
49 |
mainLoop serverInfo acceptChan messagesChan clientsIn mrooms -}
|
|
50 |
|
|
51 |
mainLoop coreChan newServerInfo mClients mRooms
|
|
52 |
|
|
53 |
startServer :: ServerInfo -> Chan CoreMessage -> Socket -> IO ()
|
|
54 |
startServer serverInfo coreChan serverSocket = do
|
|
55 |
putStrLn $ "Listening on port " ++ show (listenPort serverInfo)
|
|
56 |
|
|
57 |
forkIO $
|
|
58 |
acceptLoop
|
|
59 |
serverSocket
|
|
60 |
coreChan
|
|
61 |
0
|
|
62 |
|
|
63 |
return ()
|
|
64 |
|
|
65 |
{- forkIO $ messagesLoop messagesChan
|
|
66 |
forkIO $ timerLoop messagesChan-}
|
|
67 |
|
1833
|
68 |
startDBConnection $ serverInfo
|
1804
|
69 |
|
|
70 |
mainLoop coreChan serverInfo IntMap.empty (IntMap.singleton 0 newRoom)
|
|
71 |
|
|
72 |
|
|
73 |
|