author | unC0Rr |
Sat, 13 Nov 2010 11:33:50 +0300 | |
branch | 0.9.14 |
changeset 4266 | bc6e9859f142 |
parent 4242 | 5e3c5fe2cb14 |
child 4295 | 1f5604cd99be |
permissions | -rw-r--r-- |
4242 | 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 |
|
4242 | 9 |
import qualified Data.ByteString.UTF8 as BUTF8 |
10 |
import qualified Data.ByteString as B |
|
1804 | 11 |
---------------- |
12 |
import CoreTypes |
|
3458 | 13 |
|
4242 | 14 |
listenLoop :: Handle -> Int -> [String] -> Chan CoreMessage -> Int -> IO () |
15 |
listenLoop handle linesNumber buf chan clientID = do |
|
16 |
str <- liftM BUTF8.toString $ B.hGetLine handle |
|
17 |
if (linesNumber > 50) || (length str > 450) then |
|
18 |
writeChan chan $ ClientMessage (clientID, ["QUIT", "Protocol violation"]) |
|
19 |
else |
|
20 |
if str == "" then do |
|
21 |
writeChan chan $ ClientMessage (clientID, buf) |
|
22 |
yield |
|
23 |
listenLoop handle 0 [] chan clientID |
|
24 |
else |
|
25 |
listenLoop handle (linesNumber + 1) (buf ++ [str]) chan clientID |
|
3500
af8390d807d6
Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents:
3458
diff
changeset
|
26 |
|
4242 | 27 |
clientRecvLoop :: Handle -> Chan CoreMessage -> Int -> IO () |
28 |
clientRecvLoop handle chan clientID = |
|
29 |
listenLoop handle 0 [] chan clientID |
|
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 |
|
3500
af8390d807d6
Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents:
3458
diff
changeset
|
32 |
|
4242 | 33 |
clientSendLoop :: Handle -> Chan CoreMessage -> Chan [String] -> Int -> IO() |
34 |
clientSendLoop handle coreChan chan clientID = do |
|
35 |
answer <- readChan chan |
|
36 |
doClose <- Exception.handle |
|
37 |
(\(e :: Exception.IOException) -> if isQuit answer then return True else sendQuit e >> return False) $ do |
|
38 |
B.hPutStrLn handle $ BUTF8.fromString $ unlines answer |
|
39 |
hFlush handle |
|
40 |
return $ isQuit answer |
|
3500
af8390d807d6
Use sockets instead of handles, use bytestrings instead of strings
unc0rr
parents:
3458
diff
changeset
|
41 |
|
4242 | 42 |
if doClose then |
43 |
Exception.handle (\(_ :: Exception.IOException) -> putStrLn "error on hClose") $ hClose handle |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
44 |
else |
4242 | 45 |
clientSendLoop handle coreChan chan clientID |
1804 | 46 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
47 |
where |
4242 | 48 |
sendQuit e = writeChan coreChan $ ClientMessage (clientID, ["QUIT", show e]) |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
49 |
isQuit ("BYE":xs) = True |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
50 |
isQuit _ = False |