From af5bc8e39f3603ba8ce362f23b0f40b9a10a4ba0 Mon Sep 17 00:00:00 2001 From: Daniel Gröber Date: Sun, 22 Dec 2013 22:04:41 +0000 Subject: Add Monoid instance for ExtendedHandshake --- src/Network/BitTorrent/Exchange/Message.hs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/Network/BitTorrent') diff --git a/src/Network/BitTorrent/Exchange/Message.hs b/src/Network/BitTorrent/Exchange/Message.hs index b7567e60..8d0c62f4 100644 --- a/src/Network/BitTorrent/Exchange/Message.hs +++ b/src/Network/BitTorrent/Exchange/Message.hs @@ -677,6 +677,24 @@ defaultQueueLength = 1 instance Default ExtendedHandshake where def = ExtendedHandshake def def def def def def def def +instance Monoid ExtendedHandshake where + mempty = def { ehsCaps = mempty } + mappend old new = + ExtendedHandshake { + ehsCaps = ehsCaps old <> ehsCaps new, + ehsIPv4 = ehsIPv4 old `mergeOld` ehsIPv4 new, + ehsIPv6 = ehsIPv6 old `mergeOld` ehsIPv6 new, + ehsMetadataSize = ehsMetadataSize old `mergeNew` ehsMetadataSize new, + ehsPort = ehsPort old `mergeOld` ehsPort new, + ehsQueueLength = ehsQueueLength old `mergeNew` ehsQueueLength new, + ehsVersion = ehsVersion old `mergeOld` ehsVersion new, + ehsYourIp = ehsYourIp old `mergeOld` ehsYourIp new + } + where + mergeOld old new = old <|> new + mergeNew old new = new <|> old + + instance BEncode ExtendedHandshake where toBEncode ExtendedHandshake {..} = toDict $ "ipv4" .=? (S.encode <$> ehsIPv4) -- cgit v1.2.3