udp protocol as feature
This commit is contained in:
		
							parent
							
								
									4dfb405792
								
							
						
					
					
						commit
						f434b5bf83
					
				
					 2 changed files with 15 additions and 4 deletions
				
			
		|  | @ -22,13 +22,14 @@ rust-lzma = { version = "0.6.0", optional = true } | ||||||
| rand = { version = "0.8", optional = true } | rand = { version = "0.8", optional = true } | ||||||
| 
 | 
 | ||||||
| [features] | [features] | ||||||
| default = ["compression_lzma"] | default = ["compression_lzma", "protocol_udp"] | ||||||
| compression_zlib = ["dep:flate2"] | compression_zlib = ["dep:flate2"] | ||||||
| compression_bzip2 = ["dep:bzip2"] | compression_bzip2 = ["dep:bzip2"] | ||||||
| compression_lzma = ["dep:rust-lzma"] | compression_lzma = ["dep:rust-lzma"] | ||||||
| compression_zstd = ["dep:zstd"] | compression_zstd = ["dep:zstd"] | ||||||
| all_compressions = ["compression_zlib", "compression_bzip2", "compression_lzma", "compression_zstd"] | all_compressions = ["compression_zlib", "compression_bzip2", "compression_lzma", "compression_zstd"] | ||||||
| rand = ["dep:rand"] | rand = ["dep:rand"] | ||||||
|  | protocol_udp = [] | ||||||
| 
 | 
 | ||||||
| [[example]] | [[example]] | ||||||
| name = "random_brightness" | name = "random_brightness" | ||||||
|  |  | ||||||
|  | @ -1,7 +1,11 @@ | ||||||
| use std::fmt::Debug; | 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; | use crate::packet::Packet; | ||||||
| 
 | 
 | ||||||
|  | @ -18,6 +22,7 @@ use crate::packet::Packet; | ||||||
| /// ```
 | /// ```
 | ||||||
| pub enum Connection { | pub enum Connection { | ||||||
|     /// A real connection using the UDP protocol
 |     /// A real connection using the UDP protocol
 | ||||||
|  |     #[cfg(feature = "protocol_udp")] | ||||||
|     Udp(UdpSocket), |     Udp(UdpSocket), | ||||||
|     /// A fake connection for testing that does not actually send anything.
 |     /// A fake connection for testing that does not actually send anything.
 | ||||||
|     Fake, |     Fake, | ||||||
|  | @ -42,6 +47,7 @@ impl Connection { | ||||||
|     ///  let connection = servicepoint::Connection::open("172.23.42.29:2342")
 |     ///  let connection = servicepoint::Connection::open("172.23.42.29:2342")
 | ||||||
|     ///     .expect("connection failed");
 |     ///     .expect("connection failed");
 | ||||||
|     /// ```
 |     /// ```
 | ||||||
|  |     #[cfg(feature = "protocol_udp")] | ||||||
|     pub fn open(addr: impl ToSocketAddrs + Debug) -> std::io::Result<Self> { |     pub fn open(addr: impl ToSocketAddrs + Debug) -> std::io::Result<Self> { | ||||||
|         info!("connecting to {addr:?}"); |         info!("connecting to {addr:?}"); | ||||||
|         let socket = UdpSocket::bind("0.0.0.0:0")?; |         let socket = UdpSocket::bind("0.0.0.0:0")?; | ||||||
|  | @ -70,13 +76,17 @@ impl Connection { | ||||||
|         debug!("sending {packet:?}"); |         debug!("sending {packet:?}"); | ||||||
|         let data: Vec<u8> = packet.into(); |         let data: Vec<u8> = packet.into(); | ||||||
|         match self { |         match self { | ||||||
|  |             #[cfg(feature = "protocol_udp")] | ||||||
|             Connection::Udp(socket) => { |             Connection::Udp(socket) => { | ||||||
|                 socket |                 socket | ||||||
|                     .send(&data) |                     .send(&data) | ||||||
|                     .map_err(SendError::IoError) |                     .map_err(SendError::IoError) | ||||||
|                     .map(move |_| ()) // ignore Ok value
 |                     .map(move |_| ()) // ignore Ok value
 | ||||||
|             } |             } | ||||||
|             Connection::Fake => Ok(()), |             Connection::Fake => { | ||||||
|  |                 let _ = data; | ||||||
|  |                 Ok(()) | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vinzenz Schroeter
						Vinzenz Schroeter