author | koda |
Fri, 23 Jul 2010 15:08:44 +0200 | |
changeset 3664 | f5bdf26c843e |
parent 3657 | fa3bf50d0338 |
child 3671 | a94d1dc4a8d9 |
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 |
3566 | 11 |
import Data.Set as Set |
3500
af8390d807d6
Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents:
3458
diff
changeset
|
12 |
import qualified Data.ByteString.Char8 as B |
1804 | 13 |
-------------------------------------- |
14 |
import CoreTypes |
|
15 |
import NetRoutines |
|
16 |
import HWProtoCore |
|
17 |
import Actions |
|
1833 | 18 |
import OfficialServer.DBInteraction |
3458 | 19 |
import ServerState |
1804 | 20 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
21 |
|
2173 | 22 |
timerLoop :: Int -> Chan CoreMessage -> IO() |
2349 | 23 |
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
|
24 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1833
diff
changeset
|
25 |
|
3500
af8390d807d6
Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents:
3458
diff
changeset
|
26 |
reactCmd :: [B.ByteString] -> StateT ServerState IO () |
3451
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
27 |
reactCmd cmd = do |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
28 |
(Just ci) <- gets clientIndex |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
29 |
rnc <- gets roomsClients |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
30 |
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
|
31 |
forM_ actions processAction |
1804 | 32 |
|
3458 | 33 |
mainLoop :: StateT ServerState IO () |
3451
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
34 |
mainLoop = forever $ do |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
35 |
si <- gets serverInfo |
62089ccec75c
Uses StateT monad instead of manually maintaining the state
unc0rr
parents:
3435
diff
changeset
|
36 |
r <- liftIO $ readChan $ coreChan si |
3425 | 37 |
|
3435 | 38 |
case r of |
3566 | 39 |
Accept ci -> processAction (AddClient ci) |
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) |
3566 | 43 |
|
44 |
removed <- gets removedClients |
|
45 |
when (not $ ci `Set.member` removed) $ do |
|
46 |
modify (\as -> as{clientIndex = Just ci}) |
|
47 |
reactCmd cmd |
|
48 |
||
49 |
Remove ci -> processAction (DeleteClient ci) |
|
50 |
||
3435 | 51 |
--else |
52 |
--do |
|
53 |
--debugM "Clients" "Message from dead client" |
|
54 |
--return (serverInfo, rnc) |
|
1804 | 55 |
|
3566 | 56 |
ClientAccountInfo (ci, info) -> do |
57 |
removed <- gets removedClients |
|
58 |
when (not $ ci `Set.member` removed) $ |
|
59 |
processAction (ProcessAccountInfo info) |
|
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 -> |
3657 | 62 |
mapM_ processAction $ |
63 |
PingAll : [StatsAction | even tick] |
|
1804 | 64 |
|
3458 | 65 |
|
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
66 |
startServer :: ServerInfo -> Socket -> IO () |
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
67 |
startServer serverInfo serverSocket = do |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
68 |
putStrLn $ "Listening on port " ++ show (listenPort serverInfo) |
1804 | 69 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
70 |
forkIO $ |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
71 |
acceptLoop |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
72 |
serverSocket |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
73 |
(coreChan serverInfo) |
1804 | 74 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
75 |
return () |
3435 | 76 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
77 |
forkIO $ timerLoop 0 $ coreChan serverInfo |
1804 | 78 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
79 |
startDBConnection serverInfo |
1804 | 80 |
|
3435 | 81 |
rnc <- newRoomsAndClients newRoom |
82 |
||
3566 | 83 |
forkIO $ evalStateT mainLoop (ServerState Nothing serverInfo Set.empty rnc) |
2184 | 84 |
|
3425 | 85 |
forever $ threadDelay (60 * 60 * 10^6) >> putStrLn "***" |