• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 TransferStartedCallback & startedCallback,const TransferDoneCallback & responseCallback,void * opaqueData)41 void EpollRequestHandler::Process(CURL *easyHandle, const TransferStartedCallback &startedCallback,
42                                   const TransferDoneCallback &responseCallback, void *opaqueData)
43 {
44     auto requestInfo = new RequestInfo{easyHandle, startedCallback, responseCallback, opaqueData};
45     incomingQueue_->Push(requestInfo);
46 
47     auto start = [this]() {
48         auto f = [this]() {
49 #if defined(MAC_PLATFORM) || defined(IOS_PLATFORM)
50             pthread_setname_np(HTTP_WORK_THREAD);
51 #else
52             pthread_setname_np(pthread_self(), HTTP_WORK_THREAD);
53 #endif
54             WorkingThread();
55         };
56         workThread_ = std::thread(f);
57         workThread_.detach();
58     };
59 
60     std::call_once(init_, start);
61 }
62 
WorkingThread()63 void EpollRequestHandler::WorkingThread()
64 {
65     EpollMultiDriver requestHandler(incomingQueue_);
66 
67     while (!stop_) {
68         requestHandler.Step(sleepTimeoutMs_);
69     }
70 }
71 
72 } // namespace OHOS::NetStack::HttpOverCurl
73