Zero-copy FTP/HTTP Daemon compatible with all POSIX systems
| 1 | /* |
| 2 | MIT License |
| 3 | |
| 4 | Copyright (c) 2026 Seregon |
| 5 | |
| 6 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | of this software and associated documentation files (the "Software"), to deal |
| 8 | in the Software without restriction, including without limitation the rights |
| 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | copies of the Software, and to permit persons to whom the Software is |
| 11 | furnished to do so, subject to the following conditions: |
| 12 | |
| 13 | The above copyright notice and this permission notice shall be included in all |
| 14 | copies or substantial portions of the Software. |
| 15 | |
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | SOFTWARE. |
| 23 | */ |
| 24 | /** |
| 25 | * @file event_loop.h |
| 26 | * @brief Unified event loop abstraction (kqueue/epoll) |
| 27 | * |
| 28 | * @author Seregon |
| 29 | * @version 1.0.0 |
| 30 | * |
| 31 | * PLATFORMS: FreeBSD (PS4/PS5 kqueue), Linux (epoll) |
| 32 | * DESIGN: Single-threaded, non-blocking I/O |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | #ifndef EVENT_LOOP_H |
| 37 | #define EVENT_LOOP_H |
| 38 | |
| 39 | #include <stdint.h> |
| 40 | #include <stddef.h> |
| 41 | |
| 42 | /* Forward declarations */ |
| 43 | typedef struct event_loop event_loop_t; |
| 44 | |
| 45 | /* Event types (bitmask) */ |
| 46 | typedef enum { |
| 47 | EVENT_NONE = 0x00, |
| 48 | EVENT_READ = 0x01, /**< Socket ready for reading */ |
| 49 | EVENT_WRITE = 0x02, /**< Socket ready for writing */ |
| 50 | EVENT_ERROR = 0x04, /**< Socket error occurred */ |
| 51 | EVENT_CLOSE = 0x08, /**< Connection closed */ |
| 52 | } event_type_t; |
| 53 | |
| 54 | /** |
| 55 | * Event callback function |
| 56 | * |
| 57 | * @param fd File descriptor that triggered event |
| 58 | * @param events Bitmask of event types (EVENT_READ | EVENT_WRITE) |
| 59 | * @param data User-provided context pointer |
| 60 | * |
| 61 | * @return 0 to continue monitoring, -1 to remove handler |
| 62 | * |
| 63 | * @note Thread-safety: Called from event loop thread only |
| 64 | */ |
| 65 | typedef int (*event_callback_t)(int fd, uint32_t events, void *data); |
| 66 | |
| 67 | /** |
| 68 | * @brief Create event loop |
| 69 | * |
| 70 | * IMPLEMENTATION: |
| 71 | * - FreeBSD/PS4/PS5: Uses kqueue |
| 72 | * - Linux: Uses epoll |
| 73 | * |
| 74 | * @return Event loop instance, or NULL on error |
| 75 | * |
| 76 | * @post Returns valid event loop or NULL |
| 77 | */ |
| 78 | event_loop_t* event_loop_create(void); |
| 79 | |
| 80 | /** |
| 81 | * @brief Register file descriptor for event monitoring |
| 82 | * |
| 83 | * @param loop Event loop instance |
| 84 | * @param fd File descriptor to monitor |
| 85 | * @param events Events to monitor (EVENT_READ | EVENT_WRITE) |
| 86 | * @param callback Function to call when event occurs |
| 87 | * @param data User context passed to callback |
| 88 | * |
| 89 | * @return 0 on success, negative error code on failure |
| 90 | * |
| 91 | * @pre loop != NULL |
| 92 | * @pre fd >= 0 |
| 93 | * @pre callback != NULL |
| 94 | * @pre events != EVENT_NONE |
| 95 | */ |
| 96 | int event_loop_add(event_loop_t *loop, int fd, uint32_t events, |
| 97 | event_callback_t callback, void *data); |
| 98 | |
| 99 | /** |
| 100 | * @brief Modify events for existing file descriptor |
| 101 | * |
| 102 | * @param loop Event loop instance |
| 103 | * @param fd File descriptor |
| 104 | * @param events New event mask |
| 105 | * |
| 106 | * @return 0 on success, negative on error |
| 107 | */ |
| 108 | int event_loop_modify(event_loop_t *loop, int fd, uint32_t events); |
| 109 | |
| 110 | /** |
| 111 | * @brief Remove file descriptor from monitoring |
| 112 | * |
| 113 | * @param loop Event loop instance |
| 114 | * @param fd File descriptor to remove |
| 115 | * |
| 116 | * @return 0 on success, negative on error |
| 117 | */ |
| 118 | int event_loop_remove(event_loop_t *loop, int fd); |
| 119 | |
| 120 | /** |
| 121 | * @brief Run event loop (blocking) |
| 122 | * |
| 123 | * Processes events until event_loop_stop() is called. |
| 124 | * |
| 125 | * @param loop Event loop instance |
| 126 | * |
| 127 | * @return 0 on clean exit, negative on error |
| 128 | * |
| 129 | * @pre loop != NULL |
| 130 | * |
| 131 | * @note Blocks until stopped |
| 132 | */ |
| 133 | int event_loop_run(event_loop_t *loop); |
| 134 | |
| 135 | /** |
| 136 | * @brief Stop event loop |
| 137 | * |
| 138 | * @param loop Event loop instance |
| 139 | * |
| 140 | * @pre loop != NULL |
| 141 | * |
| 142 | * @note Thread-safe: Can be called from signal handler |
| 143 | */ |
| 144 | void event_loop_stop(event_loop_t *loop); |
| 145 | |
| 146 | /** |
| 147 | * @brief Destroy event loop and free resources |
| 148 | * |
| 149 | * @param loop Event loop instance |
| 150 | * |
| 151 | * @pre loop != NULL |
| 152 | * @pre Event loop not running |
| 153 | * |
| 154 | * @post All resources freed |
| 155 | */ |
| 156 | void event_loop_destroy(event_loop_t *loop); |
| 157 | |
| 158 | #endif /* EVENT_LOOP_H */ |
| 159 |