gameServer/hedgewars-server.hs
author unc0rr
Sun, 27 Feb 2011 20:15:09 +0300
changeset 4962 705c6186ad9d
parent 4960 3b54b1c9b768
child 4968 8e1673f0dc05
permissions -rw-r--r--
Start new server on RestartException
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4921
2efad3acbb74 Fix build of official server
unc0rr
parents: 4905
diff changeset
     1
{-# LANGUAGE CPP, ScopedTypeVariables, OverloadedStrings #-}
1804
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
4568
f85243bf890e Ok. This should pull 0.9.14.1 server into default
nemo
parents: 4295
diff changeset
     5
import Network.Socket
f85243bf890e Ok. This should pull 0.9.14.1 server into default
nemo
parents: 4295
diff changeset
     6
import Network.BSD
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     7
import Control.Concurrent.Chan
4960
unc0rr
parents: 4957
diff changeset
     8
import qualified Control.Exception as E
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     9
import System.Log.Logger
4962
705c6186ad9d Start new server on RestartException
unc0rr
parents: 4960
diff changeset
    10
import System.Process
1804
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 ServerCore
4921
2efad3acbb74 Fix build of official server
unc0rr
parents: 4905
diff changeset
    15
#if defined(OFFICIAL_SERVER)
2efad3acbb74 Fix build of official server
unc0rr
parents: 4905
diff changeset
    16
import Utils
2efad3acbb74 Fix build of official server
unc0rr
parents: 4905
diff changeset
    17
#endif
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    18
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    19
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    20
#if !defined(mingw32_HOST_OS)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    21
import System.Posix
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    22
#endif
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    23
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    24
4905
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    25
setupLoggers :: IO ()
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    26
setupLoggers =
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2349
diff changeset
    27
    updateGlobalLogger "Clients"
3947
709fdb89f76c Some screwing around in try to fix space leak. No luck yet.
unc0rr
parents: 3500
diff changeset
    28
        (setLevel INFO)
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    29
4960
unc0rr
parents: 4957
diff changeset
    30
unc0rr
parents: 4957
diff changeset
    31
server :: ServerInfo -> IO ()
unc0rr
parents: 4957
diff changeset
    32
server si = do
unc0rr
parents: 4957
diff changeset
    33
    proto <- getProtocolNumber "tcp"
unc0rr
parents: 4957
diff changeset
    34
    E.bracket
unc0rr
parents: 4957
diff changeset
    35
        (socket AF_INET Stream proto)
unc0rr
parents: 4957
diff changeset
    36
        sClose
unc0rr
parents: 4957
diff changeset
    37
        (\sock -> do
unc0rr
parents: 4957
diff changeset
    38
            setSocketOption sock ReuseAddr 1
unc0rr
parents: 4957
diff changeset
    39
            bindSocket sock (SockAddrInet (listenPort si) iNADDR_ANY)
unc0rr
parents: 4957
diff changeset
    40
            listen sock maxListenQueue
unc0rr
parents: 4957
diff changeset
    41
            startServer si sock
unc0rr
parents: 4957
diff changeset
    42
        )
unc0rr
parents: 4957
diff changeset
    43
unc0rr
parents: 4957
diff changeset
    44
handleRestart :: ShutdownException -> IO ()
unc0rr
parents: 4957
diff changeset
    45
handleRestart ShutdownException = return ()
unc0rr
parents: 4957
diff changeset
    46
handleRestart RestartException = do
4962
705c6186ad9d Start new server on RestartException
unc0rr
parents: 4960
diff changeset
    47
    _ <- createProcess (proc "./hedgewars-server" [])
4960
unc0rr
parents: 4957
diff changeset
    48
    return ()
unc0rr
parents: 4957
diff changeset
    49
4905
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    50
main :: IO ()
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    51
main = withSocketsDo $ do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    52
#if !defined(mingw32_HOST_OS)
4932
f11d80bac7ed - Take into account hlint suggestions
unc0rr
parents: 4921
diff changeset
    53
    _ <- installHandler sigPIPE Ignore Nothing
f11d80bac7ed - Take into account hlint suggestions
unc0rr
parents: 4921
diff changeset
    54
    _ <- installHandler sigCHLD Ignore Nothing
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    55
#endif
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    56
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2349
diff changeset
    57
    setupLoggers
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    58
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2349
diff changeset
    59
    dbQueriesChan <- newChan
4905
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    60
    coreChan' <- newChan
4957
3684faf5b3d1 Remove deprecated statistics MVar
unc0rr
parents: 4932
diff changeset
    61
    serverInfo' <- getOpts $ newServerInfo coreChan' dbQueriesChan
4905
7842d085acf4 Fix merge :D
unc0rr
parents: 4904
diff changeset
    62
1964
dc9ea05c9d2f - Another way of defining official server
unc0rr
parents: 1927
diff changeset
    63
#if defined(OFFICIAL_SERVER)
2867
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2349
diff changeset
    64
    dbHost' <- askFromConsole "DB host: "
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2349
diff changeset
    65
    dbLogin' <- askFromConsole "login: "
9be6693c78cb - Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents: 2349
diff changeset
    66
    dbPassword' <- askFromConsole "password: "
4960
unc0rr
parents: 4957
diff changeset
    67
    let si = serverInfo'{dbHost = dbHost', dbLogin = dbLogin', dbPassword = dbPassword'}
1964
dc9ea05c9d2f - Another way of defining official server
unc0rr
parents: 1927
diff changeset
    68
#else
4960
unc0rr
parents: 4957
diff changeset
    69
    let si = serverInfo'
1964
dc9ea05c9d2f - Another way of defining official server
unc0rr
parents: 1927
diff changeset
    70
#endif
dc9ea05c9d2f - Another way of defining official server
unc0rr
parents: 1927
diff changeset
    71
4960
unc0rr
parents: 4957
diff changeset
    72
    (server si) `E.catch` handleRestart