File indexing completed on 2026-04-17 08:35:04
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
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
0031
0032
0033
0034
0035
0036 class TProcessorEventHandler {
0037 public:
0038 virtual ~TProcessorEventHandler() = default;
0039
0040
0041
0042
0043
0044
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
0054
0055 virtual void freeContext(void* ctx, const char* fn_name) {
0056 (void)ctx;
0057 (void)fn_name;
0058 }
0059
0060
0061
0062
0063 virtual void preRead(void* ctx, const char* fn_name) {
0064 (void)ctx;
0065 (void)fn_name;
0066 }
0067
0068
0069
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
0079
0080 virtual void preWrite(void* ctx, const char* fn_name) {
0081 (void)ctx;
0082 (void)fn_name;
0083 }
0084
0085
0086
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
0096
0097 virtual void asyncComplete(void* ctx, const char* fn_name) {
0098 (void)ctx;
0099 (void)fn_name;
0100 }
0101
0102
0103
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
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
0135
0136
0137
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
0166
0167
0168
0169
0170
0171
0172
0173
0174
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
0194 std::shared_ptr<protocol::TProtocol> input;
0195 std::shared_ptr<protocol::TProtocol> output;
0196
0197
0198
0199
0200 std::shared_ptr<transport::TTransport> transport;
0201 };
0202
0203 class TProcessorFactory {
0204 public:
0205 virtual ~TProcessorFactory() = default;
0206
0207
0208
0209
0210
0211
0212
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 }
0228
0229 #endif