author | mbait |
Mon, 10 May 2010 04:43:07 +0000 | |
changeset 3455 | f33e7ad2261c |
parent 3451 | 62089ccec75c |
child 3458 | 11cd56019f00 |
permissions | -rw-r--r-- |
1804 | 1 |
module ServerCore where |
2 |
||
3 |
import Network |
|
4 |
import Control.Concurrent |
|
5 |
import Control.Concurrent.Chan |
|
6 |
import Control.Monad |
|
7 |
import qualified Data.IntMap as IntMap |
|
8 |
import System.Log.Logger |
|
3435 | 9 |
import Control.Monad.Reader |
3451
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
10 |
import Control.Monad.State |
1804 | 11 |
-------------------------------------- |
12 |
import CoreTypes |
|
13 |
import NetRoutines |
|
14 |
import HWProtoCore |
|
15 |
import Actions |
|
1833 | 16 |
import OfficialServer.DBInteraction |
3435 | 17 |
import RoomsAndClients |
1804 | 18 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
19 |
|
2173 | 20 |
timerLoop :: Int -> Chan CoreMessage -> IO() |
2349 | 21 |
timerLoop tick messagesChan = threadDelay (30 * 10^6) >> writeChan messagesChan (TimerAction tick) >> timerLoop (tick + 1) messagesChan |
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
22 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
23 |
|
3451
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
24 |
reactCmd :: [String] -> StateT ActionsState IO () |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
25 |
reactCmd cmd = do |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
26 |
(Just ci) <- gets clientIndex |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
27 |
rnc <- gets roomsClients |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
28 |
actions <- liftIO $ withRoomsAndClients rnc (\irnc -> runReader (handleCmd cmd) (ci, irnc)) |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
29 |
forM_ actions processAction |
1804 | 30 |
|
3451
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
31 |
mainLoop :: StateT ActionsState IO () |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
32 |
mainLoop = forever $ do |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
33 |
si <- gets serverInfo |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
34 |
r <- liftIO $ readChan $ coreChan si |
3425 | 35 |
|
3435 | 36 |
case r of |
37 |
Accept ci -> do |
|
3451
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
38 |
processAction (AddClient ci) |
3435 | 39 |
return () |
1804 | 40 |
|
3451
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
41 |
ClientMessage (ci, cmd) -> do |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
42 |
liftIO $ debugM "Clients" $ (show ci) ++ ": " ++ (show cmd) |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
43 |
modify (\as -> as{clientIndex = Just ci}) |
3435 | 44 |
--if clID `IntMap.member` clients then |
3451
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
45 |
reactCmd cmd |
3435 | 46 |
return () |
47 |
--else |
|
48 |
--do |
|
49 |
--debugM "Clients" "Message from dead client" |
|
50 |
--return (serverInfo, rnc) |
|
1804 | 51 |
|
3435 | 52 |
ClientAccountInfo (clID, info) -> do |
53 |
--if clID `IntMap.member` clients then |
|
3451
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
54 |
processAction (ProcessAccountInfo info) |
3435 | 55 |
return () |
56 |
--else |
|
57 |
--do |
|
58 |
--debugM "Clients" "Got info for dead client" |
|
59 |
--return (serverInfo, rnc) |
|
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
60 |
|
3435 | 61 |
TimerAction tick -> |
62 |
return () |
|
63 |
--liftM snd $ |
|
64 |
-- foldM processAction (0, serverInfo, rnc) $ |
|
65 |
-- PingAll : [StatsAction | even tick] |
|
1804 | 66 |
|
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
67 |
startServer :: ServerInfo -> Socket -> IO () |
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
68 |
startServer serverInfo serverSocket = do |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
69 |
putStrLn $ "Listening on port " ++ show (listenPort serverInfo) |
1804 | 70 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
71 |
forkIO $ |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
72 |
acceptLoop |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
73 |
serverSocket |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
74 |
(coreChan serverInfo) |
1804 | 75 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
76 |
return () |
3435 | 77 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
78 |
forkIO $ timerLoop 0 $ coreChan serverInfo |
1804 | 79 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
80 |
startDBConnection serverInfo |
1804 | 81 |
|
3435 | 82 |
rnc <- newRoomsAndClients newRoom |
83 |
||
3451
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
84 |
forkIO $ evalStateT mainLoop (ActionsState Nothing serverInfo rnc) |
2184 | 85 |
|
3425 | 86 |
forever $ threadDelay (60 * 60 * 10^6) >> putStrLn "***" |