From 22b08b78dd0a292cb3fa0eee3da4ef9375ee98e0 Mon Sep 17 00:00:00 2001 From: Sofia Date: Tue, 28 Jul 2026 21:23:48 +0300 Subject: [PATCH] Allow setting identifier for peers --- src/connections.rs | 9 ++++++--- src/lib.rs | 11 +++++++++++ src/package.rs | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/connections.rs b/src/connections.rs index 1498206..a2062cf 100644 --- a/src/connections.rs +++ b/src/connections.rs @@ -103,7 +103,10 @@ impl self.config.ping_interval { match conn.state { ConnectionState::ReceivingConnection | ConnectionState::Connecting => { - match self.udp.send_to(*addr, Package::::Hello) { + match self + .udp + .send_to(*addr, Package::::Hello(self.config.identifier.clone())) + { Ok(bytes) => { conn.bytes_tx += bytes; conn.last_sent_ping = now; @@ -255,13 +258,13 @@ impl { + Package::Hello(identifier) => { if let Some(conn) = self.connections.get_mut(addr) { if conn.state == ConnectionState::Connecting { conn.state = ConnectionState::ConnectingNearlyReady; } } else { - if !self.accepting_connections { + if !self.accepting_connections || self.config.identifier != identifier { self.connections .insert(*addr, Connection::from(*addr, ConnectionState::Closing)); } else { diff --git a/src/lib.rs b/src/lib.rs index b0d7f32..6877026 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,6 +53,7 @@ pub struct PeerConfig { timeout: Duration, disconnect_timeout: Duration, message_retry: Duration, + identifier: String, } impl Default for PeerConfig { @@ -62,6 +63,7 @@ impl Default for PeerConfig { timeout: Duration::from_millis(2000), disconnect_timeout: Duration::from_millis(500), message_retry: Duration::from_millis(100), + identifier: "ExamplePeer".to_string(), } } } @@ -100,6 +102,15 @@ impl PeerConfig { ..self } } + + /// Sets the identifier string which must be same for both connecting peers + /// in order for connection to succeed. + pub fn with_identifier(self, ident: String) -> PeerConfig { + PeerConfig { + identifier: ident, + ..self + } + } } pub struct Peer { diff --git a/src/package.rs b/src/package.rs index 386a62d..9227ebf 100644 --- a/src/package.rs +++ b/src/package.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize)] pub enum Package { - Hello, + Hello(String), Ping, Pong, Close,