AsyncHttpClient.h 3.7 KB

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