pub struct RpcSocket { /* private fields */ }
Expand description
Bi-directional socket connection to a qrpc bus system
A connection is always between a component on the bus, and the broker. The broker listens to incoming connections, and relays them. A component (service, or utility library) can either operate only in sending mode, or listen as well, so that it can be used as a dependency by other services. The sending socket is used as a listener, meaning that no specific port needs to be bound for a service.
When using the server(...)
constructor you bind a port, when
attaching a lambda via listen(...)
you use the established
connection. In your service code there is no reason to ever use
server(...)
!
When sending a message, the socket will listen for a reply from
the broker on the sending stream, to make sure that return data is
properly associated. You can control the timeout via the
connect_timeout
function.
Implementations§
Source§impl RpcSocket
impl RpcSocket
Sourcepub async fn connect(addr: &str, port: u16) -> RpcResult<Arc<Self>>
pub async fn connect(addr: &str, port: u16) -> RpcResult<Arc<Self>>
Create a client socket that connects to a remote broker
Sourcepub async fn connect_timeout(
addr: &str,
port: u16,
timeout: Duration,
) -> RpcResult<Arc<Self>>
pub async fn connect_timeout( addr: &str, port: u16, timeout: Duration, ) -> RpcResult<Arc<Self>>
Create a client socket with an explicit timeout
Sourcepub async fn listen<F: Fn(Message) + Send + 'static>(self: &Arc<Self>, cb: F)
pub async fn listen<F: Fn(Message) + Send + 'static>(self: &Arc<Self>, cb: F)
Attach a permanent listener to the sending stream
Sourcepub async fn server<F, D>(
addr: &str,
port: u16,
cb: F,
data: D,
) -> RpcResult<Arc<Self>>
pub async fn server<F, D>( addr: &str, port: u16, cb: F, data: D, ) -> RpcResult<Arc<Self>>
Bind a socket to listen for connections
This function is primarily used by the qrpc-broker and should
not be used in your service code. To listen for incoming
connections on the outgoing stream (meaning client side), use
listen(...)
Sourcepub async fn reply(self: &Arc<Self>, msg: Message) -> RpcResult<()>
pub async fn reply(self: &Arc<Self>, msg: Message) -> RpcResult<()>
Send a message as a reply to a recipient
Sourcepub async fn send<T, F>(
self: &Arc<Self>,
msg: Message,
convert: F,
) -> RpcResult<T>
pub async fn send<T, F>( self: &Arc<Self>, msg: Message, convert: F, ) -> RpcResult<T>
Send a message to the other side of this stream
This function is meant to be used by qrpc clients that only
have a single connection stream to the broker. If you wanted
to write an alternative message broker, you have to use the
io
utilities directly (as the qrpc-broker
crate does)!
After sending a message this function will wait for a reply and parse the message for you. You must provide a conversion lambda so that the types can be extracted from the message type that the SDK receives.