author | unc0rr |
Mon, 04 Feb 2013 00:13:55 +0400 | |
changeset 8479 | 8d71109b04d2 |
parent 8474 | f6abe50095d2 |
child 8497 | c5605c6f5bb3 |
permissions | -rw-r--r-- |
8474
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
1 |
{-# LANGUAGE CPP, ScopedTypeVariables, OverloadedStrings #-} |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
2 |
module Main where |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
3 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
4 |
import qualified Control.Exception as Exception |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
5 |
import System.IO |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
6 |
import System.Log.Logger |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
7 |
import qualified Data.ConfigFile as CF |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
8 |
import Control.Monad.Error |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
9 |
import System.Directory |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
10 |
import Control.Monad.State |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
11 |
import Control.Concurrent.Chan |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
12 |
import Control.Concurrent |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
13 |
import Network |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
14 |
import Network.BSD |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
15 |
import Network.Socket hiding (recv) |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
16 |
import Network.Socket.ByteString |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
17 |
import qualified Data.ByteString.Char8 as B |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
18 |
#if !defined(mingw32_HOST_OS) |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
19 |
import System.Posix |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
20 |
#endif |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
21 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
22 |
data Message = Packet [B.ByteString] |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
23 |
deriving Show |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
24 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
25 |
protocolNumber = "43" |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
26 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
27 |
takePacks :: State B.ByteString [[B.ByteString]] |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
28 |
takePacks = do |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
29 |
modify (until (not . B.isPrefixOf pDelim) (B.drop 2)) |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
30 |
packet <- state $ B.breakSubstring pDelim |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
31 |
buf <- get |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
32 |
if B.null buf then put packet >> return [] else |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
33 |
if B.null packet then return [] else do |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
34 |
packets <- takePacks |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
35 |
return (B.splitWith (== '\n') packet : packets) |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
36 |
where |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
37 |
pDelim = "\n\n" |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
38 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
39 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
40 |
recvLoop :: Socket -> Chan Message -> IO () |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
41 |
recvLoop s chan = |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
42 |
((receiveWithBufferLoop B.empty >> return "Connection closed") |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
43 |
`Exception.catch` (\(e :: Exception.SomeException) -> return . B.pack . show $ e) |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
44 |
) |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
45 |
>>= disconnected |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
46 |
where |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
47 |
disconnected msg = writeChan chan $ Packet ["BYE", msg] |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
48 |
receiveWithBufferLoop recvBuf = do |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
49 |
recvBS <- recv s 4096 |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
50 |
unless (B.null recvBS) $ do |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
51 |
let (packets, newrecvBuf) = runState takePacks $ B.append recvBuf recvBS |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
52 |
forM_ packets sendPacket |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
53 |
receiveWithBufferLoop $ B.copy newrecvBuf |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
54 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
55 |
sendPacket packet = writeChan chan $ Packet packet |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
56 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
57 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
58 |
session :: B.ByteString -> B.ByteString -> Socket -> IO () |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
59 |
session l p s = do |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
60 |
noticeM "Core" "Connected" |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
61 |
coreChan <- newChan |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
62 |
forkIO $ recvLoop s coreChan |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
63 |
forever $ do |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
64 |
p <- readChan coreChan |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
65 |
case p of |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
66 |
Packet p -> do |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
67 |
debugM "Network" $ "Recv: " ++ show p |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
68 |
onPacket p |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
69 |
where |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
70 |
answer :: [B.ByteString] -> IO () |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
71 |
answer p = do |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
72 |
debugM "Network" $ "Send: " ++ show p |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
73 |
sendAll s $ B.unlines p `B.snoc` '\n' |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
74 |
onPacket :: [B.ByteString] -> IO () |
8479
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8474
diff
changeset
|
75 |
onPacket ("CONNECTED":_) = do |
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8474
diff
changeset
|
76 |
answer ["CHECKER", protocolNumber, l, p] |
8d71109b04d2
Some work on loading replay and interaction with checker
unc0rr
parents:
8474
diff
changeset
|
77 |
answer ["READY"] |
8474
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
78 |
onPacket ["PING"] = answer ["PONG"] |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
79 |
onPacket ("BYE" : xs) = error $ show xs |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
80 |
onPacket _ = return () |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
81 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
82 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
83 |
main :: IO () |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
84 |
main = withSocketsDo $ do |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
85 |
#if !defined(mingw32_HOST_OS) |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
86 |
installHandler sigPIPE Ignore Nothing; |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
87 |
#endif |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
88 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
89 |
updateGlobalLogger "Core" (setLevel DEBUG) |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
90 |
updateGlobalLogger "Network" (setLevel DEBUG) |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
91 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
92 |
Right (login, password) <- runErrorT $ do |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
93 |
d <- liftIO $ getHomeDirectory |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
94 |
conf <- join . liftIO . CF.readfile CF.emptyCP $ d ++ "/.hedgewars/hedgewars.ini" |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
95 |
l <- CF.get conf "net" "nick" |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
96 |
p <- CF.get conf "net" "passwordhash" |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
97 |
return (B.pack l, B.pack p) |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
98 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
99 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
100 |
Exception.bracket |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
101 |
setupConnection |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
102 |
(\s -> noticeM "Core" "Shutting down" >> sClose s) |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
103 |
(session login password) |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
104 |
where |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
105 |
setupConnection = do |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
106 |
noticeM "Core" "Connecting to the server..." |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
107 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
108 |
proto <- getProtocolNumber "tcp" |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
109 |
let hints = defaultHints { addrFlags = [AI_ADDRCONFIG, AI_CANONNAME] } |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
110 |
(addr:_) <- getAddrInfo (Just hints) (Just serverAddress) Nothing |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
111 |
let (SockAddrInet _ host) = addrAddress addr |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
112 |
sock <- socket AF_INET Stream proto |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
113 |
connect sock (SockAddrInet 46631 host) |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
114 |
return sock |
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
115 |
|
f6abe50095d2
Start work on the checker. Not it could connect to the server and... crash it.
unc0rr
parents:
diff
changeset
|
116 |
serverAddress = "netserver.hedgewars.org" |