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