1
0

AsyncHttpClient.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. int send(const HttpClientTaskPtr& task) {
  98. EventLoopThread::loop()->queueInLoop(std::bind(&AsyncHttpClient::sendInLoop, this, task));
  99. return 0;
  100. }
  101. protected:
  102. void sendInLoop(HttpClientTaskPtr task) {
  103. int err = doTask(task);
  104. if (err != 0 && task->cb) {
  105. task->cb(NULL);
  106. }
  107. }
  108. int doTask(const HttpClientTaskPtr& task);
  109. static int sendRequest(const SocketChannelPtr& channel);
  110. // channel
  111. const SocketChannelPtr& getChannel(int fd) {
  112. return channels[fd];
  113. // return fd < channels.capacity() ? channels[fd] : NULL;
  114. }
  115. const SocketChannelPtr& addChannel(hio_t* io) {
  116. auto channel = std::make_shared<SocketChannel>(io);
  117. channel->newContext<HttpClientContext>();
  118. int fd = channel->fd();
  119. channels[fd] = channel;
  120. return channels[fd];
  121. }
  122. void removeChannel(const SocketChannelPtr& channel) {
  123. channel->deleteContext<HttpClientContext>();
  124. int fd = channel->fd();
  125. channels.erase(fd);
  126. }
  127. private:
  128. // NOTE: just one loop thread, no need mutex.
  129. // fd => SocketChannelPtr
  130. std::map<int, SocketChannelPtr> channels;
  131. // peeraddr => ConnPool
  132. std::map<std::string, ConnPool<int>> conn_pools;
  133. };
  134. }
  135. #endif // HV_ASYNC_HTTP_CLIENT_H_