Skip to content

Commit

Permalink
RSDK-2123 - fix streaming over rust-utils (#30)
Browse files Browse the repository at this point in the history
Co-authored-by: Cheuk <[email protected]>
  • Loading branch information
stuqdog and cheukt authored Mar 3, 2023
1 parent 34723c2 commit b81c8e6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "viam-rust-utils"
version = "0.0.10"
version = "0.0.11"
edition = "2021"
license = "Apache-2.0"
description = "Utilities designed for use with Viamrobotics's SDKs"
Expand Down
1 change: 1 addition & 0 deletions examples/src/echo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use viam::rpc::dial;
/// Tests unary, server, and bidi streaming with simple echo requests. To run, simply
/// update the credentials and uri as necessary.
async fn main() -> Result<()> {
env_logger::init();
let creds = dial::RPCCredentials::new(
None,
"robot-location-secret".to_string(),
Expand Down
29 changes: 6 additions & 23 deletions src/rpc/dial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
},
rpc::webrtc::PollableAtomicBool,
};
use ::http::header::{HeaderName, TRAILER};
use ::http::header::HeaderName;
use ::http::{
uri::{Authority, Parts, PathAndQuery, Scheme},
HeaderValue, Version,
Expand Down Expand Up @@ -129,34 +129,17 @@ impl Service<http::Request<BoxBody>> for ViamChannel {
}
};

// TODO(RSDK-654) if and when we need to provide better support for
// bidi streaming, we'll need to find a better solution than this for
// handling empty messages
let mut body = hyper::body::to_bytes(body).await.unwrap().to_vec();
if body.is_empty() {
// the body is empty if we make a call that returns an error on the RDK
// side. Python's grpc library expects to see messages with, at a minimum,
// a grpc header, so returning an empty body leads to error. In such a
// case, we instead return a body that just consists of empty header bytes
// to indicate an empty message.
body = vec![0, 0, 0, 0, 0];
status_code = STATUS_CODE_UNKNOWN;
}
let grpc_message = channel.error.read().unwrap().clone();
let response = http::response::Response::builder()
// standardized gRPC headers.
.header("content-type", "application/grpc")
.header("te", "trailers")
.header(TRAILER, "Grpc-Status")
.header(TRAILER, "Grpc-Message")
.header(TRAILER, "Grpc-Status-Details-Bin")
.header("grpc-status", &status_code.to_string())
.version(Version::HTTP_2);

let response = match grpc_message {
None => response,
Some(message) => response.header("grpc-message", message),
let response = if status_code != STATUS_CODE_OK {
response.header("grpc-status", &status_code.to_string())
} else {
response
};

let response = response.body(Body::from(body)).unwrap();
Ok(response)
};
Expand Down

0 comments on commit b81c8e6

Please sign in to comment.