author | nemo |
Sun, 07 Sep 2014 15:11:08 -0400 | |
changeset 10403 | bc9433b33947 |
parent 10051 | cc6f62d7aea2 |
child 10460 | 8dcea9087d75 |
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 |
10 |
----------------------------------- |
|
11 |
import Opts |
|
12 |
import CoreTypes |
|
13 |
import ServerCore |
|
4991 | 14 |
#if defined(OFFICIAL_SERVER) |
4974
078cd026a7b1
Add stubs for server config reading and writing routines
unc0rr
parents:
4973
diff
changeset
|
15 |
import ConfigFile |
4991 | 16 |
#endif |
1804 | 17 |
|
18 |
#if !defined(mingw32_HOST_OS) |
|
19 |
import System.Posix |
|
20 |
#endif |
|
21 |
||
22 |
||
4905 | 23 |
setupLoggers :: IO () |
5209
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5119
diff
changeset
|
24 |
setupLoggers = do |
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5119
diff
changeset
|
25 |
updateGlobalLogger "Clients" (setLevel NOTICE) |
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5119
diff
changeset
|
26 |
updateGlobalLogger "Core" (setLevel NOTICE) |
10051 | 27 |
updateGlobalLogger "REPLAYS" (setLevel NOTICE) |
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 |
|
5209
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5119
diff
changeset
|
40 |
startServer si{serverSocket = Just sock} |
4960 | 41 |
) |
42 |
||
43 |
handleRestart :: ShutdownException -> IO () |
|
5209
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5119
diff
changeset
|
44 |
handleRestart ShutdownException = do |
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5119
diff
changeset
|
45 |
noticeM "Core" "Shutting down" |
4960 | 46 |
return () |
47 |
||
4905 | 48 |
main :: IO () |
1804 | 49 |
main = withSocketsDo $ do |
50 |
#if !defined(mingw32_HOST_OS) |
|
4932 | 51 |
_ <- installHandler sigPIPE Ignore Nothing |
52 |
_ <- installHandler sigCHLD Ignore Nothing |
|
1804 | 53 |
#endif |
54 |
||
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
55 |
setupLoggers |
1804 | 56 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2349
diff
changeset
|
57 |
dbQueriesChan <- newChan |
4905 | 58 |
coreChan' <- newChan |
5209
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5119
diff
changeset
|
59 |
serverInfo' <- getOpts $ newServerInfo coreChan' dbQueriesChan Nothing Nothing |
4905 | 60 |
|
1964 | 61 |
#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
|
62 |
si <- readServerConfig serverInfo' |
1964 | 63 |
#else |
4960 | 64 |
let si = serverInfo' |
1964 | 65 |
#endif |
66 |
||
4960 | 67 |
(server si) `E.catch` handleRestart |