| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
#include "fetch.h" |
| 8 |
#include "cloudpinyin.h" |
| 9 |
#include <cstdint> |
| 10 |
#include <curl/curl.h> |
| 11 |
#include <curl/multi.h> |
| 12 |
#include <fcitx-utils/event.h> |
| 13 |
#include <fcitx-utils/eventdispatcher.h> |
| 14 |
#include <fcitx-utils/eventloopinterface.h> |
| 15 |
#include <fcitx-utils/fs.h> |
| 16 |
#include <fcitx-utils/macros.h> |
| 17 |
#include <fcntl.h> |
| 18 |
#include <memory> |
| 19 |
#include <mutex> |
| 20 |
#include <thread> |
| 21 |
#include <unistd.h> |
| 22 |
|
| 23 |
using namespace fcitx; |
| 24 |
|
| 25 |
FetchThread::FetchThread(CloudPinyin *cloudPinyin) : cloudPinyin_(cloudPinyin) { |
| 26 |
curlm_ = curl_multi_init(); |
| 27 |
curl_multi_setopt(curlm_, CURLMOPT_MAXCONNECTS, MAX_HANDLE); |
| 28 |
curl_multi_setopt(curlm_, CURLMOPT_SOCKETFUNCTION, |
| 29 |
&FetchThread::curlCallback); |
| 30 |
curl_multi_setopt(curlm_, CURLMOPT_SOCKETDATA, this); |
| 31 |
curl_multi_setopt(curlm_, CURLMOPT_TIMERFUNCTION, |
| 32 |
&FetchThread::curlTimerCallback); |
| 33 |
curl_multi_setopt(curlm_, CURLMOPT_TIMERDATA, this); |
| 34 |
|
| 35 |
thread_ = std::make_unique<std::thread>(&FetchThread::runThread, this); |
| 36 |
} |
| 37 |
|
| 38 |
FetchThread::~FetchThread() { |
| 39 |
exit(); |
| 40 |
thread_->join(); |
| 41 |
|
| 42 |
while (!workingQueue.empty()) { |
| 43 |
auto *queue = &workingQueue.front(); |
| 44 |
workingQueue.pop_front(); |
| 45 |
curl_multi_remove_handle(curlm_, queue->curl()); |
| 46 |
queue->release(); |
| 47 |
} |
| 48 |
while (!pendingQueue.empty()) { |
| 49 |
auto *queue = &pendingQueue.front(); |
| 50 |
pendingQueue.pop_front(); |
| 51 |
queue->release(); |
| 52 |
} |
| 53 |
while (!finishingQueue.empty()) { |
| 54 |
auto *queue = &finishingQueue.front(); |
| 55 |
finishingQueue.pop_front(); |
| 56 |
queue->release(); |
| 57 |
} |
| 58 |
|
| 59 |
curl_multi_cleanup(curlm_); |
| 60 |
} |
| 61 |
|
| 62 |
void FetchThread::runThread(FetchThread *self) { self->run(); } |
| 63 |
|
| 64 |
int FetchThread::curlCallback(CURL *easy, curl_socket_t s, int action, |
| 65 |
void *userp, void *socketp) { |
| 66 |
FCITX_UNUSED(easy); |
| 67 |
FCITX_UNUSED(socketp); |
| 68 |
auto *self = static_cast<FetchThread *>(userp); |
| 69 |
return self->curl(s, action); |
| 70 |
} |
| 71 |
|
| 72 |
void FetchThread::handleIO(int fd, IOEventFlags flags) { |
| 73 |
int mask = 0; |
| 74 |
if (flags & IOEventFlag::In) { |
| 75 |
mask |= CURL_CSELECT_IN; |
| 76 |
} |
| 77 |
if (flags & IOEventFlag::Out) { |
| 78 |
mask |= CURL_CSELECT_OUT; |
| 79 |
} |
| 80 |
if (flags & IOEventFlag::Err) { |
| 81 |
mask |= CURL_CSELECT_ERR; |
| 82 |
} |
| 83 |
int still_running = 0; |
| 84 |
CURLMcode mcode; |
| 85 |
do { |
| 86 |
mcode = curl_multi_socket_action(curlm_, fd, mask, &still_running); |
| 87 |
} while (mcode == CURLM_CALL_MULTI_PERFORM); |
| 88 |
|
| 89 |
processMessages(); |
| 90 |
} |
| 91 |
|
| 92 |
void FetchThread::processMessages() { |
| 93 |
int num_messages = 0; |
| 94 |
|
| 95 |
while (CURLMsg *curl_message = |
| 96 |
curl_multi_info_read(curlm_, &num_messages)) { |
| 97 |
if (curl_message->msg == CURLMSG_DONE) { |
| 98 |
void *p = nullptr; |
| 99 |
curl_easy_getinfo(curl_message->easy_handle, CURLINFO_PRIVATE, &p); |
| 100 |
auto *queue = static_cast<CurlQueue *>(p); |
| 101 |
curl_multi_remove_handle(curlm_, queue->curl()); |
| 102 |
queue->finish(curl_message->data.result); |
| 103 |
queue->remove(); |
| 104 |
finished(queue); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
int FetchThread::curl(curl_socket_t s, int action) { |
| 110 |
// if loop is gone, don't bother do anything |
| 111 |
if (!loop_) { |
| 112 |
return -1; |
| 113 |
} |
| 114 |
if (action == CURL_POLL_REMOVE) { |
| 115 |
events_.erase(s); |
| 116 |
} else { |
| 117 |
auto iter = events_.find(s); |
| 118 |
if (iter == events_.end()) { |
| 119 |
auto *that_ = this; |
| 120 |
try { |
| 121 |
auto p = events_.emplace( |
| 122 |
s, loop_->addIOEvent(s, IOEventFlags(0), |
| 123 |
[that_](EventSourceIO *, int fd, |
| 124 |
IOEventFlags flags) { |
| 125 |
auto *that = that_; |
| 126 |
// make sure "that" is valid since |
| 127 |
// io handler may free itself. |
| 128 |
that->handleIO(fd, flags); |
| 129 |
return true; |
| 130 |
})); |
| 131 |
iter = p.first; |
| 132 |
} catch (...) { |
| 133 |
return -1; |
| 134 |
} |
| 135 |
} |
| 136 |
IOEventFlags flags(0); |
| 137 |
if (action == CURL_POLL_IN) { |
| 138 |
flags = IOEventFlag::In; |
| 139 |
} else if (action == CURL_POLL_OUT) { |
| 140 |
flags = IOEventFlag::Out; |
| 141 |
} else if (action == CURL_POLL_INOUT) { |
| 142 |
flags |= IOEventFlag::In; |
| 143 |
flags |= IOEventFlag::Out; |
| 144 |
} |
| 145 |
|
| 146 |
iter->second->setEvents(flags); |
| 147 |
} |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
int FetchThread::curlTimerCallback(CURLM *multi, long timeout_ms, void *user) { |
| 152 |
FCITX_UNUSED(multi); |
| 153 |
auto *self = static_cast<FetchThread *>(user); |
| 154 |
self->curlTimer(timeout_ms); |
| 155 |
return 0; |
| 156 |
} |
| 157 |
|
| 158 |
void FetchThread::curlTimer(long timeout_ms) { |
| 159 |
if (!loop_) { |
| 160 |
return; |
| 161 |
} |
| 162 |
if (!timer_) { |
| 163 |
timer_ = loop_->addTimeEvent( |
| 164 |
CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + (timeout_ms * 1000), 1, |
| 165 |
[this](EventSourceTime *, uint64_t) { |
| 166 |
CURLMcode mcode; |
| 167 |
int still_running; |
| 168 |
do { |
| 169 |
mcode = curl_multi_socket_action( |
| 170 |
curlm_, CURL_SOCKET_TIMEOUT, 0, &still_running); |
| 171 |
} while (mcode == CURLM_CALL_MULTI_PERFORM); |
| 172 |
processMessages(); |
| 173 |
return true; |
| 174 |
}); |
| 175 |
timer_->setOneShot(); |
| 176 |
} else { |
| 177 |
timer_->setNextInterval(timeout_ms * 1000); |
| 178 |
timer_->setOneShot(); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
void FetchThread::finished(CurlQueue *queue) { |
| 183 |
{ |
| 184 |
const std::lock_guard<std::mutex> lock(finishQueueLock); |
| 185 |
finishingQueue.push_back(*queue); |
| 186 |
} |
| 187 |
cloudPinyin_->notifyFinished(); |
| 188 |
} |
| 189 |
|
| 190 |
bool FetchThread::addRequest(const SetupRequestCallback &callback) { |
| 191 |
CurlQueue *queue = nullptr; |
| 192 |
for (auto &handle : handles_) { |
| 193 |
if (!handle.busy()) { |
| 194 |
queue = &handle; |
| 195 |
break; |
| 196 |
} |
| 197 |
} |
| 198 |
if (!queue || !callback(queue)) { |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
{ |
| 203 |
const std::lock_guard<std::mutex> lock(pendingQueueLock); |
| 204 |
pendingQueue.push_back(*queue); |
| 205 |
} |
| 206 |
|
| 207 |
// Handle pending queue in fetch thread. |
| 208 |
dispatcher_.schedule([this]() { handlePendingRequests(); }); |
| 209 |
return true; |
| 210 |
} |
| 211 |
|
| 212 |
void FetchThread::exit() { |
| 213 |
dispatcher_.schedule([this]() { |
| 214 |
loop_->exit(); |
| 215 |
dispatcher_.detach(); |
| 216 |
}); |
| 217 |
} |
| 218 |
|
| 219 |
CurlQueue *FetchThread::popFinished() { |
| 220 |
const std::lock_guard<std::mutex> lock(finishQueueLock); |
| 221 |
CurlQueue *result = nullptr; |
| 222 |
if (!finishingQueue.empty()) { |
| 223 |
result = &finishingQueue.front(); |
| 224 |
finishingQueue.pop_front(); |
| 225 |
} |
| 226 |
return result; |
| 227 |
} |
| 228 |
|
| 229 |
void FetchThread::handlePendingRequests() { |
| 230 |
const std::lock_guard<std::mutex> lock(pendingQueueLock); |
| 231 |
|
| 232 |
while (!pendingQueue.empty()) { |
| 233 |
auto *queue = &pendingQueue.front(); |
| 234 |
pendingQueue.pop_front(); |
| 235 |
curl_multi_add_handle(curlm_, queue->curl()); |
| 236 |
workingQueue.push_back(*queue); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
void FetchThread::run() { |
| 241 |
loop_ = std::make_unique<fcitx::EventLoop>(); |
| 242 |
dispatcher_.attach(loop_.get()); |
| 243 |
handlePendingRequests(); |
| 244 |
loop_->exec(); |
| 245 |
// free events ahead of time |
| 246 |
timer_.reset(); |
| 247 |
events_.clear(); |
| 248 |
loop_.reset(); |
| 249 |
} |