author | unc0rr |
Sun, 27 Feb 2011 20:15:09 +0300 | |
changeset 4962 | 705c6186ad9d |
parent 4960 | 3b54b1c9b768 |
child 4968 | 8e1673f0dc05 |
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 |
|
4921 | 15 |
#if defined(OFFICIAL_SERVER) |
16 |
import Utils |
|
17 |
#endif |
|
1804 | 18 |
|
19 |
||
20 |
#if !defined(mingw32_HOST_OS) |
|
21 |
import System.Posix |
|
22 |
#endif |
|
23 |
||
24 |
||
4905 | 25 |
setupLoggers :: IO () |
1804 | 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 | 29 |
|
4960 | 30 |
|
31 |
server :: ServerInfo -> IO () |
|
32 |
server si = do |
|
33 |
proto <- getProtocolNumber "tcp" |
|
34 |
E.bracket |
|
35 |
(socket AF_INET Stream proto) |
|
36 |
sClose |
|
37 |
(\sock -> do |
|
38 |
setSocketOption sock ReuseAddr 1 |
|
39 |
bindSocket sock (SockAddrInet (listenPort si) iNADDR_ANY) |
|
40 |
listen sock maxListenQueue |
|
41 |
startServer si sock |
|
42 |
) |
|
43 |
||
44 |
handleRestart :: ShutdownException -> IO () |
|
45 |
handleRestart ShutdownException = return () |
|
46 |
handleRestart RestartException = do |
|
4962 | 47 |
_ <- createProcess (proc "./hedgewars-server" []) |
4960 | 48 |
return () |
49 |
||
4905 | 50 |
main :: IO () |
1804 | 51 |
main = withSocketsDo $ do |
52 |
#if !defined(mingw32_HOST_OS) |
|
4932 | 53 |
_ <- installHandler sigPIPE Ignore Nothing |
54 |
_ <- installHandler sigCHLD Ignore Nothing |
|
1804 | 55 |
#endif |
56 |
||
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
57 |
setupLoggers |
1804 | 58 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
59 |
dbQueriesChan <- newChan |
4905 | 60 |
coreChan' <- newChan |
4957 | 61 |
serverInfo' <- getOpts $ newServerInfo coreChan' dbQueriesChan |
4905 | 62 |
|
1964 | 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 | 67 |
let si = serverInfo'{dbHost = dbHost', dbLogin = dbLogin', dbPassword = dbPassword'} |
1964 | 68 |
#else |
4960 | 69 |
let si = serverInfo' |
1964 | 70 |
#endif |
71 |
||
4960 | 72 |
(server si) `E.catch` handleRestart |