AsyncHttpClient.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #ifndef HV_ASYNC_HTTP_CLIENT_H_
  2. #define HV_ASYNC_HTTP_CLIENT_H_
  3. #include <list>
  4. #include "EventLoopThread.h"
  5. #include "Channel.h"
  6. #include "HttpMessage.h"
  7. #include "HttpParser.h"
  8. // async => keepalive => connect_pool
  9. namespace hv {
  10. template<typename Conn>
  11. class ConnPool {
  12. public:
  13. int size() {
  14. return conns_.size();
  15. }
  16. bool get(Conn& conn) {
  17. if (conns_.empty()) return false;
  18. conn = conns_.front();
  19. conns_.pop_front();
  20. return true;
  21. }
  22. bool add(const Conn& conn) {
  23. conns_.push_back(conn);
  24. return true;
  25. }
  26. bool remove(const Conn& conn) {
  27. auto iter = conns_.begin();
  28. while (iter != conns_.end()) {
  29. if (*iter == conn) {
  30. iter = conns_.erase(iter);
  31. return true;
  32. } else {
  33. ++iter;
  34. }
  35. }
  36. return false;
  37. }
  38. private:
  39. std::list<Conn> conns_;
  40. };
  41. struct HttpClientTask {
  42. HttpRequestPtr req;
  43. HttpResponseCallback cb;
  44. uint64_t start_time;
  45. int retry_cnt;
  46. };
  47. typedef std::shared_ptr<HttpClientTask> HttpClientTaskPtr;
  48. struct HttpClientContext {
  49. HttpRequestPtr req;
  50. HttpResponseCallback cb;
  51. SocketChannelPtr channel;
  52. HttpResponsePtr resp;
  53. HttpParserPtr parser;
  54. TimerID timerID;
  55. HttpClientContext() {
  56. timerID = INVALID_TIMER_ID;
  57. }
  58. void callback() {
  59. if (timerID != INVALID_TIMER_ID) {
  60. killTimer(timerID);
  61. timerID = INVALID_TIMER_ID;
  62. }
  63. if (cb) {
  64. cb(resp);
  65. // NOTE: ensure cb just call once
  66. cb = NULL;
  67. }
  68. }
  69. void successCallback() {
  70. callback();
  71. resp = NULL;
  72. }
  73. void errorCallback() {
  74. resp = NULL;
  75. callback();
  76. }
  77. };
  78. typedef std::shared_ptr<HttpClientContext> HttpClientContextPtr;
  79. class AsyncHttpClient {
  80. public:
  81. AsyncHttpClient() {
  82. loop_thread.start(true);
  83. }
  84. ~AsyncHttpClient() {
  85. loop_thread.stop(true);
  86. }
  87. // thread-safe
  88. int send(const HttpRequestPtr& req, HttpResponseCallback resp_cb) {
  89. HttpClientTaskPtr task(new HttpClientTask);
  90. task->req = req;
  91. task->cb = resp_cb;
  92. task->start_time = hloop_now_hrtime(loop_thread.hloop());
  93. task->retry_cnt = 3;
  94. return send(task);
  95. }
  96. int send(const HttpClientTaskPtr& task) {
  97. loop_thread.loop()->queueInLoop(std::bind(&AsyncHttpClient::sendInLoop, this, task));
  98. return 0;
  99. }
  100. protected:
  101. void sendInLoop(const HttpClientTaskPtr& task) {
  102. int err = doTask(task);
  103. if (err != 0 && task->cb) {
  104. task->cb(NULL);
  105. }
  106. }
  107. // createsocket => startConnect =>
  108. // onconnect => sendRequest => startRead =>
  109. // onread => HttpParser => resp_cb
  110. int doTask(const HttpClientTaskPtr& task);
  111. // InitResponse => SubmitRequest => while(GetSendData) write => startRead
  112. static void onconnect(hio_t* io);
  113. static int sendRequest(const HttpClientContextPtr ctx);
  114. HttpClientContextPtr getContext(int fd) {
  115. return fd < client_ctxs.capacity() ? client_ctxs[fd] : NULL;
  116. }
  117. void addContext(const HttpClientContextPtr& ctx) {
  118. int fd = ctx->channel->fd();
  119. if (fd >= client_ctxs.capacity()) {
  120. client_ctxs.resize(2 * fd);
  121. }
  122. client_ctxs[fd] = ctx;
  123. // NOTE: add into conn_pools after recv response completed
  124. // conn_pools[ctx->channel->peeraddr()].add(fd);
  125. }
  126. void removeContext(const HttpClientContextPtr& ctx) {
  127. int fd = ctx->channel->fd();
  128. // NOTE: remove from conn_pools
  129. auto iter = conn_pools.find(ctx->channel->peeraddr());
  130. if (iter != conn_pools.end()) {
  131. iter->second.remove(fd);
  132. }
  133. if (fd < client_ctxs.capacity()) {
  134. client_ctxs[fd] = NULL;
  135. }
  136. }
  137. private:
  138. EventLoopThread loop_thread;
  139. // NOTE: just one loop thread, no need mutex.
  140. // with fd as index
  141. std::vector<HttpClientContextPtr> client_ctxs;
  142. // peeraddr => ConnPool
  143. std::map<std::string, ConnPool<int>> conn_pools;
  144. };
  145. }
  146. #endif // HV_ASYNC_HTTP_CLIENT_H_