TarsCurrent.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __TARS_CURRENT_H_
  17. #define __TARS_CURRENT_H_
  18. #include "util/tc_epoll_server.h"
  19. #include "tup/RequestF.h"
  20. #include "tup/tup.h"
  21. #include "servant/BaseF.h"
  22. namespace tars
  23. {
  24. class ServantHandle;
  25. //////////////////////////////////////////////////////////////
  26. /**
  27. * 当前请求的上下文
  28. */
  29. class TarsCurrent : public TC_HandleBase
  30. {
  31. public:
  32. typedef std::map<string, string> TARS_STATUS;
  33. typedef std::vector<char> TARS_BUFFER;
  34. /**
  35. * 构造函数
  36. * @param pServantHandle
  37. */
  38. TarsCurrent(ServantHandle *pServantHandle);
  39. /**
  40. * 析构
  41. */
  42. ~TarsCurrent();
  43. /**
  44. * 获取IP
  45. * @return string
  46. */
  47. const string &getIp() const;
  48. /**
  49. * get host name
  50. * @return
  51. */
  52. const string &getHostName() const;
  53. /**
  54. * 获取端口
  55. * @return int
  56. */
  57. int getPort() const;
  58. /**
  59. * 获取uid
  60. * @return uint32
  61. */
  62. uint32_t getUId() const;
  63. /**
  64. * 获取fd
  65. * @return int
  66. */
  67. int getFd() const { return _data->fd(); }
  68. /**
  69. * 是否函数返回时发送响应包给客户端
  70. * @return bool
  71. */
  72. bool isResponse() const;
  73. /**
  74. * 设置连接的关闭类型,详情参看TC_EpollServer::EM_CLOSE_T
  75. */
  76. void setCloseType(int type);
  77. /**
  78. * 获取连接关闭类型,详情请参考TC_EpollServer::EM_CLOSE_T类型
  79. */
  80. int getCloseType() const;
  81. /**
  82. * 设置是否自动回响应包
  83. */
  84. void setResponse(bool value) { _response = value; }
  85. /**
  86. * 设置返回的context(仅TARS协议有效)
  87. */
  88. void setResponseContext(const map<std::string, std::string> & context){_responseContext = context;}
  89. /**
  90. * 获取返回的context(仅TARS协议有效)
  91. */
  92. const map<std::string, std::string> & getResponseContext() const {return _responseContext;}
  93. /**
  94. * 关闭当前连接
  95. */
  96. void close();
  97. /**
  98. * 获取所属的ServantHandle
  99. */
  100. ServantHandle* getServantHandle();
  101. /**
  102. * 获取来源的Adapter
  103. * @return TC_EpollServer::BindAdapter*
  104. */
  105. TC_EpollServer::BindAdapter* getBindAdapter();
  106. /**
  107. * 获取请求buffer
  108. * @return string
  109. */
  110. const vector<char> &getRequestBuffer() const;
  111. /**
  112. * 获取服务Servant名称
  113. * @return string
  114. */
  115. string getServantName() const;
  116. /**
  117. * 请求的协议的版本号(仅TARS协议有效)
  118. *
  119. * @return short
  120. */
  121. short getRequestVersion() const;
  122. /**
  123. * 扩展map(仅TARS协议有效)
  124. * @return map<string,string>&
  125. */
  126. map<string, string>& getContext();
  127. /**
  128. * 获取保存状态信息,比如染色等(仅TARS协议有效)
  129. * @return map<string,string>&
  130. */
  131. const map<string, string>& getRequestStatus() const;
  132. /**
  133. * 函数名称(仅TARS协议有效)
  134. * @return string
  135. */
  136. string getFuncName() const;
  137. /**
  138. * 请求ID(仅TARS协议有效)
  139. * @return int
  140. */
  141. uint32_t getRequestId() const;
  142. /**
  143. * 获取包类型(仅TARS协议有效)
  144. * @return char
  145. */
  146. char getPacketType() const;
  147. /**
  148. * 获取消息类型(仅TARS协议有效)
  149. * @return tars::Int32
  150. */
  151. tars::Int32 getMessageType() const;
  152. /**
  153. * 获取接收到请求的时间
  154. */
  155. struct timeval getRecvTime() const;
  156. /**
  157. * 设置是否上报状态报告
  158. */
  159. void setReportStat(bool bReport);
  160. /**
  161. * taf协议的发送响应数据(仅TAF协议有效)
  162. * @param iRet
  163. * @param status
  164. * @param buffer
  165. */
  166. void sendResponse(int iRet);
  167. /**
  168. * taf协议的发送响应数据(仅TAF协议有效), 直接swapbuffer , 这样可以不用copy 数据
  169. * @param iRet
  170. * @param status
  171. * @param buffer
  172. */
  173. void sendResponse(int iRet, tars::TarsOutputStream<tars::BufferWriterVector>& os);
  174. /**
  175. * taf协议的发送响应数据(仅TAF协议有效), 直接swapbuffer , 这样可以不用copy 数据
  176. * @param iRet
  177. * @param status
  178. * @param buffer
  179. */
  180. void sendResponse(int iRet, tup::UniAttribute<tars::BufferWriterVector, tars::BufferReader>& attr);
  181. /**
  182. * taf协议的发送响应数据(仅TAF协议有效)
  183. * @param iRet
  184. * @param buff
  185. */
  186. void sendResponse(int iRet, const vector<char> &buff);
  187. /**
  188. * 普通协议的发送响应数据(非TAF协议有效)
  189. * @param buff
  190. * @param len
  191. */
  192. void sendResponse(const char* buff, uint32_t len);
  193. /**
  194. * 设置cookie
  195. */
  196. void setCookie(const map<string, string> &cookie)
  197. {
  198. _cookie = cookie;
  199. }
  200. /**
  201. * 获取cookie
  202. */
  203. map<string, string> & getCookie()
  204. {
  205. return _cookie;
  206. }
  207. protected:
  208. friend class ServantHandle;
  209. friend class Application;
  210. /**
  211. * 初始化
  212. * @param data
  213. */
  214. void initialize(const shared_ptr<TC_EpollServer::RecvContext> &data);
  215. /**
  216. * 初始化
  217. * @param data
  218. */
  219. void initializeClose(const shared_ptr<TC_EpollServer::RecvContext> &data);
  220. /**
  221. * 初始化
  222. * @param sRecvBuffer
  223. */
  224. void initialize(const vector<char> &sRecvBuffer);
  225. /**
  226. * 服务端上报状态,针对单向调用及TUP调用(仅对TARS协议有效)
  227. */
  228. void reportToStat(const string & sObj);
  229. /**
  230. * 发送消息
  231. * @param iRet
  232. * @param response
  233. * @param status
  234. * @param sResultDesc
  235. * @param push
  236. */
  237. void sendResponse(int iRet, const vector<char> &buffer, const map<string, string>& status, const string& sResultDesc);
  238. protected:
  239. /**
  240. * 操作类指针
  241. */
  242. ServantHandle* _servantHandle;
  243. /**
  244. * 接收到的数据
  245. */
  246. shared_ptr<TC_EpollServer::RecvContext> _data;
  247. /**
  248. * 客户端请求包
  249. */
  250. RequestPacket _request;
  251. /**
  252. * 响应
  253. */
  254. bool _response;
  255. /**
  256. * 接口处理的返回值
  257. */
  258. int _ret;
  259. /**
  260. * 是否上报stat
  261. */
  262. bool _reportStat;
  263. /**
  264. * 设置额外返回的内容
  265. */
  266. map<std::string, std::string> _responseContext;
  267. /**
  268. * cookie
  269. */
  270. map<string, string> _cookie;
  271. };
  272. //////////////////////////////////////////////////////////////
  273. }
  274. #endif