- Don't pretend client sent some message from sending thread (fixes crash when client is already deleted by recieveng thread)
authorunc0rr
Sat, 12 Mar 2011 22:42:01 +0300
changeset 5000 72d8fb26223d
parent 4999 a3a09b107652
child 5001 312f4dd41753
- Don't pretend client sent some message from sending thread (fixes crash when client is already deleted by recieveng thread) - Better exception handling in recieving thread
gameServer/ClientIO.hs
gameServer/CoreTypes.hs
--- a/gameServer/ClientIO.hs	Fri Mar 11 22:22:28 2011 +0300
+++ b/gameServer/ClientIO.hs	Sat Mar 12 22:42:01 2011 +0300
@@ -46,13 +46,9 @@
         sendPacket packet = writeChan chan $ ClientMessage (ci, packet)
 
 clientRecvLoop :: Socket -> Chan CoreMessage -> ClientIndex -> IO ()
-clientRecvLoop s chan ci =
-    do
-        msg <- (listenLoop s chan ci >> return "Connection closed") `catch` (return . B.pack . show)
-        clientOff msg
+clientRecvLoop s chan ci = Exception.block $
+        ((Exception.unblock $ listenLoop s chan ci >> return "Connection closed") `catch` (return . B.pack . show) >>= clientOff)
     `Exception.finally`
-    do
-        clientOff "Connection closed ()"
         remove
     where
         clientOff msg = writeChan chan $ ClientMessage (ci, ["QUIT", msg])
@@ -64,19 +60,17 @@
 clientSendLoop s tId cChan chan ci = do
     answer <- readChan chan
     Exception.handle
-        (\(e :: Exception.IOException) -> unless (isQuit answer) $ sendQuit e) $
+        (\(e :: Exception.IOException) -> unless (isQuit answer) . killReciever $ show e) $
             sendAll s $ B.unlines answer `B.append` B.singleton '\n'
 
     if isQuit answer then
         do
         Exception.handle (\(_ :: Exception.IOException) -> putStrLn "error on sClose") $ sClose s
-        Exception.throwTo tId ShutdownThreadException
+        killReciever "Connection closed"
         else
         clientSendLoop s tId cChan chan ci
 
     where
-        sendQuit e = do
-            print e
-            writeChan cChan $ ClientMessage (ci, ["QUIT", B.pack $ show e])
+        killReciever = Exception.throwTo tId . ShutdownThreadException
         isQuit ("BYE":_) = True
         isQuit _ = False
--- a/gameServer/CoreTypes.hs	Fri Mar 11 22:22:28 2011 +0300
+++ b/gameServer/CoreTypes.hs	Sat Mar 12 22:42:01 2011 +0300
@@ -206,7 +206,9 @@
 
 instance Exception ShutdownException
 
-data ShutdownThreadException = ShutdownThreadException
-     deriving (Show, Typeable)
+data ShutdownThreadException = ShutdownThreadException String
+     deriving Typeable
 
+instance Show ShutdownThreadException where
+    show (ShutdownThreadException s) = "kill: " ++ s
 instance Exception ShutdownThreadException