gameServer/EngineInteraction.hs
changeset 11855 ad435d95ca4b
parent 11586 2963c85c6de4
child 12113 72f5d670bbee
--- a/gameServer/EngineInteraction.hs	Sat Oct 01 20:56:42 2016 +0300
+++ b/gameServer/EngineInteraction.hs	Sat Oct 01 21:27:05 2016 +0300
@@ -63,7 +63,7 @@
 #endif
 
 toEngineMsg :: B.ByteString -> B.ByteString
-toEngineMsg msg = B.pack $ Base64.encode (fromIntegral (BW.length msg) : BW.unpack msg)
+toEngineMsg msg = Base64.encode (fromIntegral (BW.length msg) `BW.cons` msg)
 
 
 {-fromEngineMsg :: B.ByteString -> Maybe B.ByteString
@@ -85,15 +85,15 @@
 checkNetCmd :: [Word8] -> B.ByteString -> (B.ByteString, B.ByteString, Maybe (Maybe B.ByteString))
 checkNetCmd teamsIndexes msg = check decoded
     where
-        decoded = liftM (splitMessages . BW.pack) $ Base64.decode $ B.unpack msg
-        check Nothing = (B.empty, B.empty, Nothing)
-        check (Just msgs) = let (a, b) = (filter isLegal msgs, filter isNonEmpty a) in (encode a, encode b, lft a)
-        encode = B.pack . Base64.encode . BW.unpack . B.concat
+        decoded = liftM splitMessages $ Base64.decode msg
+        check (Left _) = (B.empty, B.empty, Nothing)
+        check (Right msgs) = let (a, b) = (filter isLegal msgs, filter isNonEmpty a) in (encode a, encode b, lft a)
+        encode = Base64.encode . B.concat
         isLegal m = (B.length m > 1) && (flip Set.member legalMessages . B.head . B.tail $ m) && not (isMalformed (B.head m) (B.tail m))
         lft = foldr l Nothing
         l m n = let m' = B.head $ B.tail m; tst = flip Set.member in
                       if not $ tst timedMessages m' then n
-                        else if '+' /= m' then Just Nothing else Just . Just . B.pack . Base64.encode . BW.unpack $ m
+                        else if '+' /= m' then Just Nothing else Just . Just . Base64.encode $ m
         isNonEmpty = (/=) '+' . B.head . B.tail
         legalMessages = Set.fromList $ "M#+LlRrUuDdZzAaSjJ,NpPwtgfhbc12345" ++ slotMessages
         slotMessages = "\128\129\130\131\132\133\134\135\136\137\138"