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