author | unc0rr |
Mon, 28 Feb 2011 22:04:10 +0300 | |
changeset 4973 | 53411a26df7e |
parent 4968 | 8e1673f0dc05 |
child 4974 | 078cd026a7b1 |
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 |
4973
53411a26df7e
Add server version (which is separate from protocol version) and a check in frontend for a new enough server (currently only qWarning)
unc0rr
parents:
4968
diff
changeset
|
11 |
#if defined(OFFICIAL_SERVER) |
4968 | 12 |
import Control.Monad |
4973
53411a26df7e
Add server version (which is separate from protocol version) and a check in frontend for a new enough server (currently only qWarning)
unc0rr
parents:
4968
diff
changeset
|
13 |
#endif |
1804 | 14 |
----------------------------------- |
15 |
import Opts |
|
16 |
import CoreTypes |
|
17 |
import ServerCore |
|
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 |
|
30 |
server :: ServerInfo -> IO () |
|
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 |
4957 | 60 |
serverInfo' <- getOpts $ newServerInfo coreChan' dbQueriesChan |
4905 | 61 |
|
1964 | 62 |
#if defined(OFFICIAL_SERVER) |
4968 | 63 |
[dbHost', dbLogin', dbPassword'] <- liftM read $ readFile "hedgewars-server.ini" |
4960 | 64 |
let si = serverInfo'{dbHost = dbHost', dbLogin = dbLogin', dbPassword = dbPassword'} |
1964 | 65 |
#else |
4960 | 66 |
let si = serverInfo' |
1964 | 67 |
#endif |
68 |
||
4960 | 69 |
(server si) `E.catch` handleRestart |