AsyncHttpClient.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. };
  46. typedef std::shared_ptr<HttpClientTask> HttpClientTaskPtr;
  47. struct HttpClientContext {
  48. HttpClientTaskPtr task;
  49. HttpResponsePtr resp;
  50. HttpParserPtr parser;
  51. TimerID timerID;
  52. HttpClientContext() {
  53. timerID = INVALID_TIMER_ID;
  54. }
  55. ~HttpClientContext() {
  56. if (timerID != INVALID_TIMER_ID) {
  57. killTimer(timerID);
  58. timerID = INVALID_TIMER_ID;
  59. }
  60. }
  61. void callback() {
  62. if (timerID != INVALID_TIMER_ID) {
  63. killTimer(timerID);
  64. timerID = INVALID_TIMER_ID;
  65. }
  66. if (task && task->cb) {
  67. task->cb(resp);
  68. }
  69. // NOTE: task done
  70. task = NULL;
  71. }
  72. void successCallback() {
  73. callback();
  74. resp = NULL;
  75. }
  76. void errorCallback() {
  77. resp = NULL;
  78. callback();
  79. }
  80. };
  81. class AsyncHttpClient {
  82. public:
  83. AsyncHttpClient() {
  84. loop_thread.start(true);
  85. }
  86. ~AsyncHttpClient() {
  87. // NOTE: ~EventLoopThread will stop and join
  88. // loop_thread.stop(true);
  89. }
  90. // thread-safe
  91. int send(const HttpRequestPtr& req, HttpResponseCallback resp_cb) {
  92. HttpClientTaskPtr task(new HttpClientTask);
  93. task->req = req;
  94. task->cb = std::move(resp_cb);
  95. task->start_time = hloop_now_hrtime(loop_thread.hloop());
  96. if (req->retry_count > 0 && req->retry_delay > 0) {
  97. req->retry_count = MIN(req->retry_count, req->timeout * 1000 / req->retry_delay - 1);
  98. }
  99. return send(task);
  100. }
  101. int send(const HttpClientTaskPtr& task) {
  102. loop_thread.loop()->queueInLoop(std::bind(&AsyncHttpClient::sendInLoop, this, task));
  103. return 0;
  104. }
  105. protected:
  106. void sendInLoop(HttpClientTaskPtr task) {
  107. int err = doTask(task);
  108. if (err != 0 && task->cb) {
  109. task->cb(NULL);
  110. }
  111. }
  112. int doTask(const HttpClientTaskPtr& task);
  113. static int sendRequest(const SocketChannelPtr& channel);
  114. // channel
  115. const SocketChannelPtr& getChannel(int fd) {
  116. return channels[fd];
  117. // return fd < channels.capacity() ? channels[fd] : NULL;
  118. }
  119. const SocketChannelPtr& addChannel(hio_t* io) {
  120. SocketChannelPtr channel(new SocketChannel(io));
  121. channel->newContext<HttpClientContext>();
  122. int fd = channel->fd();
  123. channels[fd] = channel;
  124. return channels[fd];
  125. }
  126. void removeChannel(const SocketChannelPtr& channel) {
  127. channel->deleteContext<HttpClientContext>();
  128. int fd = channel->fd();
  129. channels.erase(fd);
  130. }
  131. private:
  132. // NOTE: just one loop thread, no need mutex.
  133. // fd => SocketChannelPtr
  134. std::map<int, SocketChannelPtr> channels;
  135. // peeraddr => ConnPool
  136. std::map<std::string, ConnPool<int>> conn_pools;
  137. EventLoopThread loop_thread;
  138. };
  139. }
  140. #endif // HV_ASYNC_HTTP_CLIENT_H_