gameServer/hedgewars-server.hs
author unc0rr
Wed, 18 Feb 2009 15:04:40 +0000
changeset 1804 4e78ad846fb6
child 1833 e901ec5644b4
permissions -rw-r--r--
New game server: - Incomplete implementation - More robust, no memory leaks, better architecture for easy features addition - Incompatible with current client
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     1
{-# LANGUAGE CPP, ScopedTypeVariables, PatternSignatures #-}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     2
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     3
module Main where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     4
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     5
import Network.Socket
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     6
import qualified Network
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     7
import Control.Concurrent.STM
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     8
import Control.Concurrent.Chan
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     9
import Control.Exception
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    10
import System.Log.Logger
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    11
-----------------------------------
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    12
import Opts
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    13
import CoreTypes
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    14
import OfficialServer.DBInteraction
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    15
import ServerCore
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    16
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    17
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    18
#if !defined(mingw32_HOST_OS)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    19
import System.Posix
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    20
#endif
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    21
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    22
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    23
{-data Messages =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    24
	Accept ClientInfo
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    25
	| ClientMessage ([String], ClientInfo)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    26
	| CoreMessage [String]
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    27
	| TimerTick
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    28
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    29
messagesLoop :: TChan String -> IO()
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    30
messagesLoop messagesChan = forever $ do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    31
	threadDelay (25 * 10^6) -- 25 seconds
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    32
	atomically $ writeTChan messagesChan "PING"
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    33
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    34
timerLoop :: TChan String -> IO()
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    35
timerLoop messagesChan = forever $ do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    36
	threadDelay (60 * 10^6) -- 60 seconds
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    37
	atomically $ writeTChan messagesChan "MINUTELY"-}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    38
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    39
setupLoggers =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    40
	updateGlobalLogger "Clients"
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    41
		(setLevel DEBUG)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    42
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    43
main = withSocketsDo $ do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    44
#if !defined(mingw32_HOST_OS)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    45
	installHandler sigPIPE Ignore Nothing;
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    46
#endif
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    47
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    48
	setupLoggers
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    49
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    50
	stats <- atomically $ newTMVar (StatisticsInfo 0 0)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    51
	--dbQueriesChan <- atomically newTChan
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    52
	coreChan <- newChan
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    53
	serverInfo <- getOpts $ newServerInfo stats -- dbQueriesChan
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    54
	
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    55
	bracket
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    56
		(Network.listenOn $ Network.PortNumber $ listenPort serverInfo)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    57
		(sClose)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    58
		(startServer serverInfo coreChan)