1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "epoll_request_handler.h"
17
18 #include <thread>
19
20 #include "epoll_multi_driver.h"
21 #include "netstack_log.h"
22 #include "request_info.h"
23
24 namespace OHOS::NetStack::HttpOverCurl {
25 static constexpr const char *HTTP_WORK_THREAD = "OS_NET_HttpWork";
26
EpollRequestHandler(int sleepTimeoutMs)27 EpollRequestHandler::EpollRequestHandler(int sleepTimeoutMs)
28 : sleepTimeoutMs_(sleepTimeoutMs),
29 incomingQueue_(std::make_shared<HttpOverCurl::ThreadSafeStorage<RequestInfo *>>())
30 {
31 }
32
~EpollRequestHandler()33 EpollRequestHandler::~EpollRequestHandler()
34 {
35 stop_ = true;
36 if (workThread_.joinable()) {
37 workThread_.join();
38 }
39 }
40
Process(CURL * easyHandle,const TransferCallbacks callbacks,void * opaqueData)41 void EpollRequestHandler::Process(CURL *easyHandle, const TransferCallbacks callbacks, void *opaqueData)
42 {
43 auto requestInfo = new RequestInfo{easyHandle, callbacks, opaqueData};
44 incomingQueue_->Push(requestInfo);
45
46 auto start = [this]() {
47 auto f = [this]() {
48 #if defined(MAC_PLATFORM) || defined(IOS_PLATFORM)
49 pthread_setname_np(HTTP_WORK_THREAD);
50 #else
51 pthread_setname_np(pthread_self(), HTTP_WORK_THREAD);
52 #endif
53 WorkingThread();
54 };
55 workThread_ = std::thread(f);
56 workThread_.detach();
57 };
58
59 std::call_once(init_, start);
60 }
61
WorkingThread()62 void EpollRequestHandler::WorkingThread()
63 {
64 EpollMultiDriver requestHandler(incomingQueue_);
65
66 while (!stop_) {
67 requestHandler.Step(sleepTimeoutMs_);
68 }
69 }
70
71 } // namespace OHOS::NetStack::HttpOverCurl
72