author | unc0rr |
Wed, 08 Jan 2014 13:04:50 +0400 | |
branch | webgl |
changeset 9974 | fb84e878111d |
parent 5209 | f7a610e2ef5f |
child 10051 | cc6f62d7aea2 |
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) |
1804 | 27 |
|
4960 | 28 |
|
4989 | 29 |
server :: ServerInfo -> IO () |
4960 | 30 |
server si = do |
31 |
proto <- getProtocolNumber "tcp" |
|
32 |
E.bracket |
|
33 |
(socket AF_INET Stream proto) |
|
34 |
sClose |
|
35 |
(\sock -> do |
|
36 |
setSocketOption sock ReuseAddr 1 |
|
37 |
bindSocket sock (SockAddrInet (listenPort si) iNADDR_ANY) |
|
38 |
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
|
39 |
startServer si{serverSocket = Just sock} |
4960 | 40 |
) |
41 |
||
42 |
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
|
43 |
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
|
44 |
noticeM "Core" "Shutting down" |
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 |
5209
f7a610e2ef5f
On restart command close server socket and spawn new server, keep running until last client quits
unc0rr
parents:
5119
diff
changeset
|
58 |
serverInfo' <- getOpts $ newServerInfo coreChan' dbQueriesChan Nothing 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 |