openigtlink_rust/io/unified_client.rs
1//! Unified client types for OpenIGTLink
2//!
3//! This module provides simplified client enums that delegate to internal implementations.
4
5use crate::error::Result;
6use crate::io::sync_client::SyncTcpClient;
7use crate::io::unified_async_client::UnifiedAsyncClient;
8use crate::protocol::any_message::AnyMessage;
9use crate::protocol::message::{IgtlMessage, Message};
10
11/// Synchronous OpenIGTLink client
12///
13/// Simple wrapper around the synchronous TCP client.
14///
15/// **Recommended**: Use [`ClientBuilder`](crate::io::builder::ClientBuilder):
16/// ```no_run
17/// use openigtlink_rust::io::builder::ClientBuilder;
18///
19/// let client = ClientBuilder::new().tcp("127.0.0.1:18944").sync().build()?;
20/// # Ok::<(), openigtlink_rust::error::IgtlError>(())
21/// ```
22pub enum SyncIgtlClient {
23 /// Standard TCP synchronous client
24 TcpSync(SyncTcpClient),
25}
26
27impl SyncIgtlClient {
28 /// Send a message to the server
29 ///
30 /// # Arguments
31 /// * `msg` - Message to send
32 ///
33 /// # Returns
34 /// Ok(()) on success, error otherwise
35 #[inline(always)]
36 pub fn send<T: Message>(&mut self, msg: &IgtlMessage<T>) -> Result<()> {
37 match self {
38 SyncIgtlClient::TcpSync(client) => client.send(msg),
39 }
40 }
41
42 /// Receive a message from the server
43 ///
44 /// # Returns
45 /// Decoded message or error
46 #[inline(always)]
47 pub fn receive<T: Message>(&mut self) -> Result<IgtlMessage<T>> {
48 match self {
49 SyncIgtlClient::TcpSync(client) => client.receive(),
50 }
51 }
52
53 /// Enable or disable CRC verification for received messages
54 ///
55 /// # Arguments
56 /// * `verify` - true to enable CRC verification, false to disable
57 #[inline(always)]
58 pub fn set_verify_crc(&mut self, verify: bool) {
59 match self {
60 SyncIgtlClient::TcpSync(client) => client.set_verify_crc(verify),
61 }
62 }
63
64 /// Set read timeout for socket operations
65 ///
66 /// # Arguments
67 /// * `timeout` - Optional timeout duration. None for no timeout
68 ///
69 /// # Returns
70 /// Ok(()) on success, error if the socket operation fails
71 #[inline(always)]
72 pub fn set_read_timeout(&self, timeout: Option<std::time::Duration>) -> Result<()> {
73 match self {
74 SyncIgtlClient::TcpSync(client) => client.set_read_timeout(timeout),
75 }
76 }
77
78 /// Receive any message type dynamically without knowing the type in advance
79 ///
80 /// # Returns
81 /// Decoded message wrapped in AnyMessage enum, or error
82 #[inline(always)]
83 pub fn receive_any(&mut self) -> Result<AnyMessage> {
84 match self {
85 SyncIgtlClient::TcpSync(client) => client.receive_any(),
86 }
87 }
88}
89
90/// Asynchronous OpenIGTLink client
91///
92/// Unified client that supports optional TLS and reconnection features
93/// through internal state rather than separate variants.
94///
95/// **Recommended**: Use [`ClientBuilder`](crate::io::builder::ClientBuilder):
96/// ```no_run
97/// use openigtlink_rust::io::builder::ClientBuilder;
98///
99/// # async fn example() -> Result<(), openigtlink_rust::error::IgtlError> {
100/// let client = ClientBuilder::new().tcp("127.0.0.1:18944").async_mode().build().await?;
101/// # Ok(())
102/// # }
103/// ```
104pub enum AsyncIgtlClient {
105 /// Unified async client with optional TLS and reconnection
106 Unified(UnifiedAsyncClient),
107}
108
109impl AsyncIgtlClient {
110 /// Send a message to the server asynchronously
111 ///
112 /// # Arguments
113 /// * `msg` - Message to send
114 ///
115 /// # Returns
116 /// Ok(()) on success, error otherwise
117 #[inline(always)]
118 pub async fn send<T: Message>(&mut self, msg: &IgtlMessage<T>) -> Result<()> {
119 match self {
120 AsyncIgtlClient::Unified(client) => client.send(msg).await,
121 }
122 }
123
124 /// Receive a message from the server asynchronously
125 ///
126 /// # Returns
127 /// Decoded message or error
128 #[inline(always)]
129 pub async fn receive<T: Message>(&mut self) -> Result<IgtlMessage<T>> {
130 match self {
131 AsyncIgtlClient::Unified(client) => client.receive().await,
132 }
133 }
134
135 /// Enable or disable CRC verification for received messages
136 ///
137 /// # Arguments
138 /// * `verify` - true to enable CRC verification, false to disable
139 #[inline(always)]
140 pub fn set_verify_crc(&mut self, verify: bool) {
141 match self {
142 AsyncIgtlClient::Unified(client) => client.set_verify_crc(verify),
143 }
144 }
145
146 /// Get the number of reconnection attempts that have occurred
147 ///
148 /// # Returns
149 /// The total count of successful reconnections
150 #[inline(always)]
151 pub fn reconnect_count(&self) -> usize {
152 match self {
153 AsyncIgtlClient::Unified(client) => client.reconnect_count(),
154 }
155 }
156
157 /// Receive any message type dynamically without knowing the type in advance
158 ///
159 /// # Returns
160 /// Decoded message wrapped in AnyMessage enum, or error
161 #[inline(always)]
162 pub async fn receive_any(&mut self) -> Result<AnyMessage> {
163 match self {
164 AsyncIgtlClient::Unified(client) => client.receive_any().await,
165 }
166 }
167}