netserver/hedgewars-server.hs
author unc0rr
Tue, 04 Nov 2008 14:43:31 +0000
changeset 1469 5218aa76939e
parent 1468 6e6a75de2fc9
child 1473 60e1fad78d58
permissions -rw-r--r--
Handle exceptions on accept
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)
1461
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
     9
import Control.Monad
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
1461
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    20
data Messages =
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    21
	Accept ClientInfo
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    22
	| ClientMessage ([String], ClientInfo)
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    23
	| CoreMessage [String]
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    24
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    25
messagesLoop :: TChan [String] -> IO()
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    26
messagesLoop messagesChan = forever $ do
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    27
	threadDelay (30 * 10^6) -- 30 seconds
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    28
	atomically $ writeTChan messagesChan ["PING"]
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    29
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    30
acceptLoop :: Socket -> TChan ClientInfo -> IO ()
1469
5218aa76939e Handle exceptions on accept
unc0rr
parents: 1468
diff changeset
    31
acceptLoop servSock acceptChan = Control.Exception.handle (const $ putStrLn "exception on connect" >> acceptLoop servSock acceptChan) $ do
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    32
	(cHandle, host, port) <- accept servSock
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    33
	cChan <- atomically newTChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    34
	forkIO $ clientLoop cHandle cChan
1403
b8c921ed0f13 Bring back old 'Go!' button behavior
unc0rr
parents: 1398
diff changeset
    35
	atomically $ writeTChan acceptChan (ClientInfo cChan cHandle "" 0 "" False False False)
1469
5218aa76939e Handle exceptions on accept
unc0rr
parents: 1468
diff changeset
    36
	atomically $ writeTChan cChan ["ASKME"]
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    37
	acceptLoop servSock acceptChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    38
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    39
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    40
listenLoop :: Handle -> [String] -> TChan [String] -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    41
listenLoop handle buf chan = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    42
	str <- hGetLine handle
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    43
	if str == "" then do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    44
		atomically $ writeTChan chan buf
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    45
		listenLoop handle [] chan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    46
		else
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    47
		listenLoop handle (buf ++ [str]) chan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    48
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    49
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    50
clientLoop :: Handle -> TChan [String] -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    51
clientLoop handle chan =
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    52
	listenLoop handle [] chan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    53
		`catch` (const $ clientOff >> return ())
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    54
	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
    55
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    56
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    57
sendAnswers [] _ clients _ = return clients
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    58
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    59
	let recipients = handlesFunc client clients rooms
1381
e9754d1d61a9 Log reply only when send it
unc0rr
parents: 1370
diff changeset
    60
	unless (null recipients) $ putStrLn ("< " ++ (show answer))
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
	clHandles' <- forM recipients $
1468
6e6a75de2fc9 Reformat code a bit
unc0rr
parents: 1466
diff changeset
    63
		\ch -> Control.Exception.handle
6e6a75de2fc9 Reformat code a bit
unc0rr
parents: 1466
diff changeset
    64
			(\e -> putStrLn ("handle exception: " ++ show e) >>
6e6a75de2fc9 Reformat code a bit
unc0rr
parents: 1466
diff changeset
    65
				if head answer == "BYE" then
6e6a75de2fc9 Reformat code a bit
unc0rr
parents: 1466
diff changeset
    66
					return [ch]
6e6a75de2fc9 Reformat code a bit
unc0rr
parents: 1466
diff changeset
    67
				else
6e6a75de2fc9 Reformat code a bit
unc0rr
parents: 1466
diff changeset
    68
					atomically $ writeTChan (chan $ fromJust $ clientByHandle ch clients) ["QUIT"] >> return []  -- cannot just remove
6e6a75de2fc9 Reformat code a bit
unc0rr
parents: 1466
diff changeset
    69
			) $
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    70
			do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    71
			forM_ answer (\str -> hPutStrLn ch str)
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    72
			hPutStrLn ch ""
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    73
			hFlush ch
1466
c68b0a0969d3 It seems, I finally got the solution
unc0rr
parents: 1465
diff changeset
    74
			if head answer == "BYE" then return [ch] else return []
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    75
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    76
	let mclients = remove clients $ concat clHandles'
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    77
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    78
	sendAnswers answers client mclients rooms
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    79
	where
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    80
		remove list rmClHandles = deleteFirstsBy2t (\ a b -> (Miscutils.handle a) == b) list rmClHandles
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    81
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    82
1391
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    83
reactCmd :: [String] -> ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ([ClientInfo], [RoomInfo])
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    84
reactCmd cmd client clients rooms = do
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    85
	putStrLn ("> " ++ show cmd)
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    86
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    87
	let (clientsFunc, roomsFunc, answers) = handleCmd client clients rooms $ cmd
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    88
	let mrooms = roomsFunc rooms
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    89
	let mclients = (clientsFunc clients)
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    90
	let mclient = fromMaybe client $ find (== client) mclients
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    91
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    92
	clientsIn <- sendAnswers answers mclient mclients mrooms
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    93
	let quitClient = find forceQuit $ clientsIn
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    94
	if isJust quitClient then reactCmd ["QUIT"] (fromJust quitClient) clientsIn mrooms else return (clientsIn, mrooms)
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    95
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    96
1461
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    97
mainLoop :: Socket -> TChan ClientInfo -> TChan [String] -> [ClientInfo] -> [RoomInfo] -> IO ()
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    98
mainLoop servSock acceptChan messagesChan clients rooms = do
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    99
	r <- atomically $ (Accept `fmap` readTChan acceptChan) `orElse` (ClientMessage `fmap` tselect clients) `orElse` (CoreMessage `fmap` readTChan messagesChan)
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   100
	case r of
1461
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   101
		Accept ci ->
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   102
			mainLoop servSock acceptChan messagesChan (clients ++ [ci]) rooms
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   103
		ClientMessage (cmd, client) -> do
1391
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
   104
			(clientsIn, mrooms) <- reactCmd cmd client clients rooms
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   105
			
1385
ca72264f921a Shutdown private server when room is abandoned
unc0rr
parents: 1384
diff changeset
   106
			let hadRooms = (not $ null rooms) && (null mrooms)
ca72264f921a Shutdown private server when room is abandoned
unc0rr
parents: 1384
diff changeset
   107
				in unless ((not $ isDedicated globalOptions) && ((null clientsIn) || hadRooms)) $
1461
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   108
					mainLoop servSock acceptChan messagesChan clientsIn mrooms
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   109
		CoreMessage msg -> if not $ null $ clients then
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   110
			do
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   111
				let client = head clients -- don't care
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   112
				(clientsIn, mrooms) <- reactCmd msg client clients rooms
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   113
				mainLoop servSock acceptChan messagesChan clientsIn mrooms
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   114
			else
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   115
				mainLoop servSock acceptChan messagesChan clients rooms
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   116
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   117
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   118
startServer serverSocket = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   119
	acceptChan <- atomically newTChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   120
	forkIO $ acceptLoop serverSocket acceptChan
1461
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   121
	
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   122
	messagesChan <- atomically newTChan
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   123
	forkIO $ messagesLoop messagesChan
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   124
	
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   125
	mainLoop serverSocket acceptChan messagesChan [] []
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   126
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   127
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   128
main = withSocketsDo $ do
1398
29eedf717d0f Check for right define
unc0rr
parents: 1397
diff changeset
   129
#if !defined(mingw32_HOST_OS)
1396
abb28dcb6d0d - Send additional info on rooms
unc0rr
parents: 1394
diff changeset
   130
	installHandler sigPIPE Ignore Nothing;
1397
471c42a1c358 Use C preprocessor to allow compilation in windows
unc0rr
parents: 1396
diff changeset
   131
#endif
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1382
diff changeset
   132
	putStrLn $ "Listening on port " ++ show (listenPort globalOptions)
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1382
diff changeset
   133
	serverSocket <- listenOn $ PortNumber (listenPort globalOptions)
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   134
	startServer serverSocket `finally` sClose serverSocket