author | koda |
Wed, 15 Jul 2009 00:35:59 +0000 | |
changeset 2262 | 0dc94341bf42 |
parent 2245 | c011aecc95e5 |
child 2296 | 19f2f76dc346 |
permissions | -rw-r--r-- |
1804 | 1 |
{-# LANGUAGE PatternSignatures #-} |
2 |
module NetRoutines where |
|
3 |
||
4 |
import Network |
|
5 |
import Network.Socket |
|
6 |
import System.IO |
|
7 |
import Control.Concurrent |
|
8 |
import Control.Concurrent.Chan |
|
9 |
import Control.Concurrent.STM |
|
10 |
import Control.Exception |
|
11 |
import Data.Time |
|
12 |
----------------------------- |
|
13 |
import CoreTypes |
|
14 |
import ClientIO |
|
1917 | 15 |
import Utils |
1804 | 16 |
|
17 |
acceptLoop :: Socket -> Chan CoreMessage -> Int -> IO () |
|
18 |
acceptLoop servSock coreChan clientCounter = do |
|
19 |
Control.Exception.handle |
|
20 |
(\(_ :: Exception) -> putStrLn "exception on connect") $ |
|
21 |
do |
|
22 |
(socket, sockAddr) <- Network.Socket.accept servSock |
|
23 |
||
24 |
cHandle <- socketToHandle socket ReadWriteMode |
|
25 |
hSetBuffering cHandle LineBuffering |
|
26 |
clientHost <- sockAddr2String sockAddr |
|
27 |
||
28 |
currentTime <- getCurrentTime |
|
29 |
||
30 |
sendChan <- newChan |
|
31 |
||
32 |
let newClient = |
|
33 |
(ClientInfo |
|
34 |
nextID |
|
35 |
sendChan |
|
36 |
cHandle |
|
37 |
clientHost |
|
1926
cb46fbdcaa41
Add simple DoS protection mechanism (although better than previous server had)
unc0rr
parents:
1924
diff
changeset
|
38 |
currentTime |
1804 | 39 |
"" |
1841
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
40 |
"" |
fba7210b438b
Retrieve client password from web database and ask for it
unc0rr
parents:
1839
diff
changeset
|
41 |
False |
1804 | 42 |
0 |
43 |
0 |
|
1927
e2031906a347
Ping clients every 30 seconds. Disconnection due to ping timeout to be implemented.
unc0rr
parents:
1926
diff
changeset
|
44 |
0 |
1804 | 45 |
False |
46 |
False |
|
47 |
False |
|
2245
c011aecc95e5
unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents:
2004
diff
changeset
|
48 |
undefined |
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
49 |
) |
1804 | 50 |
|
51 |
writeChan coreChan $ Accept newClient |
|
52 |
||
53 |
forkIO $ clientRecvLoop cHandle coreChan nextID |
|
54 |
forkIO $ clientSendLoop cHandle coreChan sendChan nextID |
|
55 |
return () |
|
56 |
||
57 |
acceptLoop servSock coreChan nextID |
|
58 |
where |
|
59 |
nextID = clientCounter + 1 |