diff options
Diffstat (limited to 'src/Network/Tox/Crypto/Transport.hs')
-rw-r--r-- | src/Network/Tox/Crypto/Transport.hs | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/src/Network/Tox/Crypto/Transport.hs b/src/Network/Tox/Crypto/Transport.hs index 35b4904e..21100c86 100644 --- a/src/Network/Tox/Crypto/Transport.hs +++ b/src/Network/Tox/Crypto/Transport.hs | |||
@@ -48,6 +48,9 @@ module Network.Tox.Crypto.Transport | |||
48 | , isIndirectGrpChat | 48 | , isIndirectGrpChat |
49 | , LossyOrLossless(..) | 49 | , LossyOrLossless(..) |
50 | , lossyness | 50 | , lossyness |
51 | , fromEnum8 | ||
52 | , fromEnum16 | ||
53 | , toEnum8 | ||
51 | ) where | 54 | ) where |
52 | 55 | ||
53 | import Crypto.Tox | 56 | import Crypto.Tox |
@@ -61,6 +64,7 @@ import Data.ByteString as B | |||
61 | import Data.Maybe | 64 | import Data.Maybe |
62 | import Data.Monoid | 65 | import Data.Monoid |
63 | import Data.Word | 66 | import Data.Word |
67 | import Data.Bits | ||
64 | import Crypto.Hash | 68 | import Crypto.Hash |
65 | import Control.Lens | 69 | import Control.Lens |
66 | import Data.Text as T | 70 | import Data.Text as T |
@@ -418,14 +422,23 @@ class AsWord16 a where | |||
418 | 422 | ||
419 | toEnum8 ::Enum a => Word8 -> a | 423 | toEnum8 ::Enum a => Word8 -> a |
420 | toEnum8 = toEnum . fromIntegral | 424 | toEnum8 = toEnum . fromIntegral |
425 | fromEnum8 :: Enum a => a -> Word8 | ||
426 | fromEnum8 = fromIntegral . fromEnum | ||
427 | |||
421 | fromEnum16 :: Enum a => a -> Word16 | 428 | fromEnum16 :: Enum a => a -> Word16 |
422 | fromEnum16 = fromIntegral . fromEnum | 429 | fromEnum16 = fromIntegral . fromEnum |
423 | 430 | ||
431 | |||
432 | -- MessageType, for our client keep it inside 16 bits | ||
433 | -- but we should extend it to 32 or even 64 on the wire. | ||
434 | -- Bits: 000000glxxxxxxxx, x = message id or extension specific, l = if extended, lossy/lossless, g = if extended, nongroup/group | ||
435 | -- (at least one bit set in high byte means extended, if none but the g flag and possibly l flag, assume default grp extension) | ||
424 | instance AsWord16 MessageType where | 436 | instance AsWord16 MessageType where |
425 | toWord16 (Msg msgID) = fromIntegral (fromIntegral (fromEnum16 msgID) :: Word8) | 437 | toWord16 (Msg msgID) = fromIntegral (fromIntegral (fromEnum16 msgID) :: Word8) |
426 | toWord16 (GrpMsg lsy msgName) = 256 * (fromEnum16 lsy + 1) + fromIntegral (fromIntegral (fromEnum msgName) :: Word8) | 438 | toWord16 (GrpMsg lsy msgName) = 512 + 256 * (fromEnum16 lsy) + fromIntegral (fromIntegral (fromEnum msgName) :: Word8) |
427 | fromWord16 x | x < 256 = Msg (toEnum $ fromIntegral x) | 439 | fromWord16 x | x < 256 = Msg (toEnum $ fromIntegral x) |
428 | fromWord16 x = GrpMsg (toEnum8 ((fromIntegral (x `div` 256)) - 1)) (toEnum8 (fromIntegral x :: Word8)) | 440 | fromWord16 x | x < 1024, x .|. 0x0200 == 0x0200 = GrpMsg (toEnum8 (fromIntegral ((x - 512) `div` 256))) (toEnum8 (fromIntegral x :: Word8)) |
441 | fromWord16 x = error "Not clear how to convert Word16 to MessageType" | ||
429 | 442 | ||
430 | word16 :: (Functor f, AsWord16 x) => (Word16 -> f Word16) -> (x -> f x) | 443 | word16 :: (Functor f, AsWord16 x) => (Word16 -> f Word16) -> (x -> f x) |
431 | word16 = lens toWord16 (\_ x -> fromWord16 x) | 444 | word16 = lens toWord16 (\_ x -> fromWord16 x) |