pub struct UdpServer { /* private fields */ }
Expand description
UDP server for receiving OpenIGTLink messages
Listens for incoming datagrams on a specific port.
§Examples
use openigtlink_rust::io::UdpServer;
use openigtlink_rust::protocol::types::TransformMessage;
use openigtlink_rust::protocol::message::IgtlMessage;
let server = UdpServer::bind("0.0.0.0:18944")?;
loop {
let (msg, sender) = server.receive::<TransformMessage>()?;
println!("Received from {}", sender);
// Echo back
let response = IgtlMessage::new(msg.content, "Server")?;
server.send_to(&response, sender)?;
}
Implementations§
Source§impl UdpServer
impl UdpServer
Sourcepub fn bind(addr: &str) -> Result<Self>
pub fn bind(addr: &str) -> Result<Self>
Bind server to an address
§Arguments
addr
- Address to bind (e.g., “0.0.0.0:18944”)
§Errors
IgtlError::Io
- Failed to bind (port in use, permission denied, etc.)
§Examples
use openigtlink_rust::io::UdpServer;
let server = UdpServer::bind("0.0.0.0:18944")?;
Sourcepub fn receive<T: Message>(&self) -> Result<(IgtlMessage<T>, SocketAddr)>
pub fn receive<T: Message>(&self) -> Result<(IgtlMessage<T>, SocketAddr)>
Receive a message (blocking)
§Returns
Tuple of (message, sender_address)
§Errors
IgtlError::Io
- Network read failed or timeoutIgtlError::InvalidHeader
- Malformed headerIgtlError::CrcMismatch
- Data corruption
Sourcepub fn send_to<T: Message>(
&self,
msg: &IgtlMessage<T>,
target: SocketAddr,
) -> Result<()>
pub fn send_to<T: Message>( &self, msg: &IgtlMessage<T>, target: SocketAddr, ) -> Result<()>
Send a response to a specific address
§Arguments
msg
- Message to sendtarget
- Target socket address
§Errors
IgtlError::Io
- Network transmission failedIgtlError::BodyTooLarge
- Message exceeds UDP MTU
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
Sourcepub fn local_addr(&self) -> Result<SocketAddr>
pub fn local_addr(&self) -> Result<SocketAddr>
Auto Trait Implementations§
impl Freeze for UdpServer
impl RefUnwindSafe for UdpServer
impl Send for UdpServer
impl Sync for UdpServer
impl Unpin for UdpServer
impl UnwindSafe for UdpServer
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