author | nemo |
Tue, 02 Mar 2010 19:46:35 +0000 | |
changeset 2912 | 76d43f7458ad |
parent 2867 | 9be6693c78cb |
child 2952 | 18fada739b55 |
permissions | -rw-r--r-- |
2348 | 1 |
{-# LANGUAGE ScopedTypeVariables #-} |
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 |
import qualified Control.Exception as Exception |
1804 | 5 |
import Control.Concurrent.Chan |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
6 |
import Control.Concurrent |
1804 | 7 |
import Control.Monad |
8 |
import System.IO |
|
9 |
---------------- |
|
10 |
import CoreTypes |
|
11 |
||
2001 | 12 |
listenLoop :: Handle -> Int -> [String] -> Chan CoreMessage -> Int -> IO () |
13 |
listenLoop handle linesNumber buf chan clientID = do |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
14 |
str <- hGetLine handle |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
15 |
if (linesNumber > 50) || (length str > 450) then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
16 |
writeChan chan $ ClientMessage (clientID, ["QUIT", "Protocol violation"]) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
17 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
18 |
if str == "" then do |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
19 |
writeChan chan $ ClientMessage (clientID, buf) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
20 |
yield |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
21 |
listenLoop handle 0 [] chan clientID |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
22 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
23 |
listenLoop handle (linesNumber + 1) (buf ++ [str]) chan clientID |
1804 | 24 |
|
25 |
clientRecvLoop :: Handle -> Chan CoreMessage -> Int -> IO () |
|
26 |
clientRecvLoop handle chan clientID = |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
27 |
listenLoop handle 0 [] chan clientID |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
28 |
`catch` (\e -> clientOff (show e) >> return ()) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
29 |
where clientOff msg = writeChan chan $ ClientMessage (clientID, ["QUIT", msg]) -- if the client disconnects, we perform as if it sent QUIT message |
1804 | 30 |
|
31 |
clientSendLoop :: Handle -> Chan CoreMessage -> Chan [String] -> Int -> IO() |
|
32 |
clientSendLoop handle coreChan chan clientID = do |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
33 |
answer <- readChan chan |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
34 |
doClose <- Exception.handle |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
35 |
(\(e :: Exception.IOException) -> if isQuit answer then return True else sendQuit e >> return False) $ do |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
36 |
forM_ answer (hPutStrLn handle) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
37 |
hPutStrLn handle "" |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
38 |
hFlush handle |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
39 |
return $ isQuit answer |
1804 | 40 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
41 |
if doClose then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
42 |
Exception.handle (\(_ :: Exception.IOException) -> putStrLn "error on hClose") $ hClose handle |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
43 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
44 |
clientSendLoop handle coreChan chan clientID |
1804 | 45 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
46 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
47 |
sendQuit e = writeChan coreChan $ ClientMessage (clientID, ["QUIT", show e]) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
48 |
isQuit ("BYE":xs) = True |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
49 |
isQuit _ = False |