author | unc0rr |
Wed, 05 Aug 2009 05:44:13 +0000 | |
changeset 2296 | 19f2f76dc346 |
parent 2126 | cb249fa8e3da |
child 2348 | b39d826e1ccd |
permissions | -rw-r--r-- |
2296
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2126
diff
changeset
|
1 |
{-# LANGUAGE CPP, PatternSignatures #-} |
1804 | 2 |
module ClientIO where |
3 |
||
2296
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2126
diff
changeset
|
4 |
#if defined(NEW_EXCEPTIONS) |
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2126
diff
changeset
|
5 |
import qualified Control.OldException as Exception |
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2126
diff
changeset
|
6 |
#else |
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2126
diff
changeset
|
7 |
import qualified Control.Exception as Exception |
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2126
diff
changeset
|
8 |
#endif |
1804 | 9 |
import Control.Concurrent.Chan |
10 |
import Control.Monad |
|
11 |
import System.IO |
|
12 |
---------------- |
|
13 |
import CoreTypes |
|
14 |
||
2001 | 15 |
listenLoop :: Handle -> Int -> [String] -> Chan CoreMessage -> Int -> IO () |
16 |
listenLoop handle linesNumber buf chan clientID = do |
|
1804 | 17 |
str <- hGetLine handle |
2001 | 18 |
if (linesNumber > 50) || (length str > 450) then |
19 |
writeChan chan $ ClientMessage (clientID, ["QUIT", "Protocol violation"]) |
|
1804 | 20 |
else |
2001 | 21 |
if str == "" then do |
22 |
writeChan chan $ ClientMessage (clientID, buf) |
|
23 |
listenLoop handle 0 [] chan clientID |
|
24 |
else |
|
25 |
listenLoop handle (linesNumber + 1) (buf ++ [str]) chan clientID |
|
1804 | 26 |
|
27 |
clientRecvLoop :: Handle -> Chan CoreMessage -> Int -> IO () |
|
28 |
clientRecvLoop handle chan clientID = |
|
2001 | 29 |
listenLoop handle 0 [] chan clientID |
1804 | 30 |
`catch` (\e -> (clientOff $ show e) >> return ()) |
31 |
where clientOff msg = writeChan chan $ ClientMessage (clientID, ["QUIT", msg]) -- if the client disconnects, we perform as if it sent QUIT message |
|
32 |
||
33 |
clientSendLoop :: Handle -> Chan CoreMessage -> Chan [String] -> Int -> IO() |
|
34 |
clientSendLoop handle coreChan chan clientID = do |
|
35 |
answer <- readChan chan |
|
2296
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2126
diff
changeset
|
36 |
doClose <- Exception.handle |
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2126
diff
changeset
|
37 |
(\(e :: Exception.Exception) -> if isQuit answer then return True else sendQuit e >> return False) $ do |
1804 | 38 |
forM_ answer (\str -> hPutStrLn handle str) |
39 |
hPutStrLn handle "" |
|
40 |
hFlush handle |
|
41 |
return $ isQuit answer |
|
42 |
||
43 |
if doClose then |
|
2296
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2126
diff
changeset
|
44 |
Exception.handle (\(_ :: Exception.Exception) -> putStrLn "error on hClose") $ hClose handle |
1804 | 45 |
else |
46 |
clientSendLoop handle coreChan chan clientID |
|
47 |
||
48 |
where |
|
49 |
sendQuit e = writeChan coreChan $ ClientMessage (clientID, ["QUIT", show e]) |
|
2126 | 50 |
isQuit ("BYE":xs) = True |
51 |
isQuit _ = False |