AsyncHttpClient.h 3.6 KB

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