forked from actix/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_rpc.rs
More file actions
42 lines (36 loc) · 1018 Bytes
/
echo_rpc.rs
File metadata and controls
42 lines (36 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use actix::prelude::*;
use futures::channel::mpsc::Sender;
use log::info;
use crate::EchoReceived;
use crate::grpc_api::{
EchoRequest,
EchoReply,
};
pub struct EchoRpc {
addr: actix::prelude::Recipient<EchoReceived>,
tx: Sender<EchoRequest>,
}
impl Actor for EchoRpc {
type Context = Context<Self>;
}
impl Handler<EchoRequest> for EchoRpc {
type Result = ();
fn handle(&mut self, msg: EchoRequest, ctx: &mut Context<Self>) {
if let Err(_) = self.tx.try_send(msg) {
info!("Sending echo request failed. Stopping.");
ctx.stop();
}
}
}
impl StreamHandler<Result<EchoReply, tonic::Status>> for EchoRpc {
fn handle(&mut self, msg: Result<EchoReply, tonic::Status>, _: &mut Context<Self>) {
match msg {
Ok(msg) => {
self.addr.send(EchoReceived { payload: msg.payload });
}
Err(status) => {
info!("Stream error: {}", status.message());
}
}
}
}