gameServer/ClientIO.hs
author unc0rr
Fri, 26 Jun 2009 19:40:10 +0000
changeset 2197 a02adcdaa939
parent 2126 cb249fa8e3da
child 2296 19f2f76dc346
permissions -rw-r--r--
Some work on camera
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     1
{-# LANGUAGE PatternSignatures #-}
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     2
module ClientIO where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     3
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     4
import qualified Control.Exception
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     5
import Control.Concurrent.Chan
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     6
import Control.Monad
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     7
import System.IO
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     8
----------------
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     9
import CoreTypes
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    10
2001
d909152bdc21 - More verbose output
unc0rr
parents: 1804
diff changeset
    11
listenLoop :: Handle -> Int -> [String] -> Chan CoreMessage -> Int -> IO ()
d909152bdc21 - More verbose output
unc0rr
parents: 1804
diff changeset
    12
listenLoop handle linesNumber buf chan clientID = do
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    13
	str <- hGetLine handle
2001
d909152bdc21 - More verbose output
unc0rr
parents: 1804
diff changeset
    14
	if (linesNumber > 50) || (length str > 450) then
d909152bdc21 - More verbose output
unc0rr
parents: 1804
diff changeset
    15
		writeChan chan $ ClientMessage (clientID, ["QUIT", "Protocol violation"])
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    16
		else
2001
d909152bdc21 - More verbose output
unc0rr
parents: 1804
diff changeset
    17
		if str == "" then do
d909152bdc21 - More verbose output
unc0rr
parents: 1804
diff changeset
    18
			writeChan chan $ ClientMessage (clientID, buf)
d909152bdc21 - More verbose output
unc0rr
parents: 1804
diff changeset
    19
			listenLoop handle 0 [] chan clientID
d909152bdc21 - More verbose output
unc0rr
parents: 1804
diff changeset
    20
			else
d909152bdc21 - More verbose output
unc0rr
parents: 1804
diff changeset
    21
			listenLoop handle (linesNumber + 1) (buf ++ [str]) chan clientID
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    22
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    23
clientRecvLoop :: Handle -> Chan CoreMessage -> Int -> IO ()
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    24
clientRecvLoop handle chan clientID =
2001
d909152bdc21 - More verbose output
unc0rr
parents: 1804
diff changeset
    25
	listenLoop handle 0 [] chan clientID
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    26
		`catch` (\e -> (clientOff $ show e) >> return ())
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    27
	where clientOff msg = writeChan chan $ ClientMessage (clientID, ["QUIT", msg]) -- if the client disconnects, we perform as if it sent QUIT message
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    28
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    29
clientSendLoop :: Handle -> Chan CoreMessage -> Chan [String] -> Int -> IO()
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    30
clientSendLoop handle coreChan chan clientID = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    31
	answer <- readChan chan
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    32
	doClose <- Control.Exception.handle
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    33
		(\(e :: Control.Exception.Exception) -> if isQuit answer then return True else sendQuit e >> return False) $ do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    34
		forM_ answer (\str -> hPutStrLn handle str)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    35
		hPutStrLn handle ""
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    36
		hFlush handle
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    37
		return $ isQuit answer
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    38
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    39
	if doClose then
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    40
		Control.Exception.handle (\(_ :: Control.Exception.Exception) -> putStrLn "error on hClose") $ hClose handle
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    41
		else
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    42
		clientSendLoop handle coreChan chan clientID
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    43
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    44
	where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    45
		sendQuit e = writeChan coreChan $ ClientMessage (clientID, ["QUIT", show e])
2126
cb249fa8e3da - Prevent server from producing zombies
unc0rr
parents: 2001
diff changeset
    46
		isQuit ("BYE":xs) = True
cb249fa8e3da - Prevent server from producing zombies
unc0rr
parents: 2001
diff changeset
    47
		isQuit _ = False