Bläddra i källkod

#228: export class AsyncHttpClient

ithewei 3 år sedan
förälder
incheckning
9bb639240d
5 ändrade filer med 17 tillägg och 12 borttagningar
  1. 1 0
      Makefile.vars
  2. 1 0
      cmake/vars.cmake
  3. 4 0
      evpp/Channel.h
  4. 2 2
      http/client/AsyncHttpClient.cpp
  5. 9 10
      http/client/AsyncHttpClient.h

+ 1 - 0
Makefile.vars

@@ -81,6 +81,7 @@ HTTP2_HEADERS = http/http2def.h\
 HTTP_CLIENT_HEADERS =   http/client/http_client.h\
 						http/client/requests.h\
 						http/client/axios.h\
+						http/client/AsyncHttpClient.h\
 						http/client/WebSocketClient.h\
 
 HTTP_SERVER_HEADERS =   http/server/HttpServer.h\

+ 1 - 0
cmake/vars.cmake

@@ -93,6 +93,7 @@ set(HTTP_CLIENT_HEADERS
     http/client/http_client.h
     http/client/requests.h
     http/client/axios.h
+    http/client/AsyncHttpClient.h
     http/client/WebSocketClient.h)
 
 set(HTTP_SERVER_HEADERS

+ 4 - 0
evpp/Channel.h

@@ -42,6 +42,10 @@ public:
 
     virtual ~Channel() {
         close();
+        // NOTE: Detach after destructor to avoid triggering onclose
+        if (io_ && id_ == hio_id(io_)) {
+            hio_set_context(io_, NULL);
+        }
     }
 
     hio_t*      io() { return io_; }

+ 2 - 2
http/client/AsyncHttpClient.cpp

@@ -8,7 +8,7 @@ namespace hv {
 int AsyncHttpClient::doTask(const HttpClientTaskPtr& task) {
     const HttpRequestPtr& req = task->req;
     // queueInLoop timeout?
-    uint64_t now_hrtime = hloop_now_hrtime(loop_thread.hloop());
+    uint64_t now_hrtime = hloop_now_hrtime(EventLoopThread::hloop());
     int elapsed_ms = (now_hrtime - task->start_time) / 1000;
     int timeout_ms = req->timeout * 1000;
     if (timeout_ms > 0 && elapsed_ms >= timeout_ms) {
@@ -43,7 +43,7 @@ int AsyncHttpClient::doTask(const HttpClientTaskPtr& task) {
             perror("socket");
             return -30;
         }
-        hio_t* connio = hio_get(loop_thread.hloop(), connfd);
+        hio_t* connio = hio_get(EventLoopThread::hloop(), connfd);
         assert(connio != NULL);
         hio_set_peeraddr(connio, &peeraddr.sa, sockaddr_len(&peeraddr));
         addChannel(connio);

+ 9 - 10
http/client/AsyncHttpClient.h

@@ -1,6 +1,7 @@
 #ifndef HV_ASYNC_HTTP_CLIENT_H_
 #define HV_ASYNC_HTTP_CLIENT_H_
 
+#include <map>
 #include <list>
 
 #include "EventLoopThread.h"
@@ -9,8 +10,6 @@
 #include "HttpMessage.h"
 #include "HttpParser.h"
 
-// async => keepalive => connect_pool
-
 namespace hv {
 
 template<typename Conn>
@@ -96,14 +95,15 @@ struct HttpClientContext {
     }
 };
 
-class AsyncHttpClient {
+class HV_EXPORT AsyncHttpClient : private EventLoopThread {
 public:
-    AsyncHttpClient() {
-        loop_thread.start(true);
+    AsyncHttpClient(EventLoopPtr loop = NULL) : EventLoopThread(loop) {
+        if (loop == NULL) {
+            EventLoopThread::start(true);
+        }
     }
     ~AsyncHttpClient() {
-        // NOTE: ~EventLoopThread will stop and join
-        // loop_thread.stop(true);
+        EventLoopThread::stop(true);
     }
 
     // thread-safe
@@ -111,7 +111,7 @@ public:
         HttpClientTaskPtr task(new HttpClientTask);
         task->req = req;
         task->cb = std::move(resp_cb);
-        task->start_time = hloop_now_hrtime(loop_thread.hloop());
+        task->start_time = hloop_now_hrtime(EventLoopThread::hloop());
         if (req->retry_count > 0 && req->retry_delay > 0) {
             req->retry_count = MIN(req->retry_count, req->timeout * 1000 / req->retry_delay - 1);
         }
@@ -119,7 +119,7 @@ public:
     }
 
     int send(const HttpClientTaskPtr& task) {
-        loop_thread.loop()->queueInLoop(std::bind(&AsyncHttpClient::sendInLoop, this, task));
+        EventLoopThread::loop()->queueInLoop(std::bind(&AsyncHttpClient::sendInLoop, this, task));
         return 0;
     }
 
@@ -160,7 +160,6 @@ private:
     std::map<int, SocketChannelPtr>         channels;
     // peeraddr => ConnPool
     std::map<std::string, ConnPool<int>>    conn_pools;
-    EventLoopThread                         loop_thread;
 };
 
 }