11e934351Sopenharmony_ci/*
21e934351Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
31e934351Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
41e934351Sopenharmony_ci * you may not use this file except in compliance with the License.
51e934351Sopenharmony_ci * You may obtain a copy of the License at
61e934351Sopenharmony_ci *
71e934351Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
81e934351Sopenharmony_ci *
91e934351Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
101e934351Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
111e934351Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121e934351Sopenharmony_ci * See the License for the specific language governing permissions and
131e934351Sopenharmony_ci * limitations under the License.
141e934351Sopenharmony_ci */
151e934351Sopenharmony_ci
161e934351Sopenharmony_ci#ifndef COMMUNICATIONNETSTACK_EPOLLER_H
171e934351Sopenharmony_ci#define COMMUNICATIONNETSTACK_EPOLLER_H
181e934351Sopenharmony_ci
191e934351Sopenharmony_ci#include <sys/epoll.h>
201e934351Sopenharmony_ci
211e934351Sopenharmony_ci#include <string.h>
221e934351Sopenharmony_ci#include <unistd.h>
231e934351Sopenharmony_ci
241e934351Sopenharmony_ci#include "file_descriptor.h"
251e934351Sopenharmony_ci
261e934351Sopenharmony_cinamespace OHOS::NetStack::HttpOverCurl {
271e934351Sopenharmony_ci
281e934351Sopenharmony_cistruct Epoller {
291e934351Sopenharmony_ci    Epoller()
301e934351Sopenharmony_ci    {
311e934351Sopenharmony_ci        underlying_ = epoll_create1(EPOLL_CLOEXEC);
321e934351Sopenharmony_ci    }
331e934351Sopenharmony_ci
341e934351Sopenharmony_ci    ~Epoller()
351e934351Sopenharmony_ci    {
361e934351Sopenharmony_ci        close(underlying_);
371e934351Sopenharmony_ci    }
381e934351Sopenharmony_ci
391e934351Sopenharmony_ci    Epoller(const Epoller &) = delete;
401e934351Sopenharmony_ci    Epoller(Epoller &&other) = default;
411e934351Sopenharmony_ci
421e934351Sopenharmony_ci    void RegisterMe(FileDescriptor descriptor) const
431e934351Sopenharmony_ci    {
441e934351Sopenharmony_ci        RegisterMe(descriptor, EPOLLIN);
451e934351Sopenharmony_ci    }
461e934351Sopenharmony_ci
471e934351Sopenharmony_ci    void RegisterMe(FileDescriptor descriptor, uint32_t flags) const
481e934351Sopenharmony_ci    {
491e934351Sopenharmony_ci        epoll_event ev{};
501e934351Sopenharmony_ci        ev.events = flags;
511e934351Sopenharmony_ci        ev.data.fd = descriptor;
521e934351Sopenharmony_ci        epoll_ctl(underlying_, EPOLL_CTL_ADD, descriptor, &ev);
531e934351Sopenharmony_ci    }
541e934351Sopenharmony_ci
551e934351Sopenharmony_ci    void UnregisterMe(FileDescriptor descriptor) const
561e934351Sopenharmony_ci    {
571e934351Sopenharmony_ci        if (descriptor) {
581e934351Sopenharmony_ci            epoll_ctl(underlying_, EPOLL_CTL_DEL, descriptor, nullptr);
591e934351Sopenharmony_ci        }
601e934351Sopenharmony_ci    }
611e934351Sopenharmony_ci
621e934351Sopenharmony_ci    int Wait(epoll_event *events, int maxEvents, int timeout) const
631e934351Sopenharmony_ci    {
641e934351Sopenharmony_ci        return epoll_wait(underlying_, events, maxEvents, timeout);
651e934351Sopenharmony_ci    }
661e934351Sopenharmony_ci
671e934351Sopenharmony_ciprivate:
681e934351Sopenharmony_ci    FileDescriptor underlying_;
691e934351Sopenharmony_ci};
701e934351Sopenharmony_ci
711e934351Sopenharmony_ci} // namespace OHOS::NetStack::HttpOverCurl
721e934351Sopenharmony_ci
731e934351Sopenharmony_ci#endif // COMMUNICATIONNETSTACK_EPOLLER_H
74