netserver/hedgewars-server.hs
author unc0rr
Sat, 18 Oct 2008 12:03:37 +0000
changeset 1371 e206cdb009a7
parent 1370 ff8863ebde17
child 1381 e9754d1d61a9
permissions -rw-r--r--
Fix compilation
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)
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
     9
import Control.Monad (forM, forM_, filterM, liftM)
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    10
import Maybe (fromMaybe)
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
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    15
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    16
acceptLoop :: Socket -> TChan ClientInfo -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    17
acceptLoop servSock acceptChan = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    18
	(cHandle, host, port) <- accept servSock
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    19
	cChan <- atomically newTChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    20
	forkIO $ clientLoop cHandle cChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    21
	atomically $ writeTChan acceptChan (ClientInfo cChan cHandle "" 0 "" False)
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    22
	hPutStrLn cHandle "CONNECTED\n"
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    23
	hFlush cHandle
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    24
	acceptLoop servSock acceptChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    25
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    26
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    27
listenLoop :: Handle -> [String] -> TChan [String] -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    28
listenLoop handle buf chan = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    29
	str <- hGetLine handle
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    30
	if str == "" then do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    31
		atomically $ writeTChan chan buf
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    32
		listenLoop handle [] chan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    33
		else
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    34
		listenLoop handle (buf ++ [str]) chan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    35
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    36
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    37
clientLoop :: Handle -> TChan [String] -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    38
clientLoop handle chan =
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    39
	listenLoop handle [] chan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    40
		`catch` (const $ clientOff >> return ())
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    41
	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
    42
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    43
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    44
sendAnswers [] _ clients _ = return clients
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    45
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    46
	let recipients = handlesFunc client clients rooms
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    47
	putStrLn ("< " ++ (show answer))
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    48
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    49
	clHandles' <- forM recipients $
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    50
		\ch -> Control.Exception.handle (\e -> putStrLn (show e) >> hClose ch >> return [ch]) $
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    51
			if (not $ null answer) && (head answer == "off") then hClose ch >> return [ch] else -- probably client with exception, don't send him anything
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
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    56
			if (not $ null answer) && (head answer == "BYE") then hClose ch >> return [ch] else return []
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
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    65
mainLoop :: Socket -> TChan ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    66
mainLoop servSock acceptChan clients rooms = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    67
	r <- atomically $ (Left `fmap` readTChan acceptChan) `orElse` (Right `fmap` tselect clients)
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    68
	case r of
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    69
		Left ci -> do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    70
			mainLoop servSock acceptChan (clients ++ [ci]) rooms
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    71
		Right (cmd, client) -> do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    72
			putStrLn ("> " ++ show cmd)
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    73
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    74
			let (clientsFunc, roomsFunc, answers) = handleCmd client clients rooms $ cmd
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    75
			let mrooms = roomsFunc rooms
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    76
			let mclients = (clientsFunc clients)
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    77
			let mclient = fromMaybe client $ find (== client) mclients
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    78
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    79
			clientsIn <- sendAnswers answers mclient mclients mrooms
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    80
			
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    81
			mainLoop servSock acceptChan clientsIn mrooms
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    82
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    83
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    84
startServer serverSocket = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    85
	acceptChan <- atomically newTChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    86
	forkIO $ acceptLoop serverSocket acceptChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    87
	mainLoop serverSocket acceptChan [] []
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    88
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    89
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    90
main = withSocketsDo $ do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    91
	flags <- opts
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    92
	putStrLn $ "Listening on port " ++ show (getPort flags)
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    93
	serverSocket <- listenOn $ PortNumber (getPort flags)
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    94
	startServer serverSocket `finally` sClose serverSocket