blob: fd1dce7af40dc25a123747b5eb1cdad5f93d8522 [file] [log] [blame]
Jake Farrell23bf35d2011-12-08 02:02:20 +00001/*
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_TDISPATCHPROCESSOR_H_
20#define _THRIFT_TDISPATCHPROCESSOR_H_ 1
21
Roger Meier4285ba22013-06-10 21:17:23 +020022#include <thrift/TProcessor.h>
Jake Farrell23bf35d2011-12-08 02:02:20 +000023
Konrad Grochowski16a23a62014-11-13 15:33:38 +010024namespace apache {
25namespace thrift {
Jake Farrell23bf35d2011-12-08 02:02:20 +000026
27/**
28 * TDispatchProcessor is a helper class to parse the message header then call
29 * another function to dispatch based on the function name.
30 *
31 * Subclasses must implement dispatchCall() to dispatch on the function name.
32 */
33template <class Protocol_>
34class TDispatchProcessorT : public TProcessor {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010035public:
Jake Farrell23bf35d2011-12-08 02:02:20 +000036 virtual bool process(boost::shared_ptr<protocol::TProtocol> in,
37 boost::shared_ptr<protocol::TProtocol> out,
38 void* connectionContext) {
39 protocol::TProtocol* inRaw = in.get();
40 protocol::TProtocol* outRaw = out.get();
41
42 // Try to dynamic cast to the template protocol type
43 Protocol_* specificIn = dynamic_cast<Protocol_*>(inRaw);
44 Protocol_* specificOut = dynamic_cast<Protocol_*>(outRaw);
45 if (specificIn && specificOut) {
46 return processFast(specificIn, specificOut, connectionContext);
47 }
48
49 // Log the fact that we have to use the slow path
50 T_GENERIC_PROTOCOL(this, inRaw, specificIn);
51 T_GENERIC_PROTOCOL(this, outRaw, specificOut);
52
53 std::string fname;
54 protocol::TMessageType mtype;
55 int32_t seqid;
56 inRaw->readMessageBegin(fname, mtype, seqid);
57
58 // If this doesn't look like a valid call, log an error and return false so
59 // that the server will close the connection.
60 //
61 // (The old generated processor code used to try to skip a T_STRUCT and
62 // continue. However, that seems unsafe.)
63 if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010064 GlobalOutput.printf("received invalid message type %d from client", mtype);
Jake Farrell23bf35d2011-12-08 02:02:20 +000065 return false;
66 }
67
68 return this->dispatchCall(inRaw, outRaw, fname, seqid, connectionContext);
69 }
70
Konrad Grochowski16a23a62014-11-13 15:33:38 +010071protected:
Jake Farrell23bf35d2011-12-08 02:02:20 +000072 bool processFast(Protocol_* in, Protocol_* out, void* connectionContext) {
73 std::string fname;
74 protocol::TMessageType mtype;
75 int32_t seqid;
76 in->readMessageBegin(fname, mtype, seqid);
77
78 if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010079 GlobalOutput.printf("received invalid message type %d from client", mtype);
Jake Farrell23bf35d2011-12-08 02:02:20 +000080 return false;
81 }
82
Konrad Grochowski16a23a62014-11-13 15:33:38 +010083 return this->dispatchCallTemplated(in, out, fname, seqid, connectionContext);
Jake Farrell23bf35d2011-12-08 02:02:20 +000084 }
85
86 /**
87 * dispatchCall() methods must be implemented by subclasses
88 */
89 virtual bool dispatchCall(apache::thrift::protocol::TProtocol* in,
90 apache::thrift::protocol::TProtocol* out,
Konrad Grochowski16a23a62014-11-13 15:33:38 +010091 const std::string& fname,
92 int32_t seqid,
Jake Farrell23bf35d2011-12-08 02:02:20 +000093 void* callContext) = 0;
94
Konrad Grochowski16a23a62014-11-13 15:33:38 +010095 virtual bool dispatchCallTemplated(Protocol_* in,
96 Protocol_* out,
97 const std::string& fname,
98 int32_t seqid,
Jake Farrell23bf35d2011-12-08 02:02:20 +000099 void* callContext) = 0;
100};
101
102/**
103 * Non-templatized version of TDispatchProcessor, that doesn't bother trying to
104 * perform a dynamic_cast.
105 */
106class TDispatchProcessor : public TProcessor {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100107public:
Jake Farrell23bf35d2011-12-08 02:02:20 +0000108 virtual bool process(boost::shared_ptr<protocol::TProtocol> in,
109 boost::shared_ptr<protocol::TProtocol> out,
110 void* connectionContext) {
111 std::string fname;
112 protocol::TMessageType mtype;
113 int32_t seqid;
114 in->readMessageBegin(fname, mtype, seqid);
115
116 if (mtype != protocol::T_CALL && mtype != protocol::T_ONEWAY) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100117 GlobalOutput.printf("received invalid message type %d from client", mtype);
Jake Farrell23bf35d2011-12-08 02:02:20 +0000118 return false;
119 }
120
121 return dispatchCall(in.get(), out.get(), fname, seqid, connectionContext);
122 }
123
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100124protected:
Jake Farrell23bf35d2011-12-08 02:02:20 +0000125 virtual bool dispatchCall(apache::thrift::protocol::TProtocol* in,
126 apache::thrift::protocol::TProtocol* out,
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100127 const std::string& fname,
128 int32_t seqid,
Jake Farrell23bf35d2011-12-08 02:02:20 +0000129 void* callContext) = 0;
130};
131
132// Specialize TDispatchProcessorT for TProtocol and TDummyProtocol just to use
133// the generic TDispatchProcessor.
134template <>
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100135class TDispatchProcessorT<protocol::TDummyProtocol> : public TDispatchProcessor {};
Jake Farrell23bf35d2011-12-08 02:02:20 +0000136template <>
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100137class TDispatchProcessorT<protocol::TProtocol> : public TDispatchProcessor {};
138}
139} // apache::thrift
Jake Farrell23bf35d2011-12-08 02:02:20 +0000140
141#endif // _THRIFT_TDISPATCHPROCESSOR_H_