summaryrefslogtreecommitdiff
path: root/src/Network/Tox/Crypto/Transport.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/Tox/Crypto/Transport.hs')
-rw-r--r--src/Network/Tox/Crypto/Transport.hs19
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
53import Crypto.Tox 56import Crypto.Tox
@@ -61,6 +64,7 @@ import Data.ByteString as B
61import Data.Maybe 64import Data.Maybe
62import Data.Monoid 65import Data.Monoid
63import Data.Word 66import Data.Word
67import Data.Bits
64import Crypto.Hash 68import Crypto.Hash
65import Control.Lens 69import Control.Lens
66import Data.Text as T 70import Data.Text as T
@@ -418,14 +422,23 @@ class AsWord16 a where
418 422
419toEnum8 ::Enum a => Word8 -> a 423toEnum8 ::Enum a => Word8 -> a
420toEnum8 = toEnum . fromIntegral 424toEnum8 = toEnum . fromIntegral
425fromEnum8 :: Enum a => a -> Word8
426fromEnum8 = fromIntegral . fromEnum
427
421fromEnum16 :: Enum a => a -> Word16 428fromEnum16 :: Enum a => a -> Word16
422fromEnum16 = fromIntegral . fromEnum 429fromEnum16 = 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)
424instance AsWord16 MessageType where 436instance 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
430word16 :: (Functor f, AsWord16 x) => (Word16 -> f Word16) -> (x -> f x) 443word16 :: (Functor f, AsWord16 x) => (Word16 -> f Word16) -> (x -> f x)
431word16 = lens toWord16 (\_ x -> fromWord16 x) 444word16 = lens toWord16 (\_ x -> fromWord16 x)