Skip to content
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

add example for rust-protobuf codec #1789

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ path = "src/codec_buffers/server.rs"
name = "codec-buffers-client"
path = "src/codec_buffers/client.rs"

[[bin]]
name = "protobuf-codec-server"
path = "src/protobuf-codec/server.rs"

[[bin]]
name = "protobuf-codec-client"
path = "src/protobuf-codec/client.rs"


[features]
gcp = ["dep:prost-types", "tonic/tls"]
Expand Down Expand Up @@ -324,6 +332,11 @@ hyper-rustls = { version = "0.27.0", features = ["http2", "ring", "tls12"], opti
rustls-pemfile = { version = "2.0.0", optional = true }
tower-http = { version = "0.5", optional = true }
pin-project = { version = "1.0.11", optional = true }
protobuf = "3.5.0"

[build-dependencies]
tonic-build = { path = "../tonic-build", features = ["prost"] }
protobuf = "3.5.0"
heck = "0.5.0"
protobuf-parse = "3.5.0"
protobuf-codegen = "3.5.0"
89 changes: 89 additions & 0 deletions examples/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,93 @@ fn build_json_codec_service() {
.build();

tonic_build::manual::Builder::new().compile(&[greeter_service]);

protobuf_codec::build();
}

mod protobuf_codec {
use heck::ToSnakeCase;
use protobuf::reflect::{FileDescriptor, MessageDescriptor};
use protobuf_parse::Parser;
use std::fs;
use std::path::PathBuf;

pub fn build() {
protobuf_codegen::Codegen::new()
.include("proto")
.inputs(&["proto/helloworld/helloworld.proto"])
.cargo_out_dir("protos")
.run_from_script();

let parser = Parser::new()
.include("proto")
.inputs(&["proto/helloworld/helloworld.proto"])
.parse_and_typecheck()
.unwrap();

let file_descriptors =
FileDescriptor::new_dynamic_fds(parser.file_descriptors, &[]).unwrap();

build_service(&file_descriptors)
}

fn build_service(file_descriptors: &[FileDescriptor]) {
let services = file_descriptors
.iter()
.flat_map(|file_descriptor| {
file_descriptor
.services()
.map(move |service_descriptor| (file_descriptor, service_descriptor))
})
.map(|(file_descriptor, service_descriptor)| {
let builder = tonic_build::manual::Service::builder()
.name(service_descriptor.proto().name())
.package(file_descriptor.package());
let mut builder_container = Some(builder);

for method in service_descriptor.methods() {
let method_descriptor = method.proto();

let output_type = method.output_type();
let input_type = method.input_type();

builder_container = builder_container.map(|builder| {
builder.method(
tonic_build::manual::Method::builder()
.name(method_descriptor.name().to_snake_case())
.route_name(method_descriptor.name())
.output_type(type_string(&output_type))
.input_type(type_string(&input_type))
.codec_path("crate::codec::ProtobufCodec")
.build(),
)
});
}

builder_container.unwrap().build()
})
.collect::<Vec<_>>();

tonic_build::manual::Builder::new()
.out_dir({
let mut base = PathBuf::from(std::env::var("OUT_DIR").unwrap());
base.push("protobuf_codec");
fs::create_dir_all(&base).unwrap();
base
})
.compile(&services);
}

fn type_string(message_descriptor: &MessageDescriptor) -> String {
let path = message_descriptor
.file_descriptor()
.name()
.split("/")
.last()
.unwrap()
.strip_suffix(".proto")
.unwrap();

format!("crate::protos::{}::{}", path, message_descriptor.name())
}
}
27 changes: 27 additions & 0 deletions examples/src/protobuf-codec/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
mod codec;
mod protos;

pub mod hello_world {
include!(concat!(
env!("OUT_DIR"),
"/protobuf_codec/helloworld.Greeter.rs"
));
}
use crate::protos::helloworld::HelloRequest;
use hello_world::greeter_client::GreeterClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut client = GreeterClient::connect("http://[::1]:50051").await?;

let request = tonic::Request::new(HelloRequest {
name: "Tonic".into(),
..Default::default()
});

let response = client.say_hello(request).await?;

println!("RESPONSE={:?}", response);

Ok(())
}
65 changes: 65 additions & 0 deletions examples/src/protobuf-codec/codec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use bytes::{Buf, BufMut};
use protobuf::Message;
use std::marker::PhantomData;
use tonic::codec::{Codec, DecodeBuf, Decoder, EncodeBuf, Encoder};
use tonic::Status;

pub struct ProtobufCodec<T, U>(PhantomData<(T, U)>);

impl<T, U> Default for ProtobufCodec<T, U> {
fn default() -> Self {
Self(PhantomData)
}
}

impl<T, U> Codec for ProtobufCodec<T, U>
where
T: Message,
U: Message,
{
type Encode = T;
type Decode = U;
type Encoder = ProtobufEncoder<T>;
type Decoder = ProtobufDecoder<U>;

fn encoder(&mut self) -> Self::Encoder {
ProtobufEncoder(PhantomData)
}

fn decoder(&mut self) -> Self::Decoder {
ProtobufDecoder(PhantomData)
}
}

pub struct ProtobufEncoder<T>(PhantomData<T>);

impl<T> Encoder for ProtobufEncoder<T>
where
T: Message,
{
type Item = T;
type Error = Status;

fn encode(&mut self, item: Self::Item, dst: &mut EncodeBuf<'_>) -> Result<(), Self::Error> {
Ok(item
.write_to_writer(&mut dst.writer())
.map_err(|_| Status::internal("failed to encode"))?)
}
}

pub struct ProtobufDecoder<U>(PhantomData<U>);

impl<U> Decoder for ProtobufDecoder<U>
where
U: Message,
{
type Item = U;
type Error = Status;

fn decode(&mut self, src: &mut DecodeBuf<'_>) -> Result<Option<Self::Item>, Self::Error> {
Ok(Some(
U::parse_from_reader(&mut src.reader())
.map_err(|_| Status::invalid_argument("bad request"))?,
))
}
}
1 change: 1 addition & 0 deletions examples/src/protobuf-codec/protos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include!(concat!(env!("OUT_DIR"), "/protos/mod.rs"));
47 changes: 47 additions & 0 deletions examples/src/protobuf-codec/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use tonic::{transport::Server, Request, Response, Status};

mod codec;
mod protos;

pub mod hello_world {
include!(concat!(
env!("OUT_DIR"),
"/protobuf_codec/helloworld.Greeter.rs"
));
}
use crate::protos::helloworld::{HelloReply, HelloRequest};
use hello_world::greeter_server::{Greeter, GreeterServer};

#[derive(Default)]
pub struct MyGreeter {}

#[tonic::async_trait]
impl Greeter for MyGreeter {
async fn say_hello(
&self,
request: Request<HelloRequest>,
) -> Result<Response<HelloReply>, Status> {
println!("Got a request from {:?}", request.remote_addr());

let reply = HelloReply {
message: format!("Hello {}!", request.into_inner().name),
..Default::default()
};
Ok(Response::new(reply))
}
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:50051".parse().unwrap();
let greeter = MyGreeter::default();

println!("GreeterServer listening on {}", addr);

Server::builder()
.add_service(GreeterServer::new(greeter))
.serve(addr)
.await?;

Ok(())
}