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

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

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

This updates the busy_state.

Example
client.update_busy_state(BusyStatesExample::Working);

This queries the current busy_state.

Example
let current_busy_state = client.get_busy_state();

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

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

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

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

Attemps to close the TCP-connection Since the receiving side might not implement any shutdown functionality, this is optionally (and not included in Drop).

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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.