author | unc0rr |
Thu, 06 May 2010 17:39:08 +0000 | |
changeset 3435 | 4e4f88a7bdf2 |
parent 2954 | 55d272e34f9a |
child 3458 | 11cd56019f00 |
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 |
|
2952
18fada739b55
- Convert strings from utf-8 on recieve, and back to utf-8 when send them
unc0rr
parents:
2867
diff
changeset
|
9 |
import qualified Data.ByteString.UTF8 as BUTF8 |
18fada739b55
- Convert strings from utf-8 on recieve, and back to utf-8 when send them
unc0rr
parents:
2867
diff
changeset
|
10 |
import qualified Data.ByteString as B |
1804 | 11 |
---------------- |
12 |
import CoreTypes |
|
3435 | 13 |
import RoomsAndClients |
1804 | 14 |
|
3435 | 15 |
listenLoop :: Handle -> Int -> [String] -> Chan CoreMessage -> ClientIndex -> IO () |
2001 | 16 |
listenLoop handle linesNumber buf chan clientID = do |
2952
18fada739b55
- Convert strings from utf-8 on recieve, and back to utf-8 when send them
unc0rr
parents:
2867
diff
changeset
|
17 |
str <- liftM BUTF8.toString $ B.hGetLine handle |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
18 |
if (linesNumber > 50) || (length str > 450) then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
19 |
writeChan chan $ ClientMessage (clientID, ["QUIT", "Protocol violation"]) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
20 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
21 |
if str == "" then do |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
22 |
writeChan chan $ ClientMessage (clientID, buf) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
23 |
yield |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
24 |
listenLoop handle 0 [] chan clientID |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
25 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
26 |
listenLoop handle (linesNumber + 1) (buf ++ [str]) chan clientID |
1804 | 27 |
|
3435 | 28 |
clientRecvLoop :: Handle -> Chan CoreMessage -> ClientIndex -> IO () |
1804 | 29 |
clientRecvLoop handle chan clientID = |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
30 |
listenLoop handle 0 [] chan clientID |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
31 |
`catch` (\e -> clientOff (show e) >> return ()) |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
32 |
where clientOff msg = writeChan chan $ ClientMessage (clientID, ["QUIT", msg]) -- if the client disconnects, we perform as if it sent QUIT message |
1804 | 33 |
|
3435 | 34 |
clientSendLoop :: Handle -> Chan CoreMessage -> Chan [String] -> ClientIndex -> IO() |
1804 | 35 |
clientSendLoop handle coreChan chan clientID = do |
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
36 |
answer <- readChan chan |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
37 |
doClose <- Exception.handle |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
38 |
(\(e :: Exception.IOException) -> if isQuit answer then return True else sendQuit e >> return False) $ do |
2954 | 39 |
B.hPutStrLn handle $ BUTF8.fromString $ unlines answer |
40 |
hFlush handle |
|
41 |
return $ isQuit answer |
|
1804 | 42 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
43 |
if doClose then |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
44 |
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
|
45 |
else |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
46 |
clientSendLoop handle coreChan chan clientID |
1804 | 47 |
|
2867
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
48 |
where |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
49 |
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
|
50 |
isQuit ("BYE":xs) = True |
9be6693c78cb
- Unbreak support for client versions prior to 0.9.13-dev
unc0rr
parents:
2352
diff
changeset
|
51 |
isQuit _ = False |