2026-05-13 23:08:21 -07:00
|
|
|
use bytes::{Buf, BufMut, Bytes, BytesMut};
|
2026-06-18 21:16:25 -07:00
|
|
|
use http_body::Body;
|
2026-05-11 22:31:04 -07:00
|
|
|
use roto_runtime::RotoMessage;
|
2026-06-18 21:16:25 -07:00
|
|
|
use std::marker::PhantomData;
|
2026-05-13 23:08:21 -07:00
|
|
|
use std::pin::Pin;
|
2026-06-18 21:16:25 -07:00
|
|
|
use std::sync::Mutex;
|
2026-05-13 23:08:21 -07:00
|
|
|
use std::task::{Context, Poll};
|
2026-06-18 21:16:25 -07:00
|
|
|
use tonic::codec::{Codec, DecodeBuf, Decoder, EncodeBuf, Encoder};
|
2026-05-11 22:31:04 -07:00
|
|
|
|
2026-05-16 16:57:01 -07:00
|
|
|
pub mod generated {
|
|
|
|
|
pub mod helloworld;
|
2026-05-17 00:43:21 -07:00
|
|
|
pub mod interop;
|
2026-05-16 16:57:01 -07:00
|
|
|
}
|
|
|
|
|
|
2026-05-11 22:31:04 -07:00
|
|
|
pub struct RotoCodec<T, U> {
|
|
|
|
|
_phantom: PhantomData<(T, U)>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T, U> Default for RotoCodec<T, U> {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
_phantom: PhantomData,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T, U> Codec for RotoCodec<T, U>
|
|
|
|
|
where
|
|
|
|
|
T: RotoMessage + Send + 'static,
|
|
|
|
|
U: RotoMessage + Send + 'static,
|
|
|
|
|
{
|
|
|
|
|
type Encode = U;
|
|
|
|
|
type Decode = T;
|
|
|
|
|
type Encoder = RotoEncoder<U>;
|
|
|
|
|
type Decoder = RotoDecoder<T>;
|
|
|
|
|
|
|
|
|
|
fn encoder(&mut self) -> Self::Encoder {
|
|
|
|
|
RotoEncoder(PhantomData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn decoder(&mut self) -> Self::Decoder {
|
|
|
|
|
RotoDecoder(PhantomData)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct RotoEncoder<U>(PhantomData<U>);
|
|
|
|
|
|
|
|
|
|
impl<U> Encoder for RotoEncoder<U>
|
|
|
|
|
where
|
|
|
|
|
U: RotoMessage,
|
|
|
|
|
{
|
|
|
|
|
type Item = U;
|
|
|
|
|
type Error = tonic::Status;
|
|
|
|
|
|
|
|
|
|
fn encode(&mut self, message: Self::Item, buf: &mut EncodeBuf<'_>) -> Result<(), Self::Error> {
|
|
|
|
|
buf.put_slice(&message.bytes());
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct RotoDecoder<T>(PhantomData<T>);
|
|
|
|
|
|
|
|
|
|
impl<T> Decoder for RotoDecoder<T>
|
|
|
|
|
where
|
|
|
|
|
T: RotoMessage,
|
|
|
|
|
{
|
|
|
|
|
type Item = T;
|
|
|
|
|
type Error = tonic::Status;
|
|
|
|
|
|
|
|
|
|
fn decode(&mut self, buf: &mut DecodeBuf<'_>) -> Result<Option<Self::Item>, Self::Error> {
|
|
|
|
|
if buf.remaining() == 0 {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let bytes = buf.copy_to_bytes(buf.remaining());
|
|
|
|
|
match T::decode(bytes) {
|
|
|
|
|
Ok(msg) => Ok(Some(msg)),
|
|
|
|
|
Err(e) => Err(tonic::Status::internal(format!("Roto decode error: {}", e))),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-13 23:08:21 -07:00
|
|
|
|
|
|
|
|
pub struct BufferPool {
|
|
|
|
|
pool: Mutex<Vec<BytesMut>>,
|
|
|
|
|
default_capacity: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BufferPool {
|
|
|
|
|
pub fn new(default_capacity: usize) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
pool: Mutex::new(Vec::new()),
|
|
|
|
|
default_capacity,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get(&self) -> BytesMut {
|
2026-06-18 21:16:25 -07:00
|
|
|
self.pool
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.pop()
|
|
|
|
|
.unwrap_or_else(|| BytesMut::with_capacity(self.default_capacity))
|
2026-05-13 23:08:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn put(&self, mut buf: BytesMut) {
|
|
|
|
|
buf.clear();
|
|
|
|
|
if buf.capacity() >= self.default_capacity {
|
|
|
|
|
self.pool.lock().unwrap().push(buf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-15 18:57:15 -07:00
|
|
|
pub struct StatusBody {
|
|
|
|
|
pub data: Option<Bytes>,
|
|
|
|
|
pub trailers: Option<http::HeaderMap>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl StatusBody {
|
|
|
|
|
pub fn new(data: Option<Bytes>, status: u8) -> Self {
|
|
|
|
|
let mut trailers = http::HeaderMap::new();
|
|
|
|
|
trailers.insert("grpc-status", status.to_string().parse().unwrap());
|
|
|
|
|
Self {
|
|
|
|
|
data,
|
|
|
|
|
trailers: Some(trailers),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-13 23:08:21 -07:00
|
|
|
|
|
|
|
|
impl Body for StatusBody {
|
|
|
|
|
type Data = Bytes;
|
|
|
|
|
type Error = tonic::Status;
|
|
|
|
|
|
|
|
|
|
fn poll_frame(
|
|
|
|
|
mut self: Pin<&mut Self>,
|
2026-06-18 21:16:25 -07:00
|
|
|
_cx: &mut Context<'_>,
|
2026-05-13 23:08:21 -07:00
|
|
|
) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
|
2026-05-15 18:57:15 -07:00
|
|
|
if let Some(data) = self.data.take() {
|
2026-05-13 23:08:21 -07:00
|
|
|
Poll::Ready(Some(Ok(http_body::Frame::data(data))))
|
2026-05-15 18:57:15 -07:00
|
|
|
} else if let Some(trailers) = self.trailers.take() {
|
|
|
|
|
Poll::Ready(Some(Ok(http_body::Frame::trailers(trailers))))
|
2026-05-13 23:08:21 -07:00
|
|
|
} else {
|
|
|
|
|
Poll::Ready(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|