blob: f4bdfb19780a867be69de507931f8135bb02c7a2 [file] [log] [blame]
Allen George8b96bfb2016-11-02 08:01:08 -04001// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::convert::Into;
19
Allen Georgeef7a1892018-12-16 18:01:37 -050020use super::{
21 TFieldIdentifier, TInputProtocol, TListIdentifier, TMapIdentifier, TMessageIdentifier,
22 TSetIdentifier, TStructIdentifier,
23};
Allen Georgeb0d14132020-03-29 11:48:55 -040024use crate::ProtocolErrorKind;
Allen George8b96bfb2016-11-02 08:01:08 -040025
26/// `TInputProtocol` required to use a `TMultiplexedProcessor`.
27///
28/// A `TMultiplexedProcessor` reads incoming message identifiers to determine to
29/// which `TProcessor` requests should be forwarded. However, once read, those
30/// message identifier bytes are no longer on the wire. Since downstream
31/// processors expect to read message identifiers from the given input protocol
32/// we need some way of supplying a `TMessageIdentifier` with the service-name
33/// stripped. This implementation stores the received `TMessageIdentifier`
34/// (without the service name) and passes it to the wrapped `TInputProtocol`
35/// when `TInputProtocol::read_message_begin(...)` is called. It delegates all
36/// other calls directly to the wrapped `TInputProtocol`.
37///
38/// This type **should not** be used by application code.
39///
40/// # Examples
41///
42/// Create and use a `TStoredInputProtocol`.
43///
44/// ```no_run
Allen George8b96bfb2016-11-02 08:01:08 -040045/// use thrift::protocol::{TInputProtocol, TMessageIdentifier, TMessageType, TOutputProtocol};
46/// use thrift::protocol::{TBinaryInputProtocol, TBinaryOutputProtocol, TStoredInputProtocol};
47/// use thrift::server::TProcessor;
Allen George0e22c362017-01-30 07:15:00 -050048/// use thrift::transport::{TIoChannel, TTcpChannel};
Allen George8b96bfb2016-11-02 08:01:08 -040049///
50/// // sample processor
51/// struct ActualProcessor;
52/// impl TProcessor for ActualProcessor {
53/// fn process(
Allen George0e22c362017-01-30 07:15:00 -050054/// &self,
Marcin Pajkowskic6308412019-12-02 11:39:28 +010055/// _: &mut dyn TInputProtocol,
56/// _: &mut dyn TOutputProtocol
Allen George8b96bfb2016-11-02 08:01:08 -040057/// ) -> thrift::Result<()> {
58/// unimplemented!()
59/// }
60/// }
Allen George0e22c362017-01-30 07:15:00 -050061/// let processor = ActualProcessor {};
Allen George8b96bfb2016-11-02 08:01:08 -040062///
63/// // construct the shared transport
Allen George0e22c362017-01-30 07:15:00 -050064/// let mut channel = TTcpChannel::new();
65/// channel.open("localhost:9090").unwrap();
66///
67/// let (i_chan, o_chan) = channel.split().unwrap();
Allen George8b96bfb2016-11-02 08:01:08 -040068///
69/// // construct the actual input and output protocols
Allen George0e22c362017-01-30 07:15:00 -050070/// let mut i_prot = TBinaryInputProtocol::new(i_chan, true);
71/// let mut o_prot = TBinaryOutputProtocol::new(o_chan, true);
Allen George8b96bfb2016-11-02 08:01:08 -040072///
73/// // message identifier received from remote and modified to remove the service name
74/// let new_msg_ident = TMessageIdentifier::new("service_call", TMessageType::Call, 1);
75///
76/// // construct the proxy input protocol
77/// let mut proxy_i_prot = TStoredInputProtocol::new(&mut i_prot, new_msg_ident);
78/// let res = processor.process(&mut proxy_i_prot, &mut o_prot);
79/// ```
Allen George0e22c362017-01-30 07:15:00 -050080// FIXME: implement Debug
Allen George8b96bfb2016-11-02 08:01:08 -040081pub struct TStoredInputProtocol<'a> {
Danny Browning77d96c12019-08-21 13:41:07 -060082 inner: &'a mut dyn TInputProtocol,
Allen George8b96bfb2016-11-02 08:01:08 -040083 message_ident: Option<TMessageIdentifier>,
84}
85
86impl<'a> TStoredInputProtocol<'a> {
87 /// Create a `TStoredInputProtocol` that delegates all calls other than
88 /// `TInputProtocol::read_message_begin(...)` to a `wrapped`
89 /// `TInputProtocol`. `message_ident` is the modified message identifier -
90 /// with service name stripped - that will be passed to
91 /// `wrapped.read_message_begin(...)`.
Allen George0e22c362017-01-30 07:15:00 -050092 pub fn new(
Danny Browning77d96c12019-08-21 13:41:07 -060093 wrapped: &mut dyn TInputProtocol,
Allen George0e22c362017-01-30 07:15:00 -050094 message_ident: TMessageIdentifier,
Allen George7ddbcc02020-11-08 09:51:19 -050095 ) -> TStoredInputProtocol<'_> {
Allen George8b96bfb2016-11-02 08:01:08 -040096 TStoredInputProtocol {
97 inner: wrapped,
98 message_ident: message_ident.into(),
99 }
100 }
101}
102
103impl<'a> TInputProtocol for TStoredInputProtocol<'a> {
Allen Georgeb0d14132020-03-29 11:48:55 -0400104 fn read_message_begin(&mut self) -> crate::Result<TMessageIdentifier> {
Allen Georgeef7a1892018-12-16 18:01:37 -0500105 self.message_ident.take().ok_or_else(|| {
Allen Georgeb0d14132020-03-29 11:48:55 -0400106 crate::errors::new_protocol_error(
Allen Georgeef7a1892018-12-16 18:01:37 -0500107 ProtocolErrorKind::Unknown,
108 "message identifier already read",
Allen George0e22c362017-01-30 07:15:00 -0500109 )
Allen Georgeef7a1892018-12-16 18:01:37 -0500110 })
Allen George8b96bfb2016-11-02 08:01:08 -0400111 }
112
Allen Georgeb0d14132020-03-29 11:48:55 -0400113 fn read_message_end(&mut self) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400114 self.inner.read_message_end()
115 }
116
Allen Georgeb0d14132020-03-29 11:48:55 -0400117 fn read_struct_begin(&mut self) -> crate::Result<Option<TStructIdentifier>> {
Allen George8b96bfb2016-11-02 08:01:08 -0400118 self.inner.read_struct_begin()
119 }
120
Allen Georgeb0d14132020-03-29 11:48:55 -0400121 fn read_struct_end(&mut self) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400122 self.inner.read_struct_end()
123 }
124
Allen Georgeb0d14132020-03-29 11:48:55 -0400125 fn read_field_begin(&mut self) -> crate::Result<TFieldIdentifier> {
Allen George8b96bfb2016-11-02 08:01:08 -0400126 self.inner.read_field_begin()
127 }
128
Allen Georgeb0d14132020-03-29 11:48:55 -0400129 fn read_field_end(&mut self) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400130 self.inner.read_field_end()
131 }
132
Allen Georgeb0d14132020-03-29 11:48:55 -0400133 fn read_bytes(&mut self) -> crate::Result<Vec<u8>> {
Allen George8b96bfb2016-11-02 08:01:08 -0400134 self.inner.read_bytes()
135 }
136
Allen Georgeb0d14132020-03-29 11:48:55 -0400137 fn read_bool(&mut self) -> crate::Result<bool> {
Allen George8b96bfb2016-11-02 08:01:08 -0400138 self.inner.read_bool()
139 }
140
Allen Georgeb0d14132020-03-29 11:48:55 -0400141 fn read_i8(&mut self) -> crate::Result<i8> {
Allen George8b96bfb2016-11-02 08:01:08 -0400142 self.inner.read_i8()
143 }
144
Allen Georgeb0d14132020-03-29 11:48:55 -0400145 fn read_i16(&mut self) -> crate::Result<i16> {
Allen George8b96bfb2016-11-02 08:01:08 -0400146 self.inner.read_i16()
147 }
148
Allen Georgeb0d14132020-03-29 11:48:55 -0400149 fn read_i32(&mut self) -> crate::Result<i32> {
Allen George8b96bfb2016-11-02 08:01:08 -0400150 self.inner.read_i32()
151 }
152
Allen Georgeb0d14132020-03-29 11:48:55 -0400153 fn read_i64(&mut self) -> crate::Result<i64> {
Allen George8b96bfb2016-11-02 08:01:08 -0400154 self.inner.read_i64()
155 }
156
Allen Georgeb0d14132020-03-29 11:48:55 -0400157 fn read_double(&mut self) -> crate::Result<f64> {
Allen George8b96bfb2016-11-02 08:01:08 -0400158 self.inner.read_double()
159 }
160
Jiayu Liub6b6dc72022-10-08 14:28:44 +0800161 fn read_uuid(&mut self) -> crate::Result<uuid::Uuid> {
162 self.inner.read_uuid()
163 }
164
Allen Georgeb0d14132020-03-29 11:48:55 -0400165 fn read_string(&mut self) -> crate::Result<String> {
Allen George8b96bfb2016-11-02 08:01:08 -0400166 self.inner.read_string()
167 }
168
Allen Georgeb0d14132020-03-29 11:48:55 -0400169 fn read_list_begin(&mut self) -> crate::Result<TListIdentifier> {
Allen George8b96bfb2016-11-02 08:01:08 -0400170 self.inner.read_list_begin()
171 }
172
Allen Georgeb0d14132020-03-29 11:48:55 -0400173 fn read_list_end(&mut self) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400174 self.inner.read_list_end()
175 }
176
Allen Georgeb0d14132020-03-29 11:48:55 -0400177 fn read_set_begin(&mut self) -> crate::Result<TSetIdentifier> {
Allen George8b96bfb2016-11-02 08:01:08 -0400178 self.inner.read_set_begin()
179 }
180
Allen Georgeb0d14132020-03-29 11:48:55 -0400181 fn read_set_end(&mut self) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400182 self.inner.read_set_end()
183 }
184
Allen Georgeb0d14132020-03-29 11:48:55 -0400185 fn read_map_begin(&mut self) -> crate::Result<TMapIdentifier> {
Allen George8b96bfb2016-11-02 08:01:08 -0400186 self.inner.read_map_begin()
187 }
188
Allen Georgeb0d14132020-03-29 11:48:55 -0400189 fn read_map_end(&mut self) -> crate::Result<()> {
Allen George8b96bfb2016-11-02 08:01:08 -0400190 self.inner.read_map_end()
191 }
192
193 // utility
194 //
195
Allen Georgeb0d14132020-03-29 11:48:55 -0400196 fn read_byte(&mut self) -> crate::Result<u8> {
Allen George8b96bfb2016-11-02 08:01:08 -0400197 self.inner.read_byte()
198 }
199}