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 
0020 #ifndef THRIFT_PROTOCOL_THEADERPROTOCOL_H_
0021 #define THRIFT_PROTOCOL_THEADERPROTOCOL_H_ 1
0022 
0023 #include <thrift/protocol/TProtocol.h>
0024 #include <thrift/protocol/TProtocolTypes.h>
0025 #include <thrift/protocol/TVirtualProtocol.h>
0026 #include <thrift/transport/THeaderTransport.h>
0027 
0028 #include <memory>
0029 
0030 using apache::thrift::transport::THeaderTransport;
0031 
0032 namespace apache {
0033 namespace thrift {
0034 namespace protocol {
0035 
0036 /**
0037  * The header protocol for thrift. Reads unframed, framed, header format,
0038  * and http
0039  *
0040  */
0041 class THeaderProtocol : public TVirtualProtocol<THeaderProtocol> {
0042 protected:
0043 public:
0044   void resetProtocol();
0045 
0046   explicit THeaderProtocol(const std::shared_ptr<TTransport>& trans,
0047                            uint16_t protoId = T_COMPACT_PROTOCOL)
0048     : TVirtualProtocol<THeaderProtocol>(std::shared_ptr<TTransport>(new THeaderTransport(trans))),
0049       trans_(std::dynamic_pointer_cast<THeaderTransport>(getTransport())),
0050       protoId_(protoId) {
0051     trans_->setProtocolId(protoId);
0052     resetProtocol();
0053   }
0054 
0055   THeaderProtocol(const std::shared_ptr<TTransport>& inTrans,
0056                   const std::shared_ptr<TTransport>& outTrans,
0057                   uint16_t protoId = T_COMPACT_PROTOCOL)
0058     : TVirtualProtocol<THeaderProtocol>(
0059           std::shared_ptr<TTransport>(new THeaderTransport(inTrans, outTrans))),
0060       trans_(std::dynamic_pointer_cast<THeaderTransport>(getTransport())),
0061       protoId_(protoId) {
0062     trans_->setProtocolId(protoId);
0063     resetProtocol();
0064   }
0065 
0066   ~THeaderProtocol() override = default;
0067 
0068   /**
0069    * Functions to work with headers by calling into THeaderTransport
0070    */
0071   void setProtocolId(uint16_t protoId) {
0072     trans_->setProtocolId(protoId);
0073     resetProtocol();
0074   }
0075 
0076   typedef THeaderTransport::StringToStringMap StringToStringMap;
0077 
0078   // these work with write headers
0079   void setHeader(const std::string& key, const std::string& value) {
0080     trans_->setHeader(key, value);
0081   }
0082 
0083   void clearHeaders() { trans_->clearHeaders(); }
0084 
0085   StringToStringMap& getWriteHeaders() { return trans_->getWriteHeaders(); }
0086 
0087   // these work with read headers
0088   const StringToStringMap& getHeaders() const { return trans_->getHeaders(); }
0089 
0090   /**
0091    * Writing functions.
0092    */
0093 
0094   /*ol*/ uint32_t writeMessageBegin(const std::string& name,
0095                                     const TMessageType messageType,
0096                                     const int32_t seqId);
0097 
0098   /*ol*/ uint32_t writeMessageEnd();
0099 
0100   uint32_t writeStructBegin(const char* name);
0101 
0102   uint32_t writeStructEnd();
0103 
0104   uint32_t writeFieldBegin(const char* name, const TType fieldType, const int16_t fieldId);
0105 
0106   uint32_t writeFieldEnd();
0107 
0108   uint32_t writeFieldStop();
0109 
0110   uint32_t writeMapBegin(const TType keyType, const TType valType, const uint32_t size);
0111 
0112   uint32_t writeMapEnd();
0113 
0114   uint32_t writeListBegin(const TType elemType, const uint32_t size);
0115 
0116   uint32_t writeListEnd();
0117 
0118   uint32_t writeSetBegin(const TType elemType, const uint32_t size);
0119 
0120   uint32_t writeSetEnd();
0121 
0122   uint32_t writeBool(const bool value);
0123 
0124   uint32_t writeByte(const int8_t byte);
0125 
0126   uint32_t writeI16(const int16_t i16);
0127 
0128   uint32_t writeI32(const int32_t i32);
0129 
0130   uint32_t writeI64(const int64_t i64);
0131 
0132   uint32_t writeDouble(const double dub);
0133 
0134   uint32_t writeString(const std::string& str);
0135 
0136   uint32_t writeBinary(const std::string& str);
0137 
0138   /**
0139    * Reading functions
0140    */
0141 
0142   /*ol*/ uint32_t readMessageBegin(std::string& name, TMessageType& messageType, int32_t& seqId);
0143 
0144   /*ol*/ uint32_t readMessageEnd();
0145 
0146   uint32_t readStructBegin(std::string& name);
0147 
0148   uint32_t readStructEnd();
0149 
0150   uint32_t readFieldBegin(std::string& name, TType& fieldType, int16_t& fieldId);
0151 
0152   uint32_t readFieldEnd();
0153 
0154   uint32_t readMapBegin(TType& keyType, TType& valType, uint32_t& size);
0155 
0156   uint32_t readMapEnd();
0157 
0158   uint32_t readListBegin(TType& elemType, uint32_t& size);
0159 
0160   uint32_t readListEnd();
0161 
0162   uint32_t readSetBegin(TType& elemType, uint32_t& size);
0163 
0164   uint32_t readSetEnd();
0165 
0166   uint32_t readBool(bool& value);
0167   // Provide the default readBool() implementation for std::vector<bool>
0168   using TVirtualProtocol<THeaderProtocol>::readBool;
0169 
0170   uint32_t readByte(int8_t& byte);
0171 
0172   uint32_t readI16(int16_t& i16);
0173 
0174   uint32_t readI32(int32_t& i32);
0175 
0176   uint32_t readI64(int64_t& i64);
0177 
0178   uint32_t readDouble(double& dub);
0179 
0180   uint32_t readString(std::string& str);
0181 
0182   uint32_t readBinary(std::string& binary);
0183 
0184 protected:
0185   std::shared_ptr<THeaderTransport> trans_;
0186 
0187   std::shared_ptr<TProtocol> proto_;
0188   uint32_t protoId_;
0189 };
0190 
0191 class THeaderProtocolFactory : public TProtocolFactory {
0192 public:
0193   std::shared_ptr<TProtocol> getProtocol(std::shared_ptr<transport::TTransport> trans) override {
0194     auto* headerProtocol
0195         = new THeaderProtocol(trans, trans, T_BINARY_PROTOCOL);
0196     return std::shared_ptr<TProtocol>(headerProtocol);
0197   }
0198 
0199   std::shared_ptr<TProtocol> getProtocol(
0200       std::shared_ptr<transport::TTransport> inTrans,
0201       std::shared_ptr<transport::TTransport> outTrans) override {
0202     auto* headerProtocol = new THeaderProtocol(inTrans, outTrans, T_BINARY_PROTOCOL);
0203     return std::shared_ptr<TProtocol>(headerProtocol);
0204   }
0205 };
0206 }
0207 }
0208 } // apache::thrift::protocol
0209 
0210 #endif // #ifndef THRIFT_PROTOCOL_THEADERPROTOCOL_H_