netserver/newhwserv.hs
author unc0rr
Thu, 01 May 2008 14:30:12 +0000
changeset 894 2ca76a7f3121
parent 893 149244d86bf1
child 898 344ba7dba23d
permissions -rw-r--r--
- Fixed some bugs - Introduce client protocol number field - Handle PROTO command
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)
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
	where clientOff = atomically $ writeTChan chan "QUIT"
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    33
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    34
mainLoop :: Socket -> TChan ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ()
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    35
mainLoop servSock acceptChan clients rooms = do
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    36
	r <- atomically $ (Left `fmap` readTChan acceptChan) `orElse` (Right `fmap` tselect clients)
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    37
	case r of
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    38
		Left ci -> do
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    39
			mainLoop servSock acceptChan (ci:clients) rooms
890
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    40
		Right (line, client) -> do
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    41
			let (mclient, mrooms, recipients, strs) = handleCmd client clients rooms $ words line
890
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    42
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    43
			clients' <- forM recipients $
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    44
					\ci -> do
890
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    45
							forM_ strs (\str -> hPutStrLn (handle ci) str)
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    46
							hFlush (handle ci)
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    47
							return []
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    48
					`catch` const (hClose (handle ci) >> return [ci])
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    49
894
2ca76a7f3121 - Fixed some bugs
unc0rr
parents: 893
diff changeset
    50
			client' <- if (not $ null strs) && (head strs == "QUIT") then hClose (handle client) >> return [client] else return []
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    51
893
149244d86bf1 - Some improvements in core
unc0rr
parents: 892
diff changeset
    52
			mainLoop servSock acceptChan (remove (remove (mclient : filter (\cl -> handle cl /= handle client) clients) (concat clients')) client') mrooms
890
1d8c4a5ec622 - Improve server core
unc0rr
parents: 889
diff changeset
    53
			where
891
701f86df9b4c Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents: 890
diff changeset
    54
				remove list rmClients = deleteFirstsBy (\ a b -> handle a == handle b) list rmClients
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    55
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    56
startServer serverSocket = do
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    57
	acceptChan <- atomically newTChan
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    58
	forkIO $ acceptLoop serverSocket acceptChan
889
3bf9dc791f45 Some work on newhwserv
unc0rr
parents: 878
diff changeset
    59
	mainLoop serverSocket acceptChan [] []
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    60
878
45bff6dadfce - Fix baseball bat
unc0rr
parents: 877
diff changeset
    61
main = withSocketsDo $ do
877
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    62
	serverSocket <- listenOn $ Service "hedgewars"
ebb801acd8b9 Try new approach for netserver
unc0rr
parents:
diff changeset
    63
	startServer serverSocket `finally` sClose serverSocket