Skip to content

Add support for gRPC calls. #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix
Signed-off-by: Shikugawa <Shikugawa@gmail.com>
  • Loading branch information
Shikugawa committed Apr 20, 2021
commit b6a5e7a194875ffb988a95ae2e49a848bb1c40cb
3 changes: 3 additions & 0 deletions src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use crate::hostcalls;
use crate::traits::*;
use crate::types::*;
use core::panic;
use hashbrown::HashMap;
use std::cell::{Cell, RefCell};

Expand Down Expand Up @@ -416,6 +417,7 @@ impl Dispatcher {
}
} else {
// TODO(shikugawa): Try to check grpc stream tokens here
panic!("invalid token_id")
}
}

Expand All @@ -436,6 +438,7 @@ impl Dispatcher {
}
} else {
// TODO(shikugawa): Try to check grpc stream tokens here
panic!("invalid token_id")
}
}
}
Expand Down
28 changes: 24 additions & 4 deletions src/hostcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ pub fn dispatch_http_call(
extern "C" {
fn proxy_grpc_call(
upstream_data: *const u8,
upstream_size: usize, // TODO(shikugawa): remove grpc_service after next ABI released.
upstream_size: usize,
service_name_data: *const u8,
service_name_size: usize,
method_name_data: *const u8,
Expand All @@ -663,7 +663,7 @@ extern "C" {
initial_metadata_size: usize,
message_data_data: *const u8,
message_data_size: usize,
timeout_milliseconds: u32,
timeout: u32,
return_callout_id: *mut u32,
) -> Status;
}
Expand All @@ -672,12 +672,12 @@ pub fn dispatch_grpc_call(
upstream_name: &str,
service_name: &str,
method_name: &str,
initial_metadata: Vec<(&str, &str)>,
initial_metadata: Vec<(&str, &[u8])>,
message: Option<&[u8]>,
timeout: Duration,
) -> Result<u32, Status> {
let mut return_callout_id = 0;
let serialized_initial_metadata = utils::serialize_map(initial_metadata);
let serialized_initial_metadata = utils::serialize_bytes_value_map(initial_metadata);
unsafe {
match proxy_grpc_call(
upstream_name.as_ptr(),
Expand Down Expand Up @@ -836,6 +836,26 @@ mod utils {
bytes
}

pub(super) fn serialize_bytes_value_map(map: Vec<(&str, &[u8])>) -> Bytes {
let mut size: usize = 4;
for (name, value) in &map {
size += name.len() + value.len() + 10;
}
let mut bytes: Bytes = Vec::with_capacity(size);
bytes.extend_from_slice(&map.len().to_le_bytes());
for (name, value) in &map {
bytes.extend_from_slice(&name.len().to_le_bytes());
bytes.extend_from_slice(&value.len().to_le_bytes());
}
for (name, value) in &map {
bytes.extend_from_slice(&name.as_bytes());
bytes.push(0);
bytes.extend_from_slice(&value);
bytes.push(0);
}
bytes
}

pub(super) fn deserialize_map(bytes: &[u8]) -> Vec<(String, String)> {
let mut map = Vec::new();
if bytes.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub trait Context {
upstream_name: &str,
service_name: &str,
method_name: &str,
initial_metadata: Vec<(&str, &str)>,
initial_metadata: Vec<(&str, &[u8])>,
message: Option<&[u8]>,
timeout: Duration,
) -> Result<u32, Status> {
Expand Down