netserver/newhwserv.hs
author unc0rr
Wed, 30 Apr 2008 19:44:54 +0000
changeset 890 1d8c4a5ec622
parent 889 3bf9dc791f45
child 891 701f86df9b4c
permissions -rw-r--r--
- Improve server core - Protocol in separate module
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
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    19
	atomically $ writeTChan acceptChan (ClientInfo cChan cHandle "" "" False)
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    20
	acceptLoop servSock acceptChan
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    21
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    22
listenLoop :: Handle -> TChan String -> IO ()
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    23
listenLoop handle chan = do
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    24
	str <- hGetLine handle
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    25
	atomically $ writeTChan chan str
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    26
	listenLoop handle chan
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    27
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    28
clientLoop :: Handle -> TChan String -> IO ()
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    29
clientLoop handle chan =
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    30
	listenLoop handle chan
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    31
		`catch` (const $ clientOff >> return ())
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    32
		`finally` hClose handle
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    33
	where clientOff = atomically $ writeTChan chan "QUIT"
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    34
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    35
mainLoop :: Socket -> TChan ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ()
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    36
mainLoop servSock acceptChan clients rooms = do
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    37
	r <- atomically $ (Left `fmap` readTChan acceptChan) `orElse` (Right `fmap` tselect clients)
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    38
	case r of
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    39
		Left ci -> do
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    40
			mainLoop servSock acceptChan (ci:clients) rooms
890
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    41
		Right (line, client) -> do
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    42
			let (doQuit, toMe, strs) = handleCmd client sameRoom rooms line
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    43
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    44
			clients' <- forM sameRoom $
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    45
					\ci -> do
890
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    46
						if (handle ci /= handle client) || toMe then do
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    47
							forM_ strs (\str -> hPutStrLn (handle ci) str)
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    48
							hFlush (handle ci)
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    49
							return []
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    50
							else if doQuit then return [ci] else return []
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    51
					`catch` const (hClose (handle ci) >> return [ci])
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    52
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    53
			mainLoop servSock acceptChan (deleteFirstsBy (\ a b -> handle a == handle b) clients (concat clients')) rooms
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    54
			where
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    55
				sameRoom = filter (\cl -> room cl == room client) clients
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    56
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    57
startServer serverSocket = do
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    58
	acceptChan <- atomically newTChan
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    59
	forkIO $ acceptLoop serverSocket acceptChan
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    60
	mainLoop serverSocket acceptChan [] []
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    61
878
45bff6dadfce - Fix baseball bat
unc0rr
parents: 877
diff changeset
    62
main = withSocketsDo $ do
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    63
	serverSocket <- listenOn $ Service "hedgewars"
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    64
	startServer serverSocket `finally` sClose serverSocket