From 884a2aa67d7b25ccdcc19f59000fb77a9a718a66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Wed, 12 Aug 2020 01:24:28 +0200 Subject: [PATCH] aquatic ws: deserialize OutMessage properly Performance seems to be about the same --- aquatic_ws_protocol/src/lib.rs | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/aquatic_ws_protocol/src/lib.rs b/aquatic_ws_protocol/src/lib.rs index 96acb6b..479ac4a 100644 --- a/aquatic_ws_protocol/src/lib.rs +++ b/aquatic_ws_protocol/src/lib.rs @@ -283,10 +283,10 @@ impl InMessage { #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(untagged)] pub enum OutMessage { - AnnounceResponse(AnnounceResponse), - ScrapeResponse(ScrapeResponse), Offer(MiddlemanOfferToPeer), Answer(MiddlemanAnswerToPeer), + AnnounceResponse(AnnounceResponse), + ScrapeResponse(ScrapeResponse), } @@ -308,19 +308,7 @@ impl OutMessage { _ => return Err(anyhow::anyhow!("Message is neither text nor bytes")), }; - // This is brittle and could fail, but it doesn't matter too much - // since this function is only used in load tester - if text.contains("answer"){ - Ok(Self::Answer(::simd_json::serde::from_str(&mut text)?)) - } else if text.contains("offer"){ - Ok(Self::Offer(::simd_json::serde::from_str(&mut text)?)) - } else if text.contains("interval"){ - Ok(Self::AnnounceResponse(::simd_json::serde::from_str(&mut text)?)) - } else if text.contains("scrape"){ - Ok(Self::ScrapeResponse(::simd_json::serde::from_str(&mut text)?)) - } else { - Err(anyhow::anyhow!("Could not determine response type")) - } + Ok(::simd_json::serde::from_str(&mut text)?) } }