|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef SRC_CORE_EVENT_EVENT_H_ |
| 18 | +#define SRC_CORE_EVENT_EVENT_H_ |
| 19 | + |
| 20 | +#include <functional> |
| 21 | + |
| 22 | +#include "event2/event.h" |
| 23 | +#include "event2/event_struct.h" |
| 24 | +#include "src/core/event/constants.h" |
| 25 | + |
| 26 | +namespace privacy_sandbox::server_common { |
| 27 | + |
| 28 | +// Wraps the event used by libevent. This wrapper makes it easier to manage |
| 29 | +// lifecycle of the underlying event. |
| 30 | +class Event { |
| 31 | + public: |
| 32 | + using OnDelete = std::function<void(struct event*)>; |
| 33 | + // Arguments are documented here: |
| 34 | + // https://libevent.org/doc/event_8h.html#aed2307f3d9b38e07cc10c2607322d758 |
| 35 | + using Callback = void (*)(/*fd or signal=*/int, /*events=*/int16_t, |
| 36 | + /*pointer to user provided data=*/void*); |
| 37 | + |
| 38 | + explicit Event(struct event_base* base, evutil_socket_t fd, |
| 39 | + int16_t event_type, Callback event_callback, void* arg, |
| 40 | + int priority = kNumEventPriorities / 2, |
| 41 | + struct timeval* event_timeout = nullptr, |
| 42 | + OnDelete on_delete = nullptr, bool add_to_loop = true); |
| 43 | + struct event* get(); |
| 44 | + virtual ~Event(); |
| 45 | + |
| 46 | + private: |
| 47 | + int priority_; |
| 48 | + struct event* event_ = nullptr; |
| 49 | + OnDelete on_delete_; |
| 50 | +}; |
| 51 | + |
| 52 | +} // namespace privacy_sandbox::server_common |
| 53 | + |
| 54 | +#endif // SRC_CORE_EVENT_EVENT_H_ |
0 commit comments