]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | #ifndef _THRIFT_ASYNC_TASYNCDISPATCHPROCESSOR_H_ | |
20 | #define _THRIFT_ASYNC_TASYNCDISPATCHPROCESSOR_H_ 1 | |
21 | ||
22 | #include <thrift/async/TAsyncProcessor.h> | |
23 | ||
24 | namespace apache { | |
25 | namespace thrift { | |
26 | namespace async { | |
27 | ||
28 | /** | |
29 | * TAsyncDispatchProcessor is a helper class to parse the message header then | |
30 | * call another function to dispatch based on the function name. | |
31 | * | |
32 | * Subclasses must implement dispatchCall() to dispatch on the function name. | |
33 | */ | |
34 | template <class Protocol_> | |
35 | class TAsyncDispatchProcessorT : public TAsyncProcessor { | |
36 | public: | |
37 | void process(std::function<void(bool success)> _return, | |
38 | std::shared_ptr<protocol::TProtocol> in, | |
39 | std::shared_ptr<protocol::TProtocol> out) override { | |
40 | protocol::TProtocol* inRaw = in.get(); | |
41 | protocol::TProtocol* outRaw = out.get(); | |
42 | ||
43 | // Try to dynamic cast to the template protocol type | |
44 | auto* specificIn = dynamic_cast<Protocol_*>(inRaw); | |
45 | auto* specificOut = dynamic_cast<Protocol_*>(outRaw); | |
46 | if (specificIn && specificOut) { | |
47 | return processFast(_return, specificIn, specificOut); | |
48 | } | |
49 | ||
50 | // Log the fact that we have to use the slow path | |
51 | T_GENERIC_PROTOCOL(this, inRaw, specificIn); | |
52 | T_GENERIC_PROTOCOL(this, outRaw, specificOut); | |
53 | ||
54 | std::string fname; | |
55 | protocol::TMessageType mtype; | |
56 | int32_t seqid; | |
57 | inRaw->readMessageBegin(fname, mtype, seqid); | |
58 | ||
59 | // If this doesn't look like a valid call, log an error and return false so | |
60 | // that the server will close the connection. | |
61 | // | |
62 | // (The old generated processor code used to try to skip a T_STRUCT and | |
63 | // continue. However, that seems unsafe.) | |
64 | if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) { | |
65 | GlobalOutput.printf("received invalid message type %d from client", mtype); | |
66 | _return(false); | |
67 | return; | |
68 | } | |
69 | ||
70 | return this->dispatchCall(_return, inRaw, outRaw, fname, seqid); | |
71 | } | |
72 | ||
73 | void processFast(std::function<void(bool success)> _return, | |
74 | Protocol_* in, | |
75 | Protocol_* out) { | |
76 | std::string fname; | |
77 | protocol::TMessageType mtype; | |
78 | int32_t seqid; | |
79 | in->readMessageBegin(fname, mtype, seqid); | |
80 | ||
81 | if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) { | |
82 | GlobalOutput.printf("received invalid message type %d from client", mtype); | |
83 | _return(false); | |
84 | return; | |
85 | } | |
86 | ||
87 | return this->dispatchCallTemplated(_return, in, out, fname, seqid); | |
88 | } | |
89 | ||
90 | virtual void dispatchCall(std::function<void(bool ok)> _return, | |
91 | apache::thrift::protocol::TProtocol* in, | |
92 | apache::thrift::protocol::TProtocol* out, | |
93 | const std::string& fname, | |
94 | int32_t seqid) = 0; | |
95 | ||
96 | virtual void dispatchCallTemplated(std::function<void(bool ok)> _return, | |
97 | Protocol_* in, | |
98 | Protocol_* out, | |
99 | const std::string& fname, | |
100 | int32_t seqid) = 0; | |
101 | }; | |
102 | ||
103 | /** | |
104 | * Non-templatized version of TAsyncDispatchProcessor, | |
105 | * that doesn't bother trying to perform a dynamic_cast. | |
106 | */ | |
107 | class TAsyncDispatchProcessor : public TAsyncProcessor { | |
108 | public: | |
109 | void process(std::function<void(bool success)> _return, | |
110 | std::shared_ptr<protocol::TProtocol> in, | |
111 | std::shared_ptr<protocol::TProtocol> out) override { | |
112 | protocol::TProtocol* inRaw = in.get(); | |
113 | protocol::TProtocol* outRaw = out.get(); | |
114 | ||
115 | std::string fname; | |
116 | protocol::TMessageType mtype; | |
117 | int32_t seqid; | |
118 | inRaw->readMessageBegin(fname, mtype, seqid); | |
119 | ||
120 | // If this doesn't look like a valid call, log an error and return false so | |
121 | // that the server will close the connection. | |
122 | // | |
123 | // (The old generated processor code used to try to skip a T_STRUCT and | |
124 | // continue. However, that seems unsafe.) | |
125 | if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) { | |
126 | GlobalOutput.printf("received invalid message type %d from client", mtype); | |
127 | _return(false); | |
128 | return; | |
129 | } | |
130 | ||
131 | return dispatchCall(_return, inRaw, outRaw, fname, seqid); | |
132 | } | |
133 | ||
134 | virtual void dispatchCall(std::function<void(bool ok)> _return, | |
135 | apache::thrift::protocol::TProtocol* in, | |
136 | apache::thrift::protocol::TProtocol* out, | |
137 | const std::string& fname, | |
138 | int32_t seqid) = 0; | |
139 | }; | |
140 | ||
141 | // Specialize TAsyncDispatchProcessorT for TProtocol and TDummyProtocol just to | |
142 | // use the generic TDispatchProcessor. | |
143 | template <> | |
144 | class TAsyncDispatchProcessorT<protocol::TDummyProtocol> : public TAsyncDispatchProcessor {}; | |
145 | template <> | |
146 | class TAsyncDispatchProcessorT<protocol::TProtocol> : public TAsyncDispatchProcessor {}; | |
147 | } | |
148 | } | |
149 | } // apache::thrift::async | |
150 | ||
151 | #endif // _THRIFT_ASYNC_TASYNCDISPATCHPROCESSOR_H_ |