netserver/hedgewars-server.hs
author unc0rr
Sun, 26 Oct 2008 13:00:35 +0000
changeset 1415 6fbfee0e113a
parent 1403 b8c921ed0f13
child 1461 87e5a6c3882c
permissions -rw-r--r--
Allow build without net game server: introduce -DWITH_SERVER configuration parameter
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
     1
module Main where
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
     2
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
     3
import Network
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
     4
import IO
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
     5
import System.IO
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
     6
import Control.Concurrent
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
     7
import Control.Concurrent.STM
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
     8
import Control.Exception (setUncaughtExceptionHandler, handle, finally)
1384
329d3308e2e3 Support for private servers in frontend
unc0rr
parents: 1383
diff changeset
     9
import Control.Monad (forM, forM_, filterM, liftM, when, unless)
1391
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    10
import Maybe (fromMaybe, isJust, fromJust)
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    11
import Data.List
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    12
import Miscutils
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    13
import HWProto
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    14
import Opts
1397
471c42a1c358 Use C preprocessor to allow compilation in windows
unc0rr
parents: 1396
diff changeset
    15
1398
29eedf717d0f Check for right define
unc0rr
parents: 1397
diff changeset
    16
#if !defined(mingw32_HOST_OS)
1396
abb28dcb6d0d - Send additional info on rooms
unc0rr
parents: 1394
diff changeset
    17
import System.Posix
1397
471c42a1c358 Use C preprocessor to allow compilation in windows
unc0rr
parents: 1396
diff changeset
    18
#endif
471c42a1c358 Use C preprocessor to allow compilation in windows
unc0rr
parents: 1396
diff changeset
    19
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    20
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    21
acceptLoop :: Socket -> TChan ClientInfo -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    22
acceptLoop servSock acceptChan = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    23
	(cHandle, host, port) <- accept servSock
1384
329d3308e2e3 Support for private servers in frontend
unc0rr
parents: 1383
diff changeset
    24
	hPutStrLn cHandle "CONNECTED\n"
329d3308e2e3 Support for private servers in frontend
unc0rr
parents: 1383
diff changeset
    25
	hFlush cHandle
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    26
	cChan <- atomically newTChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    27
	forkIO $ clientLoop cHandle cChan
1403
b8c921ed0f13 Bring back old 'Go!' button behavior
unc0rr
parents: 1398
diff changeset
    28
	atomically $ writeTChan acceptChan (ClientInfo cChan cHandle "" 0 "" False False False)
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    29
	acceptLoop servSock acceptChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    30
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    31
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    32
listenLoop :: Handle -> [String] -> TChan [String] -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    33
listenLoop handle buf chan = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    34
	str <- hGetLine handle
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    35
	if str == "" then do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    36
		atomically $ writeTChan chan buf
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    37
		listenLoop handle [] chan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    38
		else
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    39
		listenLoop handle (buf ++ [str]) chan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    40
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    41
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    42
clientLoop :: Handle -> TChan [String] -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    43
clientLoop handle chan =
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    44
	listenLoop handle [] chan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    45
		`catch` (const $ clientOff >> return ())
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    46
	where clientOff = atomically $ writeTChan chan ["QUIT"] -- if the client disconnects, we perform as if it sent QUIT message
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    47
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    48
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    49
sendAnswers [] _ clients _ = return clients
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    50
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    51
	let recipients = handlesFunc client clients rooms
1381
e9754d1d61a9 Log reply only when send it
unc0rr
parents: 1370
diff changeset
    52
	unless (null recipients) $ putStrLn ("< " ++ (show answer))
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    53
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    54
	clHandles' <- forM recipients $
1394
962001cfcf48 If exception is on client quit, then just remove that client
unc0rr
parents: 1393
diff changeset
    55
		\ch -> Control.Exception.handle (\e -> putStrLn ("handle exception: " ++ show e) >> if head answer == "BYE" then return [ch] else return []) $ -- cannot just remove
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    56
			do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    57
			forM_ answer (\str -> hPutStrLn ch str)
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    58
			hPutStrLn ch ""
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    59
			hFlush ch
1394
962001cfcf48 If exception is on client quit, then just remove that client
unc0rr
parents: 1393
diff changeset
    60
			if head answer == "BYE" then hClose ch >> return [ch] else return []
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    61
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    62
	let mclients = remove clients $ concat clHandles'
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    63
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    64
	sendAnswers answers client mclients rooms
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    65
	where
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    66
		remove list rmClHandles = deleteFirstsBy2t (\ a b -> (Miscutils.handle a) == b) list rmClHandles
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    67
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    68
1391
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    69
reactCmd :: [String] -> ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ([ClientInfo], [RoomInfo])
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    70
reactCmd cmd client clients rooms = do
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    71
	putStrLn ("> " ++ show cmd)
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    72
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    73
	let (clientsFunc, roomsFunc, answers) = handleCmd client clients rooms $ cmd
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    74
	let mrooms = roomsFunc rooms
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    75
	let mclients = (clientsFunc clients)
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    76
	let mclient = fromMaybe client $ find (== client) mclients
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    77
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    78
	clientsIn <- sendAnswers answers mclient mclients mrooms
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    79
	let quitClient = find forceQuit $ clientsIn
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    80
	if isJust quitClient then reactCmd ["QUIT"] (fromJust quitClient) clientsIn mrooms else return (clientsIn, mrooms)
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    81
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    82
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    83
mainLoop :: Socket -> TChan ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    84
mainLoop servSock acceptChan clients rooms = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    85
	r <- atomically $ (Left `fmap` readTChan acceptChan) `orElse` (Right `fmap` tselect clients)
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    86
	case r of
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    87
		Left ci -> do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    88
			mainLoop servSock acceptChan (clients ++ [ci]) rooms
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    89
		Right (cmd, client) -> do
1391
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    90
			(clientsIn, mrooms) <- reactCmd cmd client clients rooms
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    91
			
1385
ca72264f921a Shutdown private server when room is abandoned
unc0rr
parents: 1384
diff changeset
    92
			let hadRooms = (not $ null rooms) && (null mrooms)
ca72264f921a Shutdown private server when room is abandoned
unc0rr
parents: 1384
diff changeset
    93
				in unless ((not $ isDedicated globalOptions) && ((null clientsIn) || hadRooms)) $
ca72264f921a Shutdown private server when room is abandoned
unc0rr
parents: 1384
diff changeset
    94
					mainLoop servSock acceptChan clientsIn mrooms
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    95
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    96
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    97
startServer serverSocket = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    98
	acceptChan <- atomically newTChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    99
	forkIO $ acceptLoop serverSocket acceptChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   100
	mainLoop serverSocket acceptChan [] []
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   101
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   102
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   103
main = withSocketsDo $ do
1398
29eedf717d0f Check for right define
unc0rr
parents: 1397
diff changeset
   104
#if !defined(mingw32_HOST_OS)
1396
abb28dcb6d0d - Send additional info on rooms
unc0rr
parents: 1394
diff changeset
   105
	installHandler sigPIPE Ignore Nothing;
1397
471c42a1c358 Use C preprocessor to allow compilation in windows
unc0rr
parents: 1396
diff changeset
   106
#endif
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1382
diff changeset
   107
	putStrLn $ "Listening on port " ++ show (listenPort globalOptions)
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1382
diff changeset
   108
	serverSocket <- listenOn $ PortNumber (listenPort globalOptions)
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   109
	startServer serverSocket `finally` sClose serverSocket