Skip to content

Commit

Permalink
feat(rumqttc): Add bind_device to NetworkOptions to enable TCPSocket.…
Browse files Browse the repository at this point in the history
…bind_device() (#654)

Signed-off-by: Seimon Williams <seimon@mobot.uk.com>
  • Loading branch information
seimonw authored Aug 20, 2023
1 parent 8c4fd9c commit 213cea4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions rumqttc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Added `bind_device` to `NetworkOptions` to enable `TCPSocket.bind_device()`

- Added `MqttOptions::set_request_modifier` for setting a handler for modifying a websocket request before sending it.

Expand Down
14 changes: 12 additions & 2 deletions rumqttc/src/eventloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl EventLoop {
if self.network.is_none() {
let (network, connack) = match time::timeout(
Duration::from_secs(self.network_options.connection_timeout()),
connect(&self.mqtt_options, self.network_options),
connect(&self.mqtt_options, self.network_options.clone()),
)
.await
{
Expand Down Expand Up @@ -242,7 +242,7 @@ impl EventLoop {
}

pub fn network_options(&self) -> NetworkOptions {
self.network_options
self.network_options.clone()
}

pub fn set_network_options(&mut self, network_options: NetworkOptions) -> &mut Self {
Expand Down Expand Up @@ -312,6 +312,16 @@ pub(crate) async fn socket_connect(
socket.set_recv_buffer_size(recv_buffer_size).unwrap();
}

#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
socket
.bind_device(
network_options
.bind_device
.as_ref()
.map(|bind_device| bind_device.as_bytes()),
)
.unwrap();

match socket.connect(addr).await {
Ok(s) => return Ok(s),
Err(e) => {
Expand Down
17 changes: 16 additions & 1 deletion rumqttc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,13 @@ impl From<ClientConfig> for TlsConfiguration {
}

/// Provides a way to configure low level network connection configurations
#[derive(Clone, Copy, Default)]
#[derive(Clone, Default)]
pub struct NetworkOptions {
tcp_send_buffer_size: Option<u32>,
tcp_recv_buffer_size: Option<u32>,
conn_timeout: u64,
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
bind_device: Option<String>,
}

impl NetworkOptions {
Expand All @@ -391,6 +393,8 @@ impl NetworkOptions {
tcp_send_buffer_size: None,
tcp_recv_buffer_size: None,
conn_timeout: 5,
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
bind_device: None,
}
}

Expand All @@ -412,6 +416,17 @@ impl NetworkOptions {
pub fn connection_timeout(&self) -> u64 {
self.conn_timeout
}

/// bind connection to a specific network device by name
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
#[cfg_attr(
docsrs,
doc(cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux")))
)]
pub fn set_bind_device(&mut self, bind_device: &str) -> &mut Self {
self.bind_device = Some(bind_device.to_string());
self
}
}

// TODO: Should all the options be exposed as public? Drawback
Expand Down
2 changes: 1 addition & 1 deletion rumqttc/src/v5/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ impl MqttOptions {
}

pub fn network_options(&self) -> NetworkOptions {
self.network_options
self.network_options.clone()
}

pub fn set_network_options(&mut self, network_options: NetworkOptions) -> &mut Self {
Expand Down

0 comments on commit 213cea4

Please sign in to comment.