#r-socket #tcp #transport #tcp-server #rsocket #tcp-client

rsocket_rust_transport_tcp

TCP RSocket transport implementation

10 releases

0.7.4 Jun 1, 2023
0.7.2 Sep 1, 2021
0.7.1 May 13, 2021
0.7.0 Jan 14, 2021
0.5.0 Feb 22, 2020

#8 in #r-socket

Download history 2/week @ 2024-06-16 9/week @ 2024-06-23 18/week @ 2024-06-30 13/week @ 2024-07-07 48/week @ 2024-07-14 31/week @ 2024-07-21 79/week @ 2024-07-28 42/week @ 2024-08-04 71/week @ 2024-08-11 42/week @ 2024-08-18 11/week @ 2024-08-25 3/week @ 2024-09-01 3/week @ 2024-09-08 46/week @ 2024-09-22 23/week @ 2024-09-29

72 downloads per month
Used in rsocket_rust_messaging

Apache-2.0

165KB
4.5K SLoC

RSocket Transport For TCP

Example

Add dependencies in your Cargo.toml.

[dependencies]
tokio = "0.3.6"
rsocket_rust = "0.7.0"
rsocket_rust_transport_tcp = "0.7.0"

Server

use log::info;
use rsocket_rust::prelude::{RSocketFactory, ServerResponder};
use rsocket_rust::Result;
use rsocket_rust::utils::EchoRSocket;
use rsocket_rust_transport_tcp::TcpServerTransport;

#[tokio::main]
async fn main() -> Result<()> {
    let transport: TcpServerTransport = TcpServerTransport::from("127.0.0.1:7878");

    let responder: ServerResponder = Box::new(|setup, _socket| {
        info!("accept setup: {:?}", setup);
        Ok(Box::new(EchoRSocket))
        // Or you can reject setup
        // Err(From::from("SETUP_NOT_ALLOW"))
    });

    let on_start: Box<dyn FnMut() + Send + Sync> =
        Box::new(|| info!("+++++++ echo server started! +++++++"));

    RSocketFactory::receive()
        .transport(transport)
        .acceptor(responder)
        .on_start(on_start)
        .serve()
        .await?;

    Ok(())
}

Client

use log::info;
use rsocket_rust::prelude::{ClientResponder, Payload, RSocket, RSocketFactory};
use rsocket_rust::Result;
use rsocket_rust_transport_tcp::TcpClientTransport;

#[tokio::main]
async fn main() -> Result<()> {
    let client = RSocketFactory::connect()
        .transport(TcpClientTransport::from("127.0.0.1:7878"))
        .setup(Payload::from("READY!"))
        .mime_type("text/plain", "text/plain")
        .start()
        .await?;

    let request_payload: Payload = Payload::builder()
        .set_data_utf8("Hello World!")
        .set_metadata_utf8("Rust")
        .build();

    let res = client.request_response(request_payload).await?;

    info!("got: {:?}", res);

    client.close();

    Ok(())
}

Dependencies

~5–17MB
~231K SLoC