You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
2.0 KiB
50 lines
2.0 KiB
// uasync.h |
|
|
|
// модуль асинхронных операций. добавляем сокеты и таймауты и mainloop их обслуживает. |
|
|
|
#ifndef UASYNC_H |
|
#define UASYNC_H |
|
|
|
#include <sys/time.h> |
|
#include <stddef.h> |
|
|
|
typedef void (*timeout_callback_t)(void* user_arg);// передаёт user_arg из uasync_set_timeout |
|
typedef void (*socket_callback_t)(int fd, void* user_arg);// передаёт user_arg из uasync_add_socket |
|
// user_arg полезен если нужно передать управляющую структуру. Ее можно выделить в памяти и в ней хранить всё что надо. т.е. при set_timeout передаём и получаем ее в callback-е |
|
|
|
|
|
// Error type |
|
typedef int err_t; |
|
#define ERR_OK 0 |
|
#define ERR_FAIL -1 |
|
|
|
// Opaque uasync instance handle |
|
typedef struct uasync_s uasync_t; |
|
|
|
// Instance API - основной API для работы с uasync |
|
uasync_t* uasync_create(void); |
|
void uasync_destroy(uasync_t* ua); |
|
void uasync_init_instance(uasync_t* ua); |
|
|
|
// Timeouts, timebase = 0.1 mS |
|
void* uasync_set_timeout(uasync_t* ua, int timeout_tb, void* user_arg, timeout_callback_t callback); |
|
err_t uasync_cancel_timeout(uasync_t* ua, void* t_id); |
|
|
|
// Sockets |
|
void* uasync_add_socket(uasync_t* ua, int fd, socket_callback_t read_cbk, socket_callback_t write_cbk, socket_callback_t except_cbk, void* user_arg); |
|
err_t uasync_remove_socket(uasync_t* ua, void* s_id); |
|
|
|
// Single iteration of event loop with timeout (timebase units) |
|
void uasync_poll(uasync_t* ua, int timeout_tb); |
|
|
|
// Mainloop (бесконечный цикл, __noreturn) |
|
void uasync_mainloop(uasync_t* ua); |
|
|
|
// Debug statistics |
|
void uasync_get_stats(uasync_t* ua, size_t* timer_alloc, size_t* timer_free, size_t* socket_alloc, size_t* socket_free); |
|
|
|
// Wakeup mechanism for interrupting poll |
|
int uasync_wakeup(uasync_t* ua); |
|
int uasync_get_wakeup_fd(uasync_t* ua); // returns write fd for wakeup pipe (for signal handlers) |
|
|
|
#endif // UASYNC_H
|
|
|