Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-17 08:35:01

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements. See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership. The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License. You may obtain a copy of the License at
0009  *
0010  *   http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing,
0013  * software distributed under the License is distributed on an
0014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015  * KIND, either express or implied. See the License for the
0016  * specific language governing permissions and limitations
0017  * under the License.
0018  */
0019 #ifndef _THRIFT_ASYNC_TASYNCDISPATCHPROCESSOR_H_
0020 #define _THRIFT_ASYNC_TASYNCDISPATCHPROCESSOR_H_ 1
0021 
0022 #include <thrift/async/TAsyncProcessor.h>
0023 
0024 namespace apache {
0025 namespace thrift {
0026 namespace async {
0027 
0028 /**
0029  * TAsyncDispatchProcessor is a helper class to parse the message header then
0030  * call another function to dispatch based on the function name.
0031  *
0032  * Subclasses must implement dispatchCall() to dispatch on the function name.
0033  */
0034 template <class Protocol_>
0035 class TAsyncDispatchProcessorT : public TAsyncProcessor {
0036 public:
0037   void process(std::function<void(bool success)> _return,
0038                        std::shared_ptr<protocol::TProtocol> in,
0039                        std::shared_ptr<protocol::TProtocol> out) override {
0040     protocol::TProtocol* inRaw = in.get();
0041     protocol::TProtocol* outRaw = out.get();
0042 
0043     // Try to dynamic cast to the template protocol type
0044     auto* specificIn = dynamic_cast<Protocol_*>(inRaw);
0045     auto* specificOut = dynamic_cast<Protocol_*>(outRaw);
0046     if (specificIn && specificOut) {
0047       return processFast(_return, specificIn, specificOut);
0048     }
0049 
0050     // Log the fact that we have to use the slow path
0051     T_GENERIC_PROTOCOL(this, inRaw, specificIn);
0052     T_GENERIC_PROTOCOL(this, outRaw, specificOut);
0053 
0054     std::string fname;
0055     protocol::TMessageType mtype;
0056     int32_t seqid;
0057     inRaw->readMessageBegin(fname, mtype, seqid);
0058 
0059     // If this doesn't look like a valid call, log an error and return false so
0060     // that the server will close the connection.
0061     //
0062     // (The old generated processor code used to try to skip a T_STRUCT and
0063     // continue.  However, that seems unsafe.)
0064     if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
0065       GlobalOutput.printf("received invalid message type %d from client", mtype);
0066       _return(false);
0067       return;
0068     }
0069 
0070     return this->dispatchCall(_return, inRaw, outRaw, fname, seqid);
0071   }
0072 
0073   void processFast(std::function<void(bool success)> _return,
0074                    Protocol_* in,
0075                    Protocol_* out) {
0076     std::string fname;
0077     protocol::TMessageType mtype;
0078     int32_t seqid;
0079     in->readMessageBegin(fname, mtype, seqid);
0080 
0081     if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
0082       GlobalOutput.printf("received invalid message type %d from client", mtype);
0083       _return(false);
0084       return;
0085     }
0086 
0087     return this->dispatchCallTemplated(_return, in, out, fname, seqid);
0088   }
0089 
0090   virtual void dispatchCall(std::function<void(bool ok)> _return,
0091                             apache::thrift::protocol::TProtocol* in,
0092                             apache::thrift::protocol::TProtocol* out,
0093                             const std::string& fname,
0094                             int32_t seqid) = 0;
0095 
0096   virtual void dispatchCallTemplated(std::function<void(bool ok)> _return,
0097                                      Protocol_* in,
0098                                      Protocol_* out,
0099                                      const std::string& fname,
0100                                      int32_t seqid) = 0;
0101 };
0102 
0103 /**
0104  * Non-templatized version of TAsyncDispatchProcessor,
0105  * that doesn't bother trying to perform a dynamic_cast.
0106  */
0107 class TAsyncDispatchProcessor : public TAsyncProcessor {
0108 public:
0109   void process(std::function<void(bool success)> _return,
0110                        std::shared_ptr<protocol::TProtocol> in,
0111                        std::shared_ptr<protocol::TProtocol> out) override {
0112     protocol::TProtocol* inRaw = in.get();
0113     protocol::TProtocol* outRaw = out.get();
0114 
0115     std::string fname;
0116     protocol::TMessageType mtype;
0117     int32_t seqid;
0118     inRaw->readMessageBegin(fname, mtype, seqid);
0119 
0120     // If this doesn't look like a valid call, log an error and return false so
0121     // that the server will close the connection.
0122     //
0123     // (The old generated processor code used to try to skip a T_STRUCT and
0124     // continue.  However, that seems unsafe.)
0125     if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
0126       GlobalOutput.printf("received invalid message type %d from client", mtype);
0127       _return(false);
0128       return;
0129     }
0130 
0131     return dispatchCall(_return, inRaw, outRaw, fname, seqid);
0132   }
0133 
0134   virtual void dispatchCall(std::function<void(bool ok)> _return,
0135                             apache::thrift::protocol::TProtocol* in,
0136                             apache::thrift::protocol::TProtocol* out,
0137                             const std::string& fname,
0138                             int32_t seqid) = 0;
0139 };
0140 
0141 // Specialize TAsyncDispatchProcessorT for TProtocol and TDummyProtocol just to
0142 // use the generic TDispatchProcessor.
0143 template <>
0144 class TAsyncDispatchProcessorT<protocol::TDummyProtocol> : public TAsyncDispatchProcessor {};
0145 template <>
0146 class TAsyncDispatchProcessorT<protocol::TProtocol> : public TAsyncDispatchProcessor {};
0147 }
0148 }
0149 } // apache::thrift::async
0150 
0151 #endif // _THRIFT_ASYNC_TASYNCDISPATCHPROCESSOR_H_