Struct rust_tcp_ipc::TcpIpc
source · pub struct TcpIpc<P: Protocol> { /* private fields */ }
Expand description
This is the main type of the library. Here all the logic is bundle. It can be used to easily send and receive messages via TCP, allowing for many different protcols to be used.
Implementations
sourceimpl<P: Protocol> TcpIpc<P>
impl<P: Protocol> TcpIpc<P>
sourcepub fn client<T: ToSocketAddrs>(
socket_addresses: T,
config: TcpIpcConfig,
connect_wait_time: Option<Duration>
) -> Result<TcpIpc<P>, ConnectErrors>
pub fn client<T: ToSocketAddrs>(
socket_addresses: T,
config: TcpIpcConfig,
connect_wait_time: Option<Duration>
) -> Result<TcpIpc<P>, ConnectErrors>
This connects a client to a server, allowing to send and receive commands. The input variable ‘connect_wait_time’ is the time the client waits for the Server to accept a TCP-connection. A ‘None’ value yields an infinite waiting period.
Example
let config = TcpIpcConfig {
connect_wait_time_ms: 5_000,
read_iteration_wait_time_ns: 1_000,
shutdown_wait_time_in_ns: 1_000_000,
};
let mut client =
TcpIpc::<ProtocolExample>::client("127.0.0.1:6666", config, None).expect("connecting failed");
sourcepub fn server<T: ToSocketAddrs>(
socket_addresses: T,
config: TcpIpcConfig
) -> Result<TcpIpc<P>, ConnectErrors>
pub fn server<T: ToSocketAddrs>(
socket_addresses: T,
config: TcpIpcConfig
) -> Result<TcpIpc<P>, ConnectErrors>
This sets up a server waiting for a client to connect to it. Afterwards it can be used to send and receive commands.
Example
let config = TcpIpcConfig {
read_iteration_wait_time_ns: 1_000,
shutdown_wait_time_in_ns: 1_000_000,
};
let mut server =
TcpIpc::<ProtocolExample>::server("127.0.0.1:6666", config).expect("connecting failed");
sourcepub fn update_busy_state(
&mut self,
new_busy_state: P::BusyStates
) -> BusyStateUpdateResult
pub fn update_busy_state(
&mut self,
new_busy_state: P::BusyStates
) -> BusyStateUpdateResult
sourcepub fn get_busy_state(&mut self) -> Result<P::BusyStates, BusyStateQueryResult>
pub fn get_busy_state(&mut self) -> Result<P::BusyStates, BusyStateQueryResult>
sourcepub fn get_message(
&mut self
) -> Result<Option<(<P as Protocol>::Commands, Vec<u8>)>, ReadThreadErrors<P>>
pub fn get_message(
&mut self
) -> Result<Option<(<P as Protocol>::Commands, Vec<u8>)>, ReadThreadErrors<P>>
This function check if a message was received and returns it, if so. If no message is available (or if a message is only partial available and more data is neceesary), Ok(None) is return.
Example
let message = client.get_message();
sourcepub fn clear_message_queue(
&mut self,
sleep_time: Option<Duration>
) -> Result<(), ReadThreadErrors<P>>
pub fn clear_message_queue(
&mut self,
sleep_time: Option<Duration>
) -> Result<(), ReadThreadErrors<P>>
This function attemps to clear the message queue. To do this, it waits a given duration. Then it calls get_message until no message is received, or an error is received (which is returned in turn).
Example
let result = client.clear_message_queue(std::time::Duration::from_micros(10_000));
sourcepub fn await_message(
&mut self,
maximal_wait_time: Duration,
iteration_wait_time: Option<Duration>
) -> Result<Option<(<P as Protocol>::Commands, Vec<u8>)>, ReadThreadErrors<P>>
pub fn await_message(
&mut self,
maximal_wait_time: Duration,
iteration_wait_time: Option<Duration>
) -> Result<Option<(<P as Protocol>::Commands, Vec<u8>)>, ReadThreadErrors<P>>
This function awaits for a message. If no message is received during the wait time, Ok(None) is returned. If some message is received, Ok(Some((command, payload))) is returned. If an error happens, Err(x) is returned.
Example
let message = client.await_message(std::time::Duration::from_micros(10_000), std::time::Duration::from_nanos(2_000));
sourcepub fn write_message(
&mut self,
command: P::Commands,
message_: &[u8]
) -> Result<(), WriteMessageErrors>
pub fn write_message(
&mut self,
command: P::Commands,
message_: &[u8]
) -> Result<(), WriteMessageErrors>
This function writes/sends a message. The message is given as command (as enum-variant) & a payload/message. Then the message header is added and send via TCP, including the message. If an error occurs, Err(x) is returned. If the message is writen successfully, Ok(()) is returned.
Example
let message = client.write_message(ProtocolExampleCommands::Start, "ok".as_bytes());
sourcepub fn shutdown(self) -> Result<(), ShutdownError>
pub fn shutdown(self) -> Result<(), ShutdownError>
Attemps to close the TCP-connection Since the receiving side might not implement any shutdown functionality, this is optionally (and not included in Drop).
sourcepub fn set_nodelay(&mut self, no_delay: bool) -> Result<(), Error>
pub fn set_nodelay(&mut self, no_delay: bool) -> Result<(), Error>
Attemps to change the Tcp-Stream “NoDelay”-Option
sourcepub fn get_nodelay(&self) -> Result<bool, Error>
pub fn get_nodelay(&self) -> Result<bool, Error>
Attemps to get the Tcp-Stream “NoDelay”-Option