AsyncHttpClient.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef HV_ASYNC_HTTP_CLIENT_H_
  2. #define HV_ASYNC_HTTP_CLIENT_H_
  3. #include <map>
  4. #include <list>
  5. #include "EventLoopThread.h"
  6. #include "Channel.h"
  7. #include "HttpMessage.h"
  8. #include "HttpParser.h"
  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. cancelTimer();
  57. }
  58. void cancelTimer() {
  59. if (timerID != INVALID_TIMER_ID) {
  60. killTimer(timerID);
  61. timerID = INVALID_TIMER_ID;
  62. }
  63. }
  64. void cancelTask() {
  65. cancelTimer();
  66. task = NULL;
  67. }
  68. void callback() {
  69. cancelTimer();
  70. if (task && task->cb) {
  71. task->cb(resp);
  72. }
  73. // NOTE: task done
  74. task = NULL;
  75. }
  76. void successCallback() {
  77. callback();
  78. resp = NULL;
  79. }
  80. void errorCallback() {
  81. resp = NULL;
  82. callback();
  83. }
  84. };
  85. class HV_EXPORT AsyncHttpClient : private EventLoopThread {
  86. public:
  87. AsyncHttpClient(EventLoopPtr loop = NULL) : EventLoopThread(loop) {
  88. if (loop == NULL) {
  89. EventLoopThread::start(true);
  90. }
  91. }
  92. ~AsyncHttpClient() {
  93. EventLoopThread::stop(true);
  94. }
  95. // thread-safe
  96. int send(const HttpRequestPtr& req, HttpResponseCallback resp_cb) {
  97. auto task = std::make_shared<HttpClientTask>();
  98. task->req = req;
  99. task->cb = std::move(resp_cb);
  100. task->start_time = hloop_now_hrtime(EventLoopThread::hloop());
  101. if (req->retry_count > 0 && req->retry_delay > 0) {
  102. req->retry_count = MIN(req->retry_count, req->timeout * 1000 / req->retry_delay - 1);
  103. }
  104. return send(task);
  105. }
  106. int send(const HttpClientTaskPtr& task) {
  107. EventLoopThread::loop()->queueInLoop(std::bind(&AsyncHttpClient::sendInLoop, this, task));
  108. return 0;
  109. }
  110. protected:
  111. void sendInLoop(HttpClientTaskPtr task) {
  112. int err = doTask(task);
  113. if (err != 0 && task->cb) {
  114. task->cb(NULL);
  115. }
  116. }
  117. int doTask(const HttpClientTaskPtr& task);
  118. static int sendRequest(const SocketChannelPtr& channel);
  119. // channel
  120. const SocketChannelPtr& getChannel(int fd) {
  121. return channels[fd];
  122. // return fd < channels.capacity() ? channels[fd] : NULL;
  123. }
  124. const SocketChannelPtr& addChannel(hio_t* io) {
  125. auto channel = std::make_shared<SocketChannel>(io);
  126. channel->newContext<HttpClientContext>();
  127. int fd = channel->fd();
  128. channels[fd] = channel;
  129. return channels[fd];
  130. }
  131. void removeChannel(const SocketChannelPtr& channel) {
  132. channel->deleteContext<HttpClientContext>();
  133. int fd = channel->fd();
  134. channels.erase(fd);
  135. }
  136. private:
  137. // NOTE: just one loop thread, no need mutex.
  138. // fd => SocketChannelPtr
  139. std::map<int, SocketChannelPtr> channels;
  140. // peeraddr => ConnPool
  141. std::map<std::string, ConnPool<int>> conn_pools;
  142. };
  143. }
  144. #endif // HV_ASYNC_HTTP_CLIENT_H_