gameServer/Utils.hs
changeset 6068 e18713ecf1e0
parent 5880 a6573cc5903e
child 6191 190a8e5d9956
equal deleted inserted replaced
6067:fe7990b3db66 6068:e18713ecf1e0
    23 sockAddr2String (SockAddrInet _ hostAddr) = liftM B.pack $ inet_ntoa hostAddr
    23 sockAddr2String (SockAddrInet _ hostAddr) = liftM B.pack $ inet_ntoa hostAddr
    24 sockAddr2String (SockAddrInet6 _ _ (a, b, c, d) _) =
    24 sockAddr2String (SockAddrInet6 _ _ (a, b, c, d) _) =
    25     return $ B.pack $ (foldr1 (.)
    25     return $ B.pack $ (foldr1 (.)
    26         $ List.intersperse (':':)
    26         $ List.intersperse (':':)
    27         $ concatMap (\n -> (\(a0, a1) -> [showHex a0, showHex a1]) $ divMod n 65536) [a, b, c, d]) []
    27         $ concatMap (\n -> (\(a0, a1) -> [showHex a0, showHex a1]) $ divMod n 65536) [a, b, c, d]) []
    28 
       
    29 toEngineMsg :: B.ByteString -> B.ByteString
       
    30 toEngineMsg msg = B.pack $ Base64.encode (fromIntegral (BW.length msg) : BW.unpack msg)
       
    31 
       
    32 fromEngineMsg :: B.ByteString -> Maybe B.ByteString
       
    33 fromEngineMsg msg = liftM BW.pack (Base64.decode (B.unpack msg) >>= removeLength)
       
    34     where
       
    35         removeLength (x:xs) = if length xs == fromIntegral x then Just xs else Nothing
       
    36         removeLength _ = Nothing
       
    37 
       
    38 checkNetCmd :: B.ByteString -> (Bool, Bool)
       
    39 checkNetCmd msg = check decoded
       
    40     where
       
    41         decoded = fromEngineMsg msg
       
    42         check Nothing = (False, False)
       
    43         check (Just ms) | B.length ms > 0 = let m = B.head ms in (m `Set.member` legalMessages, m == '+')
       
    44                         | otherwise        = (False, False)
       
    45         legalMessages = Set.fromList $ "M#+LlRrUuDdZzAaSjJ,sFNpPwtghbc12345" ++ slotMessages
       
    46         slotMessages = "\128\129\130\131\132\133\134\135\136\137\138"
       
    47 
    28 
    48 maybeRead :: Read a => String -> Maybe a
    29 maybeRead :: Read a => String -> Maybe a
    49 maybeRead s = case reads s of
    30 maybeRead s = case reads s of
    50     [(x, rest)] | all isSpace rest -> Just x
    31     [(x, rest)] | all isSpace rest -> Just x
    51     _         -> Nothing
    32     _         -> Nothing