author | sheepluva |
Sat, 18 Jul 2015 02:44:36 +0200 | |
changeset 11019 | 67ef02300508 |
parent 10460 | 8dcea9087d75 |
child 11046 | 47a8c19ecb60 |
permissions | -rw-r--r-- |
10460
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
1 |
{- |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
2 |
* Hedgewars, a free turn based strategy game |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
3 |
* Copyright (c) 2004-2014 Andrey Korotaev <unC0Rr@gmail.com> |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
4 |
* |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or modify |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
7 |
* the Free Software Foundation; version 2 of the License |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
8 |
* |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
12 |
* GNU General Public License for more details. |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
13 |
* |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
15 |
* along with this program; if not, write to the Free Software |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
17 |
\-} |
8dcea9087d75
Added copyrights to gameServer directory
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
7751
diff
changeset
|
18 |
|
4905 | 19 |
{-# LANGUAGE CPP #-} |
20 |
||
21 |
module Main where |
|
22 |
||
23 |
import System.IO |
|
4932 | 24 |
import System.IO.Error |
4905 | 25 |
import Control.Concurrent |
26 |
import Network |
|
27 |
import Control.OldException |
|
28 |
import Control.Monad |
|
29 |
import System.Random |
|
30 |
import Control.Monad.State |
|
31 |
import Data.List |
|
32 |
||
33 |
#if !defined(mingw32_HOST_OS) |
|
34 |
import System.Posix |
|
35 |
#endif |
|
36 |
||
37 |
type SState = Handle |
|
38 |
io = liftIO |
|
39 |
||
40 |
readPacket :: StateT SState IO [String] |
|
41 |
readPacket = do |
|
42 |
h <- get |
|
4932 | 43 |
io $ hGetPacket h [] |
4905 | 44 |
where |
45 |
hGetPacket h buf = do |
|
46 |
l <- hGetLine h |
|
4932 | 47 |
if not $ null l then hGetPacket h (buf ++ [l]) else return buf |
4905 | 48 |
|
49 |
waitPacket :: String -> StateT SState IO Bool |
|
50 |
waitPacket s = do |
|
51 |
p <- readPacket |
|
52 |
return $ head p == s |
|
53 |
||
54 |
sendPacket :: [String] -> StateT SState IO () |
|
55 |
sendPacket s = do |
|
56 |
h <- get |
|
57 |
io $ do |
|
58 |
mapM_ (hPutStrLn h) s |
|
59 |
hPutStrLn h "" |
|
60 |
hFlush h |
|
61 |
||
62 |
emulateSession :: StateT SState IO () |
|
63 |
emulateSession = do |
|
64 |
n <- io $ randomRIO (100000::Int, 100100) |
|
65 |
waitPacket "CONNECTED" |
|
4932 | 66 |
sendPacket ["NICK", "test" ++ show n] |
4905 | 67 |
waitPacket "NICK" |
7751 | 68 |
sendPacket ["PROTO", "41"] |
4905 | 69 |
waitPacket "PROTO" |
70 |
b <- waitPacket "LOBBY:JOINED" |
|
71 |
--io $ print b |
|
72 |
sendPacket ["QUIT", "BYE"] |
|
73 |
return () |
|
74 |
||
75 |
testing = Control.OldException.handle print $ do |
|
76 |
putStr "+" |
|
77 |
sock <- connectTo "127.0.0.1" (PortNumber 46631) |
|
78 |
evalStateT emulateSession sock |
|
79 |
--hClose sock |
|
80 |
putStr "-" |
|
81 |
hFlush stdout |
|
82 |
||
5058 | 83 |
forks = forever $ do |
84 |
delay <- randomRIO (0::Int, 80000) |
|
4905 | 85 |
threadDelay delay |
86 |
forkIO testing |
|
87 |
||
88 |
main = withSocketsDo $ do |
|
89 |
#if !defined(mingw32_HOST_OS) |
|
90 |
installHandler sigPIPE Ignore Nothing; |
|
91 |
#endif |
|
92 |
forks |