author | unc0rr |
Sat, 05 Mar 2011 22:23:14 +0300 | |
changeset 4987 | cf9470964dba |
parent 4982 | 3572eaf14340 |
child 4989 | 4771fed9272e |
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 |
|
4974
078cd026a7b1
Add stubs for server config reading and writing routines
unc0rr
parents:
4973
diff
changeset
|
15 |
import ConfigFile |
1804 | 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 |
|
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
|
28 |
server :: ServerInfo c -> IO () |
4960 | 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 |
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
|
58 |
serverInfo' <- getOpts $ newServerInfo coreChan' dbQueriesChan Nothing |
4905 | 59 |
|
1964 | 60 |
#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
|
61 |
si <- readServerConfig serverInfo' |
1964 | 62 |
#else |
4960 | 63 |
let si = serverInfo' |
1964 | 64 |
#endif |
65 |
||
4960 | 66 |
(server si) `E.catch` handleRestart |