bidi hacked!

This commit is contained in:
Gregory Schier
2024-02-02 01:10:54 -08:00
parent 4e781b752d
commit fdedb9bd28
7 changed files with 219 additions and 30 deletions

View File

@@ -3,12 +3,15 @@ use std::collections::HashMap;
use hyper::client::HttpConnector;
use hyper::Client;
use hyper_rustls::HttpsConnector;
use prost_reflect::{DescriptorPool, DynamicMessage};
use prost_reflect::DescriptorPool;
pub use prost_reflect::DynamicMessage;
use serde_json::Deserializer;
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;
use tokio_stream::{Stream, StreamExt};
use tonic::body::BoxBody;
use tonic::transport::Uri;
use tonic::{IntoRequest, Streaming};
use tonic::{IntoRequest, IntoStreamingRequest, Streaming};
use crate::codec::DynamicCodec;
use crate::proto::{fill_pool, method_desc_to_path};
@@ -52,6 +55,39 @@ impl GrpcConnection {
Ok(response_json)
}
pub async fn bidi_streaming(
&self,
service: &str,
method: &str,
stream: ReceiverStream<String>,
) -> Result<Streaming<DynamicMessage>> {
let service = self.pool.get_service_by_name(service).unwrap();
let method = &service.methods().find(|m| m.name() == method).unwrap();
let mut client = tonic::client::Grpc::with_origin(self.conn.clone(), self.uri.clone());
let method2 = method.clone();
let req = stream
.map(move |s| {
let mut deserializer = Deserializer::from_str(&s);
let req_message = DynamicMessage::deserialize(method2.input(), &mut deserializer)
.map_err(|e| e.to_string())
.unwrap();
deserializer.end().unwrap();
req_message
})
.into_streaming_request();
let path = method_desc_to_path(method);
let codec = DynamicCodec::new(method.clone());
client.ready().await.unwrap();
Ok(client
.streaming(req, path, codec)
.await
.map_err(|s| s.to_string())?
.into_inner())
}
pub async fn server_streaming(
&self,
service: &str,
@@ -120,6 +156,20 @@ impl GrpcManager {
.await
}
pub async fn bidi_streaming(
&mut self,
id: &str,
uri: Uri,
service: &str,
method: &str,
stream: ReceiverStream<String>,
) -> Result<Streaming<DynamicMessage>> {
println!("Bidi streaming {}", id);
self.connect(id, uri)
.await
.bidi_streaming(service, method, stream)
.await
}
pub async fn connect(&mut self, id: &str, uri: Uri) -> GrpcConnection {
let (pool, conn) = fill_pool(&uri).await;
let connection = GrpcConnection { pool, conn, uri };