author | unc0rr |
Fri, 12 Sep 2008 14:57:42 +0000 | |
changeset 1255 | 7ffc2c9e7224 |
parent 1082 | 596b1dcdc1df |
child 1302 | 4290ba4a14ca |
permissions | -rw-r--r-- |
877 | 1 |
module Main where |
2 |
||
3 |
import Network |
|
4 |
import IO |
|
5 |
import System.IO |
|
6 |
import Control.Concurrent |
|
7 |
import Control.Concurrent.STM |
|
8 |
import Control.Exception (finally) |
|
890 | 9 |
import Control.Monad (forM, forM_, filterM, liftM) |
10 |
import Data.List |
|
877 | 11 |
import Miscutils |
890 | 12 |
import HWProto |
877 | 13 |
|
889 | 14 |
acceptLoop :: Socket -> TChan ClientInfo -> IO () |
877 | 15 |
acceptLoop servSock acceptChan = do |
16 |
(cHandle, host, port) <- accept servSock |
|
17 |
cChan <- atomically newTChan |
|
18 |
forkIO $ clientLoop cHandle cChan |
|
894 | 19 |
atomically $ writeTChan acceptChan (ClientInfo cChan cHandle "" 0 "" False) |
1082 | 20 |
hPutStrLn cHandle "CONNECTED\n" |
877 | 21 |
acceptLoop servSock acceptChan |
22 |
||
1082 | 23 |
listenLoop :: Handle -> [String] -> TChan [String] -> IO () |
24 |
listenLoop handle buf chan = do |
|
877 | 25 |
str <- hGetLine handle |
1082 | 26 |
if str == "" then do |
27 |
atomically $ writeTChan chan buf |
|
28 |
listenLoop handle [] chan |
|
29 |
else |
|
30 |
listenLoop handle (buf ++ [str]) chan |
|
877 | 31 |
|
1082 | 32 |
clientLoop :: Handle -> TChan [String] -> IO () |
877 | 33 |
clientLoop handle chan = |
1082 | 34 |
listenLoop handle [] chan |
877 | 35 |
`catch` (const $ clientOff >> return ()) |
1082 | 36 |
where clientOff = atomically $ writeTChan chan ["QUIT"] |
877 | 37 |
|
889 | 38 |
mainLoop :: Socket -> TChan ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO () |
39 |
mainLoop servSock acceptChan clients rooms = do |
|
877 | 40 |
r <- atomically $ (Left `fmap` readTChan acceptChan) `orElse` (Right `fmap` tselect clients) |
41 |
case r of |
|
889 | 42 |
Left ci -> do |
43 |
mainLoop servSock acceptChan (ci:clients) rooms |
|
1082 | 44 |
Right (cmd, client) -> do |
45 |
print ("> " ++ show cmd) |
|
46 |
let (clientsFunc, roomsFunc, handlesFunc, answer) = handleCmd client clients rooms $ cmd |
|
47 |
print ("< " ++ show answer) |
|
890 | 48 |
|
1082 | 49 |
let mclients = clientsFunc clients |
50 |
let mrooms = roomsFunc rooms |
|
51 |
let recipients = handlesFunc client clients rooms |
|
52 |
||
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
898
diff
changeset
|
53 |
clHandles' <- forM recipients $ |
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
898
diff
changeset
|
54 |
\ch -> do |
1082 | 55 |
forM_ answer (\str -> hPutStrLn ch str) |
56 |
hPutStrLn ch "" |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
898
diff
changeset
|
57 |
hFlush ch |
1082 | 58 |
if (not $ null answer) && (head answer == "ROOMABANDONED") then hClose ch >> return [ch] else return [] |
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
898
diff
changeset
|
59 |
`catch` const (hClose ch >> return [ch]) |
890 | 60 |
|
1082 | 61 |
clHandle' <- if (not $ null answer) && (head answer == "QUIT") then hClose (handle client) >> return [handle client] else return [] |
891
701f86df9b4c
Properly handle QUIT command. Now, we can concentrate on protocol implementation
unc0rr
parents:
890
diff
changeset
|
62 |
|
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
898
diff
changeset
|
63 |
mainLoop servSock acceptChan (remove (remove mclients (concat clHandles')) clHandle') mrooms |
890 | 64 |
where |
901
2f5ce9a584f9
Modify protocol implementation functions interface (convertation not yet finished)
unc0rr
parents:
898
diff
changeset
|
65 |
remove list rmClHandles = deleteFirstsBy2t (\ a b -> (handle a) == b) list rmClHandles |
877 | 66 |
|
67 |
startServer serverSocket = do |
|
68 |
acceptChan <- atomically newTChan |
|
69 |
forkIO $ acceptLoop serverSocket acceptChan |
|
889 | 70 |
mainLoop serverSocket acceptChan [] [] |
877 | 71 |
|
878 | 72 |
main = withSocketsDo $ do |
877 | 73 |
serverSocket <- listenOn $ Service "hedgewars" |
74 |
startServer serverSocket `finally` sClose serverSocket |