netserver/hedgewars-server.hs
author unc0rr
Wed, 22 Oct 2008 15:31:35 +0000
changeset 1396 abb28dcb6d0d
parent 1394 962001cfcf48
child 1397 471c42a1c358
permissions -rw-r--r--
- Send additional info on rooms - Handle SIGPIPE
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
1396
abb28dcb6d0d - Send additional info on rooms
unc0rr
parents: 1394
diff changeset
    15
import System.Posix
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    16
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    17
acceptLoop :: Socket -> TChan ClientInfo -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    18
acceptLoop servSock acceptChan = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    19
	(cHandle, host, port) <- accept servSock
1384
329d3308e2e3 Support for private servers in frontend
unc0rr
parents: 1383
diff changeset
    20
	hPutStrLn cHandle "CONNECTED\n"
329d3308e2e3 Support for private servers in frontend
unc0rr
parents: 1383
diff changeset
    21
	hFlush cHandle
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    22
	cChan <- atomically newTChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    23
	forkIO $ clientLoop cHandle cChan
1391
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    24
	atomically $ writeTChan acceptChan (ClientInfo cChan cHandle "" 0 "" False False)
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    25
	acceptLoop servSock acceptChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    26
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    27
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    28
listenLoop :: Handle -> [String] -> TChan [String] -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    29
listenLoop handle buf chan = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    30
	str <- hGetLine handle
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    31
	if str == "" then do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    32
		atomically $ writeTChan chan buf
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    33
		listenLoop handle [] chan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    34
		else
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    35
		listenLoop handle (buf ++ [str]) chan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    36
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    37
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    38
clientLoop :: Handle -> TChan [String] -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    39
clientLoop handle chan =
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    40
	listenLoop handle [] chan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    41
		`catch` (const $ clientOff >> return ())
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    42
	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
    43
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    44
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    45
sendAnswers [] _ clients _ = return clients
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    46
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    47
	let recipients = handlesFunc client clients rooms
1381
e9754d1d61a9 Log reply only when send it
unc0rr
parents: 1370
diff changeset
    48
	unless (null recipients) $ putStrLn ("< " ++ (show answer))
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    49
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    50
	clHandles' <- forM recipients $
1394
962001cfcf48 If exception is on client quit, then just remove that client
unc0rr
parents: 1393
diff changeset
    51
		\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
    52
			do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    53
			forM_ answer (\str -> hPutStrLn ch str)
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    54
			hPutStrLn ch ""
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    55
			hFlush ch
1394
962001cfcf48 If exception is on client quit, then just remove that client
unc0rr
parents: 1393
diff changeset
    56
			if head answer == "BYE" then hClose ch >> return [ch] else return []
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    57
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    58
	let mclients = remove clients $ concat clHandles'
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    59
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    60
	sendAnswers answers client mclients rooms
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    61
	where
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    62
		remove list rmClHandles = deleteFirstsBy2t (\ a b -> (Miscutils.handle a) == b) list rmClHandles
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    63
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    64
1391
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    65
reactCmd :: [String] -> ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ([ClientInfo], [RoomInfo])
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    66
reactCmd cmd client clients rooms = do
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    67
	putStrLn ("> " ++ show cmd)
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    68
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    69
	let (clientsFunc, roomsFunc, answers) = handleCmd client clients rooms $ cmd
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    70
	let mrooms = roomsFunc rooms
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    71
	let mclients = (clientsFunc clients)
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    72
	let mclient = fromMaybe client $ find (== client) mclients
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    73
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    74
	clientsIn <- sendAnswers answers mclient mclients mrooms
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    75
	let quitClient = find forceQuit $ clientsIn
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    76
	if isJust quitClient then reactCmd ["QUIT"] (fromJust quitClient) clientsIn mrooms else return (clientsIn, mrooms)
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    77
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    78
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    79
mainLoop :: Socket -> TChan ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    80
mainLoop servSock acceptChan clients rooms = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    81
	r <- atomically $ (Left `fmap` readTChan acceptChan) `orElse` (Right `fmap` tselect clients)
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    82
	case r of
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    83
		Left ci -> do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    84
			mainLoop servSock acceptChan (clients ++ [ci]) rooms
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    85
		Right (cmd, client) -> do
1391
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    86
			(clientsIn, mrooms) <- reactCmd cmd client clients rooms
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    87
			
1385
ca72264f921a Shutdown private server when room is abandoned
unc0rr
parents: 1384
diff changeset
    88
			let hadRooms = (not $ null rooms) && (null mrooms)
ca72264f921a Shutdown private server when room is abandoned
unc0rr
parents: 1384
diff changeset
    89
				in unless ((not $ isDedicated globalOptions) && ((null clientsIn) || hadRooms)) $
ca72264f921a Shutdown private server when room is abandoned
unc0rr
parents: 1384
diff changeset
    90
					mainLoop servSock acceptChan clientsIn mrooms
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    91
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    92
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    93
startServer serverSocket = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    94
	acceptChan <- atomically newTChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    95
	forkIO $ acceptLoop serverSocket acceptChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    96
	mainLoop serverSocket acceptChan [] []
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    97
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    98
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    99
main = withSocketsDo $ do
1396
abb28dcb6d0d - Send additional info on rooms
unc0rr
parents: 1394
diff changeset
   100
	installHandler sigPIPE Ignore Nothing;
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1382
diff changeset
   101
	putStrLn $ "Listening on port " ++ show (listenPort globalOptions)
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1382
diff changeset
   102
	serverSocket <- listenOn $ PortNumber (listenPort globalOptions)
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   103
	startServer serverSocket `finally` sClose serverSocket