1
0

AsyncHttpClient.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. 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 HV_EXPORT AsyncHttpClient : private EventLoopThread {
  82. public:
  83. AsyncHttpClient(EventLoopPtr loop = NULL) : EventLoopThread(loop) {
  84. if (loop == NULL) {
  85. EventLoopThread::start(true);
  86. }
  87. }
  88. ~AsyncHttpClient() {
  89. EventLoopThread::stop(true);
  90. }
  91. // thread-safe
  92. int send(const HttpRequestPtr& req, HttpResponseCallback resp_cb) {
  93. HttpClientTaskPtr task(new HttpClientTask);
  94. task->req = req;
  95. task->cb = std::move(resp_cb);
  96. task->start_time = hloop_now_hrtime(EventLoopThread::hloop());
  97. if (req->retry_count > 0 && req->retry_delay > 0) {
  98. req->retry_count = MIN(req->retry_count, req->timeout * 1000 / req->retry_delay - 1);
  99. }
  100. return send(task);
  101. }
  102. int send(const HttpClientTaskPtr& task) {
  103. EventLoopThread::loop()->queueInLoop(std::bind(&AsyncHttpClient::sendInLoop, this, task));
  104. return 0;
  105. }
  106. protected:
  107. void sendInLoop(HttpClientTaskPtr task) {
  108. int err = doTask(task);
  109. if (err != 0 && task->cb) {
  110. task->cb(NULL);
  111. }
  112. }
  113. int doTask(const HttpClientTaskPtr& task);
  114. static int sendRequest(const SocketChannelPtr& channel);
  115. // channel
  116. const SocketChannelPtr& getChannel(int fd) {
  117. return channels[fd];
  118. // return fd < channels.capacity() ? channels[fd] : NULL;
  119. }
  120. const SocketChannelPtr& addChannel(hio_t* io) {
  121. SocketChannelPtr channel(new SocketChannel(io));
  122. channel->newContext<HttpClientContext>();
  123. int fd = channel->fd();
  124. channels[fd] = channel;
  125. return channels[fd];
  126. }
  127. void removeChannel(const SocketChannelPtr& channel) {
  128. channel->deleteContext<HttpClientContext>();
  129. int fd = channel->fd();
  130. channels.erase(fd);
  131. }
  132. private:
  133. // NOTE: just one loop thread, no need mutex.
  134. // fd => SocketChannelPtr
  135. std::map<int, SocketChannelPtr> channels;
  136. // peeraddr => ConnPool
  137. std::map<std::string, ConnPool<int>> conn_pools;
  138. };
  139. }
  140. #endif // HV_ASYNC_HTTP_CLIENT_H_