netserver/newhwserv.hs
author unc0rr
Sun, 05 Oct 2008 23:36:11 +0000
changeset 1306 e848447f29be
parent 1305 453882eb4467
child 1307 ce26e16d18ab
permissions -rw-r--r--
Fix server crash on client disconnect
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
     1
module Main where
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
     2
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
     3
import Network
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
     4
import IO
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
     5
import System.IO
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
     6
import Control.Concurrent
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
     7
import Control.Concurrent.STM
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
     8
import Control.Exception (finally)
890
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
     9
import Control.Monad (forM, forM_, filterM, liftM)
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    10
import Data.List
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    11
import Miscutils
890
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    12
import HWProto
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    13
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    14
acceptLoop :: Socket -> TChan ClientInfo -> IO ()
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    15
acceptLoop servSock acceptChan = do
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    16
	(cHandle, host, port) <- accept servSock
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    17
	cChan <- atomically newTChan
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    18
	forkIO $ clientLoop cHandle cChan
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    19
	atomically $ writeTChan acceptChan (ClientInfo cChan cHandle "" 0 "" False)
1082
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1081
diff changeset
    20
	hPutStrLn cHandle "CONNECTED\n"
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    21
	acceptLoop servSock acceptChan
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    22
1082
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1081
diff changeset
    23
listenLoop :: Handle -> [String] -> TChan [String] -> IO ()
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1081
diff changeset
    24
listenLoop handle buf chan = do
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    25
	str <- hGetLine handle
1082
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1081
diff changeset
    26
	if str == "" then do
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1081
diff changeset
    27
		atomically $ writeTChan chan buf
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1081
diff changeset
    28
		listenLoop handle [] chan
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1081
diff changeset
    29
		else
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1081
diff changeset
    30
		listenLoop handle (buf ++ [str]) chan
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    31
1082
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1081
diff changeset
    32
clientLoop :: Handle -> TChan [String] -> IO ()
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    33
clientLoop handle chan =
1082
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1081
diff changeset
    34
	listenLoop handle [] chan
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    35
		`catch` (const $ clientOff >> return ())
1304
05cebf68ebd8 Start refactoring standalone server (prepare to change protocol)
unc0rr
parents: 1302
diff changeset
    36
	where clientOff = atomically $ writeTChan chan ["QUIT"] -- если клиент отключается, то делаем вид, что от него пришла команда QUIT
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    37
1306
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    38
sendAnswers [] _ clients _ = return clients
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    39
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    40
	putStrLn ("< " ++ show answer)
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    41
	
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    42
	let recipients = handlesFunc client clients rooms
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    43
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    44
	clHandles' <- forM recipients $
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    45
		\ch -> do
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    46
			forM_ answer (\str -> hPutStrLn ch str)
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    47
			hPutStrLn ch ""
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    48
			hFlush ch
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    49
			if (not $ null answer) && (head answer == "BYE") then hClose ch >> return [ch] else return []
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    50
		`catch` const (hClose ch >> return [ch])
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    51
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    52
	let mclients = remove clients $ concat clHandles'
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    53
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    54
	sendAnswers answers client mclients rooms
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    55
	where
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    56
		remove list rmClHandles = deleteFirstsBy2t (\ a b -> (handle a) == b) list rmClHandles
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    57
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    58
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    59
mainLoop :: Socket -> TChan ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ()
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    60
mainLoop servSock acceptChan clients rooms = do
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    61
	r <- atomically $ (Left `fmap` readTChan acceptChan) `orElse` (Right `fmap` tselect clients)
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    62
	case r of
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    63
		Left ci -> do
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    64
			mainLoop servSock acceptChan (ci:clients) rooms
1082
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1081
diff changeset
    65
		Right (cmd, client) -> do
1302
4290ba4a14ca Remove deprecated stuff
unc0rr
parents: 1082
diff changeset
    66
			putStrLn ("> " ++ show cmd)
1305
453882eb4467 - Fix build of server (has some bugs now)
unc0rr
parents: 1304
diff changeset
    67
			let (clientsFunc, roomsFunc, answers) = handleCmd client clients rooms $ cmd
890
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    68
1082
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1081
diff changeset
    69
			let mclients = clientsFunc clients
596b1dcdc1df - Modify network protocol to use new delimiter
unc0rr
parents: 1081
diff changeset
    70
			let mrooms = roomsFunc rooms
1305
453882eb4467 - Fix build of server (has some bugs now)
unc0rr
parents: 1304
diff changeset
    71
1306
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    72
			mclients <- sendAnswers answers client clients rooms
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    73
			
e848447f29be Fix server crash on client disconnect
unc0rr
parents: 1305
diff changeset
    74
			mainLoop servSock acceptChan mclients mrooms
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    75
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    76
startServer serverSocket = do
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    77
	acceptChan <- atomically newTChan
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    78
	forkIO $ acceptLoop serverSocket acceptChan
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    79
	mainLoop serverSocket acceptChan [] []
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    80
878
45bff6dadfce - Fix baseball bat
unc0rr
parents: 877
diff changeset
    81
main = withSocketsDo $ do
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    82
	serverSocket <- listenOn $ Service "hedgewars"
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    83
	startServer serverSocket `finally` sClose serverSocket