author | unc0rr |
Sun, 27 Feb 2011 21:06:28 +0300 | |
changeset 4968 | 8e1673f0dc05 |
parent 4962 | 705c6186ad9d |
child 4973 | 53411a26df7e |
permissions | -rw-r--r-- |
4921 | 1 |
{-# LANGUAGE CPP, ScopedTypeVariables, OverloadedStrings #-} |
1804 | 2 |
|
3 |
module Main where |
|
4 |
||
4568 | 5 |
import Network.Socket |
6 |
import Network.BSD |
|
1804 | 7 |
import Control.Concurrent.Chan |
4960 | 8 |
import qualified Control.Exception as E |
1804 | 9 |
import System.Log.Logger |
4962 | 10 |
import System.Process |
4968 | 11 |
import Control.Monad |
1804 | 12 |
----------------------------------- |
13 |
import Opts |
|
14 |
import CoreTypes |
|
15 |
import ServerCore |
|
16 |
||
17 |
#if !defined(mingw32_HOST_OS) |
|
18 |
import System.Posix |
|
19 |
#endif |
|
20 |
||
21 |
||
4905 | 22 |
setupLoggers :: IO () |
1804 | 23 |
setupLoggers = |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
24 |
updateGlobalLogger "Clients" |
3947
709fdb89f76c
Some screwing around in try to fix space leak. No luck yet.
unc0rr
parents:
3500
diff
changeset
|
25 |
(setLevel INFO) |
1804 | 26 |
|
4960 | 27 |
|
28 |
server :: ServerInfo -> IO () |
|
29 |
server si = do |
|
30 |
proto <- getProtocolNumber "tcp" |
|
31 |
E.bracket |
|
32 |
(socket AF_INET Stream proto) |
|
33 |
sClose |
|
34 |
(\sock -> do |
|
35 |
setSocketOption sock ReuseAddr 1 |
|
36 |
bindSocket sock (SockAddrInet (listenPort si) iNADDR_ANY) |
|
37 |
listen sock maxListenQueue |
|
38 |
startServer si sock |
|
39 |
) |
|
40 |
||
41 |
handleRestart :: ShutdownException -> IO () |
|
42 |
handleRestart ShutdownException = return () |
|
43 |
handleRestart RestartException = do |
|
4962 | 44 |
_ <- createProcess (proc "./hedgewars-server" []) |
4960 | 45 |
return () |
46 |
||
4905 | 47 |
main :: IO () |
1804 | 48 |
main = withSocketsDo $ do |
49 |
#if !defined(mingw32_HOST_OS) |
|
4932 | 50 |
_ <- installHandler sigPIPE Ignore Nothing |
51 |
_ <- installHandler sigCHLD Ignore Nothing |
|
1804 | 52 |
#endif |
53 |
||
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
54 |
setupLoggers |
1804 | 55 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
56 |
dbQueriesChan <- newChan |
4905 | 57 |
coreChan' <- newChan |
4957 | 58 |
serverInfo' <- getOpts $ newServerInfo coreChan' dbQueriesChan |
4905 | 59 |
|
1964 | 60 |
#if defined(OFFICIAL_SERVER) |
4968 | 61 |
[dbHost', dbLogin', dbPassword'] <- liftM read $ readFile "hedgewars-server.ini" |
4960 | 62 |
let si = serverInfo'{dbHost = dbHost', dbLogin = dbLogin', dbPassword = dbPassword'} |
1964 | 63 |
#else |
4960 | 64 |
let si = serverInfo' |
1964 | 65 |
#endif |
66 |
||
4960 | 67 |
(server si) `E.catch` handleRestart |