gameServer/ClientIO.hs
changeset 5000 72d8fb26223d
parent 4998 cdcdf37e5532
child 5001 312f4dd41753
equal deleted inserted replaced
4999:a3a09b107652 5000:72d8fb26223d
    44                 recieveWithBufferLoop newrecvBuf
    44                 recieveWithBufferLoop newrecvBuf
    45 
    45 
    46         sendPacket packet = writeChan chan $ ClientMessage (ci, packet)
    46         sendPacket packet = writeChan chan $ ClientMessage (ci, packet)
    47 
    47 
    48 clientRecvLoop :: Socket -> Chan CoreMessage -> ClientIndex -> IO ()
    48 clientRecvLoop :: Socket -> Chan CoreMessage -> ClientIndex -> IO ()
    49 clientRecvLoop s chan ci =
    49 clientRecvLoop s chan ci = Exception.block $
    50     do
    50         ((Exception.unblock $ listenLoop s chan ci >> return "Connection closed") `catch` (return . B.pack . show) >>= clientOff)
    51         msg <- (listenLoop s chan ci >> return "Connection closed") `catch` (return . B.pack . show)
       
    52         clientOff msg
       
    53     `Exception.finally`
    51     `Exception.finally`
    54     do
       
    55         clientOff "Connection closed ()"
       
    56         remove
    52         remove
    57     where
    53     where
    58         clientOff msg = writeChan chan $ ClientMessage (ci, ["QUIT", msg])
    54         clientOff msg = writeChan chan $ ClientMessage (ci, ["QUIT", msg])
    59         remove = writeChan chan $ Remove ci
    55         remove = writeChan chan $ Remove ci
    60 
    56 
    62 
    58 
    63 clientSendLoop :: Socket -> ThreadId -> Chan CoreMessage -> Chan [B.ByteString] -> ClientIndex -> IO ()
    59 clientSendLoop :: Socket -> ThreadId -> Chan CoreMessage -> Chan [B.ByteString] -> ClientIndex -> IO ()
    64 clientSendLoop s tId cChan chan ci = do
    60 clientSendLoop s tId cChan chan ci = do
    65     answer <- readChan chan
    61     answer <- readChan chan
    66     Exception.handle
    62     Exception.handle
    67         (\(e :: Exception.IOException) -> unless (isQuit answer) $ sendQuit e) $
    63         (\(e :: Exception.IOException) -> unless (isQuit answer) . killReciever $ show e) $
    68             sendAll s $ B.unlines answer `B.append` B.singleton '\n'
    64             sendAll s $ B.unlines answer `B.append` B.singleton '\n'
    69 
    65 
    70     if isQuit answer then
    66     if isQuit answer then
    71         do
    67         do
    72         Exception.handle (\(_ :: Exception.IOException) -> putStrLn "error on sClose") $ sClose s
    68         Exception.handle (\(_ :: Exception.IOException) -> putStrLn "error on sClose") $ sClose s
    73         Exception.throwTo tId ShutdownThreadException
    69         killReciever "Connection closed"
    74         else
    70         else
    75         clientSendLoop s tId cChan chan ci
    71         clientSendLoop s tId cChan chan ci
    76 
    72 
    77     where
    73     where
    78         sendQuit e = do
    74         killReciever = Exception.throwTo tId . ShutdownThreadException
    79             print e
       
    80             writeChan cChan $ ClientMessage (ci, ["QUIT", B.pack $ show e])
       
    81         isQuit ("BYE":_) = True
    75         isQuit ("BYE":_) = True
    82         isQuit _ = False
    76         isQuit _ = False