This module is the low-level interface of `tcp_message_io`: it does
not perform any serialization or deserialization, but simply provides
request/response handling over `Vec<u8>` messages.
Apart from that it provides an interface which is almost identical to
the one in the main module, but using all types prefixed with `Raw`.
## Client
Client usage is straightforward:
```rust
use tcp_message_io::RawTCPClient;
use tokio;
#[tokio::main]
async fn main() {
let client = RawTCPClient::connect("127.0.0.1", 12345).await.unwrap();
let request = vec![1, 2, 3];
let response = client.send(request).await.unwrap();
}
```
## Server
Creating a server is very straightforward as well:
```rust
use tcp_message_io::{RawTCPServer, RawTCPResponse};
use tokio;
async fn echo_handler(request: Vec<u8>) -> Result<RawTCPResponse> {
// This is an echo server :-)
let response = request;
RawTCPResponse::Message(response)
}
#[tokio::main]
async fn main() {
RawTCPServer::new("127.0.0.1", 12345, echo_handler).listen().await;
}
```
[`RawTCPResponse`] can be one of the following:
- `RawTCPResponse::Message(response)` to send a response message.
- `RawTCPResponse::CloseConnection` to close the connection with the client.
This will also send an empty response to the client.
- `RawTCPResponse::StopServer` to shut the server down.
This will also send an empty response to the client and close the connection.
If the handler returns an error, the client recives an empty message,
a [`tracing`](https://docs.rs/tracing/) error message will be logged
and the server keeps listening for new messages from the same client.
This mechanism is meant for unhandled errors, and avoids leaving
the client hanging for a response.
It's left as a responsibility of the user to build an error reporting
mechanism on top of the transform if required. For example, this can
be achieved by ensuring the handler always returns `Ok(...)`, and
errors are send back in some form.