author | unc0rr |
Wed, 05 Nov 2008 08:02:12 +0000 | |
changeset 1477 | 001a52a108ed |
parent 1476 | b3b28e99570f |
child 1478 | 8bfb417d165e |
permissions | -rw-r--r-- |
1370 | 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 (setUncaughtExceptionHandler, handle, finally) |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
9 |
import Control.Monad |
1391 | 10 |
import Maybe (fromMaybe, isJust, fromJust) |
1370 | 11 |
import Data.List |
12 |
import Miscutils |
|
13 |
import HWProto |
|
14 |
import Opts |
|
1397 | 15 |
|
1398 | 16 |
#if !defined(mingw32_HOST_OS) |
1396 | 17 |
import System.Posix |
1397 | 18 |
#endif |
19 |
||
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
20 |
data Messages = |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
21 |
Accept ClientInfo |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
22 |
| ClientMessage ([String], ClientInfo) |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
23 |
| CoreMessage [String] |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
24 |
|
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
25 |
messagesLoop :: TChan [String] -> IO() |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
26 |
messagesLoop messagesChan = forever $ do |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
27 |
threadDelay (30 * 10^6) -- 30 seconds |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
28 |
atomically $ writeTChan messagesChan ["PING"] |
1370 | 29 |
|
30 |
acceptLoop :: Socket -> TChan ClientInfo -> IO () |
|
1469 | 31 |
acceptLoop servSock acceptChan = Control.Exception.handle (const $ putStrLn "exception on connect" >> acceptLoop servSock acceptChan) $ do |
1370 | 32 |
(cHandle, host, port) <- accept servSock |
1477 | 33 |
putStrLn "new client" |
1370 | 34 |
cChan <- atomically newTChan |
35 |
forkIO $ clientLoop cHandle cChan |
|
1403 | 36 |
atomically $ writeTChan acceptChan (ClientInfo cChan cHandle "" 0 "" False False False) |
1469 | 37 |
atomically $ writeTChan cChan ["ASKME"] |
1370 | 38 |
acceptLoop servSock acceptChan |
39 |
||
40 |
||
41 |
listenLoop :: Handle -> [String] -> TChan [String] -> IO () |
|
42 |
listenLoop handle buf chan = do |
|
43 |
str <- hGetLine handle |
|
44 |
if str == "" then do |
|
45 |
atomically $ writeTChan chan buf |
|
46 |
listenLoop handle [] chan |
|
47 |
else |
|
48 |
listenLoop handle (buf ++ [str]) chan |
|
49 |
||
50 |
||
51 |
clientLoop :: Handle -> TChan [String] -> IO () |
|
52 |
clientLoop handle chan = |
|
53 |
listenLoop handle [] chan |
|
54 |
`catch` (const $ clientOff >> return ()) |
|
55 |
where clientOff = atomically $ writeTChan chan ["QUIT"] -- if the client disconnects, we perform as if it sent QUIT message |
|
56 |
||
57 |
||
58 |
sendAnswers [] _ clients _ = return clients |
|
59 |
sendAnswers ((handlesFunc, answer):answers) client clients rooms = do |
|
60 |
let recipients = handlesFunc client clients rooms |
|
1476 | 61 |
--unless (null recipients) $ putStrLn ("< " ++ (show answer)) |
1370 | 62 |
|
63 |
clHandles' <- forM recipients $ |
|
1468 | 64 |
\ch -> Control.Exception.handle |
65 |
(\e -> putStrLn ("handle exception: " ++ show e) >> |
|
66 |
if head answer == "BYE" then |
|
67 |
return [ch] |
|
68 |
else |
|
69 |
atomically $ writeTChan (chan $ fromJust $ clientByHandle ch clients) ["QUIT"] >> return [] -- cannot just remove |
|
70 |
) $ |
|
1370 | 71 |
do |
72 |
forM_ answer (\str -> hPutStrLn ch str) |
|
73 |
hPutStrLn ch "" |
|
74 |
hFlush ch |
|
1466 | 75 |
if head answer == "BYE" then return [ch] else return [] |
1370 | 76 |
|
1476 | 77 |
let outHandles = concat clHandles' |
1477 | 78 |
unless (null outHandles) $ putStrLn ("bye: " ++ (show $ length outHandles) ++ "/" ++ (show $ length clients) ++ " clients") |
1476 | 79 |
mapM_ (\ch -> Control.Exception.handle (const $ putStrLn "error on hClose") (hClose ch)) outHandles |
80 |
let mclients = remove clients outHandles |
|
1370 | 81 |
|
82 |
sendAnswers answers client mclients rooms |
|
83 |
where |
|
84 |
remove list rmClHandles = deleteFirstsBy2t (\ a b -> (Miscutils.handle a) == b) list rmClHandles |
|
85 |
||
86 |
||
1391 | 87 |
reactCmd :: [String] -> ClientInfo -> [ClientInfo] -> [RoomInfo] -> IO ([ClientInfo], [RoomInfo]) |
88 |
reactCmd cmd client clients rooms = do |
|
1473 | 89 |
--putStrLn ("> " ++ show cmd) |
1391 | 90 |
|
91 |
let (clientsFunc, roomsFunc, answers) = handleCmd client clients rooms $ cmd |
|
92 |
let mrooms = roomsFunc rooms |
|
93 |
let mclients = (clientsFunc clients) |
|
94 |
let mclient = fromMaybe client $ find (== client) mclients |
|
95 |
||
96 |
clientsIn <- sendAnswers answers mclient mclients mrooms |
|
97 |
let quitClient = find forceQuit $ clientsIn |
|
1474 | 98 |
|
1473 | 99 |
if isJust quitClient then |
100 |
reactCmd ["QUIT"] (fromJust quitClient) clientsIn mrooms |
|
1474 | 101 |
else |
1473 | 102 |
return (clientsIn, mrooms) |
1391 | 103 |
|
104 |
||
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
105 |
mainLoop :: Socket -> TChan ClientInfo -> TChan [String] -> [ClientInfo] -> [RoomInfo] -> IO () |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
106 |
mainLoop servSock acceptChan messagesChan clients rooms = do |
1473 | 107 |
r <- atomically $ |
108 |
(Accept `fmap` readTChan acceptChan) `orElse` |
|
109 |
(ClientMessage `fmap` tselect clients) `orElse` |
|
110 |
(CoreMessage `fmap` readTChan messagesChan) |
|
1370 | 111 |
case r of |
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
112 |
Accept ci -> |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
113 |
mainLoop servSock acceptChan messagesChan (clients ++ [ci]) rooms |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
114 |
ClientMessage (cmd, client) -> do |
1391 | 115 |
(clientsIn, mrooms) <- reactCmd cmd client clients rooms |
1370 | 116 |
|
1385 | 117 |
let hadRooms = (not $ null rooms) && (null mrooms) |
118 |
in unless ((not $ isDedicated globalOptions) && ((null clientsIn) || hadRooms)) $ |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
119 |
mainLoop servSock acceptChan messagesChan clientsIn mrooms |
1473 | 120 |
CoreMessage msg -> |
121 |
if not $ null $ clients then |
|
122 |
do |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
123 |
let client = head clients -- don't care |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
124 |
(clientsIn, mrooms) <- reactCmd msg client clients rooms |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
125 |
mainLoop servSock acceptChan messagesChan clientsIn mrooms |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
126 |
else |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
127 |
mainLoop servSock acceptChan messagesChan clients rooms |
1370 | 128 |
|
129 |
||
130 |
startServer serverSocket = do |
|
131 |
acceptChan <- atomically newTChan |
|
132 |
forkIO $ acceptLoop serverSocket acceptChan |
|
1461
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
133 |
|
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
134 |
messagesChan <- atomically newTChan |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
135 |
forkIO $ messagesLoop messagesChan |
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
136 |
|
87e5a6c3882c
Ping clients every 30 seconds, should help with ghosts on server
unc0rr
parents:
1403
diff
changeset
|
137 |
mainLoop serverSocket acceptChan messagesChan [] [] |
1370 | 138 |
|
139 |
||
140 |
main = withSocketsDo $ do |
|
1398 | 141 |
#if !defined(mingw32_HOST_OS) |
1396 | 142 |
installHandler sigPIPE Ignore Nothing; |
1397 | 143 |
#endif |
1383 | 144 |
putStrLn $ "Listening on port " ++ show (listenPort globalOptions) |
145 |
serverSocket <- listenOn $ PortNumber (listenPort globalOptions) |
|
1370 | 146 |
startServer serverSocket `finally` sClose serverSocket |