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