gameServer/ClientIO.hs
author unc0rr
Thu, 12 Mar 2009 20:29:01 +0000
changeset 1888 e76274ce7365
parent 1804 4e78ad846fb6
child 2001 d909152bdc21
permissions -rw-r--r--
Add an ability to run engine without IPC connection. It requires two parameters: 1) ful path to share/hedgewars/Data 2) full path to record file (hwd_24 or hws_24) debug #0.txt will be created it path passed in 1st param

{-# LANGUAGE PatternSignatures #-}
module ClientIO where

import qualified Control.Exception
import Control.Concurrent.Chan
import Control.Monad
import System.IO
----------------
import CoreTypes

listenLoop :: Handle -> [String] -> Chan CoreMessage -> Int -> IO ()
listenLoop handle buf chan clientID = do
	str <- hGetLine handle
	if str == "" then do
		writeChan chan $ ClientMessage (clientID, buf)
		listenLoop handle [] chan clientID
		else
		listenLoop handle (buf ++ [str]) chan clientID

clientRecvLoop :: Handle -> Chan CoreMessage -> Int -> IO ()
clientRecvLoop handle chan clientID =
	listenLoop handle [] chan clientID
		`catch` (\e -> (clientOff $ show e) >> return ())
	where clientOff msg = writeChan chan $ ClientMessage (clientID, ["QUIT", msg]) -- if the client disconnects, we perform as if it sent QUIT message

clientSendLoop :: Handle -> Chan CoreMessage -> Chan [String] -> Int -> IO()
clientSendLoop handle coreChan chan clientID = do
	answer <- readChan chan
	doClose <- Control.Exception.handle
		(\(e :: Control.Exception.Exception) -> if isQuit answer then return True else sendQuit e >> return False) $ do
		forM_ answer (\str -> hPutStrLn handle str)
		hPutStrLn handle ""
		hFlush handle
		return $ isQuit answer

	if doClose then
		Control.Exception.handle (\(_ :: Control.Exception.Exception) -> putStrLn "error on hClose") $ hClose handle
		else
		clientSendLoop handle coreChan chan clientID

	where
		sendQuit e = writeChan coreChan $ ClientMessage (clientID, ["QUIT", show e])
		isQuit answer = head answer == "BYE"