Skip to content

Commit

Permalink
feat: add bind_addr for NetworkOptions to enable TCPSocket.bind()
Browse files Browse the repository at this point in the history
  • Loading branch information
Cao Ruijuan committed Apr 8, 2024
1 parent af55848 commit 6e668bc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions rumqttc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `size()` method on `Packet` calculates size once serialized.
* `read()` and `write()` methods on `Packet`.
* `ConnectionAborted` variant on `StateError` type to denote abrupt end to a connection
* `bind_addr` for `NetworkOptions` to enable `TCPSocket.bind()`

### Changed

Expand Down
4 changes: 4 additions & 0 deletions rumqttc/src/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ pub(crate) async fn socket_connect(
socket.set_recv_buffer_size(recv_buffer_size).unwrap();
}

if let Some(bind_addr) = network_options.bind_addr {
socket.bind(bind_addr)?;
}

#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
{
if let Some(bind_device) = &network_options.bind_device {
Expand Down
13 changes: 12 additions & 1 deletion rumqttc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@
#[macro_use]
extern crate log;

use std::fmt::{self, Debug, Formatter};
use std::{
fmt::{self, Debug, Formatter},
net::SocketAddr,
};

#[cfg(any(feature = "use-rustls", feature = "websocket"))]
use std::sync::Arc;
Expand Down Expand Up @@ -370,6 +373,7 @@ pub struct NetworkOptions {
tcp_send_buffer_size: Option<u32>,
tcp_recv_buffer_size: Option<u32>,
conn_timeout: u64,
bind_addr: Option<SocketAddr>,
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
bind_device: Option<String>,
}
Expand All @@ -380,6 +384,7 @@ impl NetworkOptions {
tcp_send_buffer_size: None,
tcp_recv_buffer_size: None,
conn_timeout: 5,
bind_addr: None,
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
bind_device: None,
}
Expand All @@ -404,6 +409,12 @@ impl NetworkOptions {
self.conn_timeout
}

/// bind connection to a specific socket address
pub fn set_bind_addr(&mut self, bind_addr: SocketAddr) -> &mut Self {
self.bind_addr = Some(bind_addr);
self
}

/// bind connection to a specific network device by name
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
#[cfg_attr(
Expand Down

0 comments on commit 6e668bc

Please sign in to comment.