test_server.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // Created by jarod on 2021/6/7.
  3. //
  4. #ifndef TARS_CPP_TEST_SERVER_H
  5. #define TARS_CPP_TEST_SERVER_H
  6. #include "util/tc_network_buffer.h"
  7. #include "util/tc_epoll_server.h"
  8. #include "util/tc_openssl.h"
  9. #include "certs.h"
  10. using namespace tars;
  11. class MyTcpServer;
  12. static TC_NetWorkBuffer::PACKET_TYPE parseLine(TC_NetWorkBuffer&in, vector<char> &out)
  13. {
  14. try
  15. {
  16. if(in.empty())
  17. {
  18. return TC_NetWorkBuffer::PACKET_LESS;
  19. }
  20. const char *data = in.mergeBuffers();
  21. const char *p = TC_Port::strnstr(data, "\r\n", in.getBufferLength());
  22. if(NULL == p)
  23. {
  24. return TC_NetWorkBuffer::PACKET_LESS;
  25. }
  26. size_t length = p - data;
  27. out.resize(length+2);
  28. memcpy(&out[0], data, length+2);
  29. in.moveHeader(length + 2);
  30. return TC_NetWorkBuffer::PACKET_FULL;
  31. }
  32. catch (exception &ex)
  33. {
  34. return TC_NetWorkBuffer::PACKET_ERR;
  35. }
  36. return TC_NetWorkBuffer::PACKET_LESS; //表示收到的包不完全
  37. }
  38. /**
  39. * 处理类, 每个处理线程一个对象
  40. */
  41. class TcpHandle : public TC_EpollServer::Handle
  42. {
  43. public:
  44. /**
  45. * 初始化
  46. */
  47. virtual void initialize()
  48. {
  49. _server = (MyTcpServer*)(getEpollServer());
  50. }
  51. /**
  52. *
  53. */
  54. virtual void handle(const shared_ptr<TC_EpollServer::RecvContext> &data)
  55. {
  56. try
  57. {
  58. shared_ptr<TC_EpollServer::SendContext> send = data->createSendContext();
  59. // LOG_CONSOLE_DEBUG << TC_Thread::CURRENT_THREADID() << ", handle:" << string(data->buffer().data(), data->buffer().size()) << endl;
  60. send->buffer()->setBuffer(data->buffer());
  61. sendResponse(send);
  62. }
  63. catch (exception &ex)
  64. {
  65. close(data);
  66. }
  67. }
  68. virtual void handleClose(const shared_ptr<TC_EpollServer::RecvContext> &data)
  69. {
  70. // LOG_CONSOLE_DEBUG << "use count:" << data.use_count() << endl;
  71. }
  72. protected:
  73. MyTcpServer *_server;
  74. };
  75. /**
  76. * 处理类, 每个处理线程一个对象
  77. */
  78. class TcpQueueHandle : public TC_EpollServer::Handle
  79. {
  80. public:
  81. /**
  82. * 初始化
  83. */
  84. virtual void initialize()
  85. {
  86. _server = (MyTcpServer*)(getEpollServer());
  87. }
  88. /**
  89. *
  90. */
  91. virtual void handle(const shared_ptr<TC_EpollServer::RecvContext> &data)
  92. {
  93. try
  94. {
  95. shared_ptr<TC_EpollServer::SendContext> send = data->createSendContext();
  96. string buff(data->buffer().data(), data->buffer().size());
  97. buff += "-" + TC_Common::tostr(TC_Thread::CURRENT_THREADID());
  98. send->buffer()->setBuffer(buff);
  99. sendResponse(send);
  100. }
  101. catch (exception &ex)
  102. {
  103. close(data);
  104. }
  105. }
  106. protected:
  107. MyTcpServer *_server;
  108. };
  109. class MyTcpServer
  110. {
  111. public:
  112. MyTcpServer()
  113. {
  114. _epollServer = new TC_EpollServer();
  115. };
  116. void initialize()
  117. {
  118. }
  119. #if TARS_SSL
  120. void bindSSL(const std::string &str, int maxConnections = 10240)
  121. {
  122. TC_EpollServer::BindAdapterPtr lsPtr = _epollServer->createBindAdapter<TcpHandle>("SSLAdapter", str, 5);
  123. //设置最大连接数
  124. lsPtr->setMaxConns(maxConnections);
  125. //设置协议解析器
  126. lsPtr->setProtocol(TC_NetWorkBuffer::parseEcho);
  127. bool verifyClient = false;
  128. string ciphers = "";
  129. shared_ptr<TC_OpenSSL::CTX> ctx = TC_OpenSSL::newCtx(CA, SERVER_CERT, SERVER_KEY, verifyClient, ciphers);
  130. assert(ctx);
  131. lsPtr->setSSLCtx(ctx);
  132. //绑定对象
  133. _epollServer->bind(lsPtr);
  134. }
  135. #endif
  136. void bindUdp(const std::string &str, int maxConnections = 10240)
  137. {
  138. TC_EpollServer::BindAdapterPtr lsPtr = _epollServer->createBindAdapter<TcpHandle>("UdpAdapter", str, 2);
  139. //设置最大连接数
  140. lsPtr->setMaxConns(maxConnections);
  141. //设置协议解析器
  142. lsPtr->setProtocol(TC_NetWorkBuffer::parseEcho);
  143. //绑定对象
  144. _epollServer->bind(lsPtr);
  145. }
  146. void bindManualTcp(const std::string &str, int maxConnections = 10240)
  147. {
  148. TC_EpollServer::BindAdapterPtr lsPtr = _epollServer->createBindAdapter<TcpHandle>("TcpManualAdapter", str, 5);
  149. //设置最大连接数
  150. lsPtr->setMaxConns(maxConnections);
  151. //设置协议解析器
  152. lsPtr->setProtocol(TC_NetWorkBuffer::parseEcho);
  153. lsPtr->enableManualListen();
  154. //绑定对象
  155. _epollServer->bind(lsPtr);
  156. }
  157. void startManualListen()
  158. {
  159. _epollServer->getBindAdapter("TcpManualAdapter")->manualListen();
  160. }
  161. void cancelManualListen()
  162. {
  163. _epollServer->getBindAdapter("TcpManualAdapter")->cancelListen();
  164. }
  165. void bindTcp(const std::string &str, int maxConnections = 10240)
  166. {
  167. TC_EpollServer::BindAdapterPtr lsPtr = _epollServer->createBindAdapter<TcpHandle>("TcpAdapter", str, 5);
  168. //设置最大连接数
  169. lsPtr->setMaxConns(maxConnections);
  170. //设置协议解析器
  171. lsPtr->setProtocol(TC_NetWorkBuffer::parseEcho);
  172. //绑定对象
  173. _epollServer->bind(lsPtr);
  174. }
  175. void bindTcpLine(const std::string &str, int maxConnections = 10240)
  176. {
  177. TC_EpollServer::BindAdapterPtr lsPtr = _epollServer->createBindAdapter<TcpHandle>("TcpLineAdapter", str, 5);
  178. //设置最大连接数
  179. lsPtr->setMaxConns(maxConnections);
  180. //设置协议解析器
  181. lsPtr->setProtocol(parseLine);
  182. //绑定对象
  183. _epollServer->bind(lsPtr);
  184. }
  185. void bindTcpQueue(const std::string &str)
  186. {
  187. TC_EpollServer::BindAdapterPtr lsPtr = _epollServer->createBindAdapter<TcpQueueHandle>("TcpQueueAdapter", str, 5);
  188. lsPtr->enableQueueMode();
  189. //设置最大连接数
  190. lsPtr->setMaxConns(10240);
  191. //设置协议解析器
  192. lsPtr->setProtocol(TC_NetWorkBuffer::parseEcho);
  193. //绑定对象
  194. _epollServer->bind(lsPtr);
  195. }
  196. void waitForShutdown()
  197. {
  198. _start = new std::thread([=]{
  199. _epollServer->waitForShutdown();
  200. });
  201. TC_Common::msleep(100);
  202. }
  203. void terminate()
  204. {
  205. _epollServer->terminate();
  206. _start->join();
  207. delete _start;
  208. delete _epollServer;
  209. }
  210. std::thread *_start;
  211. TC_EpollServer *_epollServer;
  212. };
  213. class MyHttpServer;
  214. /**
  215. * 处理类, 每个处理线程一个对象
  216. */
  217. class HttpHandle : public TC_EpollServer::Handle
  218. {
  219. public:
  220. /**
  221. * 初始化
  222. */
  223. virtual void initialize()
  224. {
  225. _server = (MyHttpServer*)(getEpollServer());
  226. }
  227. /**
  228. *
  229. */
  230. virtual void handle(const shared_ptr<TC_EpollServer::RecvContext> &data)
  231. {
  232. try
  233. {
  234. TC_HttpRequest request;
  235. request.decode(data->buffer().data(), data->buffer().size());
  236. // cout << "-------------------------------------------------------" << endl;
  237. // cout << "request: " << string(data->buffer().data(), data->buffer().size()) << endl;
  238. // cout << "-------------------------------------------------------" << endl;
  239. TC_HttpResponse response;
  240. response.setConnection("close");
  241. response.setResponse(200, "OK", request.getContent());
  242. string buffer = response.encode();
  243. shared_ptr<TC_EpollServer::SendContext> send = data->createSendContext();
  244. send->buffer()->setBuffer(buffer.c_str(), buffer.size());
  245. sendResponse(send);
  246. }
  247. catch (exception &ex)
  248. {
  249. close(data);
  250. }
  251. }
  252. protected:
  253. MyHttpServer *_server;
  254. };
  255. class MyHttpServer
  256. {
  257. public:
  258. MyHttpServer()
  259. {
  260. _epollServer = new TC_EpollServer();
  261. };
  262. void initialize()
  263. {
  264. }
  265. void bindHttp(const std::string &str)
  266. {
  267. TC_EpollServer::BindAdapterPtr lsPtr = _epollServer->createBindAdapter<HttpHandle>("HttpAdapter", str, 5); //new TC_EpollServer::BindAdapter(_epollServer);
  268. //设置最大连接数
  269. lsPtr->setMaxConns(1024);
  270. //设置协议解析器
  271. lsPtr->setProtocol(TC_NetWorkBuffer::parseHttp);
  272. //绑定对象
  273. _epollServer->bind(lsPtr);
  274. }
  275. #if TARS_SSL
  276. void bindHttpSSL(const std::string &str)
  277. {
  278. TC_EpollServer::BindAdapterPtr lsPtr = _epollServer->createBindAdapter<HttpHandle>("HttpsAdapter", str, 5);
  279. //设置最大连接数
  280. lsPtr->setMaxConns(1024);
  281. //设置协议解析器
  282. lsPtr->setProtocol(TC_NetWorkBuffer::parseHttp);
  283. bool verifyClient = false;
  284. string ciphers = "";
  285. shared_ptr<TC_OpenSSL::CTX> ctx = TC_OpenSSL::newCtx(CA, SERVER_CERT, SERVER_KEY, verifyClient, ciphers);
  286. lsPtr->setSSLCtx(ctx);
  287. //绑定对象
  288. _epollServer->bind(lsPtr);
  289. }
  290. void bindHttpSSLVerify(const std::string &str)
  291. {
  292. TC_EpollServer::BindAdapterPtr lsPtr = _epollServer->createBindAdapter<HttpHandle>("HttpsVerifyAdapter", str, 5);
  293. //设置最大连接数
  294. lsPtr->setMaxConns(1024);
  295. //设置协议解析器
  296. lsPtr->setProtocol(TC_NetWorkBuffer::parseHttp);
  297. bool verifyClient = true;
  298. string ciphers = "";
  299. shared_ptr<TC_OpenSSL::CTX> ctx = TC_OpenSSL::newCtx(CLIENT_CERT, SERVER_CERT, SERVER_KEY, verifyClient, ciphers);
  300. lsPtr->setSSLCtx(ctx);
  301. //绑定对象
  302. _epollServer->bind(lsPtr);
  303. }
  304. #endif
  305. void waitForShutdown()
  306. {
  307. _start = new std::thread([=]{
  308. _epollServer->waitForShutdown();
  309. });
  310. }
  311. void terminate()
  312. {
  313. _epollServer->terminate();
  314. _start->join();
  315. delete _start;
  316. }
  317. std::thread *_start;
  318. TC_EpollServer *_epollServer;
  319. };
  320. #endif //TARS_CPP_TEST_SERVER_H