diff options
author | James Crayne <jim.crayne@gmail.com> | 2017-11-03 00:06:14 +0000 |
---|---|---|
committer | James Crayne <jim.crayne@gmail.com> | 2017-11-19 23:40:11 +0000 |
commit | 2b58ed1338b2518e8f5ced87b43d6bbf35e0fa8f (patch) | |
tree | 2525df73b60a8a682309d10ef72ddcfc48a37371 /src | |
parent | 9588e6932050444e298ad89bd8d2b273fc8b428c (diff) |
GrpMsg should have lossyness field
Diffstat (limited to 'src')
-rw-r--r-- | src/Network/Tox/Crypto/Transport.hs | 43 |
1 files changed, 34 insertions, 9 deletions
diff --git a/src/Network/Tox/Crypto/Transport.hs b/src/Network/Tox/Crypto/Transport.hs index 8739c853..1e9156c6 100644 --- a/src/Network/Tox/Crypto/Transport.hs +++ b/src/Network/Tox/Crypto/Transport.hs | |||
@@ -403,15 +403,37 @@ instance HasMessageName CryptoMessage where | |||
403 | messageName :: (Functor f, HasMessageName x) => (MessageName -> f MessageName) -> (x -> f x) | 403 | messageName :: (Functor f, HasMessageName x) => (MessageName -> f MessageName) -> (x -> f x) |
404 | messageName = lens getMessageName setMessageName | 404 | messageName = lens getMessageName setMessageName |
405 | 405 | ||
406 | data KnownLossyness = KnownLossy | KnownLossless | ||
407 | deriving (Eq,Ord,Show,Enum) | ||
408 | |||
406 | data MessageType = Msg MessageID | 409 | data MessageType = Msg MessageID |
407 | | GrpMsg MessageName | 410 | | GrpMsg KnownLossyness MessageName |
408 | deriving (Eq,Show) | 411 | deriving (Eq,Show) |
409 | 412 | ||
413 | class AsWord16 a where | ||
414 | toWord16 :: a -> Word16 | ||
415 | fromWord16 :: Word16 -> a | ||
416 | |||
417 | toEnum8 ::Enum a => Word8 -> a | ||
418 | toEnum8 = toEnum . fromIntegral | ||
419 | fromEnum16 :: Enum a => a -> Word16 | ||
420 | fromEnum16 = fromIntegral . fromEnum | ||
421 | |||
422 | instance AsWord16 MessageType where | ||
423 | toWord16 (Msg msgID) = fromIntegral (fromIntegral (fromEnum16 msgID) :: Word8) | ||
424 | toWord16 (GrpMsg lsy msgName) = 256 * (fromEnum16 lsy + 1) + fromIntegral (fromIntegral (fromEnum msgName) :: Word8) | ||
425 | fromWord16 x | x < 256 = Msg (toEnum $ fromIntegral x) | ||
426 | fromWord16 x = GrpMsg (toEnum8 ((fromIntegral (x `div` 256)) - 1)) (toEnum8 (fromIntegral x :: Word8)) | ||
427 | |||
428 | word16 :: (Functor f, AsWord16 x) => (Word16 -> f Word16) -> (x -> f x) | ||
429 | word16 = lens toWord16 (\_ x -> fromWord16 x) | ||
430 | |||
410 | instance Ord MessageType where | 431 | instance Ord MessageType where |
411 | compare (Msg x) (Msg y) = compare x y | 432 | compare (Msg x) (Msg y) = compare x y |
412 | compare (GrpMsg x) (GrpMsg y) = compare x y | 433 | compare (GrpMsg lx x) (GrpMsg ly y) = let r1 = compare lx ly |
413 | compare (Msg _) (GrpMsg _) = LT | 434 | in if r1==EQ then compare x y else r1 |
414 | compare (GrpMsg _) (Msg _) = GT | 435 | compare (Msg _) (GrpMsg _ _) = LT |
436 | compare (GrpMsg _ _) (Msg _) = GT | ||
415 | 437 | ||
416 | class HasMessageType x where | 438 | class HasMessageType x where |
417 | getMessageType :: x -> MessageType | 439 | getMessageType :: x -> MessageType |
@@ -420,13 +442,16 @@ class HasMessageType x where | |||
420 | instance HasMessageType CryptoMessage where | 442 | instance HasMessageType CryptoMessage where |
421 | getMessageType (OneByte mid) = Msg mid | 443 | getMessageType (OneByte mid) = Msg mid |
422 | getMessageType (TwoByte mid _) = Msg mid | 444 | getMessageType (TwoByte mid _) = Msg mid |
423 | getMessageType m@(UpToN mid _) | isIndirectGrpChat mid = GrpMsg (getMessageName m) | 445 | getMessageType m@(UpToN MESSAGE_GROUPCHAT _) = GrpMsg KnownLossless (getMessageName m) |
446 | getMessageType m@(UpToN LOSSY_GROUPCHAT _) = GrpMsg KnownLossy (getMessageName m) | ||
424 | getMessageType (UpToN mid _) = Msg mid | 447 | getMessageType (UpToN mid _) = Msg mid |
425 | 448 | ||
426 | setMessageType m@(UpToN mid _) (GrpMsg mname) | isIndirectGrpChat mid = setMessageName m mname | 449 | setMessageType (OneByte _ ) (GrpMsg KnownLossless mname) = setMessageName (UpToN MESSAGE_GROUPCHAT B.empty ) mname |
427 | setMessageType (OneByte _ ) (GrpMsg mname) = setMessageName (UpToN MESSAGE_GROUPCHAT B.empty ) mname | 450 | setMessageType (TwoByte _ x) (GrpMsg KnownLossless mname) = setMessageName (UpToN MESSAGE_GROUPCHAT (B.singleton x)) mname |
428 | setMessageType (TwoByte _ x) (GrpMsg mname) = setMessageName (UpToN MESSAGE_GROUPCHAT (B.singleton x)) mname | 451 | setMessageType (OneByte _ ) (GrpMsg KnownLossy mname) = setMessageName (UpToN LOSSY_GROUPCHAT B.empty ) mname |
429 | setMessageType (UpToN _ x) (GrpMsg mname) = setMessageName (UpToN MESSAGE_GROUPCHAT x) mname | 452 | setMessageType (TwoByte _ x) (GrpMsg KnownLossy mname) = setMessageName (UpToN LOSSY_GROUPCHAT (B.singleton x)) mname |
453 | setMessageType (UpToN _ x) (GrpMsg KnownLossless mname) = setMessageName (UpToN MESSAGE_GROUPCHAT x) mname | ||
454 | setMessageType (UpToN _ x) (GrpMsg KnownLossy mname) = setMessageName (UpToN LOSSY_GROUPCHAT x) mname | ||
430 | setMessageType m (Msg mid) | Just (True,1) <- msgSizeParam mid = OneByte mid | 455 | setMessageType m (Msg mid) | Just (True,1) <- msgSizeParam mid = OneByte mid |
431 | setMessageType (OneByte mid0 ) (Msg mid) | Just (True,2) <- msgSizeParam mid = TwoByte mid 0 | 456 | setMessageType (OneByte mid0 ) (Msg mid) | Just (True,2) <- msgSizeParam mid = TwoByte mid 0 |
432 | setMessageType (TwoByte mid0 x) (Msg mid) | Just (True,2) <- msgSizeParam mid = TwoByte mid x | 457 | setMessageType (TwoByte mid0 x) (Msg mid) | Just (True,2) <- msgSizeParam mid = TwoByte mid x |