Struct 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§

Source§

impl<P: Protocol> TcpIpc<P>

Source

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");
Source

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");
Source

pub fn update_busy_state( &mut self, new_busy_state: P::BusyStates, ) -> BusyStateUpdateResult

This updates the busy_state.

§Example
client.update_busy_state(BusyStatesExample::Working);
Source

pub fn get_busy_state(&mut self) -> Result<P::BusyStates, BusyStateQueryResult>

This queries the current busy_state.

§Example
let current_busy_state = client.get_busy_state();
Source

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();
Source

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));
Source

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));
Source

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());
Source

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).

Source

pub fn set_nodelay(&mut self, no_delay: bool) -> Result<(), Error>

Attemps to change the Tcp-Stream “NoDelay”-Option

Source

pub fn get_nodelay(&self) -> Result<bool, Error>

Attemps to get the Tcp-Stream “NoDelay”-Option

Auto Trait Implementations§

§

impl<P> Freeze for TcpIpc<P>

§

impl<P> RefUnwindSafe for TcpIpc<P>

§

impl<P> Send for TcpIpc<P>

§

impl<P> !Sync for TcpIpc<P>

§

impl<P> Unpin for TcpIpc<P>

§

impl<P> UnwindSafe for TcpIpc<P>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.