From f434b5bf830c99049332db1aef77b2e6c2f262b9 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 12 Oct 2024 13:04:56 +0200 Subject: [PATCH] udp protocol as feature --- crates/servicepoint/Cargo.toml | 3 ++- crates/servicepoint/src/connection.rs | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/crates/servicepoint/Cargo.toml b/crates/servicepoint/Cargo.toml index b68b538..515bb55 100644 --- a/crates/servicepoint/Cargo.toml +++ b/crates/servicepoint/Cargo.toml @@ -22,13 +22,14 @@ rust-lzma = { version = "0.6.0", optional = true } rand = { version = "0.8", optional = true } [features] -default = ["compression_lzma"] +default = ["compression_lzma", "protocol_udp"] compression_zlib = ["dep:flate2"] compression_bzip2 = ["dep:bzip2"] compression_lzma = ["dep:rust-lzma"] compression_zstd = ["dep:zstd"] all_compressions = ["compression_zlib", "compression_bzip2", "compression_lzma", "compression_zstd"] rand = ["dep:rand"] +protocol_udp = [] [[example]] name = "random_brightness" diff --git a/crates/servicepoint/src/connection.rs b/crates/servicepoint/src/connection.rs index 224f370..76b3389 100644 --- a/crates/servicepoint/src/connection.rs +++ b/crates/servicepoint/src/connection.rs @@ -1,7 +1,11 @@ use std::fmt::Debug; -use std::net::{ToSocketAddrs, UdpSocket}; -use log::{debug, info}; +use log::debug; + +#[cfg(feature = "protocol_udp")] +use log::info; +#[cfg(feature = "protocol_udp")] +use std::net::{ToSocketAddrs, UdpSocket}; use crate::packet::Packet; @@ -18,6 +22,7 @@ use crate::packet::Packet; /// ``` pub enum Connection { /// A real connection using the UDP protocol + #[cfg(feature = "protocol_udp")] Udp(UdpSocket), /// A fake connection for testing that does not actually send anything. Fake, @@ -42,6 +47,7 @@ impl Connection { /// let connection = servicepoint::Connection::open("172.23.42.29:2342") /// .expect("connection failed"); /// ``` + #[cfg(feature = "protocol_udp")] pub fn open(addr: impl ToSocketAddrs + Debug) -> std::io::Result { info!("connecting to {addr:?}"); let socket = UdpSocket::bind("0.0.0.0:0")?; @@ -70,13 +76,17 @@ impl Connection { debug!("sending {packet:?}"); let data: Vec = packet.into(); match self { + #[cfg(feature = "protocol_udp")] Connection::Udp(socket) => { socket .send(&data) .map_err(SendError::IoError) .map(move |_| ()) // ignore Ok value } - Connection::Fake => Ok(()), + Connection::Fake => { + let _ = data; + Ok(()) + } } } }