netserver/hedgewars-server.hs
author unc0rr
Sat, 08 Nov 2008 11:54:46 +0000
changeset 1484 c01512115c12
parent 1483 89e24edb6020
child 1492 2da1fe033f23
permissions -rw-r--r--
- Add es and sv translations to hedgewars.pro - perform lupdate-qt4 && lrelease-qt4
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
1478
8bfb417d165e Add simple DoS protection
unc0rr
parents: 1477
diff changeset
    15
import Data.Time
1397
471c42a1c358 Use C preprocessor to allow compilation in windows
unc0rr
parents: 1396
diff changeset
    16
1398
29eedf717d0f Check for right define
unc0rr
parents: 1397
diff changeset
    17
#if !defined(mingw32_HOST_OS)
1396
abb28dcb6d0d - Send additional info on rooms
unc0rr
parents: 1394
diff changeset
    18
import System.Posix
1397
471c42a1c358 Use C preprocessor to allow compilation in windows
unc0rr
parents: 1396
diff changeset
    19
#endif
471c42a1c358 Use C preprocessor to allow compilation in windows
unc0rr
parents: 1396
diff changeset
    20
1461
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    21
data Messages =
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    22
	Accept ClientInfo
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    23
	| ClientMessage ([String], ClientInfo)
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    24
	| CoreMessage [String]
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    25
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    26
messagesLoop :: TChan [String] -> IO()
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    27
messagesLoop messagesChan = forever $ do
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    28
	threadDelay (30 * 10^6) -- 30 seconds
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
    29
	atomically $ writeTChan messagesChan ["PING"]
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    30
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    31
acceptLoop :: Socket -> TChan ClientInfo -> IO ()
1483
89e24edb6020 Make code flow more clear
unc0rr
parents: 1482
diff changeset
    32
acceptLoop servSock acceptChan =
89e24edb6020 Make code flow more clear
unc0rr
parents: 1482
diff changeset
    33
	Control.Exception.handle (const $ putStrLn "exception on connect" >> acceptLoop servSock acceptChan) $
89e24edb6020 Make code flow more clear
unc0rr
parents: 1482
diff changeset
    34
	do
1478
8bfb417d165e Add simple DoS protection
unc0rr
parents: 1477
diff changeset
    35
	(cHandle, host, _) <- accept servSock
1483
89e24edb6020 Make code flow more clear
unc0rr
parents: 1482
diff changeset
    36
	
1478
8bfb417d165e Add simple DoS protection
unc0rr
parents: 1477
diff changeset
    37
	currentTime <- getCurrentTime
1481
f741afa7dbf3 Show time of connection start
unc0rr
parents: 1480
diff changeset
    38
	putStrLn $ (show currentTime) ++ " new client: " ++ host
1483
89e24edb6020 Make code flow more clear
unc0rr
parents: 1482
diff changeset
    39
	
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    40
	cChan <- atomically newTChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    41
	forkIO $ clientLoop cHandle cChan
1483
89e24edb6020 Make code flow more clear
unc0rr
parents: 1482
diff changeset
    42
	
89e24edb6020 Make code flow more clear
unc0rr
parents: 1482
diff changeset
    43
	atomically $ writeTChan acceptChan (ClientInfo cChan cHandle host currentTime "" 0 "" False False False)
1469
5218aa76939e Handle exceptions on accept
unc0rr
parents: 1468
diff changeset
    44
	atomically $ writeTChan cChan ["ASKME"]
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    45
	acceptLoop servSock acceptChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    46
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    47
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    48
listenLoop :: Handle -> [String] -> TChan [String] -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    49
listenLoop handle buf chan = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    50
	str <- hGetLine handle
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    51
	if str == "" then do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    52
		atomically $ writeTChan chan buf
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    53
		listenLoop handle [] chan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    54
		else
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    55
		listenLoop handle (buf ++ [str]) chan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    56
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    57
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    58
clientLoop :: Handle -> TChan [String] -> IO ()
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    59
clientLoop handle chan =
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    60
	listenLoop handle [] chan
1478
8bfb417d165e Add simple DoS protection
unc0rr
parents: 1477
diff changeset
    61
		`catch` (\e -> (clientOff $ show e) >> return ())
8bfb417d165e Add simple DoS protection
unc0rr
parents: 1477
diff changeset
    62
	where clientOff msg = atomically $ writeTChan chan ["QUIT", msg] -- if the client disconnects, we perform as if it sent QUIT message
1370
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
sendAnswers [] _ clients _ = return clients
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    66
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    67
	let recipients = handlesFunc client clients rooms
1476
b3b28e99570f Disconnect clients on BYE message
unc0rr
parents: 1475
diff changeset
    68
	--unless (null recipients) $ putStrLn ("< " ++ (show answer))
1478
8bfb417d165e Add simple DoS protection
unc0rr
parents: 1477
diff changeset
    69
	when (head answer == "NICK") $ putStrLn (show answer)
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    70
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    71
	clHandles' <- forM recipients $
1468
6e6a75de2fc9 Reformat code a bit
unc0rr
parents: 1466
diff changeset
    72
		\ch -> Control.Exception.handle
1478
8bfb417d165e Add simple DoS protection
unc0rr
parents: 1477
diff changeset
    73
			(\e -> if head answer == "BYE" then
1468
6e6a75de2fc9 Reformat code a bit
unc0rr
parents: 1466
diff changeset
    74
					return [ch]
6e6a75de2fc9 Reformat code a bit
unc0rr
parents: 1466
diff changeset
    75
				else
1478
8bfb417d165e Add simple DoS protection
unc0rr
parents: 1477
diff changeset
    76
					atomically $ writeTChan (chan $ fromJust $ clientByHandle ch clients) ["QUIT", show e] >> return []  -- cannot just remove
1468
6e6a75de2fc9 Reformat code a bit
unc0rr
parents: 1466
diff changeset
    77
			) $
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    78
			do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    79
			forM_ answer (\str -> hPutStrLn ch str)
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    80
			hPutStrLn ch ""
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    81
			hFlush ch
1466
c68b0a0969d3 It seems, I finally got the solution
unc0rr
parents: 1465
diff changeset
    82
			if head answer == "BYE" then return [ch] else return []
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    83
1476
b3b28e99570f Disconnect clients on BYE message
unc0rr
parents: 1475
diff changeset
    84
	let outHandles = concat clHandles'
1478
8bfb417d165e Add simple DoS protection
unc0rr
parents: 1477
diff changeset
    85
	unless (null outHandles) $ putStrLn ((show $ length outHandles) ++ " / " ++ (show $ length clients) ++ " : " ++ (show answer))
1476
b3b28e99570f Disconnect clients on BYE message
unc0rr
parents: 1475
diff changeset
    86
	mapM_ (\ch -> Control.Exception.handle (const $ putStrLn "error on hClose") (hClose ch)) outHandles
b3b28e99570f Disconnect clients on BYE message
unc0rr
parents: 1475
diff changeset
    87
	let mclients = remove clients outHandles
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    88
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    89
	sendAnswers answers client mclients rooms
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    90
	where
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    91
		remove list rmClHandles = deleteFirstsBy2t (\ a b -> (Miscutils.handle a) == b) list rmClHandles
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    92
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
    93
1391
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    94
reactCmd :: [String] -> ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ([ClientInfo], [RoomInfo])
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    95
reactCmd cmd client clients rooms = do
1473
60e1fad78d58 Cleanup code a bit, some reformatting
unc0rr
parents: 1469
diff changeset
    96
	--putStrLn ("> " ++ show cmd)
1391
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    97
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    98
	let (clientsFunc, roomsFunc, answers) = handleCmd client clients rooms $ cmd
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
    99
	let mrooms = roomsFunc rooms
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
   100
	let mclients = (clientsFunc clients)
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
   101
	let mclient = fromMaybe client $ find (== client) mclients
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
   102
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
   103
	clientsIn <- sendAnswers answers mclient mclients mrooms
1483
89e24edb6020 Make code flow more clear
unc0rr
parents: 1482
diff changeset
   104
	mapM_ (\cl -> atomically $ writeTChan (chan cl) ["QUIT", "Kicked"]) $ filter forceQuit $ clientsIn
1474
8817adb86da6 Oops, fix build
unc0rr
parents: 1473
diff changeset
   105
	
1483
89e24edb6020 Make code flow more clear
unc0rr
parents: 1482
diff changeset
   106
	return (clientsIn, mrooms)
1391
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
   107
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
   108
1480
aec44e91f2d1 Remove redundant function parameter
unc0rr
parents: 1478
diff changeset
   109
mainLoop :: TChan ClientInfo -> TChan [String] -> [ClientInfo] -> [RoomInfo] -> IO ()
aec44e91f2d1 Remove redundant function parameter
unc0rr
parents: 1478
diff changeset
   110
mainLoop acceptChan messagesChan clients rooms = do
1473
60e1fad78d58 Cleanup code a bit, some reformatting
unc0rr
parents: 1469
diff changeset
   111
	r <- atomically $
60e1fad78d58 Cleanup code a bit, some reformatting
unc0rr
parents: 1469
diff changeset
   112
		(Accept `fmap` readTChan acceptChan) `orElse`
60e1fad78d58 Cleanup code a bit, some reformatting
unc0rr
parents: 1469
diff changeset
   113
		(ClientMessage `fmap` tselect clients) `orElse`
60e1fad78d58 Cleanup code a bit, some reformatting
unc0rr
parents: 1469
diff changeset
   114
		(CoreMessage `fmap` readTChan messagesChan)
1484
c01512115c12 - Add es and sv translations to hedgewars.pro
unc0rr
parents: 1483
diff changeset
   115
	
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   116
	case r of
1478
8bfb417d165e Add simple DoS protection
unc0rr
parents: 1477
diff changeset
   117
		Accept ci -> do
8bfb417d165e Add simple DoS protection
unc0rr
parents: 1477
diff changeset
   118
			let sameHostClients = filter (\cl -> host ci == host cl) clients
1483
89e24edb6020 Make code flow more clear
unc0rr
parents: 1482
diff changeset
   119
			let haveJustConnected = not $ null $ filter (\cl -> connectTime ci `diffUTCTime` connectTime cl <= 25) sameHostClients
1478
8bfb417d165e Add simple DoS protection
unc0rr
parents: 1477
diff changeset
   120
			
8bfb417d165e Add simple DoS protection
unc0rr
parents: 1477
diff changeset
   121
			when haveJustConnected $ do
1483
89e24edb6020 Make code flow more clear
unc0rr
parents: 1482
diff changeset
   122
				atomically $ do
89e24edb6020 Make code flow more clear
unc0rr
parents: 1482
diff changeset
   123
					--writeTChan (chan ci) ["ERROR", "Reconnected too fast"]
89e24edb6020 Make code flow more clear
unc0rr
parents: 1482
diff changeset
   124
					writeTChan (chan ci) ["QUIT", "Reconnected too fast"]
1478
8bfb417d165e Add simple DoS protection
unc0rr
parents: 1477
diff changeset
   125
				
1480
aec44e91f2d1 Remove redundant function parameter
unc0rr
parents: 1478
diff changeset
   126
			mainLoop acceptChan messagesChan (clients ++ [ci]) rooms
1484
c01512115c12 - Add es and sv translations to hedgewars.pro
unc0rr
parents: 1483
diff changeset
   127
			
1461
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   128
		ClientMessage (cmd, client) -> do
1391
735f6d43780b Implement kick
unc0rr
parents: 1385
diff changeset
   129
			(clientsIn, mrooms) <- reactCmd cmd client clients rooms
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   130
			
1385
ca72264f921a Shutdown private server when room is abandoned
unc0rr
parents: 1384
diff changeset
   131
			let hadRooms = (not $ null rooms) && (null mrooms)
ca72264f921a Shutdown private server when room is abandoned
unc0rr
parents: 1384
diff changeset
   132
				in unless ((not $ isDedicated globalOptions) && ((null clientsIn) || hadRooms)) $
1480
aec44e91f2d1 Remove redundant function parameter
unc0rr
parents: 1478
diff changeset
   133
					mainLoop acceptChan messagesChan clientsIn mrooms
1484
c01512115c12 - Add es and sv translations to hedgewars.pro
unc0rr
parents: 1483
diff changeset
   134
		
1473
60e1fad78d58 Cleanup code a bit, some reformatting
unc0rr
parents: 1469
diff changeset
   135
		CoreMessage msg ->
60e1fad78d58 Cleanup code a bit, some reformatting
unc0rr
parents: 1469
diff changeset
   136
			if not $ null $ clients then
60e1fad78d58 Cleanup code a bit, some reformatting
unc0rr
parents: 1469
diff changeset
   137
				do
1461
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   138
				let client = head clients -- don't care
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   139
				(clientsIn, mrooms) <- reactCmd msg client clients rooms
1480
aec44e91f2d1 Remove redundant function parameter
unc0rr
parents: 1478
diff changeset
   140
				mainLoop acceptChan messagesChan clientsIn mrooms
1461
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   141
			else
1480
aec44e91f2d1 Remove redundant function parameter
unc0rr
parents: 1478
diff changeset
   142
				mainLoop acceptChan messagesChan clients rooms
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   143
1480
aec44e91f2d1 Remove redundant function parameter
unc0rr
parents: 1478
diff changeset
   144
startServer :: Socket -> IO()
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   145
startServer serverSocket = do
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   146
	acceptChan <- atomically newTChan
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   147
	forkIO $ acceptLoop serverSocket acceptChan
1461
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   148
	
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   149
	messagesChan <- atomically newTChan
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   150
	forkIO $ messagesLoop messagesChan
87e5a6c3882c Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents: 1403
diff changeset
   151
	
1480
aec44e91f2d1 Remove redundant function parameter
unc0rr
parents: 1478
diff changeset
   152
	mainLoop acceptChan messagesChan [] []
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   153
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   154
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   155
main = withSocketsDo $ do
1398
29eedf717d0f Check for right define
unc0rr
parents: 1397
diff changeset
   156
#if !defined(mingw32_HOST_OS)
1396
abb28dcb6d0d - Send additional info on rooms
unc0rr
parents: 1394
diff changeset
   157
	installHandler sigPIPE Ignore Nothing;
1397
471c42a1c358 Use C preprocessor to allow compilation in windows
unc0rr
parents: 1396
diff changeset
   158
#endif
1383
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1382
diff changeset
   159
	putStrLn $ "Listening on port " ++ show (listenPort globalOptions)
d20e6e8928e3 Refactor options handling and storing
unc0rr
parents: 1382
diff changeset
   160
	serverSocket <- listenOn $ PortNumber (listenPort globalOptions)
1370
ff8863ebde17 Add hedgewars server to build process
unc0rr
parents:
diff changeset
   161
	startServer serverSocket `finally` sClose serverSocket