Selaa lähdekoodia

ensure thread-safe

ithewei 2 vuotta sitten
vanhempi
commit
bb2fae9094
3 muutettua tiedostoa jossa 15 lisäystä ja 12 poistoa
  1. 1 1
      evpp/EventLoop.h
  2. 13 0
      http/client/AsyncHttpClient.cpp
  3. 1 11
      http/client/AsyncHttpClient.h

+ 1 - 1
evpp/EventLoop.h

@@ -88,8 +88,8 @@ public:
 
     // Timer interfaces: setTimer, killTimer, resetTimer
     TimerID setTimer(int timeout_ms, TimerCallback cb, uint32_t repeat = INFINITE, TimerID timerID = INVALID_TIMER_ID) {
-        assertInLoopThread();
         if (loop_ == NULL) return INVALID_TIMER_ID;
+        assertInLoopThread();
         htimer_t* htimer = htimer_add(loop_, onTimer, timeout_ms, repeat);
         assert(htimer != NULL);
         if (timerID == INVALID_TIMER_ID) {

+ 13 - 0
http/client/AsyncHttpClient.cpp

@@ -2,6 +2,19 @@
 
 namespace hv {
 
+int AsyncHttpClient::send(const HttpRequestPtr& req, HttpResponseCallback resp_cb) {
+    hloop_t* loop = EventLoopThread::hloop();
+    if (loop == NULL) return -1;
+    auto task = std::make_shared<HttpClientTask>();
+    task->req = req;
+    task->cb = std::move(resp_cb);
+    task->start_time = hloop_now_hrtime(loop);
+    if (req->retry_count > 0 && req->retry_delay > 0) {
+        req->retry_count = MIN(req->retry_count, req->timeout * 1000 / req->retry_delay - 1);
+    }
+    return send(task);
+}
+
 // createsocket => startConnect =>
 // onconnect => sendRequest => startRead =>
 // onread => HttpParser => resp_cb

+ 1 - 11
http/client/AsyncHttpClient.h

@@ -114,17 +114,7 @@ public:
     }
 
     // thread-safe
-    int send(const HttpRequestPtr& req, HttpResponseCallback resp_cb) {
-        auto task = std::make_shared<HttpClientTask>();
-        task->req = req;
-        task->cb = std::move(resp_cb);
-        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);
-        }
-        return send(task);
-    }
-
+    int send(const HttpRequestPtr& req, HttpResponseCallback resp_cb);
     int send(const HttpClientTaskPtr& task) {
         EventLoopThread::loop()->queueInLoop(std::bind(&AsyncHttpClient::sendInLoop, this, task));
         return 0;