Back to home page

EIC code displayed by LXR

 
 

    


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

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 
0020 #ifndef _THRIFT_TPROCESSOR_H_
0021 #define _THRIFT_TPROCESSOR_H_ 1
0022 
0023 #include <string>
0024 #include <thrift/protocol/TProtocol.h>
0025 
0026 namespace apache {
0027 namespace thrift {
0028 
0029 /**
0030  * Virtual interface class that can handle events from the processor. To
0031  * use this you should subclass it and implement the methods that you care
0032  * about. Your subclass can also store local data that you may care about,
0033  * such as additional "arguments" to these methods (stored in the object
0034  * instance's state).
0035  */
0036 class TProcessorEventHandler {
0037 public:
0038   virtual ~TProcessorEventHandler() = default;
0039 
0040   /**
0041    * Called before calling other callback methods.
0042    * Expected to return some sort of context object.
0043    * The return value is passed to all other callbacks
0044    * for that function invocation.
0045    */
0046   virtual void* getContext(const char* fn_name, void* serverContext) {
0047     (void)fn_name;
0048     (void)serverContext;
0049     return nullptr;
0050   }
0051 
0052   /**
0053    * Expected to free resources associated with a context.
0054    */
0055   virtual void freeContext(void* ctx, const char* fn_name) {
0056     (void)ctx;
0057     (void)fn_name;
0058   }
0059 
0060   /**
0061    * Called before reading arguments.
0062    */
0063   virtual void preRead(void* ctx, const char* fn_name) {
0064     (void)ctx;
0065     (void)fn_name;
0066   }
0067 
0068   /**
0069    * Called between reading arguments and calling the handler.
0070    */
0071   virtual void postRead(void* ctx, const char* fn_name, uint32_t bytes) {
0072     (void)ctx;
0073     (void)fn_name;
0074     (void)bytes;
0075   }
0076 
0077   /**
0078    * Called between calling the handler and writing the response.
0079    */
0080   virtual void preWrite(void* ctx, const char* fn_name) {
0081     (void)ctx;
0082     (void)fn_name;
0083   }
0084 
0085   /**
0086    * Called after writing the response.
0087    */
0088   virtual void postWrite(void* ctx, const char* fn_name, uint32_t bytes) {
0089     (void)ctx;
0090     (void)fn_name;
0091     (void)bytes;
0092   }
0093 
0094   /**
0095    * Called when an async function call completes successfully.
0096    */
0097   virtual void asyncComplete(void* ctx, const char* fn_name) {
0098     (void)ctx;
0099     (void)fn_name;
0100   }
0101 
0102   /**
0103    * Called if the handler throws an undeclared exception.
0104    */
0105   virtual void handlerError(void* ctx, const char* fn_name) {
0106     (void)ctx;
0107     (void)fn_name;
0108   }
0109 
0110 protected:
0111   TProcessorEventHandler() = default;
0112 };
0113 
0114 /**
0115  * A helper class used by the generated code to free each context.
0116  */
0117 class TProcessorContextFreer {
0118 public:
0119   TProcessorContextFreer(TProcessorEventHandler* handler, void* context, const char* method)
0120     : handler_(handler), context_(context), method_(method) {}
0121   ~TProcessorContextFreer() {
0122     if (handler_ != nullptr)
0123       handler_->freeContext(context_, method_);
0124   }
0125   void unregister() { handler_ = nullptr; }
0126 
0127 private:
0128   apache::thrift::TProcessorEventHandler* handler_;
0129   void* context_;
0130   const char* method_;
0131 };
0132 
0133 /**
0134  * A processor is a generic object that acts upon two streams of data, one
0135  * an input and the other an output. The definition of this object is loose,
0136  * though the typical case is for some sort of server that either generates
0137  * responses to an input stream or forwards data from one pipe onto another.
0138  *
0139  */
0140 class TProcessor {
0141 public:
0142   virtual ~TProcessor() = default;
0143 
0144   virtual bool process(std::shared_ptr<protocol::TProtocol> in,
0145                        std::shared_ptr<protocol::TProtocol> out,
0146                        void* connectionContext) = 0;
0147 
0148   bool process(std::shared_ptr<apache::thrift::protocol::TProtocol> io, void* connectionContext) {
0149     return process(io, io, connectionContext);
0150   }
0151 
0152   std::shared_ptr<TProcessorEventHandler> getEventHandler() const { return eventHandler_; }
0153 
0154   void setEventHandler(std::shared_ptr<TProcessorEventHandler> eventHandler) {
0155     eventHandler_ = eventHandler;
0156   }
0157 
0158 protected:
0159   TProcessor() = default;
0160 
0161   std::shared_ptr<TProcessorEventHandler> eventHandler_;
0162 };
0163 
0164 /**
0165  * This is a helper class to allow std::shared_ptr to be used with handler
0166  * pointers returned by the generated handler factories.
0167  *
0168  * The handler factory classes generated by the thrift compiler return raw
0169  * pointers, and factory->releaseHandler() must be called when the handler is
0170  * no longer needed.
0171  *
0172  * A ReleaseHandler object can be instantiated and passed as the second
0173  * parameter to a shared_ptr, so that factory->releaseHandler() will be called
0174  * when the object is no longer needed, instead of deleting the pointer.
0175  */
0176 template <typename HandlerFactory_>
0177 class ReleaseHandler {
0178 public:
0179   ReleaseHandler(const std::shared_ptr<HandlerFactory_>& handlerFactory)
0180     : handlerFactory_(handlerFactory) {}
0181 
0182   void operator()(typename HandlerFactory_::Handler* handler) {
0183     if (handler) {
0184       handlerFactory_->releaseHandler(handler);
0185     }
0186   }
0187 
0188 private:
0189   std::shared_ptr<HandlerFactory_> handlerFactory_;
0190 };
0191 
0192 struct TConnectionInfo {
0193   // The input and output protocols
0194   std::shared_ptr<protocol::TProtocol> input;
0195   std::shared_ptr<protocol::TProtocol> output;
0196   // The underlying transport used for the connection
0197   // This is the transport that was returned by TServerTransport::accept(),
0198   // and it may be different than the transport pointed to by the input and
0199   // output protocols.
0200   std::shared_ptr<transport::TTransport> transport;
0201 };
0202 
0203 class TProcessorFactory {
0204 public:
0205   virtual ~TProcessorFactory() = default;
0206 
0207   /**
0208    * Get the TProcessor to use for a particular connection.
0209    *
0210    * This method is always invoked in the same thread that the connection was
0211    * accepted on.  This generally means that this call does not need to be
0212    * thread safe, as it will always be invoked from a single thread.
0213    */
0214   virtual std::shared_ptr<TProcessor> getProcessor(const TConnectionInfo& connInfo) = 0;
0215 };
0216 
0217 class TSingletonProcessorFactory : public TProcessorFactory {
0218 public:
0219   TSingletonProcessorFactory(std::shared_ptr<TProcessor> processor) : processor_(processor) {}
0220 
0221   std::shared_ptr<TProcessor> getProcessor(const TConnectionInfo&) override { return processor_; }
0222 
0223 private:
0224   std::shared_ptr<TProcessor> processor_;
0225 };
0226 }
0227 } // apache::thrift
0228 
0229 #endif // #ifndef _THRIFT_TPROCESSOR_H_