#ifndef __ZVFS_IPC_REACTOR_H__
#define __ZVFS_IPC_REACTOR_H__

#include <stdint.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

struct zvfs_reactor_opts;
struct zvfs_conn;
struct zvfs_reactor;

/* callbacks */


typedef void (*zvfs_on_accept_fn)(
        struct zvfs_conn *conn,
        void *ctx);

typedef void (*zvfs_on_read_fn)(
        struct zvfs_conn *conn,
        void *ctx);

typedef void (*zvfs_on_write_fn)(
        struct zvfs_conn *conn,
        void *ctx);

typedef void (*zvfs_on_close_fn)(
        struct zvfs_conn *conn,
        void *ctx);

/* configuration */

struct zvfs_reactor_opts {

    const char *socket_path;

    int backlog;

    int max_events;

    zvfs_on_accept_fn on_accept;

    zvfs_on_read_fn on_read;

    zvfs_on_write_fn on_write;

    zvfs_on_close_fn on_close;

    void *cb_ctx;
};

struct zvfs_conn {

    int fd;

    int want_write;

    void *user_ctx;

    struct zvfs_reactor *reactor;
};

struct zvfs_reactor {

    int epfd;

    int listen_fd;

    int running;

    struct zvfs_reactor_opts opts;
};


/* reactor lifecycle */

struct zvfs_reactor *
zvfs_reactor_create(const struct zvfs_reactor_opts *opts);

int
zvfs_reactor_run(struct zvfs_reactor *reactor);

void
zvfs_reactor_stop(struct zvfs_reactor *reactor);

void
zvfs_reactor_destroy(struct zvfs_reactor *reactor);


/* connection helpers */

int
zvfs_conn_get_fd(struct zvfs_conn *conn);

void
zvfs_conn_close(struct zvfs_conn *conn);

void
zvfs_conn_enable_write(struct zvfs_conn *conn);

void
zvfs_conn_disable_write(struct zvfs_conn *conn);

void
zvfs_conn_set_ctx(struct zvfs_conn *conn, void *ctx);

void *
zvfs_conn_get_ctx(struct zvfs_conn *conn);


#ifdef __cplusplus
}
#endif

#endif