summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/Tox/Crypto/Transport.hs18
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 #-}
3module Network.Tox.Crypto.Transport 5module Network.Tox.Crypto.Transport
4 ( parseNetCrypto 6 ( parseNetCrypto
5 , encodeNetCrypto 7 , encodeNetCrypto
@@ -40,6 +42,7 @@ import Network.Tox.DHT.Transport (Cookie)
40 42
41import Network.Socket 43import Network.Socket
42import Data.ByteString as B 44import Data.ByteString as B
45import Data.Monoid
43import Data.Word 46import Data.Word
44import Crypto.Hash 47import Crypto.Hash
45import Control.Lens 48import Control.Lens
@@ -76,6 +79,15 @@ data CryptoPacket (f :: * -> *) = CryptoPacket
76 , pktData :: f CryptoData 79 , pktData :: f CryptoData
77 } 80 }
78 81
82instance Sized CryptoData where
83 size = contramap bufferStart size
84 <> contramap bufferEnd size
85 <> contramap bufferData size
86
87instance Serialize (CryptoPacket Encrypted) where
88 get = CryptoPacket <$> get <*> get
89 put (CryptoPacket n16 dta) = put n16 >> put dta
90
79data CryptoData = CryptoData 91data 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
123instance Sized CryptoMessage where
124 size = VarSize $ \case
125 OneByte {} -> 1
126 TwoByte {} -> 2
127 UpToN { msgBytes = bs } -> 1 + B.length bs
128
111erCompat lens = error $ "Use of '" ++ lens ++ "' lens on incompatible CryptoMessage type" 129erCompat lens = error $ "Use of '" ++ lens ++ "' lens on incompatible CryptoMessage type"
112 130
113typingStatus :: Functor f => (UserStatus -> f UserStatus)-> (CryptoMessage -> f CryptoMessage) 131typingStatus :: Functor f => (UserStatus -> f UserStatus)-> (CryptoMessage -> f CryptoMessage)