netserver/Miscutils.hs
changeset 851 8ffa4ad0d8ea
parent 849 82ac0596aa3c
child 852 f756a1d3324c
equal deleted inserted replaced
850:5373abfdc4c2 851:8ffa4ad0d8ea
     3 import IO
     3 import IO
     4 import System.IO
     4 import System.IO
     5 import Control.Concurrent
     5 import Control.Concurrent
     6 import Control.Concurrent.STM
     6 import Control.Concurrent.STM
     7 import Control.Exception (finally)
     7 import Control.Exception (finally)
       
     8 
       
     9 data ClientInfo =
       
    10 	ClientInfo
       
    11 	{
       
    12 		handle :: Handle,
       
    13 		nick :: String,
       
    14 		room :: String,
       
    15 		isMaster :: Bool
       
    16 	}
       
    17 
       
    18 data RoomInfo =
       
    19 	RoomInfo
       
    20 	{
       
    21 		name :: String,
       
    22 		password :: String
       
    23 	}
       
    24 
     8 
    25 
     9 sendMsg :: Handle -> String -> IO()
    26 sendMsg :: Handle -> String -> IO()
    10 sendMsg clientHandle str = finally (return ()) (hPutStrLn clientHandle str >> hFlush clientHandle) -- catch exception when client tries to send to other
    27 sendMsg clientHandle str = finally (return ()) (hPutStrLn clientHandle str >> hFlush clientHandle) -- catch exception when client tries to send to other
    11 
    28 
    12 sendAll :: [Handle] -> String -> IO[()]
    29 sendAll :: [Handle] -> String -> IO[()]
    23 manipState state op =
    40 manipState state op =
    24 	atomically $ do
    41 	atomically $ do
    25 			ls <- readTVar state
    42 			ls <- readTVar state
    26 			writeTVar state $ op ls
    43 			writeTVar state $ op ls
    27 
    44 
       
    45 manipState2 :: TVar[ClientInfo] -> TVar[RoomInfo] -> ([ClientInfo] -> [RoomInfo] -> ([ClientInfo], [RoomInfo])) -> IO()
       
    46 manipState2 state1 state2 op =
       
    47 	atomically $ do
       
    48 			ls1 <- readTVar state1
       
    49 			ls2 <- readTVar state2
       
    50 			let (ol1, ol2) = op ls1 ls2
       
    51 			writeTVar state1 ol1
       
    52 			writeTVar state2 ol2
       
    53