author | koda |
Sun, 03 Apr 2011 16:34:12 +0200 | |
changeset 5095 | 15dd764b728c |
parent 4991 | 90d1fb9fc2e1 |
child 5119 | f475e10c4081 |
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 |
1804 | 11 |
----------------------------------- |
12 |
import Opts |
|
13 |
import CoreTypes |
|
14 |
import ServerCore |
|
4991 | 15 |
#if defined(OFFICIAL_SERVER) |
4974
078cd026a7b1
Add stubs for server config reading and writing routines
unc0rr
parents:
4973
diff
changeset
|
16 |
import ConfigFile |
4991 | 17 |
#endif |
1804 | 18 |
|
19 |
#if !defined(mingw32_HOST_OS) |
|
20 |
import System.Posix |
|
21 |
#endif |
|
22 |
||
23 |
||
4905 | 24 |
setupLoggers :: IO () |
1804 | 25 |
setupLoggers = |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
26 |
updateGlobalLogger "Clients" |
3947
709fdb89f76c
Some screwing around in try to fix space leak. No luck yet.
unc0rr
parents:
3500
diff
changeset
|
27 |
(setLevel INFO) |
1804 | 28 |
|
4960 | 29 |
|
4989 | 30 |
server :: ServerInfo -> IO () |
4960 | 31 |
server si = do |
32 |
proto <- getProtocolNumber "tcp" |
|
33 |
E.bracket |
|
34 |
(socket AF_INET Stream proto) |
|
35 |
sClose |
|
36 |
(\sock -> do |
|
37 |
setSocketOption sock ReuseAddr 1 |
|
38 |
bindSocket sock (SockAddrInet (listenPort si) iNADDR_ANY) |
|
39 |
listen sock maxListenQueue |
|
40 |
startServer si sock |
|
41 |
) |
|
42 |
||
43 |
handleRestart :: ShutdownException -> IO () |
|
44 |
handleRestart ShutdownException = return () |
|
45 |
handleRestart RestartException = do |
|
4962 | 46 |
_ <- createProcess (proc "./hedgewars-server" []) |
4960 | 47 |
return () |
48 |
||
4905 | 49 |
main :: IO () |
1804 | 50 |
main = withSocketsDo $ do |
51 |
#if !defined(mingw32_HOST_OS) |
|
4932 | 52 |
_ <- installHandler sigPIPE Ignore Nothing |
53 |
_ <- installHandler sigCHLD Ignore Nothing |
|
1804 | 54 |
#endif |
55 |
||
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
56 |
setupLoggers |
1804 | 57 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
58 |
dbQueriesChan <- newChan |
4905 | 59 |
coreChan' <- newChan |
4975
31da8979e5b1
Use Data.TConfig to read and store server config in hedgewars.ini (a little bit of hate to the author for not exporting Conf type)
unc0rr
parents:
4974
diff
changeset
|
60 |
serverInfo' <- getOpts $ newServerInfo coreChan' dbQueriesChan Nothing |
4905 | 61 |
|
1964 | 62 |
#if defined(OFFICIAL_SERVER) |
4975
31da8979e5b1
Use Data.TConfig to read and store server config in hedgewars.ini (a little bit of hate to the author for not exporting Conf type)
unc0rr
parents:
4974
diff
changeset
|
63 |
si <- readServerConfig serverInfo' |
1964 | 64 |
#else |
4960 | 65 |
let si = serverInfo' |
1964 | 66 |
#endif |
67 |
||
4960 | 68 |
(server si) `E.catch` handleRestart |