Transceiver.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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_TRANSCEIVER_H_
  17. #define __TARS_TRANSCEIVER_H_
  18. #include "servant/EndpointInfo.h"
  19. #include "servant/NetworkUtil.h"
  20. #include "servant/CommunicatorEpoll.h"
  21. #include "servant/AuthLogic.h"
  22. #include <list>
  23. #include <util/tc_network_buffer.h>
  24. using namespace std;
  25. namespace tars
  26. {
  27. class TC_OpenSSL;
  28. class AdapterProxy;
  29. class ProxyBase;
  30. /**
  31. * 网络传输基类,主要提供send/recv接口
  32. */
  33. class Transceiver
  34. {
  35. public:
  36. /*
  37. * 连接状态
  38. */
  39. enum ConnectStatus
  40. {
  41. eUnconnected,
  42. eConnecting,
  43. eConnected,
  44. };
  45. /*
  46. * 发数据的返回状态
  47. */
  48. enum ReturnStatus
  49. {
  50. eRetError = -1, //发送错误
  51. eRetOk = 0, //发送成功
  52. eRetFull = 1, //数据发送一半, 队列满了, 等事件过来继续发送
  53. eRetNotSend = 2, //连接还没有ok(ssl没有握手, 没有完成鉴权, 上一个数据包还没有发送完, 没有完成代理鉴权等), 数据没有发送
  54. };
  55. /**
  56. * 构造函数
  57. * @param ep
  58. * @param fd
  59. */
  60. Transceiver(AdapterProxy * pAdapterProxy,const EndpointInfo &ep);
  61. /**
  62. *
  63. *析构函数
  64. */
  65. virtual ~Transceiver();
  66. /**
  67. * 是否ssl
  68. */
  69. bool isSSL() const ;
  70. /*
  71. * 检查连接是否超时
  72. */
  73. void checkTimeout();
  74. /**
  75. * 重新创建连接
  76. */
  77. void reconnect();
  78. /**
  79. * 创建连接,初始化
  80. */
  81. void connect();
  82. /*
  83. * 关闭连接
  84. */
  85. virtual void close(bool destructor=false);
  86. /*
  87. * 设置当前连接态
  88. */
  89. void setConnected();
  90. /*
  91. * 往fd里面发送数据
  92. * 如果fd缓冲区已满,返回错误
  93. * 如果数据发送一半,缓冲区满了,返回成功
  94. */
  95. int sendRequest(const shared_ptr<TC_NetWorkBuffer::Buffer> &pData);
  96. /**
  97. * send buffer
  98. * @return
  99. */
  100. TC_NetWorkBuffer *getSendBuffer() { return &_sendBuffer; }
  101. /**
  102. * recv buffer
  103. * @return
  104. */
  105. TC_NetWorkBuffer *getRecvBuffer() { return &_recvBuffer; }
  106. /*
  107. * 处理请求,判断Send BufferCache是否有完整的包
  108. * @return int
  109. */
  110. virtual int doRequest();
  111. /*
  112. * 处理返回,判断Recv BufferCache是否有完整的包
  113. * @param done
  114. * @return int
  115. */
  116. virtual int doResponse() = 0;
  117. /*
  118. * 网络发送接口
  119. * @param buf
  120. * @param len
  121. * @param flag
  122. * @return int
  123. */
  124. virtual int send(const void* buf, uint32_t len, uint32_t flag) = 0;
  125. /*
  126. * 网络接收接口
  127. * @param buf
  128. * @param len
  129. * @param flag
  130. *
  131. * @return int
  132. */
  133. virtual int recv(void* buf, uint32_t len, uint32_t flag) = 0;
  134. /*
  135. * 获取文件描述符
  136. * @return int
  137. */
  138. virtual int fd() const
  139. {
  140. return _fd;
  141. }
  142. /*
  143. * 是否有效
  144. */
  145. bool isValid() const
  146. {
  147. return (_fd != -1);
  148. }
  149. /*
  150. * 获取端口信息
  151. */
  152. const EndpointInfo& getEndpointInfo() const
  153. {
  154. return _ep;
  155. }
  156. /*
  157. * 获取connect所属的adapter
  158. */
  159. AdapterProxy * getAdapterProxy()
  160. {
  161. return _adapterProxy;
  162. }
  163. /*
  164. * 判断是否已经连接到服务端
  165. */
  166. bool hasConnected()
  167. {
  168. return isValid() && (_connStatus == eConnected);
  169. }
  170. /*
  171. * 判断是否正在连接
  172. */
  173. bool isConnecting()
  174. {
  175. return isValid() && (_connStatus == eConnecting);
  176. }
  177. /*
  178. * 设置连接失败
  179. */
  180. void setConnectFailed()
  181. {
  182. _connStatus = eUnconnected;
  183. }
  184. void finishInvoke(shared_ptr<ResponsePacket> &rsp);
  185. /**
  186. * 设置鉴权状态
  187. */
  188. void setAuthState(AUTH_STATE newstate) { _authState = newstate; }
  189. /*
  190. * 获取鉴权状态
  191. */
  192. int getAuthState() const { return _authState; }
  193. /*
  194. * 发送鉴权数据
  195. */
  196. bool sendAuthData(const BasicAuthInfo& );
  197. protected:
  198. /*
  199. * 设置当前连接态
  200. */
  201. void onSetConnected();
  202. /**
  203. ** 物理连接成功回调
  204. **/
  205. void onConnect();
  206. /**
  207. ** 发送打通代理请求
  208. **/
  209. void connectProxy();
  210. /**
  211. * 检查是否代理创建成功
  212. * @param buff
  213. * @param length
  214. * @return <0: 失败, 0: 成功: 1: 需要验证
  215. */
  216. int doCheckProxy(const char *buff, size_t length);
  217. /**
  218. ** 鉴权初始化请求
  219. **/
  220. void doAuthReq();
  221. /*
  222. * AdapterProxy
  223. */
  224. AdapterProxy * _adapterProxy;
  225. /*
  226. * 连接的节点信息
  227. */
  228. EndpointInfo _ep;
  229. /*
  230. * 套接字
  231. */
  232. int _fd;
  233. /*
  234. * 事件注册信息
  235. */
  236. FDInfo _fdInfo;
  237. /*
  238. * 连接状态
  239. */
  240. ConnectStatus _connStatus;
  241. /*
  242. * 连接的超时时间
  243. */
  244. int64_t _conTimeoutTime;
  245. /*
  246. * 鉴权状态
  247. */
  248. AUTH_STATE _authState;
  249. protected:
  250. std::shared_ptr<TC_OpenSSL> _openssl;
  251. /*
  252. * 发送buffer
  253. */
  254. TC_NetWorkBuffer _sendBuffer;
  255. /*
  256. * 接收buffer
  257. */
  258. TC_NetWorkBuffer _recvBuffer;
  259. /**
  260. * 代理
  261. */
  262. ProxyBase* _proxyPointer = NULL;
  263. };
  264. //////////////////////////////////////////////////////////
  265. /**
  266. * TCP 传输实现
  267. */
  268. class TcpTransceiver : public Transceiver
  269. {
  270. public:
  271. /**
  272. * 构造函数
  273. * @param ep
  274. * @param fd
  275. */
  276. TcpTransceiver(AdapterProxy * pAdapterProxy, const EndpointInfo &ep);
  277. /**
  278. * TCP 发送实现
  279. * @param buf
  280. * @param len
  281. * @param flag
  282. * @return int
  283. */
  284. virtual int send(const void* buf, uint32_t len, uint32_t flag);
  285. /**
  286. * TCP 接收实现
  287. * @param buf
  288. * @param len
  289. * @param flag
  290. *
  291. * @return int
  292. */
  293. virtual int recv(void* buf, uint32_t len, uint32_t flag);
  294. /**
  295. * 处理返回,判断Recv BufferCache是否有完整的包
  296. * @param done
  297. * @return int, =1,表示有数据就包
  298. */
  299. virtual int doResponse();
  300. };
  301. //////////////////////////////////////////////////////////
  302. /**
  303. * UDP 传输实现
  304. */
  305. class UdpTransceiver : public Transceiver
  306. {
  307. public:
  308. enum
  309. {
  310. DEFAULT_RECV_BUFFERSIZE = 64*1024 /*缺省数据接收buffer的大小*/
  311. };
  312. /**
  313. * 构造函数
  314. */
  315. UdpTransceiver(AdapterProxy * pAdapterProxy, const EndpointInfo &ep);
  316. /**
  317. * 析构函数
  318. */
  319. ~UdpTransceiver();
  320. /**
  321. * UDP 发送实现
  322. * @param buf
  323. * @param len
  324. * @param flag
  325. * @return int
  326. */
  327. virtual int send(const void* buf, uint32_t len, uint32_t flag);
  328. /**
  329. * UDP 接收实现
  330. * @param buf
  331. * @param len
  332. * @param flag
  333. * @return int
  334. */
  335. virtual int recv(void* buf, uint32_t len, uint32_t flag);
  336. /**
  337. * 处理返回,判断Recv BufferCache是否有完整的包
  338. * @param done
  339. * @return int
  340. */
  341. virtual int doResponse();
  342. private:
  343. /*
  344. * 接收缓存
  345. */
  346. char* _pRecvBuffer;
  347. };
  348. //////////////////////////////////////////////////////////
  349. }
  350. #endif