diff options
author | joe <joe@jerkface.net> | 2017-10-29 17:09:43 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-10-29 17:09:43 -0400 |
commit | a0ddb901b9b67bf7722ccf57806962456b261292 (patch) | |
tree | 940a1c8ed1abd0e6f9e879e667d5150b6801adf1 /src/Network/Tox | |
parent | b5e6bf0c5a13f1ce9a18b456d8f2705497e4388a (diff) |
Serialize instance for CryptoPacket.
Diffstat (limited to 'src/Network/Tox')
-rw-r--r-- | src/Network/Tox/Crypto/Transport.hs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/Network/Tox/Crypto/Transport.hs b/src/Network/Tox/Crypto/Transport.hs index 8b7a7222..0011fac0 100644 --- a/src/Network/Tox/Crypto/Transport.hs +++ b/src/Network/Tox/Crypto/Transport.hs | |||
@@ -1,5 +1,7 @@ | |||
1 | {-# LANGUAGE KindSignatures #-} | 1 | {-# LANGUAGE KindSignatures #-} |
2 | {-# LANGUAGE ViewPatterns #-} | 2 | {-# LANGUAGE ViewPatterns #-} |
3 | {-# LANGUAGE FlexibleInstances #-} | ||
4 | {-# LANGUAGE LambdaCase #-} | ||
3 | module Network.Tox.Crypto.Transport | 5 | module Network.Tox.Crypto.Transport |
4 | ( parseNetCrypto | 6 | ( parseNetCrypto |
5 | , encodeNetCrypto | 7 | , encodeNetCrypto |
@@ -40,6 +42,7 @@ import Network.Tox.DHT.Transport (Cookie) | |||
40 | 42 | ||
41 | import Network.Socket | 43 | import Network.Socket |
42 | import Data.ByteString as B | 44 | import Data.ByteString as B |
45 | import Data.Monoid | ||
43 | import Data.Word | 46 | import Data.Word |
44 | import Crypto.Hash | 47 | import Crypto.Hash |
45 | import Control.Lens | 48 | import Control.Lens |
@@ -76,6 +79,15 @@ data CryptoPacket (f :: * -> *) = CryptoPacket | |||
76 | , pktData :: f CryptoData | 79 | , pktData :: f CryptoData |
77 | } | 80 | } |
78 | 81 | ||
82 | instance Sized CryptoData where | ||
83 | size = contramap bufferStart size | ||
84 | <> contramap bufferEnd size | ||
85 | <> contramap bufferData size | ||
86 | |||
87 | instance Serialize (CryptoPacket Encrypted) where | ||
88 | get = CryptoPacket <$> get <*> get | ||
89 | put (CryptoPacket n16 dta) = put n16 >> put dta | ||
90 | |||
79 | data CryptoData = CryptoData | 91 | data CryptoData = CryptoData |
80 | { -- | [our recvbuffers buffer_start, (highest packet number handled + 1), (big endian)] | 92 | { -- | [our recvbuffers buffer_start, (highest packet number handled + 1), (big endian)] |
81 | bufferStart :: Word32 | 93 | bufferStart :: Word32 |
@@ -108,6 +120,12 @@ data CryptoMessage | |||
108 | | UpToN { msgID :: MessageID, msgBytes :: ByteString } -- length < N | 120 | | UpToN { msgID :: MessageID, msgBytes :: ByteString } -- length < N |
109 | deriving (Eq,Show) | 121 | deriving (Eq,Show) |
110 | 122 | ||
123 | instance Sized CryptoMessage where | ||
124 | size = VarSize $ \case | ||
125 | OneByte {} -> 1 | ||
126 | TwoByte {} -> 2 | ||
127 | UpToN { msgBytes = bs } -> 1 + B.length bs | ||
128 | |||
111 | erCompat lens = error $ "Use of '" ++ lens ++ "' lens on incompatible CryptoMessage type" | 129 | erCompat lens = error $ "Use of '" ++ lens ++ "' lens on incompatible CryptoMessage type" |
112 | 130 | ||
113 | typingStatus :: Functor f => (UserStatus -> f UserStatus)-> (CryptoMessage -> f CryptoMessage) | 131 | typingStatus :: Functor f => (UserStatus -> f UserStatus)-> (CryptoMessage -> f CryptoMessage) |