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 #ifndef COMMUNICATIONNETSTACK_EPOLL_MULTI_DRIVER_H 17 #define COMMUNICATIONNETSTACK_EPOLL_MULTI_DRIVER_H 18 19 #include <map> 20 #include <memory> 21 22 #include "curl/curl.h" 23 24 #include "epoller.h" 25 #include "thread_safe_storage.h" 26 #include "timeout_timer.h" 27 #ifdef HTTP_HANDOVER_FEATURE 28 #include "http_handover_handler.h" 29 #endif 30 31 namespace OHOS::NetStack::HttpOverCurl { 32 33 struct RequestInfo; 34 35 class EpollMultiDriver { 36 public: 37 EpollMultiDriver() = delete; 38 explicit EpollMultiDriver(const std::shared_ptr<HttpOverCurl::ThreadSafeStorage<RequestInfo *>> &incomingQueue); 39 ~EpollMultiDriver(); 40 41 void Step(int waitEventsTimeoutMs); 42 43 private: 44 class CurlSocketContext { 45 public: 46 CurlSocketContext(HttpOverCurl::Epoller &poller, curl_socket_t socket, int action); 47 void Reassign(curl_socket_t socket, int action); 48 ~CurlSocketContext(); 49 50 private: 51 HttpOverCurl::Epoller &poller_; 52 curl_socket_t socketDescriptor_; 53 }; 54 55 int MultiTimeoutCallback(long timeoutMs); 56 int MultiSocketCallback(curl_socket_t s, int action, CurlSocketContext *socketContext); 57 58 void EpollTimerCallback(); 59 void EpollSocketCallback(int fd); 60 61 void CheckMultiInfo(); 62 63 void Initialize(); 64 void IncomingRequestCallback(); 65 66 std::shared_ptr<HttpOverCurl::ThreadSafeStorage<RequestInfo *>> incomingQueue_; 67 68 HttpOverCurl::Epoller poller_; 69 HttpOverCurl::TimeoutTimer timeoutTimer_; 70 71 CURLM *multi_ = nullptr; 72 // Number of running handles 73 int stillRunning = 0; 74 75 std::map<CURL *, RequestInfo *> ongoingRequests_; 76 #ifdef HTTP_HANDOVER_FEATURE 77 std::shared_ptr<HttpHandoverHandler> netHandoverHandler_; 78 #endif 79 }; 80 81 } // namespace OHOS::NetStack::HttpOverCurl 82 83 #endif // COMMUNICATIONNETSTACK_EPOLL_MULTI_DRIVER_H 84