pub struct UdpClient { /* private fields */ }
Expand description
UDP client for sending/receiving OpenIGTLink messages
Provides connectionless communication with low overhead. Suitable for high-frequency updates where occasional packet loss is acceptable.
§Performance Characteristics
- Latency: Lower than TCP (no connection setup, no retransmission)
- Throughput: Limited by network MTU (~1500 bytes typical Ethernet)
- Reliability: None (packets may be lost, duplicated, or reordered)
§Examples
use openigtlink_rust::io::UdpClient;
use openigtlink_rust::protocol::types::TransformMessage;
use openigtlink_rust::protocol::message::IgtlMessage;
let client = UdpClient::bind("0.0.0.0:0")?;
let transform = TransformMessage::identity();
let msg = IgtlMessage::new(transform, "Tool")?;
client.send_to(&msg, "192.168.1.100:18944")?;
Implementations§
Source§impl UdpClient
impl UdpClient
Sourcepub fn bind(local_addr: &str) -> Result<Self>
pub fn bind(local_addr: &str) -> Result<Self>
Bind to a local address
§Arguments
local_addr
- Local address to bind (use “0.0.0.0:0” for any available port)
§Errors
IgtlError::Io
- Failed to bind socket (address in use, permission denied, etc.)
§Examples
use openigtlink_rust::io::UdpClient;
// Bind to any available port
let client = UdpClient::bind("0.0.0.0:0")?;
// Bind to specific port
let client = UdpClient::bind("0.0.0.0:18945")?;
Sourcepub fn send_to<T: Message>(
&self,
msg: &IgtlMessage<T>,
target: &str,
) -> Result<()>
pub fn send_to<T: Message>( &self, msg: &IgtlMessage<T>, target: &str, ) -> Result<()>
Send a message to a remote address
§Arguments
msg
- Message to sendtarget
- Target address (e.g., “127.0.0.1:18944”)
§Errors
IgtlError::Io
- Network transmission failedIgtlError::BodyTooLarge
- Message exceeds UDP MTU (65507 bytes)
§Examples
use openigtlink_rust::io::UdpClient;
use openigtlink_rust::protocol::types::TransformMessage;
use openigtlink_rust::protocol::message::IgtlMessage;
let client = UdpClient::bind("0.0.0.0:0")?;
let transform = TransformMessage::identity();
let msg = IgtlMessage::new(transform, "Device")?;
client.send_to(&msg, "127.0.0.1:18944")?;
Sourcepub fn receive_from<T: Message>(&self) -> Result<(IgtlMessage<T>, SocketAddr)>
pub fn receive_from<T: Message>(&self) -> Result<(IgtlMessage<T>, SocketAddr)>
Receive a message (blocking)
Blocks until a datagram is received. Returns the message and sender address.
§Returns
Tuple of (message, sender_address)
§Errors
IgtlError::Io
- Network read failed or timeoutIgtlError::InvalidHeader
- Malformed headerIgtlError::CrcMismatch
- Data corruption detected
§Examples
use openigtlink_rust::io::UdpClient;
use openigtlink_rust::protocol::types::TransformMessage;
let client = UdpClient::bind("0.0.0.0:18944")?;
let (msg, sender) = client.receive_from::<TransformMessage>()?;
println!("Received from {}", sender);
Sourcepub fn set_read_timeout(&self, timeout: Option<Duration>) -> Result<()>
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> Result<()>
Set read timeout
§Arguments
timeout
- Timeout duration (None for blocking forever)
§Errors
IgtlError::Io
- Failed to set socket option
§Examples
use openigtlink_rust::io::UdpClient;
use std::time::Duration;
let client = UdpClient::bind("0.0.0.0:0")?;
client.set_read_timeout(Some(Duration::from_secs(5)))?;
Sourcepub fn set_write_timeout(&self, timeout: Option<Duration>) -> Result<()>
pub fn set_write_timeout(&self, timeout: Option<Duration>) -> Result<()>
Set write timeout
§Arguments
timeout
- Timeout duration (None for blocking forever)
§Errors
IgtlError::Io
- Failed to set socket option
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Auto Trait Implementations§
impl Freeze for UdpClient
impl RefUnwindSafe for UdpClient
impl Send for UdpClient
impl Sync for UdpClient
impl Unpin for UdpClient
impl UnwindSafe for UdpClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more