summaryrefslogtreecommitdiff
path: root/src/Data
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2018-12-16 20:54:30 -0500
committerJoe Crayne <joe@jerkface.net>2018-12-16 20:54:36 -0500
commit7dcd6ce16ce480791e4207e2c6d554a76fe75d6e (patch)
tree92710c3b822c47896df2bfb94fafbba1ef48c55c /src/Data
parent688450c2d9998db7b4389dc9642d219774c23857 (diff)
tcp: Avoid ponging on new non-pinged connections.
Diffstat (limited to 'src/Data')
-rw-r--r--src/Data/Tox/Relay.hs15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/Data/Tox/Relay.hs b/src/Data/Tox/Relay.hs
index d1e9fb99..c563db8d 100644
--- a/src/Data/Tox/Relay.hs
+++ b/src/Data/Tox/Relay.hs
@@ -4,6 +4,7 @@
4{-# LANGUAGE GeneralizedNewtypeDeriving #-} 4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5{-# LANGUAGE KindSignatures #-} 5{-# LANGUAGE KindSignatures #-}
6{-# LANGUAGE MultiParamTypeClasses #-} 6{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE PatternSynonyms #-}
7{-# LANGUAGE StandaloneDeriving #-} 8{-# LANGUAGE StandaloneDeriving #-}
8{-# LANGUAGE UndecidableInstances #-} 9{-# LANGUAGE UndecidableInstances #-}
9module Data.Tox.Relay where 10module Data.Tox.Relay where
@@ -61,9 +62,15 @@ data RelayPacket
61 | RelayData ByteString ConId 62 | RelayData ByteString ConId
62 deriving (Eq,Ord,Show,Data) 63 deriving (Eq,Ord,Show,Data)
63 64
64packetNumber :: RelayPacket -> Word8 65newtype PacketNumber = PacketNumber { packetNumberToWord8 :: Word8 }
65packetNumber (RelayData _ (ConId conid)) = conid -- 0 to 15 not allowed. 66 deriving (Eq,Ord,Show)
66packetNumber rp = fromIntegral $ pred $ constrIndex $ toConstr rp 67
68pattern PingPacket = PacketNumber 4
69pattern OnionPacketID = PacketNumber 8
70
71packetNumber :: RelayPacket -> PacketNumber
72packetNumber (RelayData _ (ConId conid)) = PacketNumber $ conid -- 0 to 15 not allowed.
73packetNumber rp = PacketNumber $ fromIntegral $ pred $ constrIndex $ toConstr rp
67 74
68instance Sized RelayPacket where 75instance Sized RelayPacket where
69 size = mappend (ConstSize 1) $ VarSize $ \x -> case x of 76 size = mappend (ConstSize 1) $ VarSize $ \x -> case x of
@@ -101,7 +108,7 @@ instance Serialize RelayPacket where
101 conid -> (`RelayData` ConId conid) <$> (remaining >>= getBytes) 108 conid -> (`RelayData` ConId conid) <$> (remaining >>= getBytes)
102 109
103 put rp = do 110 put rp = do
104 putWord8 $ packetNumber rp 111 putWord8 $ packetNumberToWord8 $ packetNumber rp
105 case rp of 112 case rp of
106 RoutingRequest k -> putPublicKey k 113 RoutingRequest k -> putPublicKey k
107 RoutingResponse rpid k -> put rpid >> putPublicKey k 114 RoutingResponse rpid k -> put rpid >> putPublicKey k