netserver/newhwserv.hs
author unc0rr
Wed, 30 Apr 2008 16:50:28 +0000
changeset 889 3bf9dc791f45
parent 878 45bff6dadfce
child 890 1d8c4a5ec622
permissions -rw-r--r--
Some work on newhwserv
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)
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
     9
import Control.Monad (forM, filterM, liftM)
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    10
import Miscutils
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    11
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    12
acceptLoop :: Socket -> TChan ClientInfo -> IO ()
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    13
acceptLoop servSock acceptChan = do
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    14
	(cHandle, host, port) <- accept servSock
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    15
	cChan <- atomically newTChan
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    16
	forkIO $ clientLoop cHandle cChan
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    17
	atomically $ writeTChan acceptChan (ClientInfo cChan cHandle "" "" False)
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    18
	acceptLoop servSock acceptChan
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    19
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    20
listenLoop :: Handle -> TChan String -> IO ()
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    21
listenLoop handle chan = do
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    22
	str <- hGetLine handle
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    23
	atomically $ writeTChan chan str
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    24
	listenLoop handle chan
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    25
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    26
clientLoop :: Handle -> TChan String -> IO ()
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    27
clientLoop handle chan =
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    28
	listenLoop handle chan
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    29
		`catch` (const $ clientOff >> return ())
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    30
		`finally` hClose handle
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    31
	where clientOff = atomically $ writeTChan chan "QUIT"
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    32
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    33
mainLoop :: Socket -> TChan ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ()
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    34
mainLoop servSock acceptChan clients rooms = do
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    35
	r <- atomically $ (Left `fmap` readTChan acceptChan) `orElse` (Right `fmap` tselect clients)
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    36
	case r of
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    37
		Left ci -> do
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    38
			mainLoop servSock acceptChan (ci:clients) rooms
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    39
		Right (line, clhandle) -> do
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    40
			--handleCmd handle line
878
45bff6dadfce - Fix baseball bat
unc0rr
parents: 877
diff changeset
    41
			clients' <- forM clients $
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    42
					\ci -> do
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    43
						hPutStrLn (handle ci) line
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    44
						hFlush (handle ci)
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    45
						return [ci]
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    46
					`catch` const (hClose (handle ci) >> return [])
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    47
			mainLoop servSock acceptChan (concat clients') rooms
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    48
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    49
startServer serverSocket = do
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    50
	acceptChan <- atomically newTChan
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    51
	forkIO $ acceptLoop serverSocket acceptChan
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    52
	mainLoop serverSocket acceptChan [] []
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    53
878
45bff6dadfce - Fix baseball bat
unc0rr
parents: 877
diff changeset
    54
main = withSocketsDo $ do
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    55
	serverSocket <- listenOn $ Service "hedgewars"
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    56
	startServer serverSocket `finally` sClose serverSocket