123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756 |
- /**
- * Tencent is pleased to support the open source community by making Tars available.
- *
- * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
- *
- * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
- * in compliance with the License. You may obtain a copy of the License at
- *
- * https://opensource.org/licenses/BSD-3-Clause
- *
- * Unless required by applicable law or agreed to in writing, software distributed
- * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- * CONDITIONS OF ANY KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations under the License.
- */
- #ifndef _WUP_H_
- #define _WUP_H_
- #include <map>
- #include <string>
- #include <vector>
- #include <sstream>
- //支持iphone
- #ifdef __APPLE__
- #include "RequestF.h"
- #elif defined ANDROID // android
- #include "RequestF.h"
- #else
- #include "tup/RequestF.h"
- #endif
- // #ifdef __GNUC__
- // # if __GNUC__ >3 || __GNUC_MINOR__ > 3
- // # include <ext/pool_allocator.h>
- // # endif
- // #endif
- using namespace std;
- using namespace tars;
- namespace tup
- {
- //存放tars返回值的key
- const string STATUS_RESULT_CODE = "STATUS_RESULT_CODE";
- const string STATUS_RESULT_DESC = "STATUS_RESULT_DESC";
- /////////////////////////////////////////////////////////////////////////////////
- // 属性封装类
- template<typename TWriter = BufferWriter, typename TReader = BufferReader,template<typename> class Alloc = std::allocator >
- //template<typename> class Alloc = __gnu_cxx::__pool_alloc >
- class UniAttribute
- {
- typedef vector<char,Alloc<char> > VECTOR_CHAR_TYPE;
- typedef map<string, VECTOR_CHAR_TYPE, less<string>,Alloc< pair<const string,VECTOR_CHAR_TYPE > > > VECTOR_CHAR_IN_MAP_TYPE;
- typedef map<string, VECTOR_CHAR_IN_MAP_TYPE, less<string>,Alloc< pair<const string,VECTOR_CHAR_IN_MAP_TYPE > > > WUP_DATA_TYPE;
- public:
- /**
- * 构造函数
- */
- UniAttribute()
- {
- _iVer = 3;
- }
- void setVersion(short iVer)
- {
- _iVer = iVer;
- }
- /**
- * 添加属性值
- *
- * @param T: 属性类型
- * @param name:属性名称
- * @param t: 属性值
- */
- template<typename T> void put(const string& name, const T& t)
- {
- os.reset();
- os.write(t, 0);
- VECTOR_CHAR_TYPE & v = _data[name];
-
- os.swap(v);
- // v.assign(os.getBuffer(), os.getBuffer() + os.getLength());
- }
- void putUnknown(const string& name, const string& value)
- {
- os.reset();
- os.writeUnknownV2(value);
- VECTOR_CHAR_TYPE & v = _data[name];
- os.swap(v);
- // v.assign(os.getBuffer(), os.getBuffer() + os.getLength());
- }
- void getUnknown(const string& name, string& value)
- {
- typename VECTOR_CHAR_IN_MAP_TYPE::iterator mit;
- mit = _data.find(name);
- if (mit != _data.end() && mit->second.size()>2)
- {
- //去掉DataHead::eStructBegin,DataHead::eStructEnd
- value = string(&mit->second[0]+1, mit->second.size()-2);
- return;
- }
- throw runtime_error(string("UniAttribute not found key:") + name);
- }
- /**
- * 获取属性值,属性不存在则抛出异常
- *
- * @throw runtime_error
- * @param T: 属性类型
- * @param name:属性名称
- * @param t: 属性值输出参数
- */
- template<typename T> void get(const string& name, T& t)
- {
- typename VECTOR_CHAR_IN_MAP_TYPE::iterator mit;
- mit = _data.find(name);
- if (mit != _data.end())
- {
- is.reset();
- is.setBuffer(mit->second);
- is.read(t, 0, true);
- return;
- }
- throw runtime_error(string("UniAttribute not found key:") + name);
- }
- /**
- * 获取属性值,属性不存在则抛出异常
- *
- * @throw runtime_error
- * @param T: 属性类型
- * @param name:属性名称
- * @return T: 属性值
- */
- template<typename T> T get(const string& name)
- {
- T t;
- get<T>(name, t);
- return t;
- }
- /**
- * 获取属性值,忽略异常,不存在的属性返回缺省值
- *
- * @param T: 属性类型
- * @param name:属性名称
- * @param t: 属性值输出参数
- * @param def: 默认值
- */
- template<typename T> void getByDefault(const string& name, T& t, const T& def)
- {
- try
- {
- get<T>(name, t);
- }
- catch (runtime_error& e)
- {
- t = def;
- }
- }
- /**
- * 获取属性值(忽略异常,def为缺省值)
- *
- * @param T: 属性类型
- * @param name:属性名称
- * @param: 默认值
- * @return T: 属性值
- */
- template<typename T> T getByDefault(const string& name, const T& def)
- {
- T t;
- getByDefault<T>(name, t, def);
- return t;
- }
- /**
- *清除全部属性值
- */
- void clear()
- {
- _data.clear();
- }
- /** 编码
- *
- * @param buff: 编码结果输出参数
- */
- void encode(string& buff)
- {
- os.reset();
- os.write(_data, 0);
- os.swap(buff);
- // buff.assign(os.getBuffer(), os.getLength());
- }
- /** 编码
- *
- * @param buff: 编码结果输出参数
- */
- void encode(vector<char>& buff)
- {
- os.reset();
- os.write(_data, 0);
- os.swap(buff);
- // buff.assign(os.getBuffer(), os.getBuffer() + os.getLength());
- }
- /** 编码
- *
- * @throw runtime_error
- * @param buff:输出存放编码结果的buffer指针
- * @param len: 输入buff长度,输出编码结果长度
- */
- void encode(char* buff, size_t & len)
- {
- os.reset();
- os.write(_data, 0);
- if(len < os.getLength()) throw runtime_error("encode error, buffer length too short");
- memcpy(buff, os.getBuffer(), os.getLength());
- len = os.getLength();
- }
- /** 解码
- *
- * @throw runtime_error
- * @param buff:待解码字节流的buffer指针
- * @param len: 待解码字节流的长度
- */
- void decode(const char* buff, size_t len)
- {
- is.reset();
- is.setBuffer(buff, len);
- _data.clear();
-
- is.read(_data, 0, true);
-
- }
- /**
- * 解码
- *
- * @throw runtime_error
- * @param buff: 待解码的字节流
- */
- void decode(const vector<char>& buff)
- {
- is.reset();
- is.setBuffer(buff);
-
- _data.clear();
-
- is.read(_data, 0, true);
-
- }
- /**
- * 获取已有的属性
- *
- * @return const map<string,map<string,vector<char>>>& : 属性map
- */
- const map<string, vector<char> >& getData() const
- {
- return _data;
- }
- /**
- * 判断属性集合是否为空
- *
- * @return bool:属性是否为空
- */
- bool isEmpty()
- {
- return _data.empty();
- }
- /**
- * 获取属性集合大小
- *
- * @return size_t: 集合大小
- */
- size_t size()
- {
- return _data.size();
- }
- /**
- * 判断属性是否存在
- *
- * @param key:属性名称
- * @return bool:是否存在
- */
- bool containsKey(const string & key)
- {
- return _data.find(key) != _data.end();
- }
- protected:
- VECTOR_CHAR_IN_MAP_TYPE _data;
- short _iVer;
- public:
- TarsInputStream<TReader> is;
- TarsOutputStream<TWriter> os;
- };
- /////////////////////////////////////////////////////////////////////////////////
- // 请求、回应包封装类
- template<typename TWriter = BufferWriter, typename TReader = BufferReader,template<typename> class Alloc = std::allocator >
- struct UniPacket : protected RequestPacket, public UniAttribute<TWriter, TReader, Alloc>
- {
- public:
- /**
- * 构造函数
- */
- UniPacket()
- {
- iVersion = 3; cPacketType = 0;
- iMessageType = 0; iRequestId = 0;
- sServantName = ""; sFuncName = "";
- iTimeout = 0; sBuffer.clear();
- context.clear(); status.clear();
- UniAttribute<TWriter, TReader,Alloc>::_iVer = iVersion;
- UniAttribute<TWriter, TReader,Alloc>::_data.clear();
- UniAttribute<TWriter, TReader,Alloc>::_data.clear();
- }
- /**
- * 拷贝构造
- * @param tup
- */
- UniPacket(const UniPacket &tup) { *this = tup;}
- void setVersion(short iVer)
- {
- UniAttribute<TWriter, TReader,Alloc>::_iVer = iVer;
- iVersion = iVer;
- }
- /**
- * 由请求包生成回应包基本结构,回填关键的请求信息
- *
- * @return UniPacket: 回应包
- */
- UniPacket createResponse()
- {
- UniPacket respPacket;
- respPacket.sServantName = sServantName;
- respPacket.sFuncName = sFuncName;
- respPacket.iRequestId = iRequestId;
- return respPacket;
- }
- /**
- * 编码,结果的包头4个字节为整个包的长度,网络字节序
- *
- * @throw runtime_error
- * @param buff: 编码结果输出参数
- */
- void encode(string& buff)
- {
- encodeBuff<string>(buff);
- // TarsOutputStream<TWriter> &os = UniAttribute<TWriter, TReader,Alloc>::os;
- // os.reset();
-
- // doEncode(os);
- // tars::Int32 iHeaderLen = htonl(sizeof(tars::Int32) + os.getLength());
- // buff.assign((const char*)&iHeaderLen, sizeof(tars::Int32));
- // buff.append(os.getBuffer(), os.getLength());
- }
- /**
- * 编码,结果的包头4个字节为整个包的长度,网络字节序
- *
- * @throw runtime_error
- * @param buff: 编码结果输出参数
- */
- void encode(vector<char>& buff)
- {
- encodeBuff<vector<char>>(buff);
- // TarsOutputStream<TWriter> & os = UniAttribute<TWriter, TReader,Alloc>::os;
- // os.reset();
- // doEncode(os);
- // tars::Int32 iHeaderLen = htonl(sizeof(tars::Int32) + os.getLength());
- // buff.resize(sizeof(tars::Int32) + os.getLength());
- // memcpy(&buff[0], &iHeaderLen, sizeof(tars::Int32));
- // memcpy(&buff[sizeof(tars::Int32)], os.getBuffer(), os.getLength());
- }
- /**
- * 编码,结果的包头4个字节为整个包的长度,网络字节序
- * @throw runtime_error
- * @param buff:存放编码结果的buffer指针
- * @param len: 输入buff长度,输出编码结果长度
- */
- void encode(char* buff, size_t & len)
- {
- TarsOutputStream<TWriter>& os = UniAttribute<TWriter, TReader>::os;
- os.reset();
- doEncode(os);
- os.reset();
-
- writeTo(os);
- tars::Int32 iHeaderLen = htonl(sizeof(tars::Int32) + os.getLength());
- if (len < sizeof(tars::Int32) + os.getLength()) throw runtime_error("encode error, buffer length too short");
- memcpy(buff, &iHeaderLen, sizeof(tars::Int32));
- memcpy(buff + sizeof(tars::Int32), os.getBuffer(), os.getLength());
- len = sizeof(tars::Int32) + os.getLength();
- // TarsOutputStream<TWriter> &os = UniAttribute<TWriter, TReader,Alloc>::os;
- // os.reset();
- // doEncode(os);
- // tars::Int32 iHeaderLen = htonl(sizeof(tars::Int32) + os.getLength());
- // if(len < sizeof(tars::Int32) + os.getLength()) throw runtime_error("encode error, buffer length too short");
- // memcpy(buff, &iHeaderLen, sizeof(tars::Int32));
- // memcpy(buff + sizeof(tars::Int32), os.getBuffer(), os.getLength());
- // len = sizeof(tars::Int32) + os.getLength();
- }
- /** 解码
- *
- * @throw runtime_error
- * @param buff:待解码字节流的buffer指针
- * @param len: 待解码字节流的长度
- */
- void decode(const char* buff, size_t len)
- {
- if(len < sizeof(tars::Int32)) throw runtime_error("packet length too short, first 4 bytes must be buffer length.");
-
- TarsInputStream<TReader> &is = UniAttribute<TWriter, TReader,Alloc>::is;
- is.reset();
- is.setBuffer(buff + sizeof(tars::Int32), len - sizeof(tars::Int32));
- readFrom(is);
- UniAttribute<TWriter, TReader,Alloc>::_iVer = iVersion;
- is.reset();
- is.setBuffer(sBuffer);
- UniAttribute<TWriter, TReader,Alloc>::_data.clear();
-
- is.read(UniAttribute<TWriter, TReader,Alloc>::_data, 0, true);
-
- }
- public:
- /**
- * 获取消息version
- * @return tars::Short
- */
- tars::Short getVersion() const { return iVersion; }
- /**
- * 获取消息ID
- * @return tars::Int32
- */
- tars::Int32 getRequestId() const { return iRequestId; }
- /**
- * 设置请求ID
- * @param value
- */
- void setRequestId(tars::Int32 value) { iRequestId = value; }
- /**
- * 获取对象名称
- * @return const std::string&
- */
- const std::string& getServantName() const { return sServantName; }
- /**
- * 设置对象名称
- * @param value
- */
- void setServantName(const std::string& value) { sServantName = value; }
- /**
- * 获取方法名
- * @return const std::string&
- */
- const std::string& getFuncName() const { return sFuncName; }
- /**
- * 设置方法名
- * @param value
- */
- void setFuncName(const std::string& value) { sFuncName = value; }
- protected:
- template<typename T>
- void encodeBuff(T& buff)
- {
- TarsOutputStream<TWriter>& os = UniAttribute<TWriter, TReader>::os;
- os.reset();
- doEncode(os);
- os.reset();
-
- tars::Int32 iHeaderLen = 0;
- // 先预留4个字节长度
- os.writeBuf((const char *)&iHeaderLen, sizeof(iHeaderLen));
-
- writeTo(os);
- os.swap(buff);
- assert(buff.size() >= 4);
- iHeaderLen = htonl((int)(buff.size()));
- memcpy(&buff[0], (const char *)&iHeaderLen, sizeof(iHeaderLen));
- }
- /**
- * 内部编码
- */
- void doEncode(TarsOutputStream<TWriter>& os)
- {
- //ServantName、FuncName不能为空
- if (sServantName.empty()) throw runtime_error("ServantName must not be empty");
- if (sFuncName.empty()) throw runtime_error("FuncName must not be empty");
- os.reset();
- os.write(UniAttribute<TWriter, TReader>::_data, 0);
- os.swap(sBuffer);
- os.reset();
- }
- // /**
- // * 内部编码
- // */
- // void doEncode(TarsOutputStream<TWriter>& os)
- // {
- // //ServantName、FuncName不能为空
- // if(sServantName.empty()) throw runtime_error("ServantName must not be empty");
- // if(sFuncName.empty()) throw runtime_error("FuncName must not be empty");
- // os.reset();
- // os.write(UniAttribute<TWriter, TReader,Alloc>::_data, 0);
-
- // sBuffer.assign(os.getBuffer(), os.getBuffer() + os.getLength());
- // os.reset();
- // writeTo(os);
- // }
- };
- /////////////////////////////////////////////////////////////////////////////////
- // 调用TARS的服务时使用的类
- template<typename TWriter = BufferWriter, typename TReader = BufferReader,template<typename> class Alloc = std::allocator>
- struct TarsUniPacket: public UniPacket<TWriter, TReader,Alloc>
- {
- public:
- TarsUniPacket(){};
- TarsUniPacket(const UniPacket<TWriter, TReader,Alloc> &tup)
- : UniPacket<TWriter, TReader,Alloc>(tup) {};
- /**
- * 设置协议版本
- * @param value
- */
- void setTarsVersion(tars::Short value) { UniPacket<TWriter, TReader,Alloc>::setVersion(value); }
- /**
- * 设置调用类型
- * @param value
- */
- void setTarsPacketType(tars::Char value) { this->cPacketType = value; }
- /**
- * 设置消息类型
- * @param value
- */
- void setTarsMessageType(tars::Int32 value) { this->iMessageType = value; }
- /**
- * 设置超时时间
- * @param value
- */
- void setTarsTimeout(tars::Int32 value) { this->iTimeout = value; }
- /**
- * 设置参数编码内容
- * @param value
- */
- void setTarsBuffer(const vector<tars::Char>& value) { this->sBuffer = value; }
- /**
- * 设置上下文
- * @param value
- */
- void setTarsContext(const map<std::string, std::string>& value) { this->context = value; }
- /**
- * 设置特殊消息的状态值
- * @param value
- */
- void setTarsStatus(const map<std::string, std::string>& value) { this->status = value; }
- /**
- * 获取协议版本
- * @return tars::Short
- */
- tars::Short getTarsVersion() const { return this->iVersion; }
- /**
- * 获取调用类型
- * @return tars::Char
- */
- tars::Char getTarsPacketType() const { return this->cPacketType; }
- /**
- * 获取消息类型
- * @return tars::Int32
- */
- tars::Int32 getTarsMessageType() const { return this->iMessageType; }
- /**
- * 获取超时时间
- * @return tars::Int32
- */
- tars::Int32 getTarsTimeout() const { return this->iTimeout; }
- /**
- * 获取参数编码后内容
- * @return const vector<tars::Char>&
- */
- const vector<tars::Char>& getTarsBuffer() const { return this->sBuffer; }
- /**
- * 获取上下文信息
- * @return const map<std::string,std::string>&
- */
- const map<std::string, std::string>& getTarsContext() const { return this->context; }
- /**
- * 获取特殊消息的状态值
- * @return const map<std::string,std::string>&
- */
- const map<std::string, std::string>& getTarsStatus() const { return this->status; }
- /**
- * 获取调用tars的返回值
- *
- * @retrun tars::Int32
- */
- tars::Int32 getTarsResultCode() const
- {
- map<std::string, std::string>::const_iterator it;
- if((it = this->status.find(STATUS_RESULT_CODE)) == this->status.end())
- {
- return 0;
- }
- else
- {
- return atoi(it->second.c_str());
- }
- }
- /**
- * 获取调用tars的返回描述
- *
- * @retrun string
- */
- string getTarsResultDesc() const
- {
- map<std::string, std::string>::const_iterator it;
- if((it = this->status.find(STATUS_RESULT_DESC)) == this->status.end())
- {
- return "";
- }
- else
- {
- return it->second;
- }
- }
- };
- // #ifdef __GNUC__
- // # if __GNUC__ >3 || __GNUC_MINOR__ > 3
- // typedef UniAttribute<BufferWriter,BufferReader, __gnu_cxx::__pool_alloc> UniAttrPoolAlloc;
- // typedef UniPacket<BufferWriter,BufferReader, __gnu_cxx::__pool_alloc> UniPacketPoolAlloc;
- // typedef TarsUniPacket<BufferWriter,BufferReader, __gnu_cxx::__pool_alloc> TarsUniPacketPoolAlloc;
- // # endif
- // #endif
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- #endif
|