# HG changeset patch
# User EJ <eivind.jahren@gmail.com>
# Date 1300629819 -3600
# Node ID 813554ab76b86325508dfcc9cb16d2f37c7a3218
# Parent  42746c5d4a80469a831aedf1bb2073fc63f0e07c
Replaced bs2packs.

diff -r 42746c5d4a80 -r 813554ab76b8 gameServer/ClientIO.hs
--- a/gameServer/ClientIO.hs	Mon Mar 21 21:04:20 2011 +0300
+++ b/gameServer/ClientIO.hs	Sun Mar 20 15:03:39 2011 +0100
@@ -2,6 +2,7 @@
 module ClientIO where
 
 import qualified Control.Exception as Exception
+import Control.Monad.State
 import Control.Concurrent.Chan
 import Control.Concurrent
 import Control.Monad
@@ -17,21 +18,17 @@
 pDelim :: B.ByteString
 pDelim = "\n\n"
 
-bs2Packets :: B.ByteString -> ([[B.ByteString]], B.ByteString)
-bs2Packets = unfoldrE extractPackets
-    where
-    extractPackets :: B.ByteString -> Either B.ByteString ([B.ByteString], B.ByteString)
-    extractPackets buf =
-        let buf' = until (not . B.isPrefixOf pDelim) (B.drop 2) buf in
-            let (bsPacket, bufTail) = B.breakSubstring pDelim buf' in
-                if B.null bufTail then
-                    Left bsPacket
-                    else
-                    if B.null bsPacket then 
-                        Left bufTail
-                        else
-                        Right (B.splitWith (== '\n') bsPacket, bufTail)
+bs2Packets = runState takePacks
 
+takePacks :: State B.ByteString [[B.ByteString]]
+takePacks
+  = do modify (until (not . B.isPrefixOf pDelim) (B.drop 2))
+       packet <- state $ B.breakSubstring pDelim
+       buf <- get
+       if B.null buf then put packet >> return [] else
+        if B.null packet then  return [] else
+         do packets <- takePacks
+            return (B.splitWith (== '\n') packet : packets)
 
 listenLoop :: Socket -> Chan CoreMessage -> ClientIndex -> IO ()
 listenLoop sock chan ci = recieveWithBufferLoop B.empty